-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Sign expression implemtation (#2037)
implementation for sign resolves #1917 --------- Co-authored-by: Colin <[email protected]>
- Loading branch information
1 parent
fe53733
commit b43c3c8
Showing
15 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -33,6 +33,7 @@ mod null; | |
mod pairwise; | ||
mod repr; | ||
mod search_sorted; | ||
mod sign; | ||
mod sort; | ||
mod struct_; | ||
mod sum; | ||
|
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,30 @@ | ||
use crate::{array::DataArray, datatypes::DaftNumericType}; | ||
use num_traits::Signed; | ||
use num_traits::Unsigned; | ||
use num_traits::{One, Zero}; | ||
|
||
use common_error::DaftResult; | ||
|
||
impl<T: DaftNumericType> DataArray<T> | ||
where | ||
T::Native: Signed, | ||
{ | ||
pub fn sign(&self) -> DaftResult<Self> { | ||
self.apply(|v| v.signum()) | ||
} | ||
} | ||
|
||
impl<T: DaftNumericType> DataArray<T> | ||
where | ||
T::Native: Unsigned, | ||
{ | ||
pub fn sign_unsigned(&self) -> DaftResult<Self> { | ||
self.apply(|v| { | ||
if v.is_zero() { | ||
T::Native::zero() | ||
} else { | ||
T::Native::one() | ||
} | ||
}) | ||
} | ||
} |
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,27 @@ | ||
use crate::datatypes::DataType; | ||
use crate::series::Series; | ||
use common_error::DaftError; | ||
use common_error::DaftResult; | ||
|
||
impl Series { | ||
pub fn sign(&self) -> DaftResult<Series> { | ||
use crate::series::array_impl::IntoSeries; | ||
use DataType::*; | ||
match self.data_type() { | ||
UInt8 => Ok(self.u8().unwrap().sign_unsigned()?.into_series()), | ||
UInt16 => Ok(self.u16().unwrap().sign_unsigned()?.into_series()), | ||
UInt32 => Ok(self.u32().unwrap().sign_unsigned()?.into_series()), | ||
UInt64 => Ok(self.u64().unwrap().sign_unsigned()?.into_series()), | ||
Int8 => Ok(self.i8().unwrap().sign()?.into_series()), | ||
Int16 => Ok(self.i16().unwrap().sign()?.into_series()), | ||
Int32 => Ok(self.i32().unwrap().sign()?.into_series()), | ||
Int64 => Ok(self.i64().unwrap().sign()?.into_series()), | ||
Float32 => Ok(self.f32().unwrap().sign()?.into_series()), | ||
Float64 => Ok(self.f64().unwrap().sign()?.into_series()), | ||
dt => Err(DaftError::TypeError(format!( | ||
"sign not implemented for {}", | ||
dt | ||
))), | ||
} | ||
} | ||
} |
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,40 @@ | ||
use common_error::{DaftError, DaftResult}; | ||
use daft_core::{datatypes::Field, schema::Schema, series::Series}; | ||
|
||
use super::super::FunctionEvaluator; | ||
use crate::Expr; | ||
|
||
pub(super) struct SignEvaluator {} | ||
|
||
impl FunctionEvaluator for SignEvaluator { | ||
fn fn_name(&self) -> &'static str { | ||
"sign" | ||
} | ||
|
||
fn to_field(&self, inputs: &[Expr], schema: &Schema, _: &Expr) -> DaftResult<Field> { | ||
if inputs.len() != 1 { | ||
return Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))); | ||
} | ||
let field = inputs.first().unwrap().to_field(schema)?; | ||
if !field.dtype.is_numeric() { | ||
return Err(DaftError::TypeError(format!( | ||
"Expected input to sign to be numeric, got {}", | ||
field.dtype | ||
))); | ||
} | ||
Ok(field) | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series], _: &Expr) -> DaftResult<Series> { | ||
if inputs.len() != 1 { | ||
return Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))); | ||
} | ||
inputs.first().unwrap().sign() | ||
} | ||
} |
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