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

fix: Standardised additional SQL interface errors #16829

Merged
merged 2 commits into from
Jun 10, 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
11 changes: 6 additions & 5 deletions crates/polars-sql/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cell::RefCell;

use polars_core::prelude::*;
use polars_error::to_compute_err;
use polars_lazy::prelude::*;
use polars_ops::frame::JoinCoalesce;
use polars_plan::prelude::*;
Expand All @@ -15,7 +14,9 @@ use sqlparser::dialect::GenericDialect;
use sqlparser::parser::{Parser, ParserOptions};

use crate::function_registry::{DefaultFunctionRegistry, FunctionRegistry};
use crate::sql_expr::{parse_sql_array, parse_sql_expr, process_join_constraint};
use crate::sql_expr::{
parse_sql_array, parse_sql_expr, process_join_constraint, to_sql_interface_err,
};
use crate::table_functions::PolarsTableFunctions;

/// The SQLContext is the main entry point for executing SQL queries.
Expand Down Expand Up @@ -115,9 +116,9 @@ impl SQLContext {

let ast = parser
.try_with_sql(query)
.map_err(to_compute_err)?
.map_err(to_sql_interface_err)?
.parse_statements()
.map_err(to_compute_err)?;
.map_err(to_sql_interface_err)?;

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())?;
Expand Down Expand Up @@ -913,7 +914,7 @@ impl SQLContext {
) -> PolarsResult<LazyFrame> {
polars_ensure!(
!contains_wildcard,
SQLSyntax: "GROUP BY error: can't process wildcard in group_by"
SQLSyntax: "GROUP BY error: cannot process wildcard in group_by"
);
let schema_before = lf.schema_with_arenas(&mut self.lp_arena, &mut self.expr_arena)?;
let group_by_keys_schema =
Expand Down
18 changes: 9 additions & 9 deletions crates/polars-sql/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,10 +1343,10 @@ impl FromSQLExpr for f64 {
SQLExpr::Value(v) => match v {
SQLValue::Number(s, _) => s
.parse()
.map_err(|_| polars_err!(SQLInterface: "can't parse literal {:?}", s)),
_ => polars_bail!(SQLInterface: "can't parse literal {:?}", v),
.map_err(|_| polars_err!(SQLInterface: "cannot parse literal {:?}", s)),
_ => polars_bail!(SQLInterface: "cannot parse literal {:?}", v),
},
_ => polars_bail!(SQLInterface: "can't parse literal {:?}", expr),
_ => polars_bail!(SQLInterface: "cannot parse literal {:?}", expr),
}
}
}
Expand All @@ -1359,9 +1359,9 @@ impl FromSQLExpr for bool {
match expr {
SQLExpr::Value(v) => match v {
SQLValue::Boolean(v) => Ok(*v),
_ => polars_bail!(SQLInterface: "can't parse boolean {:?}", v),
_ => polars_bail!(SQLInterface: "cannot parse boolean {:?}", v),
},
_ => polars_bail!(SQLInterface: "can't parse boolean {:?}", expr),
_ => polars_bail!(SQLInterface: "cannot parse boolean {:?}", expr),
}
}
}
Expand All @@ -1374,9 +1374,9 @@ impl FromSQLExpr for String {
match expr {
SQLExpr::Value(v) => match v {
SQLValue::SingleQuotedString(s) => Ok(s.clone()),
_ => polars_bail!(SQLInterface: "can't parse literal {:?}", v),
_ => polars_bail!(SQLInterface: "cannot parse literal {:?}", v),
},
_ => polars_bail!(SQLInterface: "can't parse literal {:?}", expr),
_ => polars_bail!(SQLInterface: "cannot parse literal {:?}", expr),
}
}
}
Expand All @@ -1392,9 +1392,9 @@ impl FromSQLExpr for StrptimeOptions {
format: Some(s.clone()),
..StrptimeOptions::default()
}),
_ => polars_bail!(SQLInterface: "can't parse literal {:?}", v),
_ => polars_bail!(SQLInterface: "cannot parse literal {:?}", v),
},
_ => polars_bail!(SQLInterface: "can't parse literal {:?}", expr),
_ => polars_bail!(SQLInterface: "cannot parse literal {:?}", expr),
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions crates/polars-sql/src/sql_expr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::Display;
use std::ops::Div;

use polars_core::export::regex;
use polars_core::prelude::*;
use polars_error::to_compute_err;
use polars_lazy::prelude::*;
use polars_plan::prelude::typed_lit;
use polars_plan::prelude::LiteralValue::Null;
Expand All @@ -29,6 +29,13 @@ use crate::SQLContext;
static DATE_LITERAL_RE: std::sync::OnceLock<Regex> = std::sync::OnceLock::new();
static TIME_LITERAL_RE: std::sync::OnceLock<Regex> = std::sync::OnceLock::new();

#[inline]
#[cold]
#[must_use]
pub fn to_sql_interface_err(err: impl Display) -> PolarsError {
PolarsError::SQLInterface(err.to_string().into())
}

fn timeunit_from_precision(prec: &Option<u64>) -> PolarsResult<TimeUnit> {
Ok(match prec {
None => TimeUnit::Microseconds,
Expand Down Expand Up @@ -804,7 +811,7 @@ impl SQLExprVisitor<'_> {
.map(|n: i64| AnyValue::Int64(if negate { -n } else { n }))
.map_err(|_| ())
}
.map_err(|_| polars_err!(SQLInterface: "cannot parse literal: {s:?}"))?
.map_err(|_| polars_err!(SQLInterface: "cannot parse literal: {:?}", s))?
},
#[cfg(feature = "binary_encoding")]
SQLValue::HexStringLiteral(x) => {
Expand Down Expand Up @@ -1138,8 +1145,10 @@ pub fn sql_expr<S: AsRef<str>>(s: S) -> PolarsResult<Expr> {
..Default::default()
});

let mut ast = parser.try_with_sql(s.as_ref()).map_err(to_compute_err)?;
let expr = ast.parse_select_item().map_err(to_compute_err)?;
let mut ast = parser
.try_with_sql(s.as_ref())
.map_err(to_sql_interface_err)?;
let expr = ast.parse_select_item().map_err(to_sql_interface_err)?;

Ok(match &expr {
SelectItem::ExprWithAlias { expr, alias } => {
Expand Down Expand Up @@ -1169,7 +1178,7 @@ pub(crate) fn parse_sql_array(expr: &SQLExpr, ctx: &mut SQLContext) -> PolarsRes
};
visitor.array_expr_to_series(arr.elem.as_slice())
},
_ => polars_bail!(ComputeError: "Expected array expression, found {:?}", expr),
_ => polars_bail!(SQLSyntax: "Expected array expression, found {:?}", expr),
}
}

Expand Down
40 changes: 0 additions & 40 deletions crates/polars-sql/tests/iss_7436.rs

This file was deleted.

38 changes: 0 additions & 38 deletions crates/polars-sql/tests/iss_7437.rs

This file was deleted.

27 changes: 0 additions & 27 deletions crates/polars-sql/tests/iss_7440.rs

This file was deleted.

26 changes: 0 additions & 26 deletions crates/polars-sql/tests/iss_8395.rs

This file was deleted.

45 changes: 0 additions & 45 deletions crates/polars-sql/tests/iss_8419.rs

This file was deleted.

Loading