Skip to content

Fix fuzzer grouping by use nulls error - #102597

Closed
Fgrtue wants to merge 11 commits into
masterfrom
fix_fuzzer_grouping_by_use_nulls_error
Closed

Fgrtue wants to merge 11 commits into
masterfrom
fix_fuzzer_grouping_by_use_nulls_error

Conversation

@Fgrtue

@Fgrtue Fgrtue commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Resolves #102273.

The investigation shows that the error happens when we use setting group_by_use_nulls = 1, and correlated subqeuries, then we wrap the types of grouped elements into Nullable, while correlated subquery does not account for such a change.

That is why the fix is: after we built the plan for outer query, and start building correlated subquery, provide the header of columns to the correlated subquery, so that when visiting correlated column we fix its type if it diverges from the one in the outer query.

@clickhouse-gh

clickhouse-gh Bot commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4d75b06]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, targeted) FAIL
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
Stateless tests (amd_asan_ubsan, flaky check) FAIL
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
Stateless tests (amd_tsan, flaky check) FAIL
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
Stateless tests (amd_msan, flaky check) FAIL
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
Stateless tests (amd_debug, flaky check) FAIL
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
04092_correlated_subquery_grouping_sets_nullable FAIL cidb
BuzzHouse (amd_tsan) FAIL
Logical error: Mutation of Memory table produced incomplete output: A (STID: 2380-2bfd) FAIL cidb

AI Review

Summary

This PR fixes the planner type mismatch for correlated subqueries that reference group_by_use_nulls grouping keys by passing the post-aggregation outer header into correlated-subquery planning. The type propagation part is reasonable, but it exposes nullable correlated keys without preserving their semantics when the decorrelated result is joined back to the outer stream, so the current patch can still return wrong results for the ROLLUP / GROUPING SETS rows it is meant to support.

Findings

❌ Blockers

  • [src/Planner/PlannerActionsVisitor.cpp:907, src/Planner/PlannerCorrelatedSubqueries.cpp:504] The PR now allows correlated placeholders to take the nullable type produced for ROLLUP / GROUPING SETS rows, but buildLogicalJoin still reconnects the decorrelated subquery with JoinConditionOperator::Equals. Nullable join keys do not match NULL values, so a ROLLUP row can evaluate the correlated subquery with number = NULL and then lose that subquery row when the result is joined back, filling the scalar/EXISTS result from the OUTER JOIN default instead. Fix this by either keeping nullable correlated keys unsupported, or by making the decorrelation key semantics handle nullable keys end to end and adding an executed-result regression.
Tests
  • ⚠️ The new stateless test only checks EXPLAIN actions for the corrected Nullable type. Please add the smallest executed query that proves the ROLLUP / GROUPING SETS NULL row keeps the correlated-subquery result, for example a query shaped like SELECT number, (SELECT isNull(number)) FROM numbers(1) GROUP BY number WITH ROLLUP with group_by_use_nulls = 1.
Final Verdict

Status: ❌ Block

Minimum required action: fix or reject nullable correlated-key decorrelation semantics, then cover the executed query result rather than only the action DAG type.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 13, 2026
@novikd novikd self-assigned this Apr 14, 2026
Comment thread tests/config/config.d/clusters.xml Resolved
Comment thread src/Planner/Planner.cpp Resolved
@Fgrtue

Fgrtue commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

The error stopped causing the failure of the server. and now just silently lives in the query plan. I adjusted the test so that it is seen what should be the result

/// actual post-aggregation type from the outer query's plan header if available.
auto column_type = node->getColumnType();
const auto & outer_header = planner_context->getGlobalPlannerContext()->getOuterQueryHeader();
if (outer_header && outer_header->has(column_node_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.

This lets correlated keys become Nullable (for example the rollup/grouping-sets rows produced by group_by_use_nulls), but the decorrelation join still compares the correlated identifiers with ordinary Equals in buildLogicalJoin. HashJoin explicitly skips rows with NULLs in join keys, so a NULL outer key will not join back to the subquery row that was evaluated with that same NULL value.

That changes correlated-subquery semantics for the rows this PR is trying to support. A minimal shape is:

SET enable_analyzer = 1, allow_experimental_correlated_subqueries = 1, group_by_use_nulls = 1;
SELECT number, (SELECT isNull(number))
FROM numbers(1)
GROUP BY number WITH ROLLUP;

The rollup row should evaluate the subquery with number = NULL and return 1, but with a regular join predicate the NULL key is treated as no match and the scalar result is filled from the outer join default instead. Please make the correlated-key join NULL-safe when the key type is nullable, or keep these nullable-key cases unsupported until the join semantics are fixed, and add a regression that checks the executed query result rather than only the EXPLAIN actions type.

…lickHouse/ClickHouse into fix_fuzzer_grouping_by_use_nulls_error
@clickhouse-gh

clickhouse-gh Bot commented May 29, 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: 40/41 (97.56%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Dear @novikd, 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.

novikd
novikd previously requested changes Jun 5, 2026
Comment on lines +17 to +23
SELECT replaceRegexpOne(trim(explain), ' : [0-9]+$', '') AS planner_action
FROM (
EXPLAIN actions = 1, compact = 0, pretty = 0
SELECT bitNot((SELECT bitNot(number)))
FROM numbers(1)
GROUP BY number WITH ROLLUP
)

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.

It should be fixed in the QueryAnalyzer not in the Planner. The analyzed Query Tree is invalid: https://fiddle.clickhouse.com/0782ad74-9b45-4eff-a6e6-8aeb0c7ee2e5

@clickhouse-gh

clickhouse-gh Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Dear @novikd, 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.

@alexey-milovidov

Copy link
Copy Markdown
Member

I verified the current state of this on master (checked at e366285fd4d, 2026-07-18):

Regarding the AI review's concern about NULL join keys losing rows during decorrelation: on current master I could not construct a wrong result. Supported correlated shapes (e.g. (SELECT bitNot(number))) return correct values for the NULL grouping rows, and shapes that could distinguish a join miss from a real evaluation (e.g. (SELECT coalesce(number, 42)), (SELECT isNull(number))) are rejected with a graceful Cannot evaluate correlated scalar subquery error rather than returning wrong data.

Since the GROUPING SETS shape from the original fuzzer query was not covered by the tests of #100365 (test 04053 covers WITH ROLLUP/WITH CUBE), I added a regression test for it in #110956, which also closes the original issue.

So this PR appears to be fully superseded by #100365 and can be closed. Thank you for the investigation here, @Fgrtue — it correctly identified the type divergence; the analyzer-side fix ultimately landed via the other PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-343c)

3 participants