Skip to content

[C++] Error with creating union types without type_codes for fields.size() == 128 #47859

@dahbka-lis

Description

@dahbka-lis

There is a bug for creating union types with empty type_codes. If fields.size() == 128 (kMaxTypeCode + 1) and type_codes is empty, static_cast<int8_t> returns -128 and internal::Iota generates an empty vector of type codes, but the expected vector is [0, 1, 2, ..., 127], where 127 is kMaxTypeCode.

Example:

std::vector<std::shared_ptr<Field>> fields;
for (int32_t i = 0; i <= UnionType::kMaxTypeCode; i++) {
  fields.push_back(field(std::to_string(i), int32()));
}

auto type = dense_union(fields); // Error

The validation here will not be passed because type_codes is empty for fields.size() == 128, but fields is not empty.

Otherwise:

  • I can create dense_union type with non-empty type_codes vector created by std::iota(0, 128), check the example below.
  • Unions from pyarrow support at most 128 codes and not 127.
std::vector<std::shared_ptr<Field>> fields;
for (int32_t i = 0; i <= UnionType::kMaxTypeCode; i++) {
  fields.push_back(field(std::to_string(i), int32()));
}

std::vector<int8_t> type_codes(fields.size());
std::iota(type_codes.begin(), type_codes.end(), 0);

auto type = dense_union(fields, type_codes); // OK

Component(s)

C++

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions