Skip to content

Reject arguments for RANK and DENSE_RANK window functions - #104324

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-49526-rank-dense-rank-no-arguments
May 19, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-49526-rank-dense-rank-no-arguments

Conversation

@groeneai

@groeneai groeneai commented May 7, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Per SQL standard, the RANK and DENSE_RANK window functions take zero arguments — they rank rows based solely on the OVER (ORDER BY ...) window.

ClickHouse historically accepted and silently ignored arbitrary arguments, leading to user confusion:

SELECT id, PRT,
       DENSE_RANK(PRT) OVER (ORDER BY id) AS DR,
       RANK(PRT) OVER (ORDER BY id) AS R
    FROM (SELECT number AS id, if(number = 3, 0, 1) AS PRT FROM numbers(5)) AS t
    ORDER BY id;
-- Returned ranks based on `id`, ignoring the `PRT` argument. Users assumed
-- DENSE_RANK(PRT) would rank by PRT, but the argument was always discarded.

After this change, such queries throw NUMBER_OF_ARGUMENTS_DOESNT_MATCH with a message that points users at either the standard form DENSE_RANK() OVER (ORDER BY PRT) or, for backward compatibility, the new allow_rank_dense_rank_arguments setting that restores the legacy silent-ignore behavior.

Approach

  • WindowTransform.cpp: validate argument_types.empty() in the registration lambdas of both rank and denseRank. If non-empty AND allow_rank_dense_rank_arguments = 0 (default), throw NUMBER_OF_ARGUMENTS_DOESNT_MATCH. The error message references the compatibility setting.
  • Settings.cpp: declare the new allow_rank_dense_rank_arguments (Bool, default false).
  • SettingsChangesHistory.cpp: record the behavior change in the 26.5 entry (previous_value = true, new_value = false).
  • experimental_settings_ignore.txt: whitelist the setting (it is a backward-compat setting, not experimental).
  • 04209_rank_dense_rank_no_arguments_49526.sql: new regression test covering all 6 reproducer queries from the issue + the compatibility setting flow + zero-argument forms still working + both analyzers.
  • 02478_window_frame_type_groups.sql: replace incidental dense_rank(1) with dense_rank(). The test's actual purpose is verifying GROUPS BETWEEN ... frame-type rejection — the 1 argument was incidental and would now mask the intended NOT_IMPLEMENTED check.

Pre-PR validation

a) Deterministic repro — SELECT DENSE_RANK(x) OVER (ORDER BY x) FROM (SELECT 1 AS x); deterministically throws.
b) Root cause — registration lambdas in WindowTransform.cpp did not validate argument count; both functions accepted any arity and discarded the values silently.
c) Fix matches root cause — adds the missing validation directly at the registration site, with the legacy behavior gated behind a compatibility setting per the follow-up directive.
d) Test intent preserved — new dedicated test added; existing 02478 test updated to keep its GROUPS BETWEEN ... purpose without depending on the old lenient behavior.
e) Both directions demonstrated — verified manually that the original reproducer from the issue throws by default and works under SETTINGS allow_rank_dense_rank_arguments = 1. New test passes 30/30 with randomized settings; 12 existing window function tests pass.

Changelog category (leave one):

  • Backward Incompatible Change

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

The window functions RANK and DENSE_RANK now reject arguments and throw NUMBER_OF_ARGUMENTS_DOESNT_MATCH, in line with the SQL standard. Previously, queries such as RANK(x) OVER (ORDER BY id) were silently accepted with the argument ignored, which was a frequent source of user confusion. To restore the previous lenient behavior, set allow_rank_dense_rank_arguments = 1. Closes #49526.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

The new allow_rank_dense_rank_arguments setting is documented inline in src/Core/Settings.cpp, which feeds the setting documentation page.


Closes #49526

Session: cron:clickhouse-ci-task-worker:20260507-194500

Version info

  • Merged into: 26.5.1.837

@groeneai

groeneai commented May 7, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. SELECT DENSE_RANK(x) OVER (ORDER BY x) FROM (SELECT 1 AS x); deterministically throws NUMBER_OF_ARGUMENTS_DOESNT_MATCH.
b Root cause explained? Yes. The rank and denseRank registration lambdas in src/Processors/Transforms/WindowTransform.cpp did not validate argument count; both functions accepted any arity and discarded the values silently. The WindowFunctionRank and WindowFunctionDenseRank constructors take argument_types only to feed the base IAggregateFunctionHelper constructor — none of the rank logic in windowInsertResultInto reads the argument columns.
c Fix matches root cause? Yes. The fix adds the missing validation directly at the registration site: `if (!argument_types.empty() && (settings == nullptr
d Test intent preserved? / New tests added? Yes. New 04209_rank_dense_rank_no_arguments_49526.sql covers all 6 reproducer queries (must throw), plus the compatibility setting flow (must succeed under SET allow_rank_dense_rank_arguments = 1), plus the original issue reproducer query, plus zero-argument forms still working under both analyzers. The one existing test that incidentally used dense_rank(1) (02478_window_frame_type_groups.sql) is updated to dense_rank() because its actual purpose is GROUPS BETWEEN ... rejection — the 1 argument was incidental and would now mask the intended NOT_IMPLEMENTED check.
e Both directions demonstrated? Yes. Verified manually: original reproducer throws by default and works under SETTINGS allow_rank_dense_rank_arguments = 1. New test passes 30/30 with randomized settings; 12 existing window function tests pass (01049, 01571, 01591, 01592, 01655 ×3, 02129, 02306, 02478, 02701, 03710, 04209).
f Fix is general, not a narrow patch? Yes. Both factory.registerFunction("rank", ...) and factory.registerFunction("denseRank", ...) (with dense_rank alias) are guarded — the case-insensitive lookup also goes through these registrations. nullptr settings are handled (treated as default-strict). percent_rank / cume_dist / row_number are deliberately out of scope — the directive specifically targets RANK and DENSE_RANK; addressing other no-argument window functions can be a follow-up.

Session: cron:clickhouse-ci-task-worker:20260507-194500

@groeneai

groeneai commented May 7, 2026

Copy link
Copy Markdown
Collaborator Author

cc @novikd @KochetovNicolai — could you review this? It rejects arguments for RANK and DENSE_RANK window functions per SQL standard (the registration lambdas in WindowTransform.cpp previously accepted any arity and silently dropped the values). Legacy behavior is gated behind a new allow_rank_dense_rank_arguments compatibility setting per @den-crane's follow-up on issue #49526.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label May 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [08bc397]

Summary: ✅


AI Review

Summary

This PR changes RANK and DENSE_RANK to reject arguments by default, adds allow_rank_dense_rank_arguments for backward compatibility, wires the behavior into SettingsChangesHistory for compatibility, and adds focused stateless regression coverage. I reviewed the current code and prior discussion and found no unresolved correctness, compatibility, or evidence gaps that require changes.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label May 7, 2026
@nikitamikhaylov

Copy link
Copy Markdown
Member

@groeneai resolve conflicts.

@groeneai
groeneai force-pushed the fix-49526-rank-dense-rank-no-arguments branch from e82ff96 to bcbd1bb Compare May 8, 2026 14:49
@groeneai

groeneai commented May 8, 2026

Copy link
Copy Markdown
Collaborator Author

@nikitamikhaylov rebased onto current master HEAD 8a8a36dd2e5, conflicts resolved.

The original PR HEAD e82ff96 was 268 commits behind master and GitHub couldn't auto-merge. After git fetch + git rebase origin/master, the rebase applied cleanly with no textual conflicts — Settings.cpp, SettingsChangesHistory.cpp, experimental_settings_ignore.txt and WindowTransform.cpp all have non-overlapping insertion points with the master changes. The 26.5 SettingsChangesHistory entry is correctly placed at the tail of the bucket (after input_format_column_name_matching_mode, the most recent existing entry).

New HEAD: bcbd1bb699fac6a25d3bfda3d246228b456f3ba6. PR is MERGEABLE. Diff is byte-identical to the previous validated content (7 files, +117/-7).

Session: cron:clickhouse-ci-task-worker:20260508-1445

The `RANK` and `DENSE_RANK` window functions take zero arguments per SQL
standard — they rank rows based solely on the `OVER (ORDER BY ...)` window.
ClickHouse historically accepted and silently ignored arbitrary arguments,
which led to user confusion: queries such as `DENSE_RANK(PRT) OVER (ORDER BY id)`
appeared to rank by `PRT`, but the argument was ignored and the ranking actually
followed `id`.

Reject any arguments by default and throw `NUMBER_OF_ARGUMENTS_DOESNT_MATCH`.
The legacy lenient behavior is gated behind a new compatibility setting
`allow_rank_dense_rank_arguments`. Set it to `1` to restore the pre-26.5
silent-ignore behavior for queries that relied on it.

Closes ClickHouse#49526
@groeneai
groeneai force-pushed the fix-49526-rank-dense-rank-no-arguments branch from bcbd1bb to 08bc397 Compare May 19, 2026 03:26
@groeneai

Copy link
Copy Markdown
Collaborator Author

@nikitamikhaylov rebased again onto current master HEAD 3c264823d12 — the May 8 rebase (commit bcbd1bb, base 8a8a36dd2e5) drifted behind by ~2830 commits over the 11 days since you asked, so master had re-introduced conflicts.

git rebase origin/master applied cleanly — no textual conflicts. The 26.5 block in SettingsChangesHistory.cpp had many new entries inserted, but our allow_rank_dense_rank_arguments entry stays at the tail of the 26.5 block (line 96, just after iceberg_compaction_data_cleanup at line 95, and before the 26.4 boundary at line 98). Settings.cpp, experimental_settings_ignore.txt, and WindowTransform.cpp are all intact at the same code positions.

Verified locally on the rebased binary:

  • SELECT DENSE_RANK(a) OVER (ORDER BY a) FROM (SELECT 1 AS a) → throws NUMBER_OF_ARGUMENTS_DOESNT_MATCH ✅
  • Same query with SETTINGS allow_rank_dense_rank_arguments = 1 → returns 1 (legacy behavior) ✅
  • SELECT DENSE_RANK() OVER (ORDER BY a) → returns 1 ✅
  • 04209_rank_dense_rank_no_arguments_49526 test passes 20/20 with randomized settings
  • 02478_window_frame_type_groups (incidentally modified) passes 5/5

New HEAD: 08bc397bca69fe9cbe26d9c8829628f79fdb9b30. PR is MERGEABLE. Diff is content-identical to the previously validated revision (7 files, +117/-7).

Session: cron:clickhouse-ci-task-worker:20260519-031500

@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.20% +0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 100.00% (44/44) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this May 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit 4b5cde4 May 19, 2026
165 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
valerypetrov pushed a commit to valerypetrov/ClickHouse that referenced this pull request Sep 2, 2026
…ease notes

The 26.6 Cloud release notes were missing several backward incompatible
changes that ship in that version:

* UUID parsing now rejects same-length non-hexadecimal strings (ClickHouse#104370).
* The obsolete Arrow-based Parquet reader and writer were removed (ClickHouse#100949).
* `RANK` and `DENSE_RANK` reject arguments (ClickHouse#104324).
* `Dynamic`/`Variant` types are validated in window `PARTITION BY` (ClickHouse#105450).
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 pr-backward-incompatible Pull request with backwards incompatible changes pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Window functions DENSE_RANK / RANK accept arguments and this confuses users

4 participants