Skip to content

Fix inconsistent AST formatting for SAMPLE with query-level OFFSET - #100579

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-sample-offset-ambiguity
Mar 24, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-sample-offset-ambiguity

Conversation

@Avogar

@Avogar Avogar commented Mar 24, 2026

Copy link
Copy Markdown
Member

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 inconsistent AST formatting for SAMPLE with query-level OFFSET. Closes #100576

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cc83891]

Summary: ✅


AI Review

Summary

This PR fixes ambiguous AST/query formatting for queries that combine SAMPLE with query-level OFFSET (without LIMIT) by emitting FROM ... SAMPLE ... SELECT ... OFFSET ... in that case. The change is narrowly scoped to ASTSelectQuery::formatImpl, and added stateless tests cover both the ambiguity case and the non-regression case for table-level SAMPLE ... OFFSET. I did not find correctness, safety, performance, or compatibility issues.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 24, 2026
/// of a query-level OFFSET.
bool format_from_first = sampleSize() && limitOffset() && !limitLength();

if (format_from_first && tables())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sampleSize() (which calls getFirstTableExpression(*this)) doesn't guarantee tables() already?

SELECT formatQuery('FROM system.one SAMPLE 0.5 SELECT 1 OFFSET 10');

-- When the OFFSET is a SAMPLE offset (SELECT-first), formatting should stay SELECT-first.
SELECT formatQuery('SELECT 1 FROM system.one SAMPLE 0.5 OFFSET 10');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some SAMPLE + LIMIT + OFFSET? Something like:

SELECT formatQuery('SELECT 1 FROM system.one SAMPLE 0.5 LIMIT 5 OFFSET 10');

@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 24.40% 24.70% +0.30%
Branches 76.60% 76.70% +0.10%

PR changed lines: PR changed-lines coverage: 100.00% (20/20, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 24, 2026
Merged via the queue into master with commit d655c50 Mar 24, 2026
148 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-sample-offset-ambiguity branch March 24, 2026 18:48
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 24, 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

Development

Successfully merging this pull request may close these issues.

Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-1bfa)

4 participants