Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions cpp/src/arrow/ipc/message_internal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ TEST(TestMessageInternal, TestEndiannessRoundtrip) {
}
}

// GH-51779: Schema.fbs allows DictionaryEncoding.indexType to be omitted, in
// which case the indices are signed int32.
TEST(TestMessageInternal, DictionaryEncodingWithoutIndexType) {
FBB fbb;
auto name = fbb.CreateString("f0");
auto value_type = flatbuf::CreateUtf8(fbb);
auto encoding = flatbuf::CreateDictionaryEncoding(fbb, /*id=*/0, /*indexType=*/0);
auto fb_field = flatbuf::CreateField(fbb, name, /*nullable=*/true, flatbuf::Type_Utf8,
value_type.Union(), encoding);
auto fb_schema = flatbuf::CreateSchema(fbb, flatbuf::Endianness_Little,
fbb.CreateVector({fb_field}));
fbb.Finish(flatbuf::CreateMessage(fbb, flatbuf::MetadataVersion_V5,
flatbuf::MessageHeader_Schema, fb_schema.Union()));
ASSERT_OK_AND_ASSIGN(auto metadata, WriteFlatbufferBuilder(fbb));

ASSERT_OK_AND_ASSIGN(auto message, Message::Open(metadata, /*body=*/nullptr));
DictionaryMemo memo;
ASSERT_OK_AND_ASSIGN(auto schema, ReadSchema(*message, &memo));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also check that the field was registered in memo? The schema comparison passes even if the new code path never registers the field. A reader needs that registration to match the field to the DictionaryBatch and RecordBatch that follow, and that's where #51779 failed. Something like:

  ASSERT_OK_AND_EQ(0, memo.fields().GetFieldId({0}));
  ASSERT_OK_AND_ASSIGN(auto dict_value_type, memo.GetDictionaryType(0));
  AssertTypeEqual(utf8(), dict_value_type);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 7453d8a: the test now checks the field ID and that the dictionary value type is utf8().

AssertSchemaEqual(*::arrow::schema({field("f0", dictionary(int32(), utf8()))}),
*schema);
ASSERT_OK_AND_EQ(0, memo.fields().GetFieldId({0}));
ASSERT_OK_AND_ASSIGN(auto dict_value_type, memo.GetDictionaryType(0));
AssertTypeEqual(utf8(), dict_value_type);
}

struct SampleMessageParams {
std::shared_ptr<const KeyValueMetadata> custom_metadata = {};
int64_t body_length = 0;
Expand Down
8 changes: 6 additions & 2 deletions cpp/src/arrow/ipc/metadata_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,12 @@ Status FieldFromFlatbuffer(const flatbuf::Field* field, FieldPosition field_pos,
// dictionary_memo
std::shared_ptr<DataType> index_type;
auto int_data = encoding->indexType();
CHECK_FLATBUFFERS_NOT_NULL(int_data, "DictionaryEncoding.indexType");
RETURN_NOT_OK(IntFromFlatbuffer(int_data, &index_type));
if (int_data == nullptr) {
// Schema.fbs: "If this field is null, the indices must be signed int32."
index_type = int32();
} else {
RETURN_NOT_OK(IntFromFlatbuffer(int_data, &index_type));
}
dict_value_type = type;
ARROW_ASSIGN_OR_RAISE(type,
DictionaryType::Make(index_type, type, encoding->isOrdered()));
Expand Down
Loading