Skip to content

Fix LOGICAL_ERROR 'Trying to get name of not a column: ExpressionList' - #106172

Closed
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-grouping-sets-force-aggregation
Closed

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-grouping-sets-force-aggregation

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Two unrelated call sites raise this assertion when they call IAST::getColumnName on an ASTExpressionList. Both surface in the AST fuzzer family (issues #43199, #97988, #100323; STIDs 2310-3e66 and 2310-3791). One is reachable from numbers(multiIf(*, ...), N) style queries; the other from force_aggregation_in_order=1 plus GROUPING SETS under the old analyzer.

Root causes

STID 2310-3791 — multiIf slot rebinding in resolveFunction.cpp

The multiIf constant-folding helper held its condition through auto & cond_node = multi_if_args[2 * pair];. resolveExpressionNode rebinds its argument when the node is a matcher (*), so the reference silently swapped the slot in multi_if_args for an empty ListNode. A later argument throwing UNKNOWN_IDENTIFIER then left the corrupted tree behind, which toAST re-emitted as multiIf(ExpressionList(), ...). Snapshotting cond_node into a local copy (matching the is_special_function_if pattern) prevents the rebinding from leaking into the original slot.

STID 2310-3e66 — getSortDescriptionFromGroupBy and GROUPING SETS in InterpreterSelectQuery.cpp::executeAggregation

The force_aggregation_in_order branch called getSortDescriptionFromGroupBy without the !useGroupingSetKey() guard that the optimize_aggregation_in_order branch already has. With GROUPING SETS each set is an ASTExpressionList, so getColumnName aborts. Adding the missing guard fixes it.

Regression tests

  • 04303_grouping_sets_force_aggregation_in_order.sql: GROUPING SETS with force_aggregation_in_order under the old analyzer, plus the ALTER UPDATE shape from issue Logical error: Trying to get name of not a column: A (STID: 2310-3e66) #97988 which routes the inner SELECT through replaceNonDeterministicToScalars.
  • 04304_multi_if_asterisk_table_function.sql: the fuzzer query numbers(multiIf(*, moduloOrZero(number, NULL), 'a'), 2) plus narrower variants to confirm the matcher rejection and the unaffected constant-fold path.

Both tests crash debug builds at HEAD without the fix and pass with it.

Closes #43199
Closes #97988
Closes #100323

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):

Fixed a LOGICAL_ERROR "Trying to get name of not a column: ExpressionList" raised by queries that use an asterisk inside multiIf arguments to a table function (e.g. numbers(multiIf(*, ...), 2)) and by SELECT ... GROUP BY GROUPING SETS (...) with force_aggregation_in_order = 1 under the old analyzer.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

