Skip to content

buildFilterActionsDAGImpl names grow 2^depth: PR's query OOM-kills server #123724

Open

Description

@clickgapai

Describe what's wrong

  • Planning a join on keys built as nested x + x aliases allocates column names that grow exponentially with nesting depth.
  • Trigger: default enable_join_runtime_filters = 1, join_use_nulls = 1, and an inner LEFT JOIN converted to INNER by the transitive pass.
  • Names should stay linear in depth; at depth 24 planning takes 6.1 s and 9.4 GB.
  • At depth 36 the server was OOM-killed, and max_memory_usage does not stop the plan-time growth.

Root cause: buildFilterActionsDAGImpl (ActionsDAG.cpp:4438) rebuilds every FUNCTION node with an empty result_name, so addFunctionImpl (ActionsDAG.cpp:662) concatenates child names. On a DAG with shared nodes like plus(X, X) the name length grows with the number of paths, not nodes.

Analysis details (evidence, affected locations, impact)

Why we believe this is a bug: When the transitive pass turns the inner LEFT JOIN into INNER, the runtime filter RF1(a6_6) for the outer join gets pushed through the merged x + x expression and the converted join to the right stream. tryAddJoinRuntimeFilter re-runs filter push-down regardless of settings. The rebuilt filter DAG gets fresh names. buildFilterActionsDAGImpl (ActionsDAG.cpp:4438) calls addFunction with an empty result_name, and addFunctionImpl (ActionsDAG.cpp:655-664) builds the name by concatenating the child names. So plus(X, X) doubles the name at every level: the DAG stays linear in size, but its name strings grow as 2^depth. EXPLAIN shows the result: the pushed-down filter column is RF1(plus(plus(...toNullable(val)...))) expanded down to the leaves.

Affected locations:

Impact: One read-only SELECT at default settings allocates name strings that grow exponentially with alias-nesting depth during planning. On master this needs join_use_nulls = 1 and the inner LEFT JOIN from #123509 (where this was found; not confirmed as the cause)'s test; the user-written INNER JOIN form needs no setting and also hits 26.8. max_memory_usage does not stop the plan-time string growth. At depth 36 the kernel OOM killer killed the server process.

Does it reproduce on most recent release?

Confirmed on master (501d978d7b9d); not checked on a release.

How to reproduce

The scaling script allocates tens of GB, so run it in a sandbox; the user-written INNER JOIN form needs no setting at all.

▶ Run on ClickHouse Fiddle

-- Test: a runtime filter pushed through a join on a key built as `x + x` over nested subqueries keeps column names linear in depth.

SET enable_analyzer = 1;
SET join_use_nulls = 1;
SET enable_join_runtime_filters = 1;
SET query_plan_convert_outer_join_to_inner_join = 1;
SET query_plan_convert_outer_join_to_inner_join_transitively = 1;
SET query_plan_join_swap_table = 0;
SET enable_parallel_replicas = 0;

SELECT count() > 0, max(length(explain)) < 5000
FROM
(
    EXPLAIN actions = 1
    SELECT count()
    FROM
    (
        SELECT a1_6 + a1_6 AS a2_1, a2_1 + a2_1 AS a2_2, a2_2 + a2_2 AS a2_3, a2_3 + a2_3 AS a2_4, a2_4 + a2_4 AS a2_5, a2_5 + a2_5 AS a2_6
        FROM
        (
            SELECT r.val + r.val AS a1_1, a1_1 + a1_1 AS a1_2, a1_2 + a1_2 AS a1_3, a1_3 + a1_3 AS a1_4, a1_4 + a1_4 AS a1_5, a1_5 + a1_5 AS a1_6
            FROM (SELECT number AS id FROM numbers(2)) AS l
            LEFT JOIN (SELECT number AS id, number - 1 AS val FROM numbers(1, 1)) AS r ON l.id = r.id
        )
    ) AS g
    INNER JOIN (SELECT 0 AS val) AS k ON g.a2_6 = k.val
)
WHERE explain LIKE '%Filter column: RF%';

Verified: this block reproduces on current master (501d978d7b9d) when pasted as is.

Expected behavior

The pushed-down runtime filter column name stays short and planning memory stays linear in nesting depth.

Expected output of the reproducer above:

1	1

Error message and/or stacktrace

The runtime filter column name expands the whole expression tree, reaching 376,866 chars in EXPLAIN, and memory grows to tens of GB.

Actual output of the reproducer above on master (501d978d7b9d):

1	0
Suggested fix

In buildFilterActionsDAGImpl, keep node->result_name for nodes with no replaced input in their subtree. For the remaining nodes, generate a bounded name, e.g. a per-node unique suffix, instead of concatenating the child names. Trade-off: those rebuilt nodes stop spelling out their expression in EXPLAIN.

Additional context

Same pattern as #122613 (found by: vector, fix_path; same version: 26.8), #123432 (found by: lexical, vector, fix_path; vector: cosine distance 0.15 (agrees with the lexical leg)).

Open risks:

  • buildFilterActionsDAG (ActionsDAG.cpp:4504) is also reached from SourceStepWithFilter, ReadFromMergeTree and propagatePredicateAcrossEquiJoin, so any filter pushed onto a source over such a DAG gets the same names. Not tested here apart from the join path.
  • EXPLAIN actions = 1 also prints the merged join condition with exponential length: the Join conditions: line is 110,643 chars at depth 12, with or without runtime filters. This only affects EXPLAIN.

Found during automated review of PR #123509; the bug predates that PR (it reproduces on the master build just before it merged), so the introducing change is not identified yet. Severity P1 · Finding h_pr123509_001

Activity

  1. added
    comp-query-execution
    on Oct 3, 2026
  2. groeneai commented on Oct 4, 2026

    @groeneai
    Collaborator

    @KochetovNicolai, a design question before I send a fix.

    This is wider than the join path. A plain WHERE over such a chain, with no join at all, is OOM-killed at default settings on master (3ade111) and on 26.9.9.28:

    q="SELECT number - 1 AS val FROM numbers(1, 1)"; s=val
    for i in $(seq 1 36); do q="SELECT $s + $s AS a$i FROM ($q)"; s=a$i; done
    clickhouse local -q "SELECT count() FROM ($q) WHERE $s <= 0"   # killed after ~10 s; with 12 levels: 1 in 0.4 s

    Written as one SELECT, the same expression is rejected by max_expanded_ast_elements (Query tree is too big. Maximum: 500000). The nested form passes the analyzer, and the plan then composes the levels into one DAG with 36 plus nodes but 2^36 paths. Each consumer that walks it per path blows up:

    • buildFilterActionsDAGImpl concatenates names: reached from SourceStepWithFilter::applyFilters (the query above) and from splitActionsForJOINFilterPushDown (this issue). query_plan_filter_push_down = 0 does not avoid it.
    • With query_plan_merge_expressions = 0 the query instead fails in cloneDAGWithInversionPushDown (KeyCondition, only INPUT nodes are memoized), called from ReadFromSystemNumbersStep::makePipe: Code 241 after 18 s.
    • onlyDependsOnAvailableColumns (partial JOIN push-down) and appendColumnNameWithoutAlias (RPNBuilder) recurse per path too.

    So keeping result_name in buildFilterActionsDAGImpl, as suggested above, would move the OOM into index analysis. I see three options:

    • A. Do not build such a predicate: the steps that merge a filter with the expressions below it (tryMergeExpressions, compose in optimizePrimaryKeyConditionAndLimit) skip the merge when the result's expanded size exceeds max_expanded_ast_elements.
    • B. Keep the plan as is: JOIN push-down and the read-side filter analysis (applyFilters, PREWHERE) skip a conjunct whose expanded size exceeds max_expanded_ast_elements, via one memoized helper. The filter is still evaluated where it is.
    • C. Make each consumer linear: keep names in buildFilterActionsDAGImpl, memoize cloneDAGWithInversionPushDown, and stop RPNBuilder from building a key name that can never match a key.

    I would do B: it changes only the code that walks per path, and it reuses the limit the analyzer already applies. Which do you prefer: A, B or C?

  3. groeneai commented on Oct 6, 2026

    @groeneai
    Collaborator

    The stress test fuzzer now reaches this in CI: Stress test (amd_msan) on #100173 (unrelated to that PR) was killed by the kernel OOM killer on a fuzzed 05317_exponential_null_propagation query whose join condition became ON g.a6_6 > k.val. The memory limit did not stop it.

    On master (44b568f, debug build) with join_use_nulls = 1 and enable_join_runtime_filters = 0, the same query is OOM-killed in a 16 GiB cgroup after 13 s despite max_server_memory_usage = 12G. With query_plan_filter_push_down = 0 it returns 0 in 1 s.

    Query
    SELECT count() FROM (SELECT a5_6 + a5_6 AS a6_1, a6_1 + a6_1 AS a6_2, a6_2 + a6_2 AS a6_3, a6_3 + a6_3 AS a6_4, a6_4 + a6_4 AS a6_5, a6_5 + a6_5 AS a6_6 FROM (SELECT a4_6 + a4_6 AS a5_1, a5_1 + a5_1 AS a5_2, a5_2 + a5_2 AS a5_3, a5_3 + a5_3 AS a5_4, a5_4 + a5_4 AS a5_5, moduloLegacy(a5_5, a5_5) AS a5_6 FROM (SELECT a3_6 + a3_6 AS a4_1, a4_1 + a4_1 AS a4_2, a4_2 + a4_2 AS a4_3, a4_3 + a4_3 AS a4_4, a4_4 + a4_4 AS a4_5, a4_5 + a4_5 AS a4_6 FROM (SELECT a2_6 + a2_6 AS a3_1, a3_1 + a3_1 AS a3_2, a3_2 + a3_2 AS a3_3, a3_3 + a3_3 AS a3_4, moduloOrNull(a3_4, a3_4) AS a3_5, a3_5 + a3_5 AS a3_6 FROM (SELECT a1_6 + a1_6 AS a2_1, a2_1 + a2_1 AS a2_2, a2_2 + a2_2 AS a2_3, a2_3 + a2_3 AS a2_4, a2_4 + a2_4 AS a2_5, a2_5 + a2_5 AS a2_6 FROM (SELECT r.val + r.val AS a1_1, a1_1 + a1_1 AS a1_2, a1_2 + a1_2 AS a1_3, a1_3 + a1_3 AS a1_4, a1_4 + a1_4 AS a1_5, a1_5 + a1_5 AS a1_6 FROM (SELECT number AS id FROM numbers(2)) AS l LEFT JOIN (SELECT number AS id, number - 1 AS val FROM numbers(1, 1048577)) AS r ON r.id = l.id)))))) AS g INNER JOIN (SELECT 0 AS val) AS k ON g.a6_6 > k.val

    The fix is waiting on the choice between A, B and C above.

  4. rschu1ze commented on Oct 7, 2026

    @rschu1ze
    Member
  5. clickgapai commented on Oct 7, 2026

    @clickgapai
    ContributorAuthor

    No. #123509 is already in master (the latest commit, merged Oct 2), and this still reproduces on the latest commit.

    • Fix exponential planning in transitive outer-to-inner JOIN conversion #123509 only touches convertOuterJoinToInnerJoinTransitively.cpp. The growth is in buildFilterActionsDAGImpl, which Fix exponential planning in transitive outer-to-inner JOIN conversion #123509 doesn't change:
      • It rebuilds every FUNCTION node except indexHint with an empty name (ActionsDAG.cpp:4398).
      • addFunctionImpl then builds the name by joining the child names (:661), so each x + x level doubles its length.
    • 05317_exponential_null_propagation passes because it sets --enable_join_runtime_filters 0 (line 29). With the default value 1, the same query still blows up:
      • Longest Filter column: RF… line in EXPLAIN actions = 1: 94,242 / 376,866 / 1,507,362 chars at depth 12 / 14 / 16.
      • Peak RSS in clickhouse local: 1.08 GB at depth 20 and 2.78 GB at depth 22. With runtime filters off it stays flat at about 275 MB.
    • The no-join WHERE case from groeneai's comment above also still grows on this build: 1.69 GB and 2.58 s at depth 20.

    Repro: run 05317's query with --enable_join_runtime_filters 1.

  6. groeneai commented on Oct 7, 2026

    @groeneai
    Collaborator

    No. Both reproductions above ran on master builds that already contain #123509 (3ade111 and 44b568f, both after its merge commit bdfd200). I re-checked just now on 44b568f: the join-free WHERE example from my first comment is still OOM-killed in a 16 GiB cgroup after 10 s despite max_server_memory_usage = 12G, also with query_plan_filter_push_down = 0.

    The fix is waiting on the choice between A, B and C in #123724 (comment).

  7. rschu1ze commented on Nov 23, 2026

    @rschu1ze
    Member

    @groeneai Send three PRs for A, B and C each. We need to see the code.

  8. groeneai commented on Nov 23, 2026

    @groeneai
    Collaborator

    Acknowledged. I'm preparing three separate PRs, one per option (A, B and C), and I'll link all three here once they are open.

  9. groeneai commented on Nov 24, 2026

    @groeneai
    Collaborator

    Option A: #134010. It fixes every measured shape (the join-free WHERE chain over numbers() and MergeTree, the JOIN shapes and the fuzzed 05317 query); the price is that a filter whose condition would exceed max_expanded_ast_elements is not pushed down or used for index or projection analysis.

  10. groeneai commented on Nov 24, 2026

    @groeneai
    Collaborator

    Option B: #134034. It fixes the shapes where the composed conjunct is pushed below a JOIN (the non-equi JOIN ON query, the same over MergeTree, the fuzzed 05317 query), but not the join-free WHERE chain over numbers() or MergeTree, where leaving a conjunct out of the read's own filter gives wrong results (details in the PR).

  11. groeneai commented on Nov 24, 2026

    @groeneai
    Collaborator

    Option C: #134059. It fixes every measured shape and keeps every optimization (the filter is still pushed down and used for index analysis); the price is nine changed places in 13 files, and the consumers no repro reaches (skip-index name lookups, text-index direct read and a few more) stay unfixed and are listed in the PR.

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-execution

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions