Skip to content

Logical error "Cannot fold actions for projection" with join_use_nulls, outer-to-inner JOIN conversion and join reordering #127331

Closed

Description

@jierfei1007

Company or project name

Fxiaoke

Describe what's wrong

On 26.8.31.2 (official build), a query with three relations fails during query-plan optimization:

[22000] Code: 49. DB::Exception: Cannot fold actions for projection. Node equals(__table3.extend_obj_data_id, __table5._id) requires input __table5._id which does not belong to projection. (LOGICAL_ERROR) (version 26.8.31.2 (official build))

This is not related to MergeTree projections: the exception comes from ActionsDAG::foldActionsByProjection in the join-order optimizer.

The query has the shape (A INNER JOIN B) LEFT JOIN C with join_use_nulls = 1. The WHERE clause null-rejects C, so query_plan_convert_outer_join_to_inner_join changes the LEFT JOIN to INNER. However, the toNullable(C.*) actions created for the original LEFT JOIN remain in JoinStepLogical::actions_after_join, so typeChangingSides() still reports a type-changing right side. During join reordering, if C is joined before the join predicate to B is applied, chooseJoinOrder replaces C._id with toNullable(C._id) in current_input_nodes. The later predicate equals(B.extend_obj_data_id, C._id) then references an input that is no longer in current_inputs.

The same code path appears to be present in 26.8.33.10 and 26.9.24.8, and in current master.

Does it reproduce on the most recent release?

Yes

How to reproduce

Version: 26.8.31.2 (official build), Linux x86_64, default analyzer.

The original statement was INSERT INTO ... SELECT. The SELECT below preserves the join order, filters and settings of the failing query; identifiers and data are sanitized.

CREATE TABLE object_data
(
    tenant_id String,
    owner String,
    bi_sys_flag UInt8,
    out_data_auth_code String,
    create_time Int64,
    out_tenant_id String,
    created_by String,
    bi_sys_batch_id UInt64,
    data_own_department String,
    data_auth_code String,
    object_describe_api_name String,
    out_owner String,
    value0 String,
    value1 String,
    value21 String,
    _id String,
    is_deleted UInt8,
    bi_sys_ods_part String
)
ENGINE = MergeTree
ORDER BY (tenant_id, object_describe_api_name, _id);

CREATE TABLE org_employee_user
(
    tenant_id String,
    extend_obj_data_id String,
    id String,
    bi_sys_flag UInt8,
    user_id String,
    bi_sys_batch_id UInt64,
    is_deleted UInt8,
    bi_sys_ods_part String
)
ENGINE = MergeTree
ORDER BY (tenant_id, id);

INSERT INTO object_data VALUES
('1', 'o1', 1, 'dac1', 1, 'ot', 'u1', 1, 'dod', 'dac1', 'object_test__c', 'oo1', 'o1', '', 'v21', 'id1', 0, 's');

INSERT INTO org_employee_user VALUES
('1', 'x1', 'e1', 1, 'u1', 1, 0, 's');

INSERT INTO object_data VALUES
('1', 'o2', 1, 'dac2', 1, 'ot', 'u2', 1, 'dod', 'dac2', 'org_employee_user_udef', 'oo2', 'e1', 'x', 'v21b', 'x1', 0, 's');

SELECT
    coalesce(object_data_1.value0, '') AS object_id,
    coalesce(object_data_1.value21, '') AS dim_string_1,
    coalesce(object_data_2.value1, '') AS value1,
    coalesce(object_data_2._id, '') AS c_id,
    coalesce(COUNT(object_data_1.value0), 0) AS agg_count_1
FROM
(
    SELECT tenant_id, owner, bi_sys_flag, out_data_auth_code, create_time,
           out_tenant_id, created_by, bi_sys_batch_id, data_own_department,
           data_auth_code, object_describe_api_name, out_owner, value0, value21
    FROM object_data
    PREWHERE 1 = 1 AND bi_sys_ods_part IN ('s')
             AND tenant_id = '1'
             AND object_describe_api_name = 'object_test__c'
             AND bi_sys_flag = 1
    WHERE 1 = 1 AND is_deleted = 0
) AS object_data_1
INNER JOIN
(
    SELECT tenant_id, extend_obj_data_id, id, bi_sys_flag, user_id, bi_sys_batch_id
    FROM org_employee_user
    PREWHERE 1 = 1 AND bi_sys_ods_part IN ('s')
             AND tenant_id = '1'
             AND bi_sys_flag = 1
    WHERE 1 = 1 AND is_deleted = 0
) AS org_employee_user_1
ON object_data_1.created_by = org_employee_user_1.user_id
   AND object_data_1.tenant_id = org_employee_user_1.tenant_id
