Skip to content

Fix wrong results for tables sorted by a monotonically decreasing function - #106136

Merged
fm4v merged 4 commits into
ClickHouse:masterfrom
nihalzp:not-positive-monotonicity
Jun 16, 2026
Merged

fm4v merged 4 commits into
ClickHouse:masterfrom
nihalzp:not-positive-monotonicity

Conversation

@nihalzp

@nihalzp nihalzp commented May 29, 2026 •

Copy link
Copy Markdown
Member
CREATE TABLE t0 (c0 Int32) ENGINE = MergeTree() ORDER BY (c0  / -42) SETTINGS index_granularity = 1;
INSERT INTO t0 VALUES (-1);

SELECT t0.c0
FROM t0
WHERE t0.c0 < 0
SETTINGS use_primary_key = 0; -- Correct; returns 1 row


SELECT t0.c0
FROM t0
WHERE t0.c0 < 0
SETTINGS use_primary_key = 1; -- Incorrectly prunes; returns 0 row

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix incorrect results for queries against tables whose ORDER BY contains a monotonically decreasing function such as (c0 / -42) or intDiv(c0, -42). Predicates on the underlying column (for example, c0 < 0) could wrongly prune granules that contained matching rows, producing missing results. Closes #106084. Closes #106124. Closes #106080.

Version info

  • Merged into: 26.6.1.853

@clickhouse-gh

clickhouse-gh Bot commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3b17b7a]

Summary: ✅


AI Review

Summary

This PR fixes primary-key pruning for predicates on a source column when the ORDER BY key wraps that column in an always-monotonic decreasing expression, such as divide(c0, -42) or intDiv(c0, -42). The implementation now carries the monotonic-chain direction into constant transformation and reverses range comparisons after applying a decreasing key expression; the added stateless test covers strict and non-strict comparisons, constant-on-left forms, integer division, composed negative chains, and rejected non-always-monotonic functions.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 71/73 (97.26%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🔴 AI verdict: degradation — 1 query(s) regressed out of 39 analysed

1 regressed query(s). K_source=5 K_base=30 flagged=1/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🔴 1 regressed

Flagged queries (1 of 22)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🔴 4 regression 2460 3164 +28.6% 0.0043

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: dcf8ba80-f506-4cf1-b3c4-5234d468b85e
  • MIRAI run: b0e7ef55-9711-4534-86b6-b2834509ba96
  • PR check IDs:
    • clickbench_509473_1780671725
    • clickbench_509674_1780671896
    • tpch_adapted_1_official_509808_1780672057
    • tpch_adapted_1_official_509903_1780672091
    • tpch_adapted_1_official_509988_1780672118

@fm4v
fm4v added this pull request to the merge queue Jun 16, 2026
Merged via the queue into ClickHouse:master with commit 0668b92 Jun 16, 2026
327 of 329 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

4 participants