Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): support left plan's from clause contains subquery #17621

Merged
merged 23 commits into from
Mar 26, 2025

Conversation

forsaken628
Copy link
Collaborator

@forsaken628 forsaken628 commented Mar 18, 2025

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

Summary

SubqueryRewriter::flatten_plan rewrites a correlated subquery as a join. A rewrite involves a subquery plan and an outer plan.
The previous implementation didn't pass in the outer plan but got it indirectly from the metadata, so there were a lot of limitations, but in this PR, it passes in the outer plan directly.

Currently only DummyTableScan, ConstantTableScan, Scan, EvalScalar, Limit, Sort, Filter, Join, Aggregate are supported in outer plan.

#17476 (comment) cc @KKould

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

This change is Reviewable

@github-actions github-actions bot added the pr-feature this PR introduces a new feature to the codebase label Mar 18, 2025
@forsaken628 forsaken628 force-pushed the decorrelate branch 2 times, most recently from ce02c42 to 3b17699 Compare March 18, 2025 12:38
@forsaken628 forsaken628 added the ci-benchmark Benchmark: run all test label Mar 20, 2025
@forsaken628 forsaken628 added ci-benchmark Benchmark: run all test and removed ci-benchmark Benchmark: run all test labels Mar 21, 2025
Copy link
Contributor

Docker Image for PR

  • tag: pr-17621-14eebfd-1742520259

note: this image tag is only available for internal use,
please check the internal doc for more details.

@forsaken628 forsaken628 removed the ci-benchmark Benchmark: run all test label Mar 22, 2025
@forsaken628
Copy link
Collaborator Author

forsaken628 commented Mar 24, 2025

bug reproduce on v1.2.707

CREATE OR REPLACE TABLE employees AS
SELECT * FROM (
    VALUES
        (1, 1, 50000),
        (2, 2, 60000),
        (3, 3, 70000)
) AS employees(employee_id, name, salary);

CREATE OR REPLACE TABLE departments AS
SELECT * FROM (
    VALUES
        (101, 1, 1),
        (102, 2, 2),
        (103, 3, 3)
) AS departments(department_id, department_name, manager_id);

CREATE OR REPLACE TABLE salaries AS
SELECT * FROM (
    VALUES
        (1, 50000),
        (2, 60000),
        (3, 70000),
        (4, 80000),
        (5, 90000)
) AS salaries(employee_id, salary);

SELECT 
    e.employee_id, 
    d.department_name
FROM 
    employees e
INNER JOIN 
    departments d 
ON 
    e.employee_id = d.manager_id
WHERE 
    e.salary > ALL (
        SELECT salary
        FROM salaries
        WHERE employee_id <> e.employee_id
    )
ORDER BY employee_id;

FIXED

@forsaken628
Copy link
Collaborator Author

forsaken628 commented Mar 24, 2025

bug reproduce on v1.2.707

create or replace table tab2 as select * from (values (1, 10), (1, 20), (2, 30)) tab(n, m);
create or replace table tab3 as select * from (values (10, 100), (20, 200), (30, 300)) tab(m, k);

select
  t1.n,
  t2.m + t3.k as result,
  (select count(tab3.k) from tab3 where tab3.k = t1.n * 100 and tab3.m = t2.m) count
from 
  (values (1), (2), (3)) t1(n),
  tab2 t2 , tab3 t3
where t1.n = t2.n and t2.m = t3.m
order by result;

FIXED

@forsaken628 forsaken628 marked this pull request as ready for review March 25, 2025 03:32
@forsaken628 forsaken628 requested a review from sundy-li March 25, 2025 03:32
@sundy-li sundy-li requested a review from BohuTANG March 25, 2025 06:28
@BohuTANG BohuTANG merged commit 980a16d into databendlabs:main Mar 26, 2025
148 of 150 checks passed
@forsaken628 forsaken628 deleted the decorrelate branch March 28, 2025 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: subquery with union cause panic
3 participants