Repository navigation
compareTrackAt() - devirtualization for IColumn::compareAt() for merge sorted - #99013
Conversation
|
Continuation of partial merge join optimizations #96922 after this PR: |
|
This is a great idea! |
Change to performance improvement and make sure we have at least one automated performance test to show it. |
|
I've also tried std::visit + std::variant devirtualization. It's more general but less powerfull in this case using TypesVariant = std::variant<
const IColumn *,
const ColumnVector<Int32> *,
const ColumnVector<UInt32> *,
const ColumnVector<Int64> *,
const ColumnVector<UInt64> *>;
TypesVariant first_left_column;
template <bool left_nulls, bool right_nulls>
MergeJoinEqualRange getNextEqualRangeImpl(MergeJoinCursor & rhs)
{
if (atEnd() || rhs.atEnd())
return MergeJoinEqualRange{position(), rhs.position(), 0, 0};
return std::visit(
[&] (const auto * first_column) -> MergeJoinEqualRange
{
while (true)
{
int cmp = compareAtCursor<left_nulls, right_nulls>(rhs, *first_column);
if (cmp < 0)
{
next();
if (atEnd())
break;
}
else if (cmp > 0)
{
rhs.next();
if (rhs.atEnd())
break;
}
else if (!cmp)
return MergeJoinEqualRange{position(), rhs.position(), getEqualLength(), rhs.getEqualLength()};
}
return MergeJoinEqualRange{position(), rhs.position(), 0, 0};
},
first_left_column);
} |
| @@ -122,7 +122,10 @@ int nullableCompareAt(const IColumn & left_column, const IColumn & right_column, | |||
| } | |||
| } | |||
|
|
|||
| return left_column.compareAt(lhs_pos, rhs_pos, right_column, null_direction_hint); | |||
| if constexpr (track) | |||
There was a problem hiding this comment.
Nullable columns bypass compareTrackAt when track=true
Medium Severity
When track=true, nullableCompareAt only calls compareTrackAt in the final non-nullable branch. All nullable code paths (both-nullable, left-nullable, right-nullable) still call compareAt, returning only {-1, 0, 1}. In getNextEqualRangeImpl, when nullable columns are present, nextN(-cmp) always advances by 1 instead of skipping multiple rows, silently defeating the optimization purpose of compareTrackAt.
There was a problem hiding this comment.
It's expected for now. I didn't test it for nullable types yet. It requires a decision what to check in track first: nulls or values, how much of them to take in track and how to implement null&values track gracefully.
It's some king of future work I think. At least I have to update my TPC-H for nullable types and get some meditation over it.
a27b5fc to
d5c3994
Compare
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 96.88% (93/96) |
PR ClickHouse#99013 added `IColumn::compareTrackAt` for devirtualized multi-row skipping in partial-merge join. Two coverage gaps filled: 1. `04042_partial_merge_join_float_nan`: exercises `ColumnVector<Float64>::compareTrackAt` (ColumnVector.h:157) with `NaN`/`Inf` keys in `partial_merge` join. Verifies that `NaN` matches `NaN` (merge-join semantics: `compareAt` returns 0 for equal sort positions) and that track-skipping works correctly for floating-point keys including negative infinity. 2. `04043_partial_merge_join_multikey_track`: exercises `MergeJoin.cpp:327-340` — `compareTrackAt` on the first sort key forces `track > 1` on left runs that are strictly less than the right cursor; secondary keys then use regular `compareAt`. Verifies that multi-row skipping on key1 does not accidentally skip valid (key1, key2) matches. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
I had very different results for the test on my laptop and server. So I rerun this bench with fully optimized tables on current master on server. The previous run in this PR was wtih not optimized tables. The results now are consistent with my laptops ones. And they are much better for fully optimized TPC-H tables after current default JOIN So it looks like PMJ is faster if sourses (one or both) are already sorted by joining key. And much slower if we need sort them berore JOIN. Mainly cause it's a single-threaded part of the algorithm. |
Covers compareTrackAt (devirtualized IColumn::compareAt for merge-sorted join, PR ClickHouse#99013) which the existing partial_merge tests do not exercise: - 04546: Float64/NaN/Inf keys (CompareHelper NaN handling, NaN=NaN match). NaN rows pinned last via isNaN() so ORDER BY is deterministic under randomized nan_direction_hint. - 04547: multi-key sort, first-key multi-row track skipping interacting with secondary-key matching. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Covers compareTrackAt (devirtualized IColumn::compareAt for merge-sorted join, PR ClickHouse#99013) which the existing partial_merge tests do not exercise: - 04546: Float64/NaN/Inf keys (CompareHelper NaN handling, NaN=NaN match). NaN rows pinned last via isNaN() so ORDER BY is deterministic under randomized nan_direction_hint. - 04547: multi-key sort, first-key multi-row track skipping interacting with secondary-key matching. Co-Authored-By: Claude Opus 4.8 <[email protected]>


IColumn::compareAt()is too slow when colled as virtual function in cycle. PR makes a tracked version of it. IColumn::compareTrackAt() return {-N, 0, N} instead of {-1, 0, 1}. It passes a cycle inside virtual call. Then it's possible for compiler to devirtualizecompareAt()inside the function.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
add IColumn::compareTrackAt() for sorted merges
Note
Medium Risk
Touches the core
IColumncomparison API and changesMergeJoincursor advancement logic to skip multiple rows at once, so subtle ordering/null/NaN edge cases could affect join correctness even though behavior is intended to be equivalent.Overview
Adds a new virtual
IColumn::compareTrackAt()API that extendscompareAt()to return a signed run length (e.g.,-N/0/+N) so callers can advance over multiple strictly-less/greater rows in one call, enabling better devirtualization and fewer virtual calls.Updates
MergeJoin’s sorted-range matching to usecompareTrackAt()on the first sort key and advance cursors vianextN()based on the returned magnitude, while keeping remaining key comparisons unchanged; also adjusts helper comparison code to returnInt64and removes the old per-step cursor compare helper.ColumnVectormarkssize()/compareAt()asfinaland provides afinaloverride ofcompareTrackAt()for the devirtualization path.Written by Cursor Bugbot for commit d5c3994. This will update automatically on new commits. Configure here.
Version info
26.3.1.685