Skip to content

Commit

Permalink
Update for new sqlparser API
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Nov 30, 2024
1 parent e4bbffa commit 3545647
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 61 deletions.
5 changes: 3 additions & 2 deletions datafusion/common/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,10 +775,10 @@ pub fn get_available_parallelism() -> usize {

#[cfg(test)]
mod tests {
use super::*;
use crate::ScalarValue::Null;
use arrow::array::Float64Array;

use super::*;
use sqlparser::tokenizer::Span;

#[test]
fn test_bisect_linear_left_and_right() -> Result<()> {
Expand Down Expand Up @@ -1006,6 +1006,7 @@ mod tests {
let expected_parsed = vec![Ident {
value: identifier.to_string(),
quote_style,
span: Span::empty(),
}];

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ use arrow_array::{
Array, ArrayRef, Float32Array, Float64Array, Int32Array, RecordBatch, StringArray,
};
use arrow_schema::{DataType, Field, Schema};
use parking_lot::Mutex;
use regex::Regex;
use sqlparser::ast::Ident;

use datafusion::execution::context::{FunctionFactory, RegisterFunction, SessionState};
use datafusion::prelude::*;
use datafusion::{execution::registry::FunctionRegistry, test_util};
Expand All @@ -48,6 +44,10 @@ use datafusion_expr::{
Volatility,
};
use datafusion_functions_nested::range::range_udf;
use parking_lot::Mutex;
use regex::Regex;
use sqlparser::ast::Ident;
use sqlparser::tokenizer::Span;

/// test that casting happens on udfs.
/// c11 is f32, but `custom_sqrt` requires f64. Casting happens but the logical plan and
Expand Down Expand Up @@ -1187,6 +1187,7 @@ async fn create_scalar_function_from_sql_statement_postgres_syntax() -> Result<(
name: Some(Ident {
value: "name".into(),
quote_style: None,
span: Span::empty(),
}),
data_type: DataType::Utf8,
default_expr: None,
Expand All @@ -1196,6 +1197,7 @@ async fn create_scalar_function_from_sql_statement_postgres_syntax() -> Result<(
language: Some(Ident {
value: "plrust".into(),
quote_style: None,
span: Span::empty(),
}),
behavior: None,
function_body: Some(lit(body)),
Expand Down
2 changes: 2 additions & 0 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ use sqlparser::ast::{
/// // to 42 = 5 AND b = 6
/// assert_eq!(rewritten.data, lit(42).eq(lit(5)).and(col("b").eq(lit(6))));
#[derive(Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
// TODO make the enum smaller with more boxing (looks like Wildcard is now bigger)
#[allow(clippy::large_enum_variant)]
pub enum Expr {
/// An expression with a specific name.
Alias(Alias),
Expand Down
5 changes: 5 additions & 0 deletions datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ impl FunctionArgs {
"Calling {name}: SEPARATOR not supported in function arguments: {sep}"
)
}
FunctionArgumentClause::JsonNullClause(jn) => {
return not_impl_err!(
"Calling {name}: JSON NULL clause not supported in function arguments: {jn}"
)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}
not_impl_err!("AnyOp not supported by ExprPlanner: {binary_expr:?}")
}
SQLExpr::Wildcard => Ok(Expr::Wildcard {
SQLExpr::Wildcard(_token) => Ok(Expr::Wildcard {
qualifier: None,
options: WildcardOptions::default(),
}),
SQLExpr::QualifiedWildcard(object_name) => Ok(Expr::Wildcard {
SQLExpr::QualifiedWildcard(object_name, _token) => Ok(Expr::Wildcard {
qualifier: Some(self.object_name_to_table_reference(object_name)?),
options: WildcardOptions::default(),
}),
Expand Down
14 changes: 12 additions & 2 deletions datafusion/sql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
use std::collections::VecDeque;
use std::fmt;

use sqlparser::tokenizer::TokenWithSpan;
use sqlparser::{
ast::{
ColumnDef, ColumnOptionDef, Expr, ObjectName, OrderByExpr, Query,
Statement as SQLStatement, TableConstraint, Value,
},
dialect::{keywords::Keyword, Dialect, GenericDialect},
parser::{Parser, ParserError},
tokenizer::{Token, TokenWithLocation, Tokenizer, Word},
tokenizer::{Token, Tokenizer, Word},
};

// Use `Parser::expected` instead, if possible
Expand Down Expand Up @@ -337,7 +338,7 @@ impl<'a> DFParser<'a> {
fn expected<T>(
&self,
expected: &str,
found: TokenWithLocation,
found: TokenWithSpan,
) -> Result<T, ParserError> {
parser_err!(format!("Expected {expected}, found: {found}"))
}
Expand Down Expand Up @@ -875,6 +876,7 @@ mod tests {
use super::*;
use sqlparser::ast::Expr::Identifier;
use sqlparser::ast::{BinaryOperator, DataType, Expr, Ident};
use sqlparser::tokenizer::Span;

fn expect_parse_ok(sql: &str, expected: Statement) -> Result<(), ParserError> {
let statements = DFParser::parse_sql(sql)?;
Expand Down Expand Up @@ -910,6 +912,7 @@ mod tests {
name: Ident {
value: name.into(),
quote_style: None,
span: Span::empty(),
},
data_type,
collation: None,
Expand Down Expand Up @@ -1218,6 +1221,7 @@ mod tests {
expr: Identifier(Ident {
value: "c1".to_owned(),
quote_style: None,
span: Span::empty(),
}),
asc,
nulls_first,
Expand Down Expand Up @@ -1249,6 +1253,7 @@ mod tests {
expr: Identifier(Ident {
value: "c1".to_owned(),
quote_style: None,
span: Span::empty(),
}),
asc: Some(true),
nulls_first: None,
Expand All @@ -1258,6 +1263,7 @@ mod tests {
expr: Identifier(Ident {
value: "c2".to_owned(),
quote_style: None,
span: Span::empty(),
}),
asc: Some(false),
nulls_first: Some(true),
Expand Down Expand Up @@ -1289,11 +1295,13 @@ mod tests {
left: Box::new(Identifier(Ident {
value: "c1".to_owned(),
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::Minus,
right: Box::new(Identifier(Ident {
value: "c2".to_owned(),
quote_style: None,
span: Span::empty(),
})),
},
asc: Some(true),
Expand Down Expand Up @@ -1334,11 +1342,13 @@ mod tests {
left: Box::new(Identifier(Ident {
value: "c1".to_owned(),
quote_style: None,
span: Span::empty(),
})),
op: BinaryOperator::Minus,
right: Box::new(Identifier(Ident {
value: "c2".to_owned(),
quote_style: None,
span: Span::empty(),
})),
},
asc: Some(true),
Expand Down
10 changes: 9 additions & 1 deletion datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
plan: LogicalPlan,
alias: TableAlias,
) -> Result<LogicalPlan> {
let plan = self.apply_expr_alias(plan, alias.columns)?;
let idents = alias.columns.into_iter().map(|c| c.name).collect();
let plan = self.apply_expr_alias(plan, idents)?;

LogicalPlanBuilder::from(plan)
.alias(TableReference::bare(
Expand Down Expand Up @@ -586,6 +587,13 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
| SQLDataType::Nullable(_)
| SQLDataType::LowCardinality(_)
| SQLDataType::Trigger
// MySQL datatypes
| SQLDataType::TinyBlob
| SQLDataType::MediumBlob
| SQLDataType::LongBlob
| SQLDataType::TinyText
| SQLDataType::MediumText
| SQLDataType::LongText
=> not_impl_err!(
"Unsupported SQL type {sql_type:?}"
),
Expand Down
1 change: 1 addition & 0 deletions datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
opt_rename,
opt_replace: _opt_replace,
opt_ilike: _opt_ilike,
wildcard_token: _wildcard_token,
} = options;

if opt_rename.is_some() {
Expand Down
Loading

0 comments on commit 3545647

Please sign in to comment.