LEFT JOIN
(
    SELECT tenant_id, is_deleted, bi_sys_flag, value1, object_describe_api_name,
           value0, _id, bi_sys_batch_id
    FROM object_data
    PREWHERE 1 = 1 AND bi_sys_ods_part IN ('s')
             AND tenant_id = '1'
             AND object_describe_api_name = 'org_employee_user_udef'
             AND bi_sys_flag = 1
    WHERE 1 = 1 AND is_deleted = 0
) AS object_data_2
ON org_employee_user_1.id = object_data_2.value0
   AND org_employee_user_1.extend_obj_data_id = object_data_2._id
   AND org_employee_user_1.tenant_id = object_data_2.tenant_id
WHERE object_data_1.tenant_id = '1'
  AND (org_employee_user_1.extend_obj_data_id = ''
       OR object_data_2._id > '0'
       OR org_employee_user_1.extend_obj_data_id IS NULL)
  AND object_data_2.value1 IN ('x')
GROUP BY object_id, dim_string_1, value1, c_id
SETTINGS final = 1,
         optimize_move_to_prewhere_if_final = 1,
         join_use_nulls = 1,
         join_algorithm = 'grace_hash',
         grace_hash_join_initial_buckets = 4,
         max_bytes_before_external_sort = 1073741824,
         max_bytes_before_external_group_by = 1073741824,
         group_by_two_level_threshold_bytes = 134217728;

Expected behavior

The query should complete normally. Disabling either query_plan_convert_outer_join_to_inner_join or query_plan_optimize_join_order_limit avoids the logical error.

Error message and/or stacktrace

[22000] Code: 49. DB::Exception: Cannot fold actions for projection. Node equals(__table3.extend_obj_data_id, __table5._id) requires input __table5._id which does not belong to projection. (LOGICAL_ERROR) (version 26.8.31.2 (official build))

Related issues and pull requests

Related: #110121

Additional context

The conversion is performed by tryConvertOuterJoinToInnerJoin (src/Processors/QueryPlan/Optimizations/convertOuterJoinToInnerJoin.cpp), which only assigns join_operator.kind = JoinKind::Inner and leaves the toNullable actions created by JoinStepLogical::addToNullableIfNeeded in place.

Join reordering then uses typeChangingSides() and rewrites the relation input in chooseJoinOrder (src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp). The guard added in #102516 only applies when flattening a child join and does not cover a top-level outer join that was converted to INNER.

Workaround used in production: add query_plan_convert_outer_join_to_inner_join = 0 to the query SETTINGS; if that is not possible, query_plan_optimize_join_order_limit = 0 also avoids the failing path.

Version info

Activity

  1. added
    potential bug
    on Oct 22, 2026
  2. added
    comp-query-optimizer
    on Oct 22, 2026
  3. PedroTadim commented on Oct 22, 2026

    @PedroTadim
    Member

    This doesn't reproduce on master it seems. @groeneai can you indentify which PR needs to be backported to 26.8?

  4. groeneai commented on Oct 22, 2026

    @groeneai
    Collaborator

    It isn't fixed on master, only the default join order changed.

    • Bisecting master CI builds, it stops failing within a 5-merge window whose only engine change is Don't merge clauses with FUNCTION_UNKNOWN in ConditionSelectivityEstimator #118137. That PR changes ConditionSelectivityEstimator, whose row estimates feed the join-order optimizer (RelationStatisticsEstimator.cpp), so the greedy algorithm picks another order. The failing path is untouched.
    • Current master (31d10e5) still throws the same exception when another order is forced: add query_plan_optimize_join_order_randomize = 11 to the query's SETTINGS (also 13, 14, 18, 20, 22). Same on 26.9 and 26.7. With query_plan_convert_outer_join_to_inner_join = 0 added it passes.
    • 26.6 passes with all of those seeds. The path comes from my Convert outer join to inner when a both-sides filter is null-rejecting under join_use_nulls=1 #110121 (outer-to-inner conversion under join_use_nulls = 1, 26.7+), and the analysis in the report matches: the converted join keeps its toNullable actions, so the reorderer applies that type change when object_data_2 is joined before the ON conjunct that references object_data_2._id.

    So backporting #118137 would only hide it. I'm preparing a fix on master and will link the PR here.

  5. groeneai commented on Oct 22, 2026

    @groeneai
    Collaborator

    Fix on master: #127416.

    Correction to my previous comment: #110121 is not the only way in. A smaller three-table query (in the PR description) with only join_use_nulls = 1 fails with default settings on 26.6.8.7, 26.7, 26.8 and 26.9, and on 26.5 with enable_join_transitive_predicates = 1, so 26.6 is affected too.

  6. added
    external
    on Oct 23, 2026
  7. added a commit that references this issue on Nov 2, 2026
    b159477
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai_p1comp-query-optimizerexternalpotential bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions