Skip to content

Add a test (was: Re-resolve function types in buildFilterActionsDAG when argument types change during filter pushdown through JOIN) - #99278

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix_right_join_using
Jul 19, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix_right_join_using

Conversation

@davenger

@davenger davenger commented Mar 11, 2026 •

Copy link
Copy Markdown
Member

Fix for "Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-3d11)"
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99096&sha=607070210d48036cdfe476268ad0ba7fa9304144&name_0=PR&name_1=AST+fuzzer+%28amd_tsan%29
#99096

Related issues: #96101, #95678, #89802, #98337

Recent related change: Commit cdb778b ("Fix filter pushdown through legacy JoinStep with join_use_nulls", 2026-02-28) restored a type equality check in get_available_columns_for_filter for the legacy JoinStep path. However, that fix only guards the direct column availability path — it does not cover the equivalent USING column substitution path, which is where our bug occurs.

Reproduction

  SET enable_analyzer = 1;
  SET query_plan_use_new_logical_join_step = 0;
  SET enable_join_runtime_filters = 0;

  CREATE TABLE t1 (id UInt8, value String) ENGINE = MergeTree ORDER BY tuple();
  CREATE TABLE t2 (id UInt16, value String) ENGINE = MergeTree ORDER BY tuple();
  CREATE TABLE t3 (id Nullable(UInt64), value String) ENGINE = MergeTree ORDER BY tuple();

  -- Triggers: "Unexpected return type from equals. Expected Nullable(UInt8). Got UInt8"
  SELECT 1 FROM t1 RIGHT JOIN t2 USING (id) LEFT JOIN t3 USING (id) WHERE id = 10;

What happens

  1. The WHERE id = 10 filter is built against the LEFT JOIN output where id is Nullable(UInt64) (from t3), so equals(id, 10) returns Nullable(UInt8).
  2. Filter pushdown pushes this filter through the LEFT JOIN, then through the RIGHT JOIN via equivalent USING columns — replacing __table2.id (UInt16) with __table1.id (UInt8).
  3. buildFilterActionsDAG clones function nodes but keeps the original function_base and result_type. After column replacement, the equals function now receives (UInt8, UInt8) arguments but still claims to return Nullable(UInt8).
  4. At execution time, ExpressionActions detects the mismatch and throws a LOGICAL_ERROR.

Fix:

In buildFilterActionsDAG's FUNCTION case, after building children from potentially-replaced nodes, compare the new argument types against the originals. If they differ, re-resolve the function via FunctionToOverloadResolverAdaptor::build to get the correct function_base and result_type.

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

Fix logical error in some cases during filter pushdown through JOIN

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Touches expression DAG construction during filter pushdown, which can affect query planning/execution types across JOIN queries. Change is localized but could alter inferred result types for functions in edge cases.

Overview
Prevents LOGICAL_ERROR type mismatches during filter pushdown through JOIN ... USING by detecting when cloned function nodes in buildFilterActionsDAG have different argument types after input-column replacement and re-building the function to refresh function_base/result_type.

Adds a stateless regression test covering RIGHT JOIN + LEFT JOIN with USING and nullable/non-nullable id columns to ensure equals/filter functions remain type-consistent after pushdown.

Written by Cursor Bugbot for commit c8e62f0. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.7.1.1187 (included in 26.7 and later)

davenger and others added 2 commits March 11, 2026 20:04
@clickhouse-gh

clickhouse-gh Bot commented Mar 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e024891]

Summary: ✅


AI Review

Summary

The current PR no longer contains the production fix described in its body; it only adds a stateless test. That test still uses SET query_plan_use_logical_join_step = 0, but both query_plan_use_logical_join_step and query_plan_use_new_logical_join_step are obsolete no-op settings in the current tree, so it does not force the legacy JoinStep path the PR claims to cover. As written, the PR neither demonstrates nor fixes the reported JOIN ... USING filter-pushdown bug, so I would request changes.

PR Metadata
  • Changelog category: CI Fix or Improvement is acceptable for a test-only change.
  • Changelog entry: not required for this category, and the current text is inaccurate because the diff no longer contains a production fix. Exact replacement: remove the current entry text, or replace it with Add a regression test for filter pushdown through JOIN with mismatched nullability in USING columns.
Findings

⚠️ Majors

  • [tests/queries/0_stateless/03838_right_join_using_nullable_type_mismatch.sql:8] The PR contract still says this reproduces the legacy JoinStep filter-pushdown type mismatch, but query_plan_use_logical_join_step is an obsolete setting (MAKE_OBSOLETE(...)) and there are no remaining planner consumers of it. The only code change in this PR was reverted, so this test now runs on the always-logical join path and can pass without covering the bug described in the PR body. Suggested fix: either restore a live reproducer/fix for the affected path, or rewrite the PR as a test for the current logical join behavior and update the title/body/comments accordingly.
Tests
  • ⚠️ Add the smallest proof that fails without the intended fix on a live code path: either a regression that still reaches the affected legacy JoinStep path, or a rewritten test and PR description showing that the current logical path was the real target. Right now the obsolete setting makes the added test weaker than the bug-fix claim.
