Skip to content

Commit 78bfbc0

Browse files
committed
Fix overflow bug with child_fields.size() = 128 (kMaxTypeCode + 1)
1 parent ed91f6f commit 78bfbc0

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

cpp/src/arrow/type.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,15 +3270,15 @@ std::shared_ptr<DataType> run_end_encoded(std::shared_ptr<DataType> run_end_type
32703270
std::shared_ptr<DataType> sparse_union(FieldVector child_fields,
32713271
std::vector<int8_t> type_codes) {
32723272
if (type_codes.empty()) {
3273-
type_codes = internal::Iota(static_cast<int8_t>(child_fields.size()));
3273+
type_codes = internal::Iota<int8_t>(0, child_fields.size());
32743274
}
32753275
return std::make_shared<SparseUnionType>(std::move(child_fields),
32763276
std::move(type_codes));
32773277
}
32783278
std::shared_ptr<DataType> dense_union(FieldVector child_fields,
32793279
std::vector<int8_t> type_codes) {
32803280
if (type_codes.empty()) {
3281-
type_codes = internal::Iota(static_cast<int8_t>(child_fields.size()));
3281+
type_codes = internal::Iota<int8_t>(0, child_fields.size());
32823282
}
32833283
return std::make_shared<DenseUnionType>(std::move(child_fields), std::move(type_codes));
32843284
}
@@ -3310,7 +3310,7 @@ std::shared_ptr<DataType> sparse_union(const ArrayVector& children,
33103310
std::vector<std::string> field_names,
33113311
std::vector<int8_t> type_codes) {
33123312
if (type_codes.empty()) {
3313-
type_codes = internal::Iota(static_cast<int8_t>(children.size()));
3313+
type_codes = internal::Iota<int8_t>(0, children.size());
33143314
}
33153315
auto fields = FieldsFromArraysAndNames(std::move(field_names), children);
33163316
return sparse_union(std::move(fields), std::move(type_codes));
@@ -3320,7 +3320,7 @@ std::shared_ptr<DataType> dense_union(const ArrayVector& children,
33203320
std::vector<std::string> field_names,
33213321
std::vector<int8_t> type_codes) {
33223322
if (type_codes.empty()) {
3323-
type_codes = internal::Iota(static_cast<int8_t>(children.size()));
3323+
type_codes = internal::Iota<int8_t>(0, children.size());
33243324
}
33253325
auto fields = FieldsFromArraysAndNames(std::move(field_names), children);
33263326
return dense_union(std::move(fields), std::move(type_codes));

cpp/src/arrow/util/range.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ std::vector<T> Iota(T length) {
4444
return Iota(static_cast<T>(0), length);
4545
}
4646

47+
/// Create a vector containing the values from start with length elements
48+
template <typename T>
49+
std::vector<T> Iota(T start, size_t length) {
50+
std::vector<T> result(length);
51+
std::iota(result.begin(), result.end(), start);
52+
return result;
53+
}
54+
4755
/// Create a range from a callable which takes a single index parameter
4856
/// and returns the value of iterator on each call and a length.
4957
/// Only iterators obtained from the same range should be compared, the

0 commit comments

Comments
 (0)