Skip to content

Fix insert-select from delta lake cluster with replicated merge tree - #101299

Merged
scanhex12 merged 15 commits into
masterfrom
fix_delta_lake_pruning
Apr 1, 2026
Merged

scanhex12 merged 15 commits into
masterfrom
fix_delta_lake_pruning

Conversation

@scanhex12

@scanhex12 scanhex12 commented Mar 31, 2026 •

Copy link
Copy Markdown
Member

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

Fix insert-select from delta lake cluster with replicated merge tree

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.506
  • Backported to: 26.3.5.7, 26.2.8.12

@clickhouse-gh

clickhouse-gh Bot commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0bd16fa]

Summary: ❌

job_name test_name status info comment
Stress test (arm_msan) failure
Server died FAIL cidb
MemorySanitizer: use-of-uninitialized-value (STID: 1003-358c) FAIL cidb, issue

AI Review

Summary

This PR updates INSERT ... SELECT distributed write path for cluster-backed object storages (notably DeltaLake) to pass filter DAG/predicate into task pruning, including PREWHERE+WHERE and virtual columns. High-level verdict: good direction, but there is one blocker in exception handling and one major test-robustness gap.

Missing context
  • ⚠️ No CI logs / Praktika report links were provided in this review task, so runtime validation across integration suites could not be verified.
Findings

❌ Blockers

  • [src/Interpreters/InterpreterInsertQuery.cpp:872-881] The broad catch (...) in filter extraction can swallow interruption exceptions (e.g. cancellation/time/resource-limit related exceptions) and continue execution without pruning. This can violate expected interrupt semantics by allowing remote execution to proceed after an interrupt condition.
    Suggested fix: rethrow interruption/cancellation exceptions first, and only fallback-to-no-pruning for analysis-specific failures.

⚠️ Majors

  • [tests/integration/test_storage_delta/test.py:4935-5027] Cluster table cleanup is not protected by finally. If any assertion fails, replicated tables can be left in Keeper and cause later test instability.
    Suggested fix: wrap table lifecycle in try/finally and keep DROP TABLE ... ON CLUSTER in finally.
Tests
  • ⚠️ Add an assertion for pruning metrics on the second insert path (WHERE _path LIKE ...) using its query_id and system.query_log (ProfileEvents['DeltaLakePartitionPrunedFiles'] > 0). Current test checks row correctness, but a regression that disables pruning could still pass.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Broad fallback catch (...) currently risks masking interruption exceptions in an optional optimization path.
Compilation time ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Preserve interruption semantics by rethrowing cancellation/interrupt-related exceptions in distributedWriteIntoReplicatedMergeTreeOrDataLakeFromClusterStorage filter extraction path.
    2. Make integration-test cleanup failure-safe with try/finally around CREATE TABLE ... ON CLUSTER resources.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 31, 2026
@scanhex12 scanhex12 added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Mar 31, 2026
nodes_to_process.insert(nodes_to_process.end(), node->children.begin(), node->children.end());

if (auto * with_filter = dynamic_cast<SourceStepWithFilter *>(node->step.get()))
return with_filter->detachFilterActionsDAG()

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 line is missing a trailing ;, so the file does not compile.

Suggested fix:

-            return with_filter->detachFilterActionsDAG()
+            return with_filter->detachFilterActionsDAG();

@kssenii kssenii self-assigned this Mar 31, 2026
@scanhex12
scanhex12 requested a review from kssenii March 31, 2026 10:47
INSERT INTO {table_name}_dst (event_time, account_id, impressions)
SELECT event_time, account_id, impressions
FROM {table_function}
WHERE (event_time >= '2026-02-01') AND (event_time < '2026-02-02')

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 test validates simple partition pruning, but it does not cover the multi-source shape (WHERE scalar subquery) that adds another source step in the plan.

Please add a case like:

INSERT INTO ...
SELECT ...
FROM deltaLakeCluster(...)
WHERE event_time = '2026-02-01'
  AND (SELECT count() FROM system.one) = 1

Without this, the regression around selecting the wrong SourceStepWithFilter remains untested.

else if (select_query->where())
condition_ast = select_query->where()->clone();

if (condition_ast)

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.

The new pruning path now builds a filter DAG directly from prewhere/where and forwards any analysis exception to the caller. Because this optimization is optional, this can turn valid INSERT ... SELECT queries into exceptions (for example, predicates that are valid for normal execution but not representable in this isolated filter-analysis pass).

Please treat filter extraction here as best-effort: if DAG construction fails, log at debug/trace and continue with predicate = nullptr / filter_actions_dag = nullptr so the query still executes without pruning.

@scanhex12

Copy link
Copy Markdown
Member Author

Issue about virtual columns here: #101515

filter_dag = ExpressionAnalyzer(condition_ast, syntax, local_context).getActionsDAG(true, true);
predicate = filter_dag->getOutputs().at(0);
}
catch (...)

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.

Catching all exceptions here and silently continuing can swallow cancellation-like exceptions (QUERY_WAS_CANCELLED, timeout/resource-limit exceptions) and proceed with remote execution even though the query should stop.

This optimization should stay best-effort for analysis failures, but cancellation/interrupt exceptions should be rethrown. Please narrow the catch or explicitly rethrow cancellation exceptions before falling back to nullptr predicate.

robot-clickhouse added a commit that referenced this pull request Apr 1, 2026
robot-clickhouse added a commit that referenced this pull request Apr 1, 2026
@scanhex12 scanhex12 added v25.12-must-backport v25.12-affected pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR and removed v25.12-must-backport pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Apr 1, 2026
@fm4v fm4v removed the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 2, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 2, 2026
kssenii added a commit that referenced this pull request Apr 7, 2026
Backport #101299 to 26.2: Fix insert-select from delta lake cluster with replicated merge tree
kssenii added a commit that referenced this pull request Apr 8, 2026
Backport #101299 to 26.3: Fix insert-select from delta lake cluster with replicated merge tree
robot-clickhouse added a commit that referenced this pull request Apr 9, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 9, 2026
@groeneai groeneai mentioned this pull request Jun 7, 2026
1 task done
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 26, 2026
`INSERT INTO <replicated table> SELECT ... FROM <cluster table function>` with
`parallel_distributed_insert_select` distributes the read tasks from the
initiator, and the initiator prunes the object list with the `WHERE`/`PREWHERE`
condition of the `SELECT`.

That condition is analyzed on its own, out of the context of the query, so
aliases introduced in the `WITH` clause or in the `SELECT` list were not
resolvable. A perfectly normal query such as

    INSERT INTO wikistat
    WITH splitByChar(' ', line) AS values
    SELECT ...
    FROM s3Cluster(default, 'https://.../pageviews-*.gz', 'LineAsString')
    WHERE length(values) >= 3

failed the analysis with `UNKNOWN_IDENTIFIER`, which disabled the pruning and,
worse, wrote an `<Error>` message with a full stack trace into the server log.

Substitute the aliases on a copy of the `SELECT` before extracting the
condition, the same way the regular query analysis does. The condition above
becomes `length(splitByChar(' ', line)) >= 3`, which analyzes fine, so the
pruning now works for aliased conditions and nothing is logged.

The analysis is still best-effort - a condition that cannot be analyzed in
isolation (for example, one that qualifies columns with the table alias) falls
back to no pruning, which is correct, just slower. That is an expected outcome
rather than an error, so it is logged at the `debug` level now.

Related: ClickHouse#101299
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v25.12-affected v25.12-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants