Skip to content

Fix wrong ORDER BY for LowCardinality(Float) with NaN in read-in-order - #106588

Closed
jh0x wants to merge 10 commits into
ClickHouse:masterfrom
jh0x:jh-lc-float
Closed

jh0x wants to merge 10 commits into
ClickHouse:masterfrom
jh0x:jh-lc-float

Conversation

@jh0x

@jh0x jh0x commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #101838

In the end didn't do the changes from the bot as the problem isn't specific to float/NaN/nullable keys — the legacy ReadInOrderOptimizer ignores sorting_key.reverse_flags for every type. On master a plain integer reverse key already reads in the wrong order through this path:

CREATE TABLE t (c0 Int32) ENGINE = MergeTree()
ORDER BY c0 DESC SETTINGS allow_experimental_reverse_key = 1;
INSERT INTO t VALUES (0),(5),(1),(3);
SET optimize_read_in_order = 1, enable_analyzer = 0, query_plan_read_in_order = 0;
SELECT * FROM t ORDER BY c0 DESC;  -- 0,1,3,5  --> (should be 5,3,1,0)
SELECT * FROM t ORDER BY c0 ASC;   -- 5,3,1,0 -->  (should be 0,1,3,5)

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 wrong ORDER BY results with query-plan read-in-order over LowCardinality(Float*)/Nullable sorting keys containing NaN/NULL


Workflow [PR]
Sync PR [sync-upstream/pr/106588]

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jun 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [adba169]

Summary: ✅


AI Review

Summary

This PR fixes the query-plan read-in-order path for LowCardinality(Float*) / Nullable(...) sorting keys whose NaN / NULL placement cannot be preserved by the matched key order, and the latest update correctly restores optimization when that leading special-value key is fixed by the filter. The added stateless regression coverage exercises the direct LowCardinality(Float*) cases, monotonic reversing functions, and the fixed-leading-key case that the last patch changed. I did not find a remaining correctness or metadata issue in the touched query-plan path.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit adba169.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 15/16 (93.75%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 5, 2026
@jh0x

jh0x commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@PedroTadim it looks like arm-tidy got some weird results on a file that is not being changed here (and was fine in the first run):

DEBUG:clang-tidy-cache.py:Calling real clang-tidy.
/ClickHouse/src/Storages/ObjectStorage/DataLakes/Iceberg/Mutations.cpp:731:13: error: variable 'last_version' is not initialized [cppcoreguidelines-init-variables,-warnings-as-errors]
  731 |         int last_version;
      |             ^           
      |                          = 0
/ClickHouse/src/Storages/ObjectStorage/DataLakes/Iceberg/Mutations.cpp:733:27: error: variable 'compression_method' is not initialized [cppcoreguidelines-init-variables,-warnings-as-errors]
  733 |         CompressionMethod compression_method;
      |                           ^
199402 warnings generated.
Suppressed 200315 warnings (

Even the line numbers and code don't match:
image

Comment thread src/Storages/ReadInOrderOptimizer.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.50% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.10% 77.10% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 16/16 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@antaljanosbenjamin antaljanosbenjamin self-assigned this Jul 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Dear @antaljanosbenjamin, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@abashkeev abashkeev added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 4, 2026
@zlareb1 zlareb1 added the close in a month if not active This will be closed in case of no information label Sep 30, 2026
@zlareb1

zlareb1 commented Sep 30, 2026

Copy link
Copy Markdown
Member

@jh0x are you still working on this? @groeneai supersede this with a new PR if no update in a day.

@jh0x

jh0x commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

@zlareb1 I'll bring in master and double check the bot comment.

@groeneai

Copy link
Copy Markdown
Collaborator

@zlareb1 @ jh0x is picking this up, so I am not opening a superseding PR.

Before more work goes into it: #118469 (open) already fixes #101838. Its NaN guard uses isFloat(*removeLowCardinalityAndNullable(...)) in optimizeReadInOrder.cpp and in the legacy ReadInOrderOptimizer.cpp, and its test covers a LowCardinality(Float64) key. Measured with #118469's amd_release CI build and master 064b09a3, comparing every query with optimize_read_in_order = 0:

The two PRs replace the same guard in optimizeReadInOrder.cpp, so they conflict. #118469 lists #101838 as Related, not Closes.

@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing adba169d9 with master 8dc18243e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline.

Job report

@m-selmi

m-selmi commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

@jh0x thanks for the PR. As far as I can see this is now superseded by the already merged #118469 and can be closed.

@jh0x jh0x closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors close in a month if not active This will be closed in case of no information comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isFloat() in read-in-order optimization doesn't unwrap LowCardinality, causing wrong ORDER BY for LowCardinality(Float64) with NaN

7 participants