Skip to content

Commit

Permalink
feat: Update sqlparser-rs, enabling "LEFT" keyword to be optional f…
Browse files Browse the repository at this point in the history
…or anti/semi joins
  • Loading branch information
alexander-beedie committed Jan 6, 2025
1 parent 11dd4b3 commit 804944f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
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 @@ -77,7 +77,7 @@ serde_json = "1"
simd-json = { version = "0.14", features = ["known-key"] }
simdutf8 = "0.1.4"
slotmap = "1"
sqlparser = "0.52"
sqlparser = "0.53"
stacker = "0.1"
streaming-iterator = "0.1.9"
strength_reduce = "0.2"
Expand Down
13 changes: 8 additions & 5 deletions crates/polars-sql/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ impl SQLContext {
| JoinOperator::LeftOuter(constraint)
| JoinOperator::RightOuter(constraint)
| JoinOperator::Inner(constraint)
| JoinOperator::Anti(constraint)
| JoinOperator::Semi(constraint)
| JoinOperator::LeftAnti(constraint)
| JoinOperator::LeftSemi(constraint)
| JoinOperator::RightAnti(constraint)
Expand All @@ -592,9 +594,9 @@ impl SQLContext {
JoinOperator::RightOuter(_) => JoinType::Right,
JoinOperator::Inner(_) => JoinType::Inner,
#[cfg(feature = "semi_anti_join")]
JoinOperator::LeftAnti(_) | JoinOperator::RightAnti(_) => JoinType::Anti,
JoinOperator::Anti(_) | JoinOperator::LeftAnti(_) | JoinOperator::RightAnti(_) => JoinType::Anti,
#[cfg(feature = "semi_anti_join")]
JoinOperator::LeftSemi(_) | JoinOperator::RightSemi(_) => JoinType::Semi,
JoinOperator::Semi(_) | JoinOperator::LeftSemi(_) | JoinOperator::RightSemi(_) => JoinType::Semi,
join_type => polars_bail!(SQLInterface: "join type '{:?}' not currently supported", join_type),
},
)?
Expand Down Expand Up @@ -1028,10 +1030,10 @@ impl SQLContext {
.columns
.iter()
.map(|c| {
if c.value.is_empty() {
if c.name.value.is_empty() {
None
} else {
Some(PlSmallStr::from_str(c.value.as_str()))
Some(PlSmallStr::from_str(c.name.value.as_str()))
}
})
.collect();
Expand Down Expand Up @@ -1399,7 +1401,8 @@ impl SQLContext {
)
} else {
let existing_columns: Vec<_> = schema.iter_names().collect();
let new_columns: Vec<_> = alias.columns.iter().map(|c| c.value.clone()).collect();
let new_columns: Vec<_> =
alias.columns.iter().map(|c| c.name.value.clone()).collect();
Ok(lf.rename(existing_columns, new_columns, true))
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/polars-sql/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ use polars_plan::plans::{typed_lit, LiteralValue};
use polars_plan::prelude::LiteralValue::Null;
use polars_plan::prelude::{col, cols, lit, StrptimeOptions};
use polars_utils::pl_str::PlSmallStr;
use sqlparser::ast::helpers::attached_token::AttachedToken;
use sqlparser::ast::{
DateTimeField, DuplicateTreatment, Expr as SQLExpr, Function as SQLFunction, FunctionArg,
FunctionArgExpr, FunctionArgumentClause, FunctionArgumentList, FunctionArguments, Ident,
OrderByExpr, Value as SQLValue, WindowSpec, WindowType,
};
use sqlparser::tokenizer::Span;

use crate::sql_expr::{adjust_one_indexed_param, parse_extract_date_part, parse_sql_expr};
use crate::SQLContext;
Expand Down Expand Up @@ -1058,6 +1060,7 @@ impl SQLFunctionVisitor<'_> {
&DateTimeField::Custom(Ident {
value: p.to_string(),
quote_style: None,
span: Span::empty(),
}),
)
},
Expand Down Expand Up @@ -1511,7 +1514,7 @@ impl SQLFunctionVisitor<'_> {
f(parse_sql_expr(sql_expr, self.ctx, self.active_schema)?)
},
[FunctionArgExpr::Wildcard] => f(parse_sql_expr(
&SQLExpr::Wildcard,
&SQLExpr::Wildcard(AttachedToken::empty()),
self.ctx,
self.active_schema,
)?),
Expand Down Expand Up @@ -1813,6 +1816,7 @@ fn _extract_func_args(
.iter()
.map(|arg| match arg {
FunctionArg::Named { arg, .. } => arg,
FunctionArg::ExprNamed { arg, .. } => arg,
FunctionArg::Unnamed(arg) => arg,
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-sql/src/sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl SQLExprVisitor<'_> {
},
SQLExpr::UnaryOp { op, expr } => self.visit_unary_op(op, expr),
SQLExpr::Value(value) => self.visit_literal(value),
SQLExpr::Wildcard => Ok(Expr::Wildcard),
SQLExpr::Wildcard(_) => Ok(Expr::Wildcard),
e @ SQLExpr::Case { .. } => self.visit_case_when_then(e),
other => {
polars_bail!(SQLInterface: "expression {:?} is not currently supported", other)
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/sql/test_joins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def foods_ipc_path() -> Path:
"SELECT * FROM tbl_a LEFT SEMI JOIN tbl_b USING (a,c)",
pl.DataFrame({"a": [2], "b": [0], "c": ["y"]}),
),
(
"SELECT * FROM tbl_a SEMI JOIN tbl_b USING (a,c)",
pl.DataFrame({"a": [2], "b": [0], "c": ["y"]}),
),
(
"SELECT * FROM tbl_a LEFT SEMI JOIN tbl_b USING (a)",
pl.DataFrame({"a": [1, 2, 3], "b": [4, 0, 6], "c": ["w", "y", "z"]}),
Expand All @@ -31,6 +35,10 @@ def foods_ipc_path() -> Path:
"SELECT * FROM tbl_a LEFT ANTI JOIN tbl_b USING (a)",
pl.DataFrame(schema={"a": pl.Int64, "b": pl.Int64, "c": pl.String}),
),
(
"SELECT * FROM tbl_a ANTI JOIN tbl_b USING (a)",
pl.DataFrame(schema={"a": pl.Int64, "b": pl.Int64, "c": pl.String}),
),
(
"SELECT * FROM tbl_a LEFT SEMI JOIN tbl_b USING (b) LEFT SEMI JOIN tbl_c USING (c)",
pl.DataFrame({"a": [1, 3], "b": [4, 6], "c": ["w", "z"]}),
Expand Down

0 comments on commit 804944f

Please sign in to comment.