diff --git a/cpp/src/arrow/compute/kernels/vector_sort_internal.h b/cpp/src/arrow/compute/kernels/vector_sort_internal.h index 06d911b2c0c..9d2ad650f1e 100644 --- a/cpp/src/arrow/compute/kernels/vector_sort_internal.h +++ b/cpp/src/arrow/compute/kernels/vector_sort_internal.h @@ -780,14 +780,15 @@ struct ResolvedTableSortKey { // so we can't simply access the column from the table directly. ArrayVector chunks; chunks.reserve(batches.size()); + auto physical_type = GetPhysicalType(f.type->GetSharedPtr()); int64_t null_count = 0; for (const auto& batch : batches) { ARROW_ASSIGN_OR_RAISE(auto child, f.path.GetFlattened(*batch)); null_count += child->null_count(); - chunks.push_back(std::move(child)); + chunks.push_back(GetPhysicalArray(*child, physical_type)); } - return ResolvedTableSortKey(f.type->GetSharedPtr(), std::move(chunks), f.order, + return ResolvedTableSortKey(physical_type, std::move(chunks), f.order, f.null_placement, null_count); }; diff --git a/cpp/src/arrow/compute/kernels/vector_sort_test.cc b/cpp/src/arrow/compute/kernels/vector_sort_test.cc index 40bd0936203..cd31c850628 100644 --- a/cpp/src/arrow/compute/kernels/vector_sort_test.cc +++ b/cpp/src/arrow/compute/kernels/vector_sort_test.cc @@ -1739,6 +1739,29 @@ TEST_F(TestTableSortIndices, Decimal) { AssertSortIndices(table, options, "[3, 4, 0, 2, 1]"); } +TEST_F(TestTableSortIndices, Timestamp) { + auto schema = ::arrow::schema({ + {field("a", timestamp(TimeUnit::MICRO))}, + {field("b", timestamp(TimeUnit::MICRO))}, + }); + std::vector sort_keys{SortKey("a", SortOrder::Ascending), + SortKey("b", SortOrder::Descending)}; + + auto table = TableFromJSON(schema, {R"([{"a": 1, "b": 2}, + {"a": 3, "b": 4}, + {"a": 8, "b": 6} + ])", + R"([{"a": 6, "b": null}, + {"a": 6, "b": 7} + ])"}); + SortOptions options(sort_keys); + AssertSortIndices(table, options, "[0, 1, 4, 3, 2]"); + options.sort_keys[0].null_placement = NullPlacement::AtStart; + AssertSortIndices(table, options, "[0, 1, 4, 3, 2]"); + options.sort_keys[1].null_placement = NullPlacement::AtStart; + AssertSortIndices(table, options, "[0, 1, 3, 4, 2]"); +} + TEST_F(TestTableSortIndices, NullType) { auto schema = arrow::schema({ field("a", null()),