Repository navigation
Conversation
getMinMaxCountProjectionBlock answers min(col)/max(col) from part-level bounds, feeding each bound straight into an aggregate state. That is only correct when the bound is a real row value ordered the way the aggregate orders, and there are three ways it is not: - a NULL: keys sort NULLS LAST so the top index entry is a NULL while max skips NULLs, and on the computed-bounds path the POSITIVE_INFINITY pruning sentinel materializes as a SQL NULL; - a NaN, with no Nullable involved: the writer sorts NaN above every number (KeyOrder.cpp) and max skips it (SingleValueData.cpp), so a plain Float64 sorting key holding a NaN answers max with nan; - a Tuple bound, with no NULL or NaN involved: getExtremes runs per column, so the bound envelopes each component's extreme and need not be any row. This one returns a wrong min as well as a wrong max. The reported symptom is max() returning NULL. The worse shape is silent: when the true maximum shares a part with a NULL, that bound is skipped and max returns the largest value of the remaining parts, so a table whose maximum is 200 answers 4 with nothing to suggest the answer is wrong. Decline the implicit projection for such a query, reusing the escape hatch already present for a part with no final mark. The query then reads rows, which was correct in every case measured. A part whose bound pair is one repeated degenerate value stays exempt, since min/max skipping it is the right contribution; that exemption matches by exact value, because Range::createWholeUniverse() is [-Inf, +Inf] and both of those ends satisfy Field::isNull(), so an isNull()-shaped test would re-admit a pure sentinel pair and reintroduce the bug it exists to prevent. The two bound sources need different predicates and this is deliberate. Primary-key bounds are stored index rows, so a composite there is a genuine value and rejecting it would needlessly lose the projection for ORDER BY (a, b). Computed bounds may be synthesized, so a Tuple is additionally suspect. Only ColumnTuple::getExtremes assembles a value; ColumnArray, ColumnObject and ColumnMap select a real row, so the check names Tuple rather than every composite. Alternatives rejected: skipping only the poisoned part reproduces the silent shape above, and making the write path NULL-aware would change the on-disk MinMaxIndex format, do nothing for existing parts, and risk the IS NULL pruning regression the sentinel's own comment documents. The Field walk is iterative with an explicit worklist, as Field::isContainer requires, because key values are user-controlled and can nest arbitrarily. Closes: ClickHouse#111938
The degenerate-pair exemption added with the unusable-bound guard inferred that both index ends being the same degenerate value means the part holds no ordinary comparable value, so skipping it is the right contribution. That inference needs the pair to sample the part's two physical ends, and it does not when the part has no final mark: the index stores one entry per granule start, and the actual last row is appended only under with_final_mark, which is can_use_adaptive_granularity and therefore false at index_granularity_bytes = 0. A single-granule non-adaptive Wide part has one index entry, so lowest == highest while ordinary values sit unsampled below. The pre-existing no-final-mark hatch does not cover this, because ProjectionsDescription emits max(k), min(k) swapped for a reversed key and primary_key_max_column_name is taken second from the end, so it names min(k) on a DESC key. need_primary_key_max_column is then false for exactly the DESC max(k) query that reads the last index entry. Since the writer passes nulls_direction = 1 for both key directions (MergeTreeDataWriter.cpp:791-793, and SortDescription.h:51 defines 1 as "NULLs and NaNs are greater"), a NULL or NaN on a DESC key sorts physically first and is that single entry, so max returned it: nan and NULL against a correct value of 200. Make the exemption conditional on the part having a final mark. Declining is the safe direction, since it costs a projection rather than an answer. The computed-bounds predicate keeps its unconditional exemption: those bounds come from getExtremes over the whole column, so both ends are observed whatever the granularity. The narrow cost is a part that is both all-degenerate and non-adaptive, which now reads rows instead of taking the exemption. Telling that apart from a part with unsampled ordinary values would need its real last row, which is the one value such a part does not store.
The two tables exercising the no-final-mark shape pinned index_granularity_bytes, enable_mixed_granularity_parts and min_bytes_for_wide_part, but not index_granularity. The test runner injects a randomized MergeTree setting only when the name is absent from the CREATE (ClientBase::addMergeTreeSettings), so index_granularity was injected from random.randint(1, 65536). On the non-adaptive path the granularity is used verbatim, so a 3-row part gets ceil(3/g) marks. At g of 1 or 2 the part gains extra index rows, the two ends of the index stop being the same degenerate value, and the arm no longer reaches the repeated-degenerate branch it exists to cover: it would pass with the fix reverted. It also makes the part-shape assertions read 3 or 2 marks instead of 1, so the test fails. The answers stay correct at every granularity, because on a DESC key the NULL or NaN sorts physically first and remains the first index row. Pinning the granularity in the CREATE is stronger than a settings clamp comment: the name is then present, so the runner never injects it, and there is no parse step that can fail silently. It also keeps the other arms exercising randomized MergeTree settings, which a blanket no-random-merge-tree-settings tag would not. Verified with the runner's own injection flag: with the pin, --index_granularity 1 and 2 both still produce a one-mark part and the test passes; without it they produce three-mark and two-mark parts and the test fails.
Internal second-model review (click to expand)Three review rounds by an independent reviewer plus a second model, over five design rounds Two real defects were caught and fixed during review, both in the test's ability to detect a
Verification. Every guard was shown independently load-bearing by mutation: removing either Fifteen Recorded honestly rather than as passes. Broadening the computed-bound guard beyond Also noticed and deliberately not raised: |
|
Workflow [PR], commit [20a8dd7] Summary: ✅
AI ReviewSummaryThis PR makes Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 20a8dd7.
Changed lines: Changed C/C++ lines covered: 56/69 (81.16%) · Uncovered code |
Pre-PR validation gate (click to expand)
Each guard was checked to be independently load-bearing by mutation: removing either bound guard, dropping the depth walk, broadening either predicate, dropping the NaN half, rejecting real infinities, writing the degenerate-pair exemption as "both ends Two results recorded honestly rather than as passes. Broadening the computed-bound guard from Of the 302 minmax/projection tests I ran, 282 pass and 20 fail in my sandbox both before and after the change. I re-ran exactly those 20 against a pristine pre-change binary and the failing sets are identical, so none is caused by this change. They need infrastructure a bare local server does not have: ZooKeeper ( |
|
cc @CurtizJ @hanfei1991, could you review this? getMinMaxCountProjectionBlock feeds a part-level bound straight into a min/max aggregate state, which is only valid when the bound is a real row value ordered as the aggregate orders it, so a NULL or NaN key end and a component-wise synthesized Tuple bound all produce wrong answers; the fix declines the implicit projection for those queries. |
A pull_request workflow run raised from a fork does not start until a maintainer approves it. The run for the previous head was created but never approved, so GitHub cancelled it 58 hours later with zero jobs. That head therefore carries no check runs and cannot acquire any on its own, since a cancelled run is terminal. This commit is empty: its tree is identical to its parent's, so the code under test is unchanged and the merge commit below it is untouched. It exists only to raise a fresh run that can be approved.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 36 s compile time in total, 7 of them have a recent master baseline. |
Closes: #111938
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
minandmaxreturning a wrong value when the implicit_minmax_count_projectionanswered them from a part-level bound that is not a real row value: aNULLin aNullablesorting key, aNaNin a float key, or aTuplebound assembled component-wise. Such a query now reads rows instead of using the projection.Description
getMinMaxCountProjectionBlockanswersmin(col)/max(col)from part-level bounds, feeding each straight into an aggregate state. That is only correct when the bound is a real row value, ordered as the aggregate orders it. Three ways it is not:maxskips NULLs, and thePOSITIVE_INFINITYpruning sentinel materializes as a SQL NULL.Nullableinvolved: the writer sorts NaN above every number whilemaxskips it, soFloat64 ORDER BY kover(1),(200),(nan)givesnan.getExtremesruns per column, so aTuplebound envelopes each component's extreme and need not be any row. This breaksmintoo.Worse than the reported
NULLis the silent shape: when the true maximum shares a part with a NULL,maxreturns the largest of the other parts,4instead of200.The fix declines the implicit projection for such a query, reusing the escape hatch this function already has earlier (
return {}when a part has no final mark). A part whose bound pair is one repeated degenerate value stays exempt, since skipping it is the right contribution; for an index bound that exemption also requires a final mark, without which the index samples granule starts only and a repeated pair proves nothing.I rejected skipping only the poisoned part, which is the silent shape above, and changing the write path, since the sentinel is needed for
IS NULLpruning and existing parts would stay broken.The new test covers each class across nullable, low-cardinality, tuple and float keys, ASC and DESC, one and several parts, and a part with no final mark. Issue #104412 is a different defect here, verified unaffected.
Workflow [PR]
Sync PR [sync-upstream/pr/115151]
Version info
26.9.1.395(included in26.9and later)