-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functions to daft-connect (#3780)
adds the following: - all of spark's [math functions](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/functions.html#math-functions) we currently have implemented. - all of spark's [string functions](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/functions.html#string-functions) we currently have implemented - all of spark's [normal functions](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/functions.html#normal-functions) we currently have implemented
- Loading branch information
1 parent
b0face1
commit 79f642a
Showing
13 changed files
with
529 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use daft_core::count_mode::CountMode; | ||
use daft_dsl::col; | ||
use daft_schema::dtype::DataType; | ||
use spark_connect::Expression; | ||
|
||
use super::{FunctionModule, SparkFunction, UnaryFunction}; | ||
use crate::{error::ConnectResult, invalid_argument_err, spark_analyzer::SparkAnalyzer}; | ||
|
||
pub struct AggregateFunctions; | ||
|
||
impl FunctionModule for AggregateFunctions { | ||
fn register(parent: &mut super::SparkFunctions) { | ||
parent.add_fn("count", CountFunction); | ||
parent.add_fn("mean", UnaryFunction(|arg| arg.mean())); | ||
parent.add_fn("stddev", UnaryFunction(|arg| arg.stddev())); | ||
parent.add_fn("min", UnaryFunction(|arg| arg.min())); | ||
parent.add_fn("max", UnaryFunction(|arg| arg.max())); | ||
parent.add_fn("sum", UnaryFunction(|arg| arg.sum())); | ||
} | ||
} | ||
|
||
struct CountFunction; | ||
|
||
impl SparkFunction for CountFunction { | ||
fn to_expr( | ||
&self, | ||
args: &[Expression], | ||
analyzer: &SparkAnalyzer, | ||
) -> ConnectResult<daft_dsl::ExprRef> { | ||
match args { | ||
[arg] => { | ||
let arg = analyzer.to_daft_expr(arg)?; | ||
|
||
let arg = if arg.as_literal().and_then(|lit| lit.as_i32()) == Some(1i32) { | ||
col("*") | ||
} else { | ||
arg | ||
}; | ||
|
||
let count = arg.count(CountMode::All).cast(&DataType::Int64); | ||
|
||
Ok(count) | ||
} | ||
_ => invalid_argument_err!("requires exactly one argument"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.