…ist`"

Two unrelated call sites raise this assertion when they call
`IAST::getColumnName` on an `ASTExpressionList`. Both sites have appeared in
the AST fuzzer family (issues ClickHouse#43199, ClickHouse#97988, ClickHouse#100323; STIDs 2310-3e66 and
2310-3791).

1. `resolveFunction.cpp`: the `multiIf` constant-folding helper held its
   condition through `auto & cond_node = multi_if_args[2 * pair];`.
   `resolveExpressionNode` rebinds its argument when the node is a matcher
   (`*`), so the reference silently swapped the slot in `multi_if_args` for
   an empty `ListNode`. A later argument throwing `UNKNOWN_IDENTIFIER` then
   left the corrupted tree behind, which `toAST` re-emitted as
   `multiIf(ExpressionList(), ...)`. The helper now snapshots `cond_node`
   into a local copy, matching the existing `is_special_function_if` pattern.

2. `InterpreterSelectQuery.cpp::executeAggregation`: the
   `force_aggregation_in_order` branch called `getSortDescriptionFromGroupBy`
   without the `!useGroupingSetKey()` guard that the
   `optimize_aggregation_in_order` branch already has. With `GROUPING SETS`
   each set is an `ASTExpressionList`, so `getColumnName` aborts. Added the
   missing guard.

Regression tests:
* `04303_grouping_sets_force_aggregation_in_order.sql`: GROUPING SETS with
  `force_aggregation_in_order` under the old analyzer, plus the ALTER
  UPDATE shape from issue ClickHouse#97988 which routes through
  `replaceNonDeterministicToScalars`.
* `04304_multi_if_asterisk_table_function.sql`: the fuzzer query
  `numbers(multiIf(*, moduloOrZero(number, NULL), 'a'), 2)` plus narrower
  variants for the matcher rejection and the unaffected constant-fold path.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (per TASK.md Phase 4):

a) Deterministic repro? Yes. Each fix has a query that crashes a debug build at master HEAD d53619ccd83d:

  • STID 2310-3791: SELECT if(number % 2, materialize(toLowCardinality('a')), materialize(toLowCardinality('a'))) FROM numbers(multiIf(*, moduloOrZero(number, NULL), 'a'), 2);
  • STID 2310-3e66: SET enable_analyzer=0, force_aggregation_in_order=1; SELECT 1 GROUP BY GROUPING SETS ((1), (1));
  • Both are checked by the new tests 04303_grouping_sets_force_aggregation_in_order.sql and 04304_multi_if_asterisk_table_function.sql.

b) Root cause explained? Yes. STID 2310-3791: auto & cond_node lets resolveMatcher rebind a multiIf argument slot in-place; the catch in resolveTableFunction then preserves a corrupt tree whose toAST emits multiIf(ExpressionList(), ...). STID 2310-3e66: the force_aggregation_in_order branch missed the !useGroupingSetKey() guard, so getSortDescriptionFromGroupBy calls getColumnName on an ASTExpressionList GROUPING SET.

c) Fix matches root cause? Yes. Snapshot the multiIf condition into a local copy (mirrors the existing is_special_function_if pattern). Add the missing !useGroupingSetKey() guard (mirrors the optimize_aggregation_in_order branch above).

d) Test intent preserved? New tests added? Yes. 04303 and 04304 are new regression tests with serverError expectations matching the desired clean errors. Existing 01883_grouping_sets_*, 02304_grouping_*, 02374_combine_multi_if_*, 03168_fuzz_multiIf_short_circuit, and 00328_long_case_construction all pass on the fix branch.

e) Both directions demonstrated? Yes. With each fix temporarily reverted the corresponding regression test query aborts with exit 134 (SIGABRT); both fixes restored both queries return a clean error / correct output.

f) Fix is general? I checked the other auto & ... = arguments[...] patterns in resolveFunction.cpp (lines 1317 and 1685) — neither passes the reference into anything that rebinds, so they are safe. The getSortDescriptionFromGroupBy call site at line 2840 is already guarded via the upstream optimize_aggregation_in_order flag.

Session: cron:clickhouse-ci-task-worker:20260530-214500.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @novikd @KochetovNicolai for review — touches comp-query-analyzer ( multiIf handling) and comp-query-execution ().

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @novikd @KochetovNicolai for review. Touches comp-query-analyzer (Analyzer/Resolve/resolveFunction.cpp multiIf handling) and comp-query-execution (Interpreters/InterpreterSelectQuery.cpp executeAggregation).

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

clickhouse-gh Bot commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6699e02]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_llvm_coverage, 3/5) ERROR
LLVM Coverage DROPPED

AI Review

Summary

This PR fixes two LOGICAL_ERROR paths around IAST::getColumnName on ASTExpressionList: the new-analyzer multiIf constant-folding path now snapshots condition nodes before matcher resolution can rebind the argument slot, and the old-analyzer force_aggregation_in_order path skips GROUPING SETS before calling getSortDescriptionFromGroupBy. I did not find a correctness, safety, or metadata issue that warrants an inline review comment.

Missing context / blind spots
  • ⚠️ The latest PR CI was still running in the local ci/tmp/result_pr.json, and the direct S3 report for head 6699e02f did not yet expose completed test rows. Completed targeted stateless/debug jobs would close this.
  • ⚠️ This checkout has no local clickhouse binary or build directory, so I could not run 04303_grouping_sets_force_aggregation_in_order or 04304_multi_if_asterisk_table_function locally.
Final Verdict

Status: ✅ Approve

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

clickhouse-gh Bot commented May 31, 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: 9/9 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai check the test failures

The C++ fix that closes the original LOGICAL_ERROR ("Trying to get name of
not a column: `ExpressionList`") lives in the new analyzer's `resolveFunction`
(it snapshots the `multiIf` condition into a local copy before
`resolveExpressionNode`). Under the old analyzer, the same fuzzer queries reach
a different error path and return `UNKNOWN_IDENTIFIER` (47) instead of
`UNSUPPORTED_METHOD` (1), so the test failed in the
`amd_llvm_coverage, old analyzer, s3 storage, DatabaseReplicated, WasmEdge`
variant on 778a2da.

Pin `SET enable_analyzer = 1` at the top of 04304 so the regression test
exercises the path the fix actually changed. 04303 already pins the analyzer
explicitly per query, so no behaviour change there.

Also drop the long block-comments at the top of both files: the explanation
belongs in the commit message and PR description, not in the test source
(per @azat's directive on PR ClickHouse#106261).

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106172&sha=778a2daac011ae48ed5c59773d688ba2f09431e6&name_0=PR
PR: ClickHouse#106172

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

groeneai commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

@PedroTadim Triage of the four failing CI checks on 778a2daa:

# Check Class Action
1 Stateless tests (amd_llvm_coverage, old analyzer, s3, DatabaseReplicated, WasmEdge) — 04304_multi_if_asterisk_table_function PR-caused (test gap) Fixed in 6699e02f — pin SET enable_analyzer = 1 in 04304
2 Stress test (arm_debug) — Assertion 'px != 0' failed (STID: 3685-4830) Chronic trunk bug Tracked separately; not caused by this PR
3 Integration tests (arm_binary, distributed plan, 2/4) — test_storage_rabbitmq/test.py::test_rabbitmq_commit_on_block_write Known flaky Not caused by this PR
4 Bugfix validation (functional tests) — The test runner was terminated unexpectedly Infra Runner crash, no per-test signal

1. Stateless old-analyzer failure (PR-caused, fixed)

The C++ fix for STID 2310-3791 lives in the new analyzer's resolveFunction — it snapshots the multiIf condition into a local copy before resolveExpressionNode. Under the old analyzer the same fuzzer queries reach a different error path and surface as UNKNOWN_IDENTIFIER (47) instead of UNSUPPORTED_METHOD (1):

Code: 47. DB::Exception: Missing columns: 'number' while processing: 'multiIf(*, moduloOrZero(number, NULL), 'a')', required columns: 'number', available columns: '_dummy'.

The variant name even includes old analyzer, so the runner injected enable_analyzer = 0. Pinning SET enable_analyzer = 1 at the top of 04304 makes the regression test exercise the path the C++ fix actually changed. 04303 already pins the analyzer per query, so it is unaffected.

While there I also stripped the long block-comments from both 04303 and 04304 — explanation belongs in the commit message and PR description, not in the test source (per @ azat's directive on PR #106261).

2. 'px != 0' (STID 3685-4830) — chronic trunk bug

CIDB 30d: 283 hits across 159 PRs, 35 master hits. Last seen 2026-06-04 16:10 UTC on independent runs. Not introduced by this PR — the diff is two test files plus two scoped analyzer/interpreter edits, none of which touches Sortable/SetVariantsTemplate or wherever px != 0 is asserted.

3. test_rabbitmq_commit_on_block_write — known flaky

CIDB 30d: 15 hits across 11 PRs, only 1 master hit. Classic RabbitMQ batch-commit timing flake unrelated to query analysis.

4. Bugfix validation — runner crash

test_context_raw is The test runner was terminated unexpectedly, no per-test diagnostic. Job-level infra failure, not a PR-caused regression of the bugfix-validation framework.

Result

Only (1) was PR-caused; the fixup is in 6699e02f. (2)–(4) are tracked elsewhere or are known flaky/infra noise. Will rerun CI on the new head and reply once the targeted variant goes green.

@groeneai

groeneai commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

@PedroTadim CI fully finished on 6699e02fda9f. Triage:

Check Class Action
Integration tests (amd_llvm_coverage, 3/5) infra All 1432 tests PASSED. Job died in the LLVM coverage post-processing step with LLVM ERROR: out of memory while merging profdata. Chronic — 20+ unrelated PRs hit the same OOM in the last 7 days (and LLVM Coverage dropped as a downstream dep). Not PR-caused.
Mergeable Check / PR / Report messages infra rollup Aggregator failure flowing from the integration job above.

143 pass / 4 fail / 0 pending / 20 skipping; the 4 fails are the single infra root + 3 rollups. No PR-caused failures, no new flaky tests to track.

@novikd novikd left a comment

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.

This PR includes changes across different Analyzers, which correspond to different closed GH issues.
One with the new analyzer: test does not produce an error, and code changes do not make sense.
One with the old analyzer: test produces LOGICAL_ERROR -- no crash.

I think we should close this PR because we do not change old analyzer (except crashses) and mutations now use new analyzer.


-- A multiIf whose condition is a real expression must keep working.
SELECT count() FROM numbers(multiIf(1, 5, 10), 1);
SELECT count() FROM numbers(multiIf(0, 5, 10), 1);

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.

This test does not produce any error on the master.

/// Skip on grouping sets: `getSortDescriptionFromGroupBy` calls `getColumnName` on
/// `GROUPING SETS` children which are `ASTExpressionList` and raise `LOGICAL_ERROR`.
/// Mirrors the guard on the `optimize_aggregation_in_order` branch above.
if (!group_by_info && settings[Setting::force_aggregation_in_order] && !query_analyzer->useGroupingSetKey())

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.

src/Interpreters/InterpreterSelectQuery.cpp should not be modified.

/// Snapshot the condition; `resolveExpressionNode` rebinds a matcher (`*`)
/// argument to an empty `ListNode`, which would corrupt the slot. Mirrors
/// the `is_special_function_if` pattern above.
QueryTreeNodePtr cond_node = multi_if_args[2 * pair];

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.

This change makes no sense. No point in keeping a reference to the MatcherNode because it is:

  • Unresolved
  • Can not be constant folded

@novikd novikd self-assigned this Jun 5, 2026
@novikd novikd closed this Jun 5, 2026
@groeneai

groeneai commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Acknowledging the policy reading.

The PR bundled two fixes against different analyzers:

Session: cron:clickhouse-ci-task-worker:20260605-163200

jh0x pushed a commit to jh0x/ClickHouse that referenced this pull request Jun 19, 2026
…h matcher inside table function

Closes: ClickHouse#43199
Closes: ClickHouse#97988
Closes: ClickHouse#100323
Related: ClickHouse#106172

The new-analyzer constant-folding helper for `multiIf` in `resolveFunction.cpp`
held its condition through `auto & cond_node = multi_if_args[2 * pair];`.
`resolveExpressionNode` rebinds its argument when the node is a matcher (`*`),
which silently swapped the slot in `multi_if_args` for an empty `ListNode`.

When a later argument throws `UNKNOWN_IDENTIFIER`, `resolveTableFunction`
catches it (`QueryAnalyzer.cpp:4329-4351`) and skips analysis, but the
original argument tree already carries the corrupted `ListNode` child.
`table_function_node_typed.toAST()` re-emits `multiIf(ExpressionList(), ...)`,
`parseArguments` accepts it, and `TableFunctionNumbers::evaluateArgument`
calls `evaluateConstantExpression`, which raises a `LOGICAL_ERROR` from
`IAST::appendColumnName` (`IAST.h:122`) when walking `ASTFunction`'s children.

Snapshot `cond_node` into a local copy before `resolveExpressionNode` so
matcher rebinding cannot leak back into the original slot. Mirrors the
`is_special_function_if` pattern at `resolveFunction.cpp:485`.

Reproducer (aborts master HEAD `378a25bb3b15`, returns clean
`UNSUPPORTED_METHOD` with the fix):

  SELECT * FROM numbers(multiIf(*, number, 1), 2);

CIDB shows 25+ unrelated PR sightings and 2 master hits in the last 30 days
(BuzzHouse `amd_debug` 2026-06-01 sha `7ca836227b`, AST fuzzer `amd_debug`
2026-05-30 sha `d2e9760e19`) under runner test name
"Logical error: Trying to get name of not a column: A (STID: 2310-*)".
The "A" is a placeholder; the underlying message is
"Trying to get name of not a column: ExpressionList".

This is a tightened resubmit of ClickHouse#106172 with only the new-analyzer half;
the bundled old-analyzer hunk in `InterpreterSelectQuery.cpp` and the
`04303` test were dropped per the "old analyzer frozen except crashes"
policy that closed ClickHouse#106172 on 2026-06-05.

Reference: ClickHouse#100436 (comment)

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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-bugfix Pull request with bugfix, not backported by default

Projects

None yet

3 participants