Skip to content

[WIP] Use analyzer to calculate projections - #75728

Closed
novikd wants to merge 11 commits into
masterfrom
analyzer-for-projections
Closed

novikd wants to merge 11 commits into
masterfrom
analyzer-for-projections

Conversation

@novikd

@novikd novikd commented Feb 7, 2025

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Use analyzer to calculate projections.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Feb 7, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b155446]

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 7, 2025
@novikd
novikd force-pushed the analyzer-for-projections branch from fc121f4 to 9353bc7 Compare February 14, 2025 11:08
@novikd
novikd force-pushed the analyzer-for-projections branch from 8a2c36f to a89cb5c Compare February 19, 2025 16:29
@clickhouse-gh

clickhouse-gh Bot commented Apr 29, 2025

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jul 1, 2025

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

4 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Sep 2, 2025

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

…tions

Resolves conflicts in 12 files after the branch was 14+ months stale
(diverged 2025-02-19, ~61k commits behind). Adopts master's evolved APIs
for `getProjectionFromAST`, `getMinMaxCountProjection`, the `Planner`
constructor (which gained a `post_filter_` parameter and parallel
replicas table-union support), and the `addExpressionStep`/`addFilterStep`
templates that now thread `planner_context`, `select_query_options`, and
`correlated_subtrees`.

Master's `ProjectionsDescription` rewrite (`StorageProjectionSource`,
two-phase analyze-then-execute, `_part_offset`/`_parent_part_offset`
handling, `ProjectionDataSource`/`ProjectionDataSink`) supersedes the
PR's earlier analyzer-driven metadata path, so the unused
`buildMetadataAndFillProjectionKeys` helper added by the PR is dropped.
The `optimizeUseAggregateProjection` analyzer branch from the PR is kept
alongside master's non-analyzer fallback (which now uses
`getSampleBlockWithSubcolumns`). `calculateByQuery` keeps both paths,
with the analyzer branch using `TemporaryTableHolder` + `StorageValues`
+ `InterpreterSelectQueryAnalyzer` and the non-analyzer path using
master's `ProjectionDataSource`.

`AggregationAnalysisResult::aggregation_keys` is reverted to master's
`Names` (the PR had switched it to `NamesAndTypes`); call sites in
`Planner` and `optimizeUseAggregateProjection` follow.

The `Planner` constructor merges both new parameters:
`(query_tree_, select_query_options_, post_filter_ = nullptr,
qualify_column_names = true)`. `qualify_column_names` flows into
`GlobalPlannerContext` and `buildColumnIdentifier`.
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8b52951]

Summary: ❌

job_name test_name status info comment
Style check FAIL
cpp FAIL cidb
Fast test FAIL
03128_argMin_combinator_projection FAIL cidb
01710_aggregate_projections FAIL cidb
04099_distinct_aggregate_projection_partial_projection_data FAIL cidb
01710_projections_in_distributed_query FAIL cidb
01710_projections_optimize_aggregation_in_order FAIL cidb
01739_index_hint FAIL cidb
01710_query_log_with_projection_info FAIL cidb
01505_trivial_count_with_partition_predicate FAIL cidb
03703_distinct_projection_type_mismatch FAIL cidb
02968_projection_merge FAIL cidb
23 more test cases not shown
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED

AI Review

Summary

This PR switches projection analysis/calculation to analyzer-based paths in several places, but it currently has one correctness regression in minmax_count projection key handling and several unresolved CI style failures. Verdict: request changes until the blocker and CI-breaking issues are fixed.

Findings

❌ Blockers

  • [src/Storages/ProjectionsDescription.cpp:549] result.sample_block.getPositionByName(key.name) is now unconditional after constant columns were erased. For constant partition keys (e.g. tuple()), the key is absent from sample_block, so table initialization can throw an exception.
    Suggested fix: restore the previous guard (if (result.sample_block.has(key.name))) before appending partition_value_indices.

⚠️ Majors

  • [src/Planner/PlannerExpressionAnalysis.cpp:33] Include style fails Style check; use angle brackets for local headers.
  • [src/Planner/Planner.h:12] Include style fails Style check; use angle brackets.
  • [src/Interpreters/InterpreterSelectQueryAnalyzer.cpp:36] Include style fails Style check; use angle brackets.
  • [src/Interpreters/InterpreterSelectQueryAnalyzer.cpp:37] Include style fails Style check; use angle brackets.
  • [src/Processors/QueryPlan/Optimizations/optimizeUseAggregateProjection.cpp:22] Include style fails Style check; use angle brackets.

💡 Nits

  • [src/Interpreters/InterpreterSelectQueryAnalyzer.cpp:231] Typo in type name: StorageSubstituionOptions → StorageSubstitutionOptions.
  • PR metadata quality: Changelog entry is understandable but too generic (Use analyzer to calculate projections.). Prefer a user-facing entry that names the affected behavior and impact.
Tests
  • ⚠️ No targeted projection regression test is added for constant partition keys (e.g. empty/constant GROUP BY key path in minmax_count projection). Add a stateless/integration test that covers table initialization and projection calculation with constant partition expressions to prevent reintroducing this exception.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ✅
No magic constants ✅
Backward compatibility ❌ Existing projection setups with constant partition keys can now fail with an exception during initialization.
SettingsChangesHistory.cpp ➖
PR metadata quality ⚠️ Changelog entry is too generic; should describe concrete user-facing impact.
Safe rollout ⚠️ Merge is currently blocked by correctness regression and failing Style check.
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Restore safe handling for missing constant aggregation keys in minmax_count projection (sample_block.has(key.name) guard before getPositionByName).
  2. Fix all include-style Style check failures by switching quoted local includes to angle brackets in touched files.
  3. Add a regression test for constant partition-key projection initialization/calculation.

result.partition_value_indices.push_back(result.sample_block.getPositionByName(key.name));
}
result.sample_block_for_keys.insert({nullptr, key.type, key.name});
result.partition_value_indices.push_back(result.sample_block.getPositionByName(key.name));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

result.sample_block.getPositionByName(key.name) now runs unconditionally, but result.sample_block has just had all constant columns removed (erase(constant_positions)). For partition keys like tuple() (common for non-partitioned MergeTree), the aggregation key is constant and won't exist in result.sample_block, so this throws during table initialization.

Please keep the previous presence check (or equivalent guard) before pushing partition_value_indices, e.g. only append the index when result.sample_block.has(key.name).

#include <Planner/Utils.h>

#include <Core/Settings.h>
#include "Core/NamesAndTypes.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style check is currently failing on this include because local headers in ClickHouse sources should use angle brackets.

Please change this to #include <Core/NamesAndTypes.h>.

Comment thread src/Planner/Planner.h

#include <Processors/QueryPlan/QueryPlan.h>
#include <Storages/SelectQueryInfo.h>
#include "Planner/PlannerExpressionAnalysis.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style check is failing here for quoted include style.

Please switch this to #include <Planner/PlannerExpressionAnalysis.h>.


#include <Interpreters/Context.h>
#include <Interpreters/QueryLog.h>
#include "Planner/PlannerContext.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style check reports this include due to quotes.

Please use angle brackets: #include <Planner/PlannerContext.h>.

#include <Interpreters/Context.h>
#include <Interpreters/QueryLog.h>
#include "Planner/PlannerContext.h"
#include "Planner/PlannerExpressionAnalysis.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style check reports this include due to quotes.

Please use angle brackets: #include <Planner/PlannerExpressionAnalysis.h>.


#include <Columns/ColumnAggregateFunction.h>
#include <Common/logger_useful.h>
#include "Storages/StorageValues.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Style check is failing on this line because quoted include syntax is not allowed here.

Please replace it with #include <Storages/StorageValues.h>.

namespace
{

struct StorageSubstituionOptions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typo: StorageSubstituionOptions should be StorageSubstitutionOptions.

Renaming avoids carrying the misspelling into future call sites and diagnostics.

@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Dear @novikd, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@abashkeev abashkeev added the comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). label Sep 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Duplicate of #96886

@alexey-milovidov alexey-milovidov marked this as a duplicate of #96886 Sep 5, 2026
alexey-milovidov added a commit to niyue/ClickHouse that referenced this pull request Oct 14, 2026
`ProjectionDescription::calculateByQuery` interpreted the `SELECT` of a projection
with the legacy `InterpreterSelectQuery`. Use `InterpreterSelectQueryAnalyzer`.

The columns of a projection part are named after the expressions of the projection
query as they are written, because the projection metadata (`sample_block`) is
derived from the AST. The Analyzer names the columns of a plan by its own rules - a
column by the identifier of its table expression (`__table1.a`), a constant with its
data type appended (`1_UInt8`), a set by its key - so the result of the pipeline is
renamed back to the names of the metadata by mapping every expression of the query
tree to the name its AST gives. Two more differences have to be reconciled: a
`tuple` at the top level of `GROUP BY`, which the Analyzer unwraps into separate
keys, and the `ORDER BY` expression, which `ASTProjectionSelectQuery::cloneToASTSelect`
appends to the `SELECT` list and which may therefore duplicate a projection column.

A column of the metadata missing from the result is now reported instead of being
silently dropped by the part writer, which would write a projection part without
that column and read it back as the default value of its type.

The synthetic table expression which stands in for the missing `FROM` clause of a
projection query borrows the name of `system.one`. The Analyzer plans a read from a
table expression as a read from a table, so it checks the access rights and applies
the quotas for it, and calculating a projection must not require anything beyond the
rights of the query which writes the data.

Related: ClickHouse#75728

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants