Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ recursive = "0.1.1"
regex = "1.8"
rstest = "0.25.0"
serde_json = "1"
sqlparser = { version = "0.55.0", default-features = false, features = ["std", "visitor"] }
sqlparser = { version = "0.58.0", default-features = false, features = ["std", "visitor"] }
tempfile = "3"
testcontainers = { version = "0.24", features = ["default"] }
testcontainers-modules = { version = "0.12" }
Expand Down
29 changes: 15 additions & 14 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
operand,
conditions,
else_result,
case_token: _,
end_token: _,
} => self.sql_case_identifier_to_expr(
operand,
conditions,
Expand Down Expand Up @@ -446,6 +448,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
substring_from,
substring_for,
special: _,
shorthand: _,
} => self.sql_substring_to_expr(
expr,
substring_from,
Expand Down Expand Up @@ -813,7 +816,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
negated: bool,
expr: SQLExpr,
pattern: SQLExpr,
escape_char: Option<String>,
escape_char: Option<Value>,
schema: &DFSchema,
planner_context: &mut PlannerContext,
case_insensitive: bool,
Expand All @@ -823,13 +826,12 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
return not_impl_err!("ANY in LIKE expression");
}
let pattern = self.sql_expr_to_logical_expr(pattern, schema, planner_context)?;
let escape_char = if let Some(char) = escape_char {
if char.len() != 1 {
return plan_err!("Invalid escape character in LIKE expression");
let escape_char = match escape_char {
Some(Value::SingleQuotedString(char)) if char.len() == 1 => {
Some(char.chars().next().unwrap())
}
Some(char.chars().next().unwrap())
} else {
None
Some(value) => return plan_err!("Invalid escape character in LIKE expression. Expected a single character wrapped with single quotes, got {value}"),
None => None,
};
Ok(Expr::Like(Like::new(
negated,
Expand All @@ -845,7 +847,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
negated: bool,
expr: SQLExpr,
pattern: SQLExpr,
escape_char: Option<String>,
escape_char: Option<Value>,
schema: &DFSchema,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
Expand All @@ -854,13 +856,12 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
if pattern_type != DataType::Utf8 && pattern_type != DataType::Null {
return plan_err!("Invalid pattern in SIMILAR TO expression");
}
let escape_char = if let Some(char) = escape_char {
if char.len() != 1 {
return plan_err!("Invalid escape character in SIMILAR TO expression");
let escape_char = match escape_char {
Some(Value::SingleQuotedString(char)) if char.len() == 1 => {
Some(char.chars().next().unwrap())
}
Some(char.chars().next().unwrap())
} else {
None
Some(value) => return plan_err!("Invalid escape character in SIMILAR TO expression. Expected a single character wrapped with single quotes, got {value}"),
None => None,
};
Ok(Expr::SimilarTo(Like::new(
negated,
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/src/expr/subquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
planner_context.set_outer_query_schema(Some(input_schema.clone().into()));

let mut spans = Spans::new();
if let SetExpr::Select(select) = subquery.body.as_ref() {
if let SetExpr::Select(select) = &subquery.body.as_ref() {
for item in &select.projection {
if let SelectItem::UnnamedExpr(SQLExpr::Identifier(ident)) = item {
if let Some(span) = Span::try_from_sqlparser_span(ident.span) {
Expand Down
10 changes: 5 additions & 5 deletions datafusion/sql/src/expr/substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::{not_impl_err, plan_err};
use datafusion_common::{DFSchema, Result, ScalarValue};
use datafusion_expr::planner::PlannerResult;
use datafusion_expr::Expr;
use datafusion_expr::{planner::PlannerResult, Expr};

use sqlparser::ast::Expr as SQLExpr;

impl<S: ContextProvider> SqlToRel<'_, S> {
Expand Down Expand Up @@ -62,12 +62,14 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
substring_from: None,
substring_for: None,
special: false,
shorthand: false,
};

return plan_err!("Substring without for/from is not valid {orig_sql:?}");
}
};

// Try to plan the substring expression using one of the registered planners
for planner in self.context_provider.get_expr_planners() {
match planner.plan_substring(substring_args)? {
PlannerResult::Planned(expr) => return Ok(expr),
Expand All @@ -77,8 +79,6 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}
}

not_impl_err!(
"Substring not supported by UserDefinedExtensionPlanners: {substring_args:?}"
)
not_impl_err!("Substring could not be planned by registered expr planner. Hint: enable the `unicode_expressions" )
}
}
9 changes: 9 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
| SQLDataType::AnyType
| SQLDataType::Table(_)
| SQLDataType::VarBit(_)
| SQLDataType::UTinyInt
| SQLDataType::USmallInt
| SQLDataType::HugeInt
| SQLDataType::UHugeInt
| SQLDataType::UBigInt
| SQLDataType::TimestampNtz
| SQLDataType::NamedTable { .. }
| SQLDataType::TsVector
| SQLDataType::TsQuery
| SQLDataType::GeometricType(_) => {
not_impl_err!("Unsupported SQL type {sql_type:?}")
}
Expand Down
61 changes: 45 additions & 16 deletions datafusion/sql/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use datafusion_expr::{
CreateMemoryTable, DdlStatement, Distinct, Expr, LogicalPlan, LogicalPlanBuilder,
};
use sqlparser::ast::{
Expr as SQLExpr, Ident, Offset as SQLOffset, OrderBy, OrderByExpr, OrderByKind,
Query, SelectInto, SetExpr,
Expr as SQLExpr, Ident, LimitClause, OrderBy, OrderByExpr, OrderByKind, Query,
SelectInto, SetExpr,
};
use sqlparser::tokenizer::Span;

Expand All @@ -54,8 +54,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
let select_into = select.into.take();
let plan =
self.select_to_plan(*select, query.order_by, planner_context)?;
let plan =
self.limit(plan, query.offset, query.limit, planner_context)?;
let plan = self.limit(plan, query.limit_clause, planner_context)?;
// Process the `SELECT INTO` after `LIMIT`.
self.select_into(plan, select_into)
}
Expand All @@ -77,7 +76,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
None,
)?;
let plan = self.order_by(plan, order_by_rex)?;
self.limit(plan, query.offset, query.limit, planner_context)
self.limit(plan, query.limit_clause, planner_context)
}
}
}
Expand All @@ -86,23 +85,53 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
fn limit(
&self,
input: LogicalPlan,
skip: Option<SQLOffset>,
fetch: Option<SQLExpr>,
limit_clause: Option<LimitClause>,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
if skip.is_none() && fetch.is_none() {
let Some(limit_clause) = limit_clause else {
return Ok(input);
}
};

// skip and fetch expressions are not allowed to reference columns from the input plan
let empty_schema = DFSchema::empty();

let skip = skip
.map(|o| self.sql_to_expr(o.value, &empty_schema, planner_context))
.transpose()?;
let fetch = fetch
.map(|e| self.sql_to_expr(e, &empty_schema, planner_context))
.transpose()?;
let (skip, fetch, limit_by_exprs) = match limit_clause {
LimitClause::LimitOffset {
limit,
offset,
limit_by,
} => {
let skip = offset
.map(|o| self.sql_to_expr(o.value, &empty_schema, planner_context))
.transpose()?;

let fetch = limit
.map(|e| self.sql_to_expr(e, &empty_schema, planner_context))
.transpose()?;

let limit_by_exprs = limit_by
.into_iter()
.map(|e| self.sql_to_expr(e, &empty_schema, planner_context))
.collect::<Result<Vec<_>>>()?;

(skip, fetch, limit_by_exprs)
}
LimitClause::OffsetCommaLimit { offset, limit } => {
let skip =
Some(self.sql_to_expr(offset, &empty_schema, planner_context)?);
let fetch =
Some(self.sql_to_expr(limit, &empty_schema, planner_context)?);
(skip, fetch, vec![])
}
};

if !limit_by_exprs.is_empty() {
return not_impl_err!("LIMIT BY clause is not supported yet");
}

if skip.is_none() && fetch.is_none() {
return Ok(input);
}

LogicalPlanBuilder::from(input)
.limit_by_expr(skip, fetch)?
.build()
Expand Down
Loading