diff --git a/cpp/src/arrow/array/array_test.cc b/cpp/src/arrow/array/array_test.cc index 64ea3fd71a7..e02bc0e1aad 100644 --- a/cpp/src/arrow/array/array_test.cc +++ b/cpp/src/arrow/array/array_test.cc @@ -4433,4 +4433,25 @@ TEST_F(TestDayTimeIntervalBuilder, TestConstructors) { ASSERT_TRUE(builder3.type()->Equals(type)); } +TEST(ArrayTest, MakeEmptyPreservesDictionaryOrderedFlagInArray) { + auto dict_type = arrow::dictionary(arrow::int32(), arrow::utf8(), /*ordered=*/true); + auto schema = arrow::schema({arrow::field("col", dict_type)}); + + ASSERT_OK_AND_ASSIGN(auto batch, arrow::RecordBatch::MakeEmpty(schema)); + + ASSERT_EQ(batch->num_rows(), 0); + ASSERT_EQ(batch->num_columns(), 1); + + // Schema should be correct + auto schema_type = + std::static_pointer_cast(batch->schema()->field(0)->type()); + // Ensure array type preserves the ordered flag + auto array_type = + std::static_pointer_cast(batch->column(0)->type()); + + ASSERT_TRUE(schema_type->ordered()); + ASSERT_TRUE(array_type->ordered()) << "MakeEmpty() lost ordered flag in array type"; + ASSERT_TRUE(array_type->Equals(*schema_type)); +} + } // namespace arrow diff --git a/cpp/src/arrow/array/util.cc b/cpp/src/arrow/array/util.cc index 1c19bd5a546..0785e568325 100644 --- a/cpp/src/arrow/array/util.cc +++ b/cpp/src/arrow/array/util.cc @@ -924,7 +924,12 @@ Result> MakeEmptyArray(std::shared_ptr type, std::unique_ptr builder; RETURN_NOT_OK(MakeBuilder(memory_pool, type, &builder)); RETURN_NOT_OK(builder->Resize(0)); - return builder->Finish(); + ARROW_ASSIGN_OR_RAISE(auto result, builder->Finish()); + // Preserve original DictionaryType (including ordered flag) + if (type->id() == Type::DICTIONARY) { + result->data()->type = type; + } + return result; } namespace internal {