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(rust,python,cli): add SQL support for timestamp precision modifier #13936

Merged
merged 3 commits into from
Jan 24, 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
12 changes: 6 additions & 6 deletions crates/polars-sql/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ impl SQLContext {
right,
} => self.process_union(left, right, set_quantifier, query),
SetExpr::SetOperation { op, .. } => {
polars_bail!(InvalidOperation: "{} operation not yet supported", op)
polars_bail!(InvalidOperation: "'{}' operation not yet supported", op)
},
op => polars_bail!(InvalidOperation: "{} operation not yet supported", op),
op => polars_bail!(InvalidOperation: "'{}' operation not yet supported", op),
}
}

Expand Down Expand Up @@ -232,7 +232,7 @@ impl SQLContext {
concatenated.map(|lf| lf.unique(None, UniqueKeepStrategy::Any))
},
#[allow(unreachable_patterns)]
_ => polars_bail!(InvalidOperation: "UNION {} is not yet supported", quantifier),
_ => polars_bail!(InvalidOperation: "'UNION {}' is not yet supported", quantifier),
}
}

Expand Down Expand Up @@ -610,11 +610,11 @@ impl SQLContext {
self.table_map.insert(alias.name.value.clone(), lf.clone());
Ok((alias.name.value.clone(), lf))
} else {
polars_bail!(ComputeError: "Derived tables must have aliases");
polars_bail!(ComputeError: "derived tables must have aliases");
}
},
// Support bare table, optional with alias for now
_ => polars_bail!(ComputeError: "not implemented"),
_ => polars_bail!(ComputeError: "not yet implemented: {}", relation),
}
}

Expand Down Expand Up @@ -774,7 +774,7 @@ impl SQLContext {
cols(schema.iter_names())
},
e => polars_bail!(
ComputeError: "Invalid wildcard expression: {:?}",
ComputeError: "invalid wildcard expression: {:?}",
e
),
};
Expand Down
42 changes: 21 additions & 21 deletions crates/polars-sql/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,10 @@ impl SQLFunctionVisitor<'_> {
polars_bail!(InvalidOperation: "Round does not (yet) support negative 'decimals': {}", function.args[1])
}
},
_ => polars_bail!(InvalidOperation: "Invalid 'decimals' for Round: {}", function.args[1]),
_ => polars_bail!(InvalidOperation: "invalid 'decimals' for Round: {}", function.args[1]),
}))
}),
_ => polars_bail!(InvalidOperation:"Invalid number of arguments for Round: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for Round: {}", function.args.len()),
},
Sign => self.visit_unary(Expr::sign),
Sqrt => self.visit_unary(Expr::sqrt),
Expand Down Expand Up @@ -811,7 +811,7 @@ impl SQLFunctionVisitor<'_> {
3 => self.try_visit_ternary(|cond: Expr, expr1: Expr, expr2: Expr| {
Ok(when(cond).then(expr1).otherwise(expr2))
}),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for If: {}", function.args.len()
_ => polars_bail!(InvalidOperation: "invalid number of arguments for If: {}", function.args.len()
),
},
IfNull => match function.args.len() {
Expand All @@ -826,13 +826,13 @@ impl SQLFunctionVisitor<'_> {
Date => match function.args.len() {
1 => self.visit_unary(|e| e.str().to_date(StrptimeOptions::default())),
2 => self.visit_binary(|e, fmt| e.str().to_date(fmt)),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for Date: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for Date: {}", function.args.len()),
},
DatePart => self.try_visit_binary(|e, part| {
match part {
Expr::Literal(LiteralValue::String(p)) => parse_date_part(e, &p),
_ => {
polars_bail!(InvalidOperation: "Invalid 'part' for DatePart: {}", function.args[1]);
polars_bail!(InvalidOperation: "invalid 'part' for DatePart: {}", function.args[1]);
}
}
}),
Expand All @@ -842,12 +842,12 @@ impl SQLFunctionVisitor<'_> {
// ----
BitLength => self.visit_unary(|e| e.str().len_bytes() * lit(8)),
Concat => if function.args.is_empty() {
polars_bail!(InvalidOperation: "Invalid number of arguments for Concat: 0");
polars_bail!(InvalidOperation: "invalid number of arguments for Concat: 0");
} else {
self.visit_variadic(|exprs: &[Expr]| concat_str(exprs, "", true))
},
ConcatWS => if function.args.len() < 2 {
polars_bail!(InvalidOperation: "Invalid number of arguments for ConcatWS: {}", function.args.len());
polars_bail!(InvalidOperation: "invalid number of arguments for ConcatWS: {}", function.args.len());
} else {
self.try_visit_variadic(|exprs: &[Expr]| {
match &exprs[0] {
Expand All @@ -867,7 +867,7 @@ impl SQLFunctionVisitor<'_> {
let len = if n > 0 { lit(n) } else { (e.clone().str().len_chars() + lit(n)).clip_min(lit(0)) };
e.str().slice(lit(0), len)
},
Expr::Literal(_) => polars_bail!(InvalidOperation: "Invalid 'n_chars' for Left: {}", function.args[1]),
Expr::Literal(_) => polars_bail!(InvalidOperation: "invalid 'n_chars' for Left: {}", function.args[1]),
_ => {
when(length.clone().gt_eq(lit(0)))
.then(e.clone().str().slice(lit(0), length.clone().abs()))
Expand All @@ -880,7 +880,7 @@ impl SQLFunctionVisitor<'_> {
LTrim => match function.args.len() {
1 => self.visit_unary(|e| e.str().strip_chars_start(lit(Null))),
2 => self.visit_binary(|e, s| e.str().strip_chars_start(s)),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for LTrim: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for LTrim: {}", function.args.len()),
},
OctetLength => self.visit_unary(|e| e.str().len_bytes()),
StrPos => {
Expand All @@ -894,23 +894,23 @@ impl SQLFunctionVisitor<'_> {
match (pat, flags) {
(Expr::Literal(LiteralValue::String(s)), Expr::Literal(LiteralValue::String(f))) => {
if f.is_empty() {
polars_bail!(InvalidOperation: "Invalid/empty 'flags' for RegexpLike: {}", function.args[2]);
polars_bail!(InvalidOperation: "invalid/empty 'flags' for RegexpLike: {}", function.args[2]);
};
lit(format!("(?{}){}", f, s))
},
_ => {
polars_bail!(InvalidOperation: "Invalid arguments for RegexpLike: {}, {}", function.args[1], function.args[2]);
polars_bail!(InvalidOperation: "invalid arguments for RegexpLike: {}, {}", function.args[1], function.args[2]);
},
},
true))
}),
_ => polars_bail!(InvalidOperation:"Invalid number of arguments for RegexpLike: {}",function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for RegexpLike: {}",function.args.len()),
},
Replace => match function.args.len() {
3 => self.try_visit_ternary(|e, old, new| {
Ok(e.str().replace_all(old, new, true))
}),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for Replace: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for Replace: {}", function.args.len()),
},
Reverse => self.visit_unary(|e| e.str().reverse()),
Right => self.try_visit_binary(|e, length| {
Expand All @@ -921,7 +921,7 @@ impl SQLFunctionVisitor<'_> {
let offset = if n < 0 { lit(n.abs()) } else { e.clone().str().len_chars().cast(DataType::Int32) - lit(n) };
e.str().slice(offset, lit(Null))
},
Expr::Literal(_) => polars_bail!(InvalidOperation: "Invalid 'n_chars' for Right: {}", function.args[1]),
Expr::Literal(_) => polars_bail!(InvalidOperation: "invalid 'n_chars' for Right: {}", function.args[1]),
_ => {
when(length.clone().lt(lit(0)))
.then(e.clone().str().slice(length.clone().abs(), lit(Null)))
Expand All @@ -932,7 +932,7 @@ impl SQLFunctionVisitor<'_> {
RTrim => match function.args.len() {
1 => self.visit_unary(|e| e.str().strip_chars_end(lit(Null))),
2 => self.visit_binary(|e, s| e.str().strip_chars_end(s)),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for RTrim: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for RTrim: {}", function.args.len()),
},
StartsWith => self.visit_binary(|e, s| e.str().starts_with(s)),
Substring => match function.args.len() {
Expand All @@ -942,7 +942,7 @@ impl SQLFunctionVisitor<'_> {
Expr::Literal(Null) => lit(Null),
Expr::Literal(LiteralValue::Int64(n)) if n <= 0 => e,
Expr::Literal(LiteralValue::Int64(n)) => e.str().slice(lit(n - 1), lit(Null)),
Expr::Literal(_) => polars_bail!(InvalidOperation: "Invalid 'start' for Substring: {}", function.args[1]),
Expr::Literal(_) => polars_bail!(InvalidOperation: "invalid 'start' for Substring: {}", function.args[1]),
_ => start.clone() + lit(1),
})
}),
Expand All @@ -956,9 +956,9 @@ impl SQLFunctionVisitor<'_> {
(Expr::Literal(LiteralValue::Int64(n)), _) => {
e.str().slice(lit(0), (length.clone() + lit(n - 1)).clip_min(lit(0)))
},
(Expr::Literal(_), _) => polars_bail!(InvalidOperation: "Invalid 'start' for Substring: {}", function.args[1]),
(Expr::Literal(_), _) => polars_bail!(InvalidOperation: "invalid 'start' for Substring: {}", function.args[1]),
(_, Expr::Literal(LiteralValue::Float64(_))) => {
polars_bail!(InvalidOperation: "Invalid 'length' for Substring: {}", function.args[1])
polars_bail!(InvalidOperation: "invalid 'length' for Substring: {}", function.args[1])
},
_ => {
let adjusted_start = start.clone() - lit(1);
Expand All @@ -968,7 +968,7 @@ impl SQLFunctionVisitor<'_> {
}
})
}),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for Substring: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for Substring: {}", function.args.len()),
}
Upper => self.visit_unary(|e| e.str().to_uppercase()),
// ----
Expand Down Expand Up @@ -1006,10 +1006,10 @@ impl SQLFunctionVisitor<'_> {
e.list().eval(col("").fill_null(lit(v)), false).list().join(sep, false)
})
},
_ => polars_bail!(InvalidOperation: "Invalid null value for ArrayToString: {}", function.args[2]),
_ => polars_bail!(InvalidOperation: "invalid null value for ArrayToString: {}", function.args[2]),
}
}),
_ => polars_bail!(InvalidOperation: "Invalid number of arguments for ArrayToString: {}", function.args.len()),
_ => polars_bail!(InvalidOperation: "invalid number of arguments for ArrayToString: {}", function.args.len()),
}
ArrayUnique => self.visit_unary(|e| e.list().unique()),
Explode => self.visit_unary(|e| e.explode()),
Expand Down
49 changes: 35 additions & 14 deletions crates/polars-sql/src/sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use sqlparser::ast::ExactNumberInfo;
use sqlparser::ast::{
ArrayAgg, ArrayElemTypeDef, BinaryOperator as SQLBinaryOperator, BinaryOperator, CastFormat,
DataType as SQLDataType, DateTimeField, Expr as SQLExpr, Function as SQLFunction, Ident,
JoinConstraint, OrderByExpr, Query as Subquery, SelectItem, TrimWhereField, UnaryOperator,
Value as SQLValue,
JoinConstraint, OrderByExpr, Query as Subquery, SelectItem, TimezoneInfo, TrimWhereField,
UnaryOperator, Value as SQLValue,
};
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::{Parser, ParserOptions};
Expand Down Expand Up @@ -62,11 +62,32 @@ pub(crate) fn map_sql_polars_datatype(data_type: &SQLDataType) -> PolarsResult<D
SQLDataType::Int2(_) => DataType::Int16,
SQLDataType::Int4(_) => DataType::Int32,
SQLDataType::Int8(_) => DataType::Int64,
SQLDataType::Interval => DataType::Duration(TimeUnit::Milliseconds),
SQLDataType::Interval => DataType::Duration(TimeUnit::Microseconds),
SQLDataType::Real => DataType::Float32,
SQLDataType::SmallInt(_) => DataType::Int16,
SQLDataType::Time { .. } => DataType::Time,
SQLDataType::Timestamp { .. } => DataType::Datetime(TimeUnit::Milliseconds, None),
SQLDataType::Time(_, tz) => match tz {
TimezoneInfo::None => DataType::Time,
_ => {
polars_bail!(ComputeError: "`time` with timezone is not supported; found tz={}", tz)
},
},
SQLDataType::Timestamp(prec, tz) => {
let tu = match prec {
None => TimeUnit::Microseconds,
Some(3) => TimeUnit::Milliseconds,
Some(6) => TimeUnit::Microseconds,
Some(9) => TimeUnit::Nanoseconds,
Some(n) => {
polars_bail!(ComputeError: "unsupported `timestamp` precision; expected 3, 6 or 9, found prec={}", n)
},
};
match tz {
TimezoneInfo::None => DataType::Datetime(tu, None),
_ => {
polars_bail!(ComputeError: "`timestamp` with timezone is not (yet) supported; found tz={}", tz)
},
}
},
SQLDataType::TinyInt(_) => DataType::Int8,
SQLDataType::UnsignedBigInt(_) => DataType::UInt64,
SQLDataType::UnsignedInt(_) | SQLDataType::UnsignedInteger(_) => DataType::UInt32,
Expand Down Expand Up @@ -270,7 +291,7 @@ impl SQLExprVisitor<'_> {
}
},
_ => polars_bail!(
ComputeError: "Invalid identifier {:?}",
ComputeError: "invalid identifier {:?}",
idents
),
}
Expand Down Expand Up @@ -351,23 +372,23 @@ impl SQLExprVisitor<'_> {
// ----
SQLBinaryOperator::PGRegexMatch => match right {
Expr::Literal(LiteralValue::String(_)) => left.str().contains(right, true),
_ => polars_bail!(ComputeError: "Invalid pattern for '~' operator: {:?}", right),
_ => polars_bail!(ComputeError: "invalid pattern for '~' operator: {:?}", right),
},
SQLBinaryOperator::PGRegexNotMatch => match right {
Expr::Literal(LiteralValue::String(_)) => left.str().contains(right, true).not(),
_ => polars_bail!(ComputeError: "Invalid pattern for '!~' operator: {:?}", right),
_ => polars_bail!(ComputeError: "invalid pattern for '!~' operator: {:?}", right),
},
SQLBinaryOperator::PGRegexIMatch => match right {
Expr::Literal(LiteralValue::String(pat)) => {
left.str().contains(lit(format!("(?i){}", pat)), true)
},
_ => polars_bail!(ComputeError: "Invalid pattern for '~*' operator: {:?}", right),
_ => polars_bail!(ComputeError: "invalid pattern for '~*' operator: {:?}", right),
},
SQLBinaryOperator::PGRegexNotIMatch => match right {
Expr::Literal(LiteralValue::String(pat)) => {
left.str().contains(lit(format!("(?i){}", pat)), true).not()
},
_ => polars_bail!(ComputeError: "Invalid pattern for '!~*' operator: {:?}", right),
_ => polars_bail!(ComputeError: "invalid pattern for '!~*' operator: {:?}", right),
},
other => polars_bail!(ComputeError: "SQL operator {:?} is not yet supported", other),
})
Expand All @@ -388,7 +409,7 @@ impl SQLExprVisitor<'_> {
(UnaryOperator::Plus, _) => lit(0) + expr,
(UnaryOperator::Minus, _) => lit(0) - expr,
(UnaryOperator::Not, _) => expr.not(),
other => polars_bail!(InvalidOperation: "Unary operator {:?} is not supported", other),
other => polars_bail!(InvalidOperation: "unary operator {:?} is not supported", other),
})
}

Expand Down Expand Up @@ -424,7 +445,7 @@ impl SQLExprVisitor<'_> {
BinaryOperator::LtEq => Ok(left.lt_eq(right.min())),
BinaryOperator::Eq => polars_bail!(ComputeError: "ALL cannot be used with ="),
BinaryOperator::NotEq => polars_bail!(ComputeError: "ALL cannot be used with !="),
_ => polars_bail!(ComputeError: "Invalid comparison operator"),
_ => polars_bail!(ComputeError: "invalid comparison operator"),
}
}

Expand All @@ -447,7 +468,7 @@ impl SQLExprVisitor<'_> {
BinaryOperator::LtEq => Ok(left.lt_eq(right.max())),
BinaryOperator::Eq => Ok(left.is_in(right)),
BinaryOperator::NotEq => Ok(left.is_in(right).not()),
_ => polars_bail!(ComputeError: "Invalid comparison operator"),
_ => polars_bail!(ComputeError: "invalid comparison operator"),
}
}

Expand Down Expand Up @@ -891,7 +912,7 @@ pub(super) fn process_join_constraint(
return Ok((using.clone(), using.clone()));
}
}
polars_bail!(InvalidOperation: "Unsupported SQL join constraint:\n{:?}", constraint);
polars_bail!(InvalidOperation: "unsupported SQL join constraint:\n{:?}", constraint);
}

/// parse a SQL expression to a polars expression
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/sql/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_round_ndigits_errors() -> None:
df = pl.DataFrame({"n": [99.999]})
with pl.SQLContext(df=df, eager_execution=True) as ctx:
with pytest.raises(
InvalidOperationError, match="Invalid 'decimals' for Round: ??"
InvalidOperationError, match="invalid 'decimals' for Round: ??"
):
ctx.execute("SELECT ROUND(n,'??') AS n FROM df")
with pytest.raises(
Expand Down
Loading