Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Streamline SQL INTERVAL handling and improve related error messages, update sqlparser-rs lib #16744

Merged
merged 2 commits into from
Jun 6, 2024
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 @@ -72,7 +72,7 @@ serde_json = "1"
simd-json = { version = "0.13", features = ["known-key"] }
simdutf8 = "0.1.4"
smartstring = "1"
sqlparser = "0.39"
sqlparser = "0.45"
stacker = "0.1"
streaming-iterator = "0.1.9"
strength_reduce = "0.2"
Expand Down
4 changes: 1 addition & 3 deletions crates/polars-sql/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl SQLContext {
.parse_statements()
.map_err(to_compute_err)?;

polars_ensure!(ast.len() == 1, SQLInterface: "one (and only one) statement at a time please");
polars_ensure!(ast.len() == 1, SQLInterface: "one (and only one) statement can be parsed at a time");
let res = self.execute_statement(ast.first().unwrap())?;

// Ensure the result uses the proper arenas.
Expand Down Expand Up @@ -175,13 +175,11 @@ impl SQLContext {

pub(crate) fn execute_query(&mut self, query: &Query) -> PolarsResult<LazyFrame> {
self.register_ctes(query)?;

self.execute_query_no_ctes(query)
}

pub(crate) fn execute_query_no_ctes(&mut self, query: &Query) -> PolarsResult<LazyFrame> {
let lf = self.process_set_expr(&query.body, query)?;

self.process_limit_offset(lf, &query.limit, &query.offset)
}

Expand Down
1 change: 1 addition & 0 deletions crates/polars-sql/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn all_keywords() -> Vec<&'static str> {
keywords::IN,
keywords::INNER,
keywords::INT,
keywords::INTERVAL,
keywords::JOIN,
keywords::LEFT,
keywords::LIMIT,
Expand Down
22 changes: 5 additions & 17 deletions crates/polars-sql/src/sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,11 @@ impl SQLExprVisitor<'_> {
|| interval.leading_precision.is_some()
|| interval.fractional_seconds_precision.is_some()
{
polars_bail!(SQLInterface: "interval with explicit leading field or precision is not supported: {:?}", interval)
polars_bail!(SQLSyntax: "unsupported interval syntax: '{}'", interval)
}
let mut negative = false;
let s = match &*interval.value {
SQLExpr::UnaryOp {
op: UnaryOperator::Minus,
expr,
} if matches!(**expr, SQLExpr::Value(SQLValue::SingleQuotedString(_))) => {
if let SQLExpr::Value(SQLValue::SingleQuotedString(ref s)) = **expr {
negative = true;
Some(s)
} else {
unreachable!()
}
SQLExpr::UnaryOp { .. } => {
polars_bail!(SQLSyntax: "unary ops are not valid on interval strings; found {}", interval.value)
},
SQLExpr::Value(SQLValue::SingleQuotedString(s)) => Some(s),
_ => None,
Expand All @@ -439,10 +430,7 @@ impl SQLExprVisitor<'_> {
Some(s) if s.contains('-') => {
polars_bail!(SQLInterface: "minus signs are not yet supported in interval strings; found '{}'", s)
},
Some(s) => {
let d = Duration::parse_interval(s);
Ok(lit(if negative { -d } else { d }))
},
Some(s) => Ok(lit(Duration::parse_interval(s))),
None => polars_bail!(SQLSyntax: "invalid interval {:?}", interval),
}
}
Expand Down Expand Up @@ -1193,7 +1181,7 @@ fn parse_extract(expr: Expr, field: &DateTimeField) -> PolarsResult<Expr> {
DateTimeField::Year => expr.dt().year(),
DateTimeField::Quarter => expr.dt().quarter(),
DateTimeField::Month => expr.dt().month(),
DateTimeField::Week => expr.dt().week(),
DateTimeField::Week(_) => expr.dt().week(),
DateTimeField::IsoWeek => expr.dt().week(),
DateTimeField::DayOfYear | DateTimeField::Doy => expr.dt().ordinal_day(),
DateTimeField::DayOfWeek | DateTimeField::Dow => {
Expand Down
8 changes: 7 additions & 1 deletion py-polars/tests/unit/sql/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_intervals() -> None:
INTERVAL '100ms 100us' AS i2,
-- long form with/without commas (case-insensitive)
INTERVAL '1 week, 2 hours, 3 minutes, 4 seconds' AS i3,
INTERVAL '1 Quarter 2 Months 987 Microseconds' AS i4,
INTERVAL '1 QUARTER 2 Months 987 microseconds' AS i4,
FROM df
"""
)
Expand All @@ -124,3 +124,9 @@ def test_intervals() -> None:
match="minus signs are not yet supported in interval strings; found '-7d'",
):
ctx.execute("SELECT INTERVAL '-7d' AS one_week_ago FROM df")

with pytest.raises(
SQLSyntaxError,
match="unary ops are not valid on interval strings; found -'7d'",
):
ctx.execute("SELECT INTERVAL -'7d' AS one_week_ago FROM df")