-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds sql `image_decode` function. - moves `image_decode` out of `daft-dsl` and into `daft-functions`. - some small changes to `SQLFunction` so that it can work with `ScalarUDF`
- Loading branch information
1 parent
60ebf82
commit 734c13f
Showing
15 changed files
with
364 additions
and
227 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use daft_core::{ | ||
datatypes::{DataType, Field, ImageMode}, | ||
schema::Schema, | ||
series::Series, | ||
}; | ||
|
||
use common_error::{DaftError, DaftResult}; | ||
use daft_dsl::{ | ||
functions::{ScalarFunction, ScalarUDF}, | ||
ExprRef, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// Container for the keyword arguments for `image_decode` | ||
/// ex: | ||
/// ```text | ||
/// image_decode(input) | ||
/// image_decode(input, mode='RGB') | ||
/// image_decode(input, mode='RGB', on_error='raise') | ||
/// image_decode(input, on_error='null') | ||
/// image_decode(input, on_error='null', mode='RGB') | ||
/// image_decode(input, mode='RGB', on_error='null') | ||
/// ``` | ||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
pub struct ImageDecode { | ||
pub mode: Option<ImageMode>, | ||
pub raise_on_error: bool, | ||
} | ||
|
||
impl Default for ImageDecode { | ||
fn default() -> Self { | ||
Self { | ||
mode: None, | ||
raise_on_error: true, | ||
} | ||
} | ||
} | ||
|
||
#[typetag::serde] | ||
impl ScalarUDF for ImageDecode { | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"image_decode" | ||
} | ||
|
||
fn to_field(&self, inputs: &[ExprRef], schema: &Schema) -> DaftResult<Field> { | ||
match inputs { | ||
[input] => { | ||
let field = input.to_field(schema)?; | ||
if !matches!(field.dtype, DataType::Binary) { | ||
return Err(DaftError::TypeError(format!( | ||
"ImageDecode can only decode BinaryArrays, got {}", | ||
field | ||
))); | ||
} | ||
Ok(Field::new(field.name, DataType::Image(self.mode))) | ||
} | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series]) -> DaftResult<Series> { | ||
let raise_error_on_failure = self.raise_on_error; | ||
match inputs { | ||
[input] => input.image_decode(raise_error_on_failure, self.mode), | ||
_ => Err(DaftError::ValueError(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
} | ||
|
||
pub fn decode(input: ExprRef, args: Option<ImageDecode>) -> ExprRef { | ||
ScalarFunction::new(args.unwrap_or_default(), vec![input]).into() | ||
} | ||
|
||
#[cfg(feature = "python")] | ||
use { | ||
daft_dsl::python::PyExpr, | ||
pyo3::{pyfunction, PyResult}, | ||
}; | ||
|
||
#[cfg(feature = "python")] | ||
#[pyfunction] | ||
#[pyo3(name = "image_decode")] | ||
pub fn py_decode( | ||
expr: PyExpr, | ||
raise_on_error: Option<bool>, | ||
mode: Option<ImageMode>, | ||
) -> PyResult<PyExpr> { | ||
let image_decode = ImageDecode { | ||
mode, | ||
raise_on_error: raise_on_error.unwrap_or(true), | ||
}; | ||
|
||
Ok(decode(expr.into(), Some(image_decode)).into()) | ||
} |
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,11 @@ | ||
pub mod decode; | ||
|
||
#[cfg(feature = "python")] | ||
use pyo3::prelude::*; | ||
|
||
#[cfg(feature = "python")] | ||
pub fn register_modules(_py: Python, parent: &PyModule) -> PyResult<()> { | ||
parent.add_wrapped(wrap_pyfunction!(decode::py_decode))?; | ||
|
||
Ok(()) | ||
} |
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
Oops, something went wrong.