Final Verdict
  • Status: ⚠️ Request changes

Comment thread src/Interpreters/ActionsDAG.cpp Outdated
@@ -3402,12 +3402,44 @@ std::optional<ActionsDAG> ActionsDAG::buildFilterActionsDAG(
auto [arguments, all_const] = getFunctionArguments(function_children);
auto function_base = function_overload_resolver ? function_overload_resolver->build(arguments) : node->function_base;

/// When input columns were replaced (e.g. equivalent USING columns pushed through JOIN),

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.

We perform type correction while pushing filters down through JOIN in
https://github.com/ClickHouse/ClickHouse/blob/master/src/Processors/QueryPlan/Optimizations/filterPushDown.cpp#L677 , perhaps the bug is located there.

In general, we should be able to push down transformations introduced by JOIN into the filter without re-resolving functions. For example, if we have a filter a :: Nullable(Int) -> greater(a, 0), and before the JOIN a had type Int while toNullable was added by the JOIN, then we push down a :: Int -> toNullable(a) AS a -> greater(a, 0) so the greater function remains unchanged.

Is this issue reproducible only with query_plan_use_new_logical_join_step = 0? I believe we can remove this setting after the next LTS release, since it is currently supported only as a fallback in case the new logic has issues.

@davenger davenger Mar 12, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it looks like it only happens with query_plan_use_new_logical_join_step = 0.
Not sure what will be the best solution if this code path is going to be removed soon. Maybe we should stop fuzzing it.

…AG.cpp`

Adapted the type re-resolution fix to use master's new `addFunction(FunctionBasePtr, ...)`
overload instead of the removed `addFunctionImpl` direct call.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@vdimir
vdimir disabled auto-merge March 24, 2026 09:30
@vdimir

vdimir commented Mar 24, 2026

Copy link
Copy Markdown
Member

Let's maybe rather disable query_plan_use_new_logical_join_step for randomization instead, given that query_plan_use_new_logical_join_step = 0 code path will be removed after next lts

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Mar 30, 2026
When a JOIN uses USING on columns with different types (e.g., UInt16 vs
UInt64), and the WHERE clause contains a function expression pushed into
the join condition (e.g., `round(a.y, a.x) = b.y`), the server could
crash with "Logical error: Unexpected return type from round".

Root cause: During physical join construction, `getSubDAG` and
`cloneSubdagWithInputs` extract per-side expression DAGs from the join's
expression_actions. When the USING column is promoted to a common type,
function nodes in the extracted DAG keep the promoted result_type and
function_base. After merging with the actual stream header (which has the
original non-promoted types), the function nodes have stale types that
don't match their actual arguments, causing a type mismatch during
header evaluation.

Fix: Add `ActionsDAG::resolveFunctionTypes()` method that re-resolves
function_base, result_type, and executable function for any FUNCTION node
whose argument types differ from what function_base expects. Call it after
DAG extraction in both `buildPhysicalJoinImpl` and `cloneSubdagWithInputs`.

This is the same class of bug as PR ClickHouse#99278 (which fixes
`buildFilterActionsDAG`) but in the physical join construction and
runtime filter code paths.

Closes ClickHouse#96101
@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread src/Interpreters/ActionsDAG.cpp Resolved
-- See: https://github.com/ClickHouse/ClickHouse/issues/96101

SET enable_analyzer = 1;
SET query_plan_use_new_logical_join_step = 0;

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.

query_plan_use_new_logical_join_step is obsolete in the current tree (MAKE_OBSOLETE(..., true)), and the released behavior is that the logical join step is always used. So this line no longer forces the legacy JoinStep path that reproduced the Unexpected return type exception; the test can pass on the current JoinStepLogical path without proving the old failure mode. Please either remove this obsolete setting and update the comments/PR metadata to describe the active path being tested, or add a regression that still reaches a live code path for this issue.

@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 334 line(s), 26 function(s) across 114 file(s) · Details

Top files
  • src/Storages/WindowView/StorageWindowView.cpp: 22 line(s), 1 function(s)
  • src/AggregateFunctions/AggregateFunctionWindowFunnel.cpp: 19 line(s), 6 function(s)
  • src/AggregateFunctions/AggregateFunctionQuantileGK.cpp: 14 line(s)
  • src/Functions/if.cpp: 14 line(s)
  • src/Storages/StorageReplicatedMergeTree.cpp: 13 line(s)

Full report

@alexey-milovidov alexey-milovidov self-assigned this Jul 19, 2026
@alexey-milovidov alexey-milovidov changed the title Re-resolve function types in buildFilterActionsDAG when argument types change during filter pushdown through JOIN Add a test (was: Re-resolve function types in buildFilterActionsDAG when argument types change during filter pushdown through JOIN) Jul 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 19, 2026
Merged via the queue into master with commit 125a5b1 Jul 19, 2026
351 of 352 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix_right_join_using branch July 19, 2026 15:50
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 19, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Add a test (was: Re-resolve function types in buildFilterActionsDAG when argument types change during filter pushdown through JOIN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

4 participants