Skip to content

Commit 2e0791c

Browse files
committed
feat: Improve subquery support
1 parent 6b006e5 commit 2e0791c

File tree

23 files changed

+1015
-347
lines changed

23 files changed

+1015
-347
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datafusion-cli/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datafusion/common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ cranelift-module = { version = "0.82.0", optional = true }
4444
ordered-float = "2.10"
4545
parquet = { git = 'https://github.com/cube-js/arrow-rs.git', rev = "096ef28dde6b1ae43ce89ba2c3a9d98295f2972e", features = ["arrow"], optional = true }
4646
pyo3 = { version = "0.16", optional = true }
47-
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "10782e5d11fc0e2900c9359dddee0fbefbffd359" }
47+
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "0bea7fa17907b96f32abf4bd6bb1cde43fe8d244" }

datafusion/core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pin-project-lite= "^0.2.7"
7979
pyo3 = { version = "0.16", optional = true }
8080
rand = "0.8"
8181
smallvec = { version = "1.6", features = ["union"] }
82-
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "10782e5d11fc0e2900c9359dddee0fbefbffd359" }
82+
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "0bea7fa17907b96f32abf4bd6bb1cde43fe8d244" }
8383
tempfile = "3"
8484
tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread", "sync", "fs", "parking_lot"] }
8585
tokio-stream = "0.1"

datafusion/core/src/datasource/listing/helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ impl ExpressionVisitor for ApplicabilityVisitor<'_> {
9797
| Expr::ILike { .. }
9898
| Expr::SimilarTo { .. }
9999
| Expr::InList { .. }
100+
| Expr::InSubquery { .. }
100101
| Expr::GetIndexedField { .. }
101102
| Expr::Case { .. } => Recursion::Continue(self),
102103

datafusion/core/src/logical_plan/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ use super::{dfschema::ToDFSchema, expr_rewriter::coerce_plan_expr_for_schema, Di
4747
use super::{exprlist_to_fields, Expr, JoinConstraint, JoinType, LogicalPlan, PlanType};
4848
use crate::logical_plan::{
4949
columnize_expr, normalize_col, normalize_cols, rewrite_sort_cols_by_aggs, Column,
50-
CrossJoin, DFField, DFSchema, DFSchemaRef, Limit, Partitioning, Repartition, Values,
50+
CrossJoin, DFField, DFSchema, DFSchemaRef, Limit, Partitioning, Repartition,
51+
SubqueryType, Values,
5152
};
5253
use crate::sql::utils::group_window_expr_by_sort_keys;
5354

@@ -527,7 +528,7 @@ impl LogicalPlanBuilder {
527528
/// Apply correlated sub query
528529
pub fn subquery(
529530
&self,
530-
subqueries: impl IntoIterator<Item = impl Into<LogicalPlan>>,
531+
subqueries: impl IntoIterator<Item = impl Into<(LogicalPlan, SubqueryType)>>,
531532
) -> Result<Self> {
532533
let subqueries = subqueries.into_iter().map(|l| l.into()).collect::<Vec<_>>();
533534
let schema = Arc::new(Subquery::merged_schema(&self.plan, &subqueries));

datafusion/core/src/logical_plan/expr_rewriter.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,16 @@ impl ExprRewritable for Expr {
122122
op,
123123
right: rewrite_boxed(right, rewriter)?,
124124
},
125-
Expr::AnyExpr { left, op, right } => Expr::AnyExpr {
125+
Expr::AnyExpr {
126+
left,
127+
op,
128+
right,
129+
all,
130+
} => Expr::AnyExpr {
126131
left: rewrite_boxed(left, rewriter)?,
127132
op,
128133
right: rewrite_boxed(right, rewriter)?,
134+
all,
129135
},
130136
Expr::Like(Like {
131137
negated,
@@ -263,6 +269,15 @@ impl ExprRewritable for Expr {
263269
list: rewrite_vec(list, rewriter)?,
264270
negated,
265271
},
272+
Expr::InSubquery {
273+
expr,
274+
subquery,
275+
negated,
276+
} => Expr::InSubquery {
277+
expr: rewrite_boxed(expr, rewriter)?,
278+
subquery: rewrite_boxed(subquery, rewriter)?,
279+
negated,
280+
},
266281
Expr::Wildcard => Expr::Wildcard,
267282
Expr::QualifiedWildcard { qualifier } => {
268283
Expr::QualifiedWildcard { qualifier }

datafusion/core/src/logical_plan/expr_schema.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl ExprSchemable for Expr {
111111
| Expr::IsNull(_)
112112
| Expr::Between { .. }
113113
| Expr::InList { .. }
114+
| Expr::InSubquery { .. }
114115
| Expr::AnyExpr { .. }
115116
| Expr::IsNotNull(_) => Ok(DataType::Boolean),
116117
Expr::BinaryExpr {
@@ -158,7 +159,7 @@ impl ExprSchemable for Expr {
158159
| Expr::Between { expr, .. }
159160
| Expr::InList { expr, .. } => expr.nullable(input_schema),
160161
Expr::Column(c) => input_schema.nullable(c),
161-
Expr::OuterColumn(_, _) => Ok(true),
162+
Expr::OuterColumn(_, _) | Expr::InSubquery { .. } => Ok(true),
162163
Expr::Literal(value) => Ok(value.is_null()),
163164
Expr::Case {
164165
when_then_expr,

datafusion/core/src/logical_plan/expr_visitor.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ impl ExprVisitable for Expr {
191191
list.iter()
192192
.try_fold(visitor, |visitor, arg| arg.accept(visitor))
193193
}
194+
Expr::InSubquery { expr, subquery, .. } => {
195+
let visitor = expr.accept(visitor)?;
196+
subquery.accept(visitor)
197+
}
194198
}?;
195199

196200
visitor.post_visit(self)

datafusion/core/src/logical_plan/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ pub use plan::{
6868
CreateCatalogSchema, CreateExternalTable, CreateMemoryTable, CrossJoin, Distinct,
6969
DropTable, EmptyRelation, Filter, JoinConstraint, JoinType, Limit, LogicalPlan,
7070
Partitioning, PlanType, PlanVisitor, Repartition, StringifiedPlan, Subquery,
71-
TableScan, ToStringifiedPlan, Union, Values,
71+
SubqueryType, TableScan, ToStringifiedPlan, Union, Values,
7272
};
7373
pub use registry::FunctionRegistry;

0 commit comments

Comments
 (0)