-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Test upgrade to sqlparser-rs 0.54 #14198
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,14 +21,14 @@ use datafusion_expr::planner::{ | |
PlannerResult, RawBinaryExpr, RawDictionaryExpr, RawFieldAccessExpr, | ||
}; | ||
use sqlparser::ast::{ | ||
BinaryOperator, CastFormat, CastKind, DataType as SQLDataType, DictionaryField, | ||
Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias, MapEntry, StructField, Subscript, | ||
TrimWhereField, Value, | ||
AccessExpr, BinaryOperator, CastFormat, CastKind, DataType as SQLDataType, | ||
DictionaryField, Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias, MapEntry, | ||
StructField, Subscript, TrimWhereField, Value, | ||
}; | ||
|
||
use datafusion_common::{ | ||
internal_datafusion_err, internal_err, not_impl_err, plan_err, DFSchema, Result, | ||
ScalarValue, | ||
internal_datafusion_err, internal_err, not_impl_err, plan_err, Column, DFSchema, | ||
Result, ScalarValue, | ||
}; | ||
use datafusion_expr::expr::ScalarFunction; | ||
use datafusion_expr::expr::{InList, WildcardOptions}; | ||
|
@@ -236,14 +236,14 @@ impl<S: ContextProvider> SqlToRel<'_, S> { | |
self.sql_identifier_to_expr(id, schema, planner_context) | ||
} | ||
|
||
SQLExpr::MapAccess { .. } => { | ||
not_impl_err!("Map Access") | ||
} | ||
|
||
// <expr>["foo"], <expr>[4] or <expr>[4:5] | ||
SQLExpr::Subscript { expr, subscript } => { | ||
self.sql_subscript_to_expr(*expr, subscript, schema, planner_context) | ||
} | ||
SQLExpr::CompoundFieldAccess { root, access_chain } => self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code to plan CompoundFieldAccess is directly copy/pasted from @goldmedal 's PR |
||
.sql_compound_field_access_to_expr( | ||
*root, | ||
access_chain, | ||
schema, | ||
planner_context, | ||
), | ||
|
||
SQLExpr::CompoundIdentifier(ids) => { | ||
self.sql_compound_identifier_to_expr(ids, schema, planner_context) | ||
|
@@ -984,84 +984,141 @@ impl<S: ContextProvider> SqlToRel<'_, S> { | |
Ok(Expr::Cast(Cast::new(Box::new(expr), dt))) | ||
} | ||
|
||
fn sql_subscript_to_expr( | ||
fn sql_compound_field_access_to_expr( | ||
&self, | ||
expr: SQLExpr, | ||
subscript: Box<Subscript>, | ||
root: SQLExpr, | ||
access_chain: Vec<AccessExpr>, | ||
schema: &DFSchema, | ||
planner_context: &mut PlannerContext, | ||
) -> Result<Expr> { | ||
let expr = self.sql_expr_to_logical_expr(expr, schema, planner_context)?; | ||
|
||
let field_access = match *subscript { | ||
Subscript::Index { index } => { | ||
// index can be a name, in which case it is a named field access | ||
match index { | ||
SQLExpr::Value( | ||
Value::SingleQuotedString(s) | Value::DoubleQuotedString(s), | ||
) => GetFieldAccess::NamedStructField { | ||
name: ScalarValue::from(s), | ||
}, | ||
SQLExpr::JsonAccess { .. } => { | ||
return not_impl_err!("JsonAccess"); | ||
let mut root = self.sql_expr_to_logical_expr(root, schema, planner_context)?; | ||
let fields = access_chain | ||
.into_iter() | ||
.map(|field| match field { | ||
AccessExpr::Subscript(subscript) => { | ||
match subscript { | ||
Subscript::Index { index } => { | ||
// index can be a name, in which case it is a named field access | ||
match index { | ||
SQLExpr::Value( | ||
Value::SingleQuotedString(s) | ||
| Value::DoubleQuotedString(s), | ||
) => Ok(Some(GetFieldAccess::NamedStructField { | ||
name: ScalarValue::from(s), | ||
})), | ||
SQLExpr::JsonAccess { .. } => { | ||
not_impl_err!("JsonAccess") | ||
} | ||
// otherwise treat like a list index | ||
_ => Ok(Some(GetFieldAccess::ListIndex { | ||
key: Box::new(self.sql_expr_to_logical_expr( | ||
index, | ||
schema, | ||
planner_context, | ||
)?), | ||
})), | ||
} | ||
} | ||
Subscript::Slice { | ||
lower_bound, | ||
upper_bound, | ||
stride, | ||
} => { | ||
// Means access like [:2] | ||
let lower_bound = if let Some(lower_bound) = lower_bound { | ||
self.sql_expr_to_logical_expr( | ||
lower_bound, | ||
schema, | ||
planner_context, | ||
) | ||
} else { | ||
not_impl_err!("Slice subscript requires a lower bound") | ||
}?; | ||
|
||
// means access like [2:] | ||
let upper_bound = if let Some(upper_bound) = upper_bound { | ||
self.sql_expr_to_logical_expr( | ||
upper_bound, | ||
schema, | ||
planner_context, | ||
) | ||
} else { | ||
not_impl_err!("Slice subscript requires an upper bound") | ||
}?; | ||
|
||
// stride, default to 1 | ||
let stride = if let Some(stride) = stride { | ||
self.sql_expr_to_logical_expr( | ||
stride, | ||
schema, | ||
planner_context, | ||
)? | ||
} else { | ||
lit(1i64) | ||
}; | ||
|
||
Ok(Some(GetFieldAccess::ListRange { | ||
start: Box::new(lower_bound), | ||
stop: Box::new(upper_bound), | ||
stride: Box::new(stride), | ||
})) | ||
} | ||
} | ||
// otherwise treat like a list index | ||
_ => GetFieldAccess::ListIndex { | ||
key: Box::new(self.sql_expr_to_logical_expr( | ||
index, | ||
schema, | ||
planner_context, | ||
)?), | ||
}, | ||
} | ||
} | ||
Subscript::Slice { | ||
lower_bound, | ||
upper_bound, | ||
stride, | ||
} => { | ||
// Means access like [:2] | ||
let lower_bound = if let Some(lower_bound) = lower_bound { | ||
self.sql_expr_to_logical_expr(lower_bound, schema, planner_context) | ||
} else { | ||
not_impl_err!("Slice subscript requires a lower bound") | ||
}?; | ||
|
||
// means access like [2:] | ||
let upper_bound = if let Some(upper_bound) = upper_bound { | ||
self.sql_expr_to_logical_expr(upper_bound, schema, planner_context) | ||
} else { | ||
not_impl_err!("Slice subscript requires an upper bound") | ||
}?; | ||
|
||
// stride, default to 1 | ||
let stride = if let Some(stride) = stride { | ||
self.sql_expr_to_logical_expr(stride, schema, planner_context)? | ||
} else { | ||
lit(1i64) | ||
}; | ||
|
||
GetFieldAccess::ListRange { | ||
start: Box::new(lower_bound), | ||
stop: Box::new(upper_bound), | ||
stride: Box::new(stride), | ||
AccessExpr::Dot(expr) => { | ||
let expr = | ||
self.sql_expr_to_logical_expr(expr, schema, planner_context)?; | ||
match expr { | ||
Expr::Column(Column { name, relation }) => { | ||
if let Some(relation) = &relation { | ||
// If the first part of the dot access is a column reference, we should | ||
// check if the column is from the same table as the root expression. | ||
// If it is, we should replace the root expression with the column reference. | ||
// Otherwise, we should treat the dot access as a named field access. | ||
if relation.table() == root.schema_name().to_string() { | ||
root = Expr::Column(Column { | ||
name, | ||
relation: Some(relation.clone()), | ||
}); | ||
Ok(None) | ||
} else { | ||
plan_err!( | ||
"table name mismatch: {} != {}", | ||
relation.table(), | ||
root.schema_name() | ||
) | ||
} | ||
} else { | ||
Ok(Some(GetFieldAccess::NamedStructField { | ||
name: ScalarValue::from(name), | ||
})) | ||
} | ||
} | ||
_ => not_impl_err!( | ||
"Dot access not supported for non-column expr: {expr:?}" | ||
), | ||
} | ||
} | ||
} | ||
}; | ||
}) | ||
.collect::<Result<Vec<_>>>()?; | ||
|
||
let mut field_access_expr = RawFieldAccessExpr { expr, field_access }; | ||
for planner in self.context_provider.get_expr_planners() { | ||
match planner.plan_field_access(field_access_expr, schema)? { | ||
PlannerResult::Planned(expr) => return Ok(expr), | ||
PlannerResult::Original(expr) => { | ||
field_access_expr = expr; | ||
fields | ||
.into_iter() | ||
.flatten() | ||
.try_fold(root, |expr, field_access| { | ||
let mut field_access_expr = RawFieldAccessExpr { expr, field_access }; | ||
for planner in self.context_provider.get_expr_planners() { | ||
match planner.plan_field_access(field_access_expr, schema)? { | ||
PlannerResult::Planned(expr) => return Ok(expr), | ||
PlannerResult::Original(expr) => { | ||
field_access_expr = expr; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
not_impl_err!( | ||
"GetFieldAccess not supported by ExprPlanner: {field_access_expr:?}" | ||
) | ||
not_impl_err!( | ||
"GetFieldAccess not supported by ExprPlanner: {field_access_expr:?}" | ||
) | ||
}) | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i made this public and cleaned up the signature so I could reuse it when dealing with the changes to
USING
planningHowever I am not quite sure what a
USING(foo.bar)
would actually mean 🤔 Maybe when joining across multiple schemas...I could also revert this change and just make the planner error if it got a multi-part ObjectName in a
USING
clause ..