Skip to content

Commit 92b47ed

Browse files
committed
fix: Prioritize inner context compound identifier field over outer context
Signed-off-by: Alex Qyoun-ae <[email protected]>
1 parent e3bb7fe commit 92b47ed

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

datafusion/core/src/sql/planner.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,12 +1991,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
19911991
)));
19921992
}
19931993
}
1994-
if let Some(f) = self.context.outer_query_context_schema.iter().find_map(|s| s.field_with_qualified_name(&relation, &name).ok()) {
1995-
return Ok(Expr::OuterColumn(f.data_type().clone(), Column {
1996-
relation: Some(relation),
1997-
name,
1998-
}))
1999-
}
20001994

20011995
match schema.field_with_qualified_name(&relation, &name) {
20021996
Ok(_) => {
@@ -2022,6 +2016,17 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
20222016
expr: Box::new(Expr::Column(field.qualified_column())),
20232017
key: Box::new(Expr::Literal(ScalarValue::Utf8(Some(name)))),
20242018
})
2019+
} else if let Some(f) = self
2020+
.context
2021+
.outer_query_context_schema
2022+
.iter()
2023+
.find_map(|s| s.field_with_qualified_name(&relation, &name).ok())
2024+
{
2025+
// Access to an outer column from a subquery
2026+
return Ok(Expr::OuterColumn(f.data_type().clone(), Column {
2027+
relation: Some(relation),
2028+
name,
2029+
}))
20252030
} else {
20262031
// This is a fix for Sort with relation. See filter_idents_test test for more information.
20272032
Ok(Expr::Column(Column {
@@ -5376,13 +5381,13 @@ mod tests {
53765381

53775382
#[test]
53785383
fn subquery_any() {
5379-
let sql = "select person.id from person where person.id = any(select person.id from person)";
5384+
let sql = "select person.id from person where person.id = any(select person.id)";
53805385
let expected = "Projection: #person.id\
53815386
\n Filter: #person.id = ANY(#__subquery-0.person.id)\
53825387
\n Subquery: types=[AnyAll]\
53835388
\n TableScan: person projection=None\
53845389
\n Projection: ^#person.id, alias=__subquery-0\
5385-
\n TableScan: person projection=None";
5390+
\n EmptyRelation";
53865391
quick_test(sql, expected);
53875392
}
53885393

@@ -5402,14 +5407,34 @@ mod tests {
54025407
fn subquery_in() {
54035408
let sql =
54045409
"select person.id, person.id in (select person.id from person) from person";
5405-
let expected = "Projection: #person.id, #person.id IN (#__subquery-0.person.id)\
5410+
let expected = "Projection: #person.id, #person.id IN (#__subquery-0.id)\
54065411
\n Subquery: types=[AnyAll]\
54075412
\n TableScan: person projection=None\
5408-
\n Projection: ^#person.id, alias=__subquery-0\
5413+
\n Projection: #person.id, alias=__subquery-0\
54095414
\n TableScan: person projection=None";
54105415
quick_test(sql, expected);
54115416
}
54125417

5418+
#[test]
5419+
fn subquery_compound_identifier_self_reference() {
5420+
let sql = "SELECT person.id \
5421+
FROM person \
5422+
WHERE person.id IN ( \
5423+
SELECT person.id \
5424+
FROM person \
5425+
WHERE person.id > 10 \
5426+
)";
5427+
let expected = "\
5428+
Projection: #person.id\
5429+
\n Filter: #person.id IN (#__subquery-0.id)\
5430+
\n Subquery: types=[AnyAll]\
5431+
\n TableScan: person projection=None\
5432+
\n Projection: #person.id, alias=__subquery-0\
5433+
\n Filter: #person.id > Int64(10)\
5434+
\n TableScan: person projection=None";
5435+
quick_test(sql, expected);
5436+
}
5437+
54135438
#[test]
54145439
fn join_on_disjunction_condition() {
54155440
let sql = "SELECT id, order_id \

0 commit comments

Comments
 (0)