Skip to content

fix(sql): handle correlated subqueries over union#19607

Merged
KKould merged 3 commits intodatabendlabs:mainfrom
sundy-li:issue-19574-20260324-2019
Apr 8, 2026
Merged

fix(sql): handle correlated subqueries over union#19607
KKould merged 3 commits intodatabendlabs:mainfrom
sundy-li:issue-19574-20260324-2019

Conversation

@sundy-li
Copy link
Copy Markdown
Member

@sundy-li sundy-li commented Mar 24, 2026

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

  • preserve correlated-column mappings across decorrelated UNION / UNION ALL branches
  • add replay and service regressions for correlated EXISTS subqueries over set operations

Changes

  • compute branch-level need_cross_join in flatten_sub_union_all whenever a branch does not cover the full correlated key set
  • replace unwrap()-based correlated mapping lookups with get_derived() so missing mappings surface as errors instead of panics
  • add shared optimizer replay cases for the reported UNION and UNION ALL repros
  • add service-side regression tests for the same two query shapes

Tests

  • Unit Test

  • Logic Test

  • Benchmark Test

  • No Test - Pair with the reviewer to explain why

  • TEST_SUBDIR=regressions cargo test --package databend-common-sql --test it -- planner::test_lite_replay_service_optimizer_cases --exact --nocapture

  • cargo fmt --all

Type of change

  • Bug fix

Fixes #19574


This change is Reviewable

@github-actions github-actions bot added the pr-bugfix this PR patches a bug in codebase label Mar 24, 2026
@sundy-li sundy-li added agent-reviewable Ready for agent review agent-approved Approved by agent and removed agent-reviewable Ready for agent review labels Mar 24, 2026
@KKould KKould self-requested a review April 8, 2026 07:01
@KKould
Copy link
Copy Markdown
Member

KKould commented Apr 8, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@KKould KKould requested a review from forsaken628 April 8, 2026 08:45
@KKould KKould enabled auto-merge April 8, 2026 09:34
@KKould KKould added this pull request to the merge queue Apr 8, 2026
Merged via the queue into databendlabs:main with commit 6562ea1 Apr 8, 2026
91 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-approved Approved by agent pr-bugfix this PR patches a bug in codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic in decorrelate optimizer with correlated subquery over UNION

3 participants