Skip to content

Commit

Permalink
[FEAT]: more sql functions (#2596)
Browse files Browse the repository at this point in the history
fills out the remaining numerics, and adds most of the utf8 functions. 


Also adds a few helper methods to `Expr` and `LiteralValue` for
extracting the underlying scalar values.
  • Loading branch information
universalmind303 authored Aug 13, 2024
1 parent 97120fd commit 006eceb
Show file tree
Hide file tree
Showing 7 changed files with 730 additions and 67 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions src/daft-dsl/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,14 @@ impl Expr {
.and_then(|_| String::from_utf8(buffer).ok())
}

/// If the expression is a literal, return it. Otherwise, return None.
pub fn as_literal(&self) -> Option<&lit::LiteralValue> {
match self {
Expr::Literal(lit) => Some(lit),
_ => None,
}
}

pub fn has_agg(&self) -> bool {
use Expr::*;

Expand Down
65 changes: 65 additions & 0 deletions src/daft-dsl/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,71 @@ impl LiteralValue {
Python(..) => display_sql_err,
}
}

/// If the liter is a boolean, return it. Otherwise, return None.
pub fn as_bool(&self) -> Option<bool> {
match self {
LiteralValue::Boolean(b) => Some(*b),
_ => None,
}
}

/// If the literal is a string, return it. Otherwise, return None.
pub fn as_str(&self) -> Option<&str> {
match self {
LiteralValue::Utf8(s) => Some(s),
_ => None,
}
}
/// If the literal is `Binary`, return it. Otherwise, return None.
pub fn as_binary(&self) -> Option<&[u8]> {
match self {
LiteralValue::Binary(b) => Some(b),
_ => None,
}
}
/// If the literal is `Int32`, return it. Otherwise, return None.
pub fn as_i32(&self) -> Option<i32> {
match self {
LiteralValue::Int32(i) => Some(*i),
_ => None,
}
}
/// If the literal is `UInt32`, return it. Otherwise, return None.
pub fn as_u32(&self) -> Option<u32> {
match self {
LiteralValue::UInt32(i) => Some(*i),
_ => None,
}
}
/// If the literal is `Int64`, return it. Otherwise, return None.
pub fn as_i64(&self) -> Option<i64> {
match self {
LiteralValue::Int64(i) => Some(*i),
_ => None,
}
}
/// If the literal is `UInt64`, return it. Otherwise, return None.
pub fn as_u64(&self) -> Option<u64> {
match self {
LiteralValue::UInt64(i) => Some(*i),
_ => None,
}
}
/// If the literal is `Float64`, return it. Otherwise, return None.
pub fn as_f64(&self) -> Option<f64> {
match self {
LiteralValue::Float64(f) => Some(*f),
_ => None,
}
}
/// If the literal is a series, return it. Otherwise, return None.
pub fn as_series(&self) -> Option<&Series> {
match self {
LiteralValue::Series(series) => Some(series),
_ => None,
}
}
}

pub trait Literal {
Expand Down
1 change: 1 addition & 0 deletions src/daft-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ daft-dsl = {path = "../daft-dsl"}
daft-plan = {path = "../daft-plan"}
pyo3 = {workspace = true, optional = true}
sqlparser = {workspace = true}
strum = {version = "0.26.3", features = ["derive"]}
snafu.workspace = true

[dev-dependencies]
Expand Down
15 changes: 15 additions & 0 deletions src/daft-sql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ impl From<ParserError> for PlannerError {
PlannerError::SQLParserError { source: value }
}
}
impl From<strum::ParseError> for PlannerError {
fn from(value: strum::ParseError) -> Self {
PlannerError::ParseError {
message: value.to_string(),
}
}
}

impl PlannerError {
pub fn column_not_found<A: Into<String>, B: Into<String>>(column_name: A, relation: B) -> Self {
Expand Down Expand Up @@ -98,6 +105,14 @@ macro_rules! invalid_operation_err {
return Err($crate::error::PlannerError::invalid_operation(format!($($arg)*)))
};
}
#[macro_export]
macro_rules! ensure {
($condition:expr, $($arg:tt)*) => {
if !$condition {
return Err($crate::error::PlannerError::invalid_operation(format!($($arg)*)))
}
};
}

impl From<PlannerError> for DaftError {
fn from(value: PlannerError) -> Self {
Expand Down
Loading

0 comments on commit 006eceb

Please sign in to comment.