Skip to content

Commit

Permalink
[FEAT] Adds SQL function modules (#2725)
Browse files Browse the repository at this point in the history
This commit defines _SQLFunctions_ and _SQLModule_ which are responsible
for registering the Daft _FunctionExpr_ to be accessible from SQL
statements. It is basic and operates on-top-of _FunctionExpr_ .. that
is, without modifying it.


* SQLFunctions – map of names (String) to functions (SQLFunction)
* SQLFunction – wrapper type over _FunctionExpr_ to get the _validate_
method.
* SQLModule – holds necessary logic for a _FunctionExpr_ variant i.e.
NumericExpr, Utf8Expr, etc.

### Overview

```rust


/// SQLModule for FunctionExpr::Numeric
impl SQLModule for SQLModuleNumeric {
    fn register(parent: &mut SQLFunctions) {
        use FunctionExpr::Numeric as f;
        use NumericExpr::*;
        parent.add("abs", f(Abs));
        ....
    }
}

// register
let mut functions = SQLFunctions::new();
functions.register::<SQLModuleNumeric>();


// usage
let args = plan_args(args)
let func = functions.get("abs")
let expr = func.validate(args)?
```

### Future Considerations

* Remove SQLFunctions global lazy-static singleton; rather, make it
owned by (field of) the planner.
* Remove SQLFunction wrapper type in favor of extending
FunctionEvaluator trait
* Add arity checks once there are variants.
* Use multi-map to support function variants.
* Extract input validation duplication from _to_field_ and _evaluate_ to
be used by SQLPlanner as well.
* Derive a module from macros on the daft_dsl declarations
  • Loading branch information
RCHowell authored Aug 28, 2024
1 parent d297f51 commit 372d9bc
Show file tree
Hide file tree
Showing 20 changed files with 721 additions and 619 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ jaq-parse = "1.0.0"
jaq-std = "1.2.0"
num-derive = "0.3.3"
num-traits = "0.2"
once_cell = "1.19.0"
pretty_assertions = "1.4.0"
rand = "^0.8"
rayon = "1.10.0"
Expand Down
2 changes: 1 addition & 1 deletion src/daft-sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ common-error = {path = "../common/error"}
daft-core = {path = "../daft-core"}
daft-dsl = {path = "../daft-dsl"}
daft-plan = {path = "../daft-plan"}
once_cell = {workspace = true}
pyo3 = {workspace = true, optional = true}
sqlparser = {workspace = true}
strum = {version = "0.26.3", features = ["derive"]}
snafu.workspace = true

[dev-dependencies]
Expand Down
7 changes: 0 additions & 7 deletions src/daft-sql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ 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
Loading

0 comments on commit 372d9bc

Please sign in to comment.