Skip to content

Commit bf108d4

Browse files
nfrmtkpitrouCopilot
authored
GH-47252: [C++][Compute] Fix sort_indices for temporal types in arrow::Table (#50270)
### Rationale for this change I was unable to use compute::SortIndices with timestamp type because of crash. ### What changes are included in this PR? Fix. The issue was that comparator for merging record batches was not converting timestamp type to its physical variant. so it crashed on null pointer reference on [checked_cast result](https://github.com/apache/arrow/blob/d0919579cbdd37549ba163c0d61843f04bac92cd/cpp/src/arrow/compute/kernels/chunked_internal.h#L55) ### Are these changes tested? yes ### Are there any user-facing changes? **This PR contains a "Critical Fix".** (If the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld), please provide explanation. If not, you can remove this.) i've already provided, i suppose * GitHub Issue: #47252 Lead-authored-by: Ivan Lykov <nfrmtk.aka.fkfka@gmail.com> Co-authored-by: Antoine Pitrou <pitrou@free.fr> Co-authored-by: nfrmtk <nfrmtk.aka.fkfka@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent 85d0d9d commit bf108d4

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

cpp/src/arrow/compute/kernels/vector_sort_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,14 +780,15 @@ struct ResolvedTableSortKey {
780780
// so we can't simply access the column from the table directly.
781781
ArrayVector chunks;
782782
chunks.reserve(batches.size());
783+
auto physical_type = GetPhysicalType(f.type->GetSharedPtr());
783784
int64_t null_count = 0;
784785
for (const auto& batch : batches) {
785786
ARROW_ASSIGN_OR_RAISE(auto child, f.path.GetFlattened(*batch));
786787
null_count += child->null_count();
787-
chunks.push_back(std::move(child));
788+
chunks.push_back(GetPhysicalArray(*child, physical_type));
788789
}
789790

790-
return ResolvedTableSortKey(f.type->GetSharedPtr(), std::move(chunks), f.order,
791+
return ResolvedTableSortKey(physical_type, std::move(chunks), f.order,
791792
f.null_placement, null_count);
792793
};
793794

cpp/src/arrow/compute/kernels/vector_sort_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,29 @@ TEST_F(TestTableSortIndices, Decimal) {
17391739
AssertSortIndices(table, options, "[3, 4, 0, 2, 1]");
17401740
}
17411741

1742+
TEST_F(TestTableSortIndices, Timestamp) {
1743+
auto schema = ::arrow::schema({
1744+
{field("a", timestamp(TimeUnit::MICRO))},
1745+
{field("b", timestamp(TimeUnit::MICRO))},
1746+
});
1747+
std::vector<SortKey> sort_keys{SortKey("a", SortOrder::Ascending),
1748+
SortKey("b", SortOrder::Descending)};
1749+
1750+
auto table = TableFromJSON(schema, {R"([{"a": 1, "b": 2},
1751+
{"a": 3, "b": 4},
1752+
{"a": 8, "b": 6}
1753+
])",
1754+
R"([{"a": 6, "b": null},
1755+
{"a": 6, "b": 7}
1756+
])"});
1757+
SortOptions options(sort_keys);
1758+
AssertSortIndices(table, options, "[0, 1, 4, 3, 2]");
1759+
options.sort_keys[0].null_placement = NullPlacement::AtStart;
1760+
AssertSortIndices(table, options, "[0, 1, 4, 3, 2]");
1761+
options.sort_keys[1].null_placement = NullPlacement::AtStart;
1762+
AssertSortIndices(table, options, "[0, 1, 3, 4, 2]");
1763+
}
1764+
17421765
TEST_F(TestTableSortIndices, NullType) {
17431766
auto schema = arrow::schema({
17441767
field("a", null()),

0 commit comments

Comments
 (0)