Skip to content

Commit 5210a2b

Browse files
PokIsemainesilezhoujonahgao
authored
chore(deps): Update sqlparser to 0.55.0 (#15183)
* chore(deps): Update sqlparser to 0.55.0 * merge upstream * style: cargo fmt * fix: import * style: cargo fmt * fix: struct.slt, update.slt and lateral join * chore: cargo fmt & remove unwrap * fix: remove meaningless comment for rustfmt * refactor: plan_datafusion_err, remove clone * remove println --------- Co-authored-by: silezhou <[email protected]> Co-authored-by: jonahgao <[email protected]>
1 parent 4248264 commit 5210a2b

27 files changed

+502
-321
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ recursive = "0.1.1"
162162
regex = "1.8"
163163
rstest = "0.24.0"
164164
serde_json = "1"
165-
sqlparser = { version = "0.54.0", features = ["visitor"] }
165+
sqlparser = { version = "0.55.0", features = ["visitor"] }
166166
tempfile = "3"
167167
tokio = { version = "1.44", features = ["macros", "rt", "sync"] }
168168
url = "2.5.4"

datafusion/datasource/src/statistics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use arrow::{
3030
compute::SortColumn,
3131
row::{Row, Rows},
3232
};
33-
use datafusion_common::{plan_err, DataFusionError, Result};
33+
use datafusion_common::{plan_datafusion_err, plan_err, DataFusionError, Result};
3434
use datafusion_physical_expr::{expressions::Column, PhysicalSortExpr};
3535
use datafusion_physical_expr_common::sort_expr::LexOrdering;
3636

@@ -202,10 +202,10 @@ impl MinMaxStatistics {
202202
.zip(max_values.column_by_name(column.name()))
203203
}
204204
.ok_or_else(|| {
205-
DataFusionError::Plan(format!(
205+
plan_datafusion_err!(
206206
"missing column in MinMaxStatistics::new: '{}'",
207207
column.name()
208-
))
208+
)
209209
})
210210
})
211211
.collect::<Result<Vec<_>>>()?

datafusion/expr/src/window_frame.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::fmt::{self, Formatter};
2929
use std::hash::Hash;
3030

3131
use datafusion_common::{plan_err, sql_err, DataFusionError, Result, ScalarValue};
32-
use sqlparser::ast;
32+
use sqlparser::ast::{self, ValueWithSpan};
3333
use sqlparser::parser::ParserError::ParserError;
3434

3535
/// The frame specification determines which output rows are read by an aggregate
@@ -368,7 +368,7 @@ fn convert_frame_bound_to_scalar_value(
368368
match units {
369369
// For ROWS and GROUPS we are sure that the ScalarValue must be a non-negative integer ...
370370
ast::WindowFrameUnits::Rows | ast::WindowFrameUnits::Groups => match v {
371-
ast::Expr::Value(ast::Value::Number(value, false)) => {
371+
ast::Expr::Value(ValueWithSpan{value: ast::Value::Number(value, false), span: _}) => {
372372
Ok(ScalarValue::try_from_string(value, &DataType::UInt64)?)
373373
},
374374
ast::Expr::Interval(ast::Interval {
@@ -379,7 +379,7 @@ fn convert_frame_bound_to_scalar_value(
379379
fractional_seconds_precision: None,
380380
}) => {
381381
let value = match *value {
382-
ast::Expr::Value(ast::Value::SingleQuotedString(item)) => item,
382+
ast::Expr::Value(ValueWithSpan{value: ast::Value::SingleQuotedString(item), span: _}) => item,
383383
e => {
384384
return sql_err!(ParserError(format!(
385385
"INTERVAL expression cannot be {e:?}"
@@ -395,14 +395,14 @@ fn convert_frame_bound_to_scalar_value(
395395
// ... instead for RANGE it could be anything depending on the type of the ORDER BY clause,
396396
// so we use a ScalarValue::Utf8.
397397
ast::WindowFrameUnits::Range => Ok(ScalarValue::Utf8(Some(match v {
398-
ast::Expr::Value(ast::Value::Number(value, false)) => value,
398+
ast::Expr::Value(ValueWithSpan{value: ast::Value::Number(value, false), span: _}) => value,
399399
ast::Expr::Interval(ast::Interval {
400400
value,
401401
leading_field,
402402
..
403403
}) => {
404404
let result = match *value {
405-
ast::Expr::Value(ast::Value::SingleQuotedString(item)) => item,
405+
ast::Expr::Value(ValueWithSpan{value: ast::Value::SingleQuotedString(item), span: _}) => item,
406406
e => {
407407
return sql_err!(ParserError(format!(
408408
"INTERVAL expression cannot be {e:?}"
@@ -514,10 +514,10 @@ mod tests {
514514
let window_frame = ast::WindowFrame {
515515
units: ast::WindowFrameUnits::Rows,
516516
start_bound: ast::WindowFrameBound::Preceding(Some(Box::new(
517-
ast::Expr::Value(ast::Value::Number("2".to_string(), false)),
517+
ast::Expr::value(ast::Value::Number("2".to_string(), false)),
518518
))),
519519
end_bound: Some(ast::WindowFrameBound::Preceding(Some(Box::new(
520-
ast::Expr::Value(ast::Value::Number("1".to_string(), false)),
520+
ast::Expr::value(ast::Value::Number("1".to_string(), false)),
521521
)))),
522522
};
523523

@@ -575,10 +575,9 @@ mod tests {
575575
test_bound!(Range, None, ScalarValue::Null);
576576

577577
// Number
578-
let number = Some(Box::new(ast::Expr::Value(ast::Value::Number(
579-
"42".to_string(),
580-
false,
581-
))));
578+
let number = Some(Box::new(ast::Expr::Value(
579+
ast::Value::Number("42".to_string(), false).into(),
580+
)));
582581
test_bound!(Rows, number.clone(), ScalarValue::UInt64(Some(42)));
583582
test_bound!(Groups, number.clone(), ScalarValue::UInt64(Some(42)));
584583
test_bound!(
@@ -589,9 +588,9 @@ mod tests {
589588

590589
// Interval
591590
let number = Some(Box::new(ast::Expr::Interval(ast::Interval {
592-
value: Box::new(ast::Expr::Value(ast::Value::SingleQuotedString(
593-
"1".to_string(),
594-
))),
591+
value: Box::new(ast::Expr::Value(
592+
ast::Value::SingleQuotedString("1".to_string()).into(),
593+
)),
595594
leading_field: Some(ast::DateTimeField::Day),
596595
fractional_seconds_precision: None,
597596
last_field: None,

datafusion/sql/src/expr/function.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use datafusion_expr::{
3030
use sqlparser::ast::{
3131
DuplicateTreatment, Expr as SQLExpr, Function as SQLFunction, FunctionArg,
3232
FunctionArgExpr, FunctionArgumentClause, FunctionArgumentList, FunctionArguments,
33-
NullTreatment, ObjectName, OrderByExpr, WindowType,
33+
NullTreatment, ObjectName, OrderByExpr, Spanned, WindowType,
3434
};
3535

3636
/// Suggest a valid function based on an invalid input function name
@@ -216,13 +216,21 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
216216
// it shouldn't have ordering requirement as function argument
217217
// required ordering should be defined in OVER clause.
218218
let is_function_window = over.is_some();
219-
let sql_parser_span = name.0[0].span;
219+
let sql_parser_span = name.0[0].span();
220220
let name = if name.0.len() > 1 {
221221
// DF doesn't handle compound identifiers
222222
// (e.g. "foo.bar") for function names yet
223223
name.to_string()
224224
} else {
225-
crate::utils::normalize_ident(name.0[0].clone())
225+
match name.0[0].as_ident() {
226+
Some(ident) => crate::utils::normalize_ident(ident.clone()),
227+
None => {
228+
return plan_err!(
229+
"Expected an identifier in function name, but found {:?}",
230+
name.0[0]
231+
)
232+
}
233+
}
226234
};
227235

228236
if name.eq("make_map") {

datafusion/sql/src/expr/identifier.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use datafusion_common::{
2222
};
2323
use datafusion_expr::planner::PlannerResult;
2424
use datafusion_expr::{Case, Expr};
25-
use sqlparser::ast::{Expr as SQLExpr, Ident};
25+
use sqlparser::ast::{CaseWhen, Expr as SQLExpr, Ident};
2626

2727
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
2828
use datafusion_expr::UNNAMED_TABLE;
@@ -216,8 +216,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
216216
pub(super) fn sql_case_identifier_to_expr(
217217
&self,
218218
operand: Option<Box<SQLExpr>>,
219-
conditions: Vec<SQLExpr>,
220-
results: Vec<SQLExpr>,
219+
conditions: Vec<CaseWhen>,
221220
else_result: Option<Box<SQLExpr>>,
222221
schema: &DFSchema,
223222
planner_context: &mut PlannerContext,
@@ -231,13 +230,22 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
231230
} else {
232231
None
233232
};
234-
let when_expr = conditions
233+
let when_then_expr = conditions
235234
.into_iter()
236-
.map(|e| self.sql_expr_to_logical_expr(e, schema, planner_context))
237-
.collect::<Result<Vec<_>>>()?;
238-
let then_expr = results
239-
.into_iter()
240-
.map(|e| self.sql_expr_to_logical_expr(e, schema, planner_context))
235+
.map(|e| {
236+
Ok((
237+
Box::new(self.sql_expr_to_logical_expr(
238+
e.condition,
239+
schema,
240+
planner_context,
241+
)?),
242+
Box::new(self.sql_expr_to_logical_expr(
243+
e.result,
244+
schema,
245+
planner_context,
246+
)?),
247+
))
248+
})
241249
.collect::<Result<Vec<_>>>()?;
242250
let else_expr = if let Some(e) = else_result {
243251
Some(Box::new(self.sql_expr_to_logical_expr(
@@ -249,15 +257,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
249257
None
250258
};
251259

252-
Ok(Expr::Case(Case::new(
253-
expr,
254-
when_expr
255-
.iter()
256-
.zip(then_expr.iter())
257-
.map(|(w, t)| (Box::new(w.to_owned()), Box::new(t.to_owned())))
258-
.collect(),
259-
else_expr,
260-
)))
260+
Ok(Expr::Case(Case::new(expr, when_then_expr, else_expr)))
261261
}
262262
}
263263

datafusion/sql/src/expr/mod.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use datafusion_expr::planner::{
2222
use sqlparser::ast::{
2323
AccessExpr, BinaryOperator, CastFormat, CastKind, DataType as SQLDataType,
2424
DictionaryField, Expr as SQLExpr, ExprWithAlias as SQLExprWithAlias, MapEntry,
25-
StructField, Subscript, TrimWhereField, Value,
25+
StructField, Subscript, TrimWhereField, Value, ValueWithSpan,
2626
};
2727

2828
use datafusion_common::{
@@ -211,7 +211,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
211211
// more context.
212212
match sql {
213213
SQLExpr::Value(value) => {
214-
self.parse_value(value, planner_context.prepare_param_data_types())
214+
self.parse_value(value.into(), planner_context.prepare_param_data_types())
215215
}
216216
SQLExpr::Extract { field, expr, .. } => {
217217
let mut extract_args = vec![
@@ -253,12 +253,10 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
253253
SQLExpr::Case {
254254
operand,
255255
conditions,
256-
results,
257256
else_result,
258257
} => self.sql_case_identifier_to_expr(
259258
operand,
260259
conditions,
261-
results,
262260
else_result,
263261
schema,
264262
planner_context,
@@ -292,7 +290,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
292290
}
293291

294292
SQLExpr::TypedString { data_type, value } => Ok(Expr::Cast(Cast::new(
295-
Box::new(lit(value)),
293+
Box::new(lit(value.into_string().unwrap())),
296294
self.convert_data_type(&data_type)?,
297295
))),
298296

@@ -544,9 +542,10 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
544542
planner_context,
545543
)?),
546544
match *time_zone {
547-
SQLExpr::Value(Value::SingleQuotedString(s)) => {
548-
DataType::Timestamp(TimeUnit::Nanosecond, Some(s.into()))
549-
}
545+
SQLExpr::Value(ValueWithSpan {
546+
value: Value::SingleQuotedString(s),
547+
span: _,
548+
}) => DataType::Timestamp(TimeUnit::Nanosecond, Some(s.into())),
550549
_ => {
551550
return not_impl_err!(
552551
"Unsupported ast node in sqltorel: {time_zone:?}"
@@ -1062,10 +1061,12 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
10621061
Subscript::Index { index } => {
10631062
// index can be a name, in which case it is a named field access
10641063
match index {
1065-
SQLExpr::Value(
1066-
Value::SingleQuotedString(s)
1067-
| Value::DoubleQuotedString(s),
1068-
) => Ok(Some(GetFieldAccess::NamedStructField {
1064+
SQLExpr::Value(ValueWithSpan {
1065+
value:
1066+
Value::SingleQuotedString(s)
1067+
| Value::DoubleQuotedString(s),
1068+
span: _,
1069+
}) => Ok(Some(GetFieldAccess::NamedStructField {
10691070
name: ScalarValue::from(s),
10701071
})),
10711072
SQLExpr::JsonAccess { .. } => {
@@ -1128,9 +1129,10 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
11281129
}
11291130
}
11301131
AccessExpr::Dot(expr) => match expr {
1131-
SQLExpr::Value(
1132-
Value::SingleQuotedString(s) | Value::DoubleQuotedString(s),
1133-
) => Ok(Some(GetFieldAccess::NamedStructField {
1132+
SQLExpr::Value(ValueWithSpan {
1133+
value: Value::SingleQuotedString(s) | Value::DoubleQuotedString(s),
1134+
span : _
1135+
}) => Ok(Some(GetFieldAccess::NamedStructField {
11341136
name: ScalarValue::from(s),
11351137
})),
11361138
_ => {

datafusion/sql/src/expr/order_by.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use datafusion_common::{
2121
};
2222
use datafusion_expr::expr::Sort;
2323
use datafusion_expr::{Expr, SortExpr};
24-
use sqlparser::ast::{Expr as SQLExpr, OrderByExpr, Value};
24+
use sqlparser::ast::{
25+
Expr as SQLExpr, OrderByExpr, OrderByOptions, Value, ValueWithSpan,
26+
};
2527

2628
impl<S: ContextProvider> SqlToRel<'_, S> {
2729
/// Convert sql [OrderByExpr] to `Vec<Expr>`.
@@ -62,9 +64,8 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
6264
let mut expr_vec = vec![];
6365
for e in exprs {
6466
let OrderByExpr {
65-
asc,
6667
expr,
67-
nulls_first,
68+
options: OrderByOptions { asc, nulls_first },
6869
with_fill,
6970
} = e;
7071

@@ -73,7 +74,10 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
7374
}
7475

7576
let expr = match expr {
76-
SQLExpr::Value(Value::Number(v, _)) if literal_to_column => {
77+
SQLExpr::Value(ValueWithSpan {
78+
value: Value::Number(v, _),
79+
span: _,
80+
}) if literal_to_column => {
7781
let field_index = v
7882
.parse::<usize>()
7983
.map_err(|err| plan_datafusion_err!("{}", err))?;

datafusion/sql/src/expr/unary_op.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use datafusion_expr::{
2121
type_coercion::{is_interval, is_timestamp},
2222
Expr, ExprSchemable,
2323
};
24-
use sqlparser::ast::{Expr as SQLExpr, UnaryOperator, Value};
24+
use sqlparser::ast::{Expr as SQLExpr, UnaryOperator, Value, ValueWithSpan};
2525

2626
impl<S: ContextProvider> SqlToRel<'_, S> {
2727
pub(crate) fn parse_sql_unary_op(
@@ -61,9 +61,10 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
6161
match expr {
6262
// Optimization: if it's a number literal, we apply the negative operator
6363
// here directly to calculate the new literal.
64-
SQLExpr::Value(Value::Number(n, _)) => {
65-
self.parse_sql_number(&n, true)
66-
}
64+
SQLExpr::Value(ValueWithSpan {
65+
value: Value::Number(n, _),
66+
span: _,
67+
}) => self.parse_sql_number(&n, true),
6768
SQLExpr::Interval(interval) => {
6869
self.sql_interval_to_expr(true, interval)
6970
}

datafusion/sql/src/expr/value.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ use datafusion_expr::expr::{BinaryExpr, Placeholder};
3232
use datafusion_expr::planner::PlannerResult;
3333
use datafusion_expr::{lit, Expr, Operator};
3434
use log::debug;
35-
use sqlparser::ast::{BinaryOperator, Expr as SQLExpr, Interval, UnaryOperator, Value};
35+
use sqlparser::ast::{
36+
BinaryOperator, Expr as SQLExpr, Interval, UnaryOperator, Value, ValueWithSpan,
37+
};
3638
use sqlparser::parser::ParserError::ParserError;
3739
use std::borrow::Cow;
3840
use std::cmp::Ordering;
@@ -254,8 +256,14 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
254256

255257
fn interval_literal(interval_value: SQLExpr, negative: bool) -> Result<String> {
256258
let s = match interval_value {
257-
SQLExpr::Value(Value::SingleQuotedString(s) | Value::DoubleQuotedString(s)) => s,
258-
SQLExpr::Value(Value::Number(ref v, long)) => {
259+
SQLExpr::Value(ValueWithSpan {
260+
value: Value::SingleQuotedString(s) | Value::DoubleQuotedString(s),
261+
span: _,
262+
}) => s,
263+
SQLExpr::Value(ValueWithSpan {
264+
value: Value::Number(ref v, long),
265+
span: _,
266+
}) => {
259267
if long {
260268
return not_impl_err!(
261269
"Unsupported interval argument. Long number not supported: {interval_value:?}"

0 commit comments

Comments
 (0)