-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] List slice expression (#2479)
Adds an expression that returns a specified subset of elements from a list. The API to use this expression is `daft.Expression.list.slice(start: int | Expression, end: int | Expression)`. - `start` is the 0-indexed position of the list to start retrieving elements. If this value is negative, then it's the position from the _end_ of the array. i.e. `-1` points to the last element of the array - `end` is the 0-indexed position of the list to stop retrieving elements. If this value is negative, then it's the position from the _end_ of the array. i.e. `-1` points to the last element of the array --------- Co-authored-by: Desmond Cheong <[email protected]>
- Loading branch information
1 parent
6655d30
commit 7b81de6
Showing
9 changed files
with
427 additions
and
24 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
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,53 @@ | ||
use crate::ExprRef; | ||
use daft_core::{datatypes::Field, schema::Schema, series::Series}; | ||
|
||
use super::super::FunctionEvaluator; | ||
use crate::functions::FunctionExpr; | ||
use common_error::{DaftError, DaftResult}; | ||
|
||
pub(super) struct SliceEvaluator {} | ||
|
||
impl FunctionEvaluator for SliceEvaluator { | ||
fn fn_name(&self) -> &'static str { | ||
"slice" | ||
} | ||
|
||
fn to_field(&self, inputs: &[ExprRef], schema: &Schema, _: &FunctionExpr) -> DaftResult<Field> { | ||
match inputs { | ||
[input, start, end] => { | ||
let input_field = input.to_field(schema)?; | ||
let start_field = start.to_field(schema)?; | ||
let end_field = end.to_field(schema)?; | ||
|
||
if !start_field.dtype.is_integer() { | ||
return Err(DaftError::TypeError(format!( | ||
"Expected start index to be integer, received: {}", | ||
start_field.dtype | ||
))); | ||
} | ||
|
||
if !end_field.dtype.is_integer() { | ||
return Err(DaftError::TypeError(format!( | ||
"Expected end index to be integer, received: {}", | ||
end_field.dtype | ||
))); | ||
} | ||
Ok(input_field.to_exploded_field()?.to_list_field()?) | ||
} | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 3 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series], _: &FunctionExpr) -> DaftResult<Series> { | ||
match inputs { | ||
[input, start, end] => input.list_slice(start, end), | ||
_ => Err(DaftError::ValueError(format!( | ||
"Expected 3 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
} |
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.