Skip to content

Fix:22477 any all schema error#22915

Open
HairstonE wants to merge 3 commits into
apache:mainfrom
HairstonE:fix/22477-any-all-schema-error
Open

Fix:22477 any all schema error#22915
HairstonE wants to merge 3 commits into
apache:mainfrom
HairstonE:fix/22477-any-all-schema-error

Conversation

@HairstonE

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

= ANY (SELECT ...) and <> ALL (SELECT ...) decorrelate into stacked mark joins. The optimizer was pruning each mark join's right child to zero columns, which dropped its table reference. Without a qualifier, every mark column came out named just mark, resulting in the schema error.

What changes are included in this PR?

Mark joins now keep one column from the right child instead of pruning it down to nothing. The right child holds onto its table reference, the mark now stays qualified.

Are these changes tested?

A unit test builds three stacked mark joins and checks the plan optimizes without the schema error. An sqllogictest runs = ANY and <> ALL end-to-end against a small table.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jun 11, 2026

@alamb alamb left a comment

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.

Thank you for this PR @HairstonE

@@ -764,12 +780,12 @@ fn split_join_requirements(
// The mark column is synthetic (produced by the join itself),
// so discard it and route only to the left child.
let (left_indices, _mark) = indices.split_off(left_len);
(left_indices, RequiredIndices::new())
(left_indices, RequiredIndices::new().append(&[0]))

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.

Looks like this code was added in #21265 from @buraksenn

@buraksenn do you have some time to help review this proposed change?

I think it would help if we can also document in comments why [0] is being appended

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.

It seems like we only need to add the column in some cases -- why does this code add it unconditionally?

@@ -1660,6 +1598,25 @@ logical_plan
21)----------Projection: column1 AS v
22)------------Values: (Int64(5)), (Int64(NULL))

# same-table `= ANY` / `<> ALL` must plan without

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.

without the code change in this PR, this test fails as expected

cargo test --profile=ci --test sqllogictests
...

Completed 475 test files in 9 seconds                                                                                                                                              External error: 2 errors in file /Users/andrewlamb/Software/datafusion3/datafusion/sqllogictest/test_files/subquery.slt

1. query failed: DataFusion error: Optimizer rule 'optimize_projections' failed
caused by
Schema error: Schema contains duplicate unqualified field name mark
[SQL] select id from set_cmp_self where age = any(select age from set_cmp_self);
at /Users/andrewlamb/Software/datafusion3/datafusion/sqllogictest/test_files/subquery.slt:1606


2. query failed: DataFusion error: Optimizer rule 'optimize_projections' failed
caused by
Schema error: Schema contains duplicate unqualified field name mark
[SQL] select id from set_cmp_self where age <> all(select age from set_cmp_self);
at /Users/andrewlamb/Software/datafusion3/datafusion/sqllogictest/test_files/subquery.slt:1613

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

= ANY / <> ALL subquery fails to plan: Schema contains duplicate unqualified field name mark

2 participants