Skip to content

Commit 2ed2daa

Browse files
committed
catchup refactors
Signed-off-by: tison <[email protected]>
1 parent d64ac06 commit 2ed2daa

File tree

5 files changed

+56
-22
lines changed

5 files changed

+56
-22
lines changed

datafusion/sql/src/expr/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
748748
negated: bool,
749749
expr: SQLExpr,
750750
pattern: SQLExpr,
751-
escape_char: Option<char>,
751+
escape_char: Option<String>,
752752
schema: &DFSchema,
753753
planner_context: &mut PlannerContext,
754754
case_insensitive: bool,
@@ -758,6 +758,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
758758
if pattern_type != DataType::Utf8 && pattern_type != DataType::Null {
759759
return plan_err!("Invalid pattern in LIKE expression");
760760
}
761+
let escape_char = if let Some(char) = escape_char {
762+
if char.len() != 1 {
763+
return plan_err!("Invalid escape character in LIKE expression");
764+
}
765+
Some(char.chars().next().unwrap())
766+
} else {
767+
None
768+
};
761769
Ok(Expr::Like(Like::new(
762770
negated,
763771
Box::new(self.sql_expr_to_logical_expr(expr, schema, planner_context)?),
@@ -772,7 +780,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
772780
negated: bool,
773781
expr: SQLExpr,
774782
pattern: SQLExpr,
775-
escape_char: Option<char>,
783+
escape_char: Option<String>,
776784
schema: &DFSchema,
777785
planner_context: &mut PlannerContext,
778786
) -> Result<Expr> {
@@ -781,6 +789,14 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
781789
if pattern_type != DataType::Utf8 && pattern_type != DataType::Null {
782790
return plan_err!("Invalid pattern in SIMILAR TO expression");
783791
}
792+
let escape_char = if let Some(char) = escape_char {
793+
if char.len() != 1 {
794+
return plan_err!("Invalid escape character in SIMILAR TO expression");
795+
}
796+
Some(char.chars().next().unwrap())
797+
} else {
798+
None
799+
};
784800
Ok(Expr::SimilarTo(Like::new(
785801
negated,
786802
Box::new(self.sql_expr_to_logical_expr(expr, schema, planner_context)?),

datafusion/sql/src/parser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ impl<'a> DFParser<'a> {
498498
pub fn parse_option_value(&mut self) -> Result<Value, ParserError> {
499499
let next_token = self.parser.next_token();
500500
match next_token.token {
501-
Token::Word(Word { value, .. }) => Ok(Value::UnQuotedString(value)),
502501
Token::SingleQuotedString(s) => Ok(Value::SingleQuotedString(s)),
503502
Token::DoubleQuotedString(s) => Ok(Value::DoubleQuotedString(s)),
504503
Token::EscapedStringLiteral(s) => Ok(Value::EscapedStringLiteral(s)),

datafusion/sql/src/planner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
366366
pub(crate) fn convert_data_type(&self, sql_type: &SQLDataType) -> Result<DataType> {
367367
match sql_type {
368368
SQLDataType::Array(ArrayElemTypeDef::AngleBracket(inner_sql_type))
369-
| SQLDataType::Array(ArrayElemTypeDef::SquareBracket(inner_sql_type)) => {
369+
| SQLDataType::Array(ArrayElemTypeDef::SquareBracket(inner_sql_type, _)) => {
370370
// Arrays may be multi-dimensional.
371371
let inner_data_type = self.convert_data_type(inner_sql_type)?;
372372
Ok(DataType::new_list(inner_data_type, true))

datafusion/sql/src/select.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use datafusion_expr::{
3939
Expr, Filter, GroupingSet, LogicalPlan, LogicalPlanBuilder, Partitioning,
4040
};
4141
use sqlparser::ast::{
42-
Distinct, Expr as SQLExpr, GroupByExpr, OrderByExpr, ReplaceSelectItem,
43-
WildcardAdditionalOptions, WindowType,
42+
Distinct, Expr as SQLExpr, GroupByExpr, NamedWindowExpr, OrderByExpr,
43+
ReplaceSelectItem, WildcardAdditionalOptions, WindowType,
4444
};
4545
use sqlparser::ast::{NamedWindowDefinition, Select, SelectItem, TableWithJoins};
4646

@@ -727,10 +727,17 @@ fn match_window_definitions(
727727
}
728728
| SelectItem::UnnamedExpr(SQLExpr::Function(f)) = proj
729729
{
730-
for NamedWindowDefinition(window_ident, window_spec) in named_windows.iter() {
730+
for NamedWindowDefinition(window_ident, window_expr) in named_windows.iter() {
731731
if let Some(WindowType::NamedWindow(ident)) = &f.over {
732732
if ident.eq(window_ident) {
733-
f.over = Some(WindowType::WindowSpec(window_spec.clone()))
733+
f.over = Some(match window_expr {
734+
NamedWindowExpr::NamedWindow(ident) => {
735+
WindowType::NamedWindow(ident.clone())
736+
}
737+
NamedWindowExpr::WindowSpec(spec) => {
738+
WindowType::WindowSpec(spec.clone())
739+
}
740+
})
734741
}
735742
}
736743
}

datafusion/sql/src/statement.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ use datafusion_expr::{
5353
Volatility, WriteOp,
5454
};
5555
use sqlparser::ast;
56-
use sqlparser::ast::{Assignment, ColumnDef, CreateTableOptions, Delete, DescribeAlias, Expr as SQLExpr, Expr, FromTable, Ident, Insert, ObjectName, ObjectType, Query, SchemaName, SetExpr, ShowCreateObject, ShowStatementFilter, Statement, TableConstraint, TableFactor, TableWithJoins, TransactionMode, UnaryOperator, Value};
56+
use sqlparser::ast::{
57+
Assignment, ColumnDef, CreateTableOptions, Delete, DescribeAlias, Expr as SQLExpr,
58+
Expr, FromTable, Ident, Insert, ObjectName, ObjectType, Query, SchemaName, SetExpr,
59+
ShowCreateObject, ShowStatementFilter, Statement, TableConstraint, TableFactor,
60+
TableWithJoins, TransactionMode, UnaryOperator, Value,
61+
};
5762
use sqlparser::parser::ParserError::ParserError;
5863

5964
fn ident_to_string(ident: &Ident) -> String {
@@ -417,18 +422,19 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
417422
}
418423
ObjectType::Schema => {
419424
let name = match name {
420-
TableReference::Bare { table } => Ok(SchemaReference::Bare { schema: table } ) ,
421-
TableReference::Partial { schema, table } => Ok(SchemaReference::Full { schema: table,catalog: schema }),
425+
TableReference::Bare { table } => Ok(SchemaReference::Bare { schema: table }),
426+
TableReference::Partial { schema, table } => Ok(SchemaReference::Full { schema: table, catalog: schema }),
422427
TableReference::Full { catalog: _, schema: _, table: _ } => {
423428
Err(ParserError("Invalid schema specifier (has 3 parts)".to_string()))
424-
},
429+
}
425430
}?;
426431
Ok(LogicalPlan::Ddl(DdlStatement::DropCatalogSchema(DropCatalogSchema {
427432
name,
428433
if_exists,
429434
cascade,
430435
schema: DFSchemaRef::new(DFSchema::empty()),
431-
})))},
436+
})))
437+
}
432438
_ => not_impl_err!(
433439
"Only `DROP TABLE/VIEW/SCHEMA ...` statement is supported currently"
434440
),
@@ -863,9 +869,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
863869

864870
let mut options = HashMap::new();
865871
for (key, value) in statement.options {
866-
let value_string = value_to_string(&value).ok_or_else(|| {
867-
plan_err!("Unsupported Value in COPY statement {}", value)
868-
})?;
872+
let value_string = match value_to_string(&value) {
873+
None => {
874+
return plan_err!("Unsupported Value in COPY statement {}", value)
875+
}
876+
Some(v) => v,
877+
};
869878
if !(&key.contains('.')) {
870879
// If config does not belong to any namespace, assume it is
871880
// a format option and apply the format prefix for backwards
@@ -885,9 +894,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
885894
} else {
886895
let e = || {
887896
DataFusionError::Configuration(
888-
"Format not explicitly set and unable to get file extension! Use STORED AS to define file format."
889-
.to_string(),
890-
)
897+
"Format not explicitly set and unable to get file extension! Use STORED AS to define file format."
898+
.to_string(),
899+
)
891900
};
892901
// try to infer file format from file extension
893902
let extension: &str = &Path::new(&statement.target)
@@ -1130,9 +1139,12 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
11301139
// parse value string from Expr
11311140
let value_string = match &value[0] {
11321141
SQLExpr::Identifier(i) => ident_to_string(i),
1133-
SQLExpr::Value(v) => value_to_string(v).ok_or_else(|| {
1134-
plan_err!("Unsupported Value {}", value[0])
1135-
})?,
1142+
SQLExpr::Value(v) => match value_to_string(v) {
1143+
None => {
1144+
return plan_err!("Unsupported Value {}", value[0]);
1145+
}
1146+
Some(v) => v,
1147+
},
11361148
// for capture signed number e.g. +8, -8
11371149
SQLExpr::UnaryOp { op, expr } => match op {
11381150
UnaryOperator::Plus => format!("+{expr}"),

0 commit comments

Comments
 (0)