-
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] Add str.lstrip() and str.rstrip() functions (#1944)
* Added the `lstrip` function to match https://ibis-project.org/reference/expression-strings#ibis.expr.types.strings.StringValue.lstrip * Added the `rstrip` function to match https://ibis-project.org/reference/expression-strings#ibis.expr.types.strings.StringValue.rstrip * Added tests showing example usage Closes #1922 and #1923
- Loading branch information
Showing
15 changed files
with
286 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
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,46 @@ | ||
use daft_core::{ | ||
datatypes::{DataType, Field}, | ||
schema::Schema, | ||
series::Series, | ||
}; | ||
|
||
use crate::Expr; | ||
use common_error::{DaftError, DaftResult}; | ||
|
||
use super::super::FunctionEvaluator; | ||
|
||
pub(super) struct LstripEvaluator {} | ||
|
||
impl FunctionEvaluator for LstripEvaluator { | ||
fn fn_name(&self) -> &'static str { | ||
"lstrip" | ||
} | ||
|
||
fn to_field(&self, inputs: &[Expr], schema: &Schema, _: &Expr) -> DaftResult<Field> { | ||
match inputs { | ||
[data] => match data.to_field(schema) { | ||
Ok(data_field) => match &data_field.dtype { | ||
DataType::Utf8 => Ok(Field::new(data_field.name, DataType::Utf8)), | ||
_ => Err(DaftError::TypeError(format!( | ||
"Expects input to lstrip to be utf8, but received {data_field}", | ||
))), | ||
}, | ||
Err(e) => Err(e), | ||
}, | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series], _: &Expr) -> DaftResult<Series> { | ||
match inputs { | ||
[data] => data.utf8_lstrip(), | ||
_ => Err(DaftError::ValueError(format!( | ||
"Expected 1 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
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,46 @@ | ||
use daft_core::{ | ||
datatypes::{DataType, Field}, | ||
schema::Schema, | ||
series::Series, | ||
}; | ||
|
||
use crate::Expr; | ||
use common_error::{DaftError, DaftResult}; | ||
|
||
use super::super::FunctionEvaluator; | ||
|
||
pub(super) struct RstripEvaluator {} | ||
|
||
impl FunctionEvaluator for RstripEvaluator { | ||
fn fn_name(&self) -> &'static str { | ||
"rstrip" | ||
} | ||
|
||
fn to_field(&self, inputs: &[Expr], schema: &Schema, _: &Expr) -> DaftResult<Field> { | ||
match inputs { | ||
[data] => match data.to_field(schema) { | ||
Ok(data_field) => match &data_field.dtype { | ||
DataType::Utf8 => Ok(Field::new(data_field.name, DataType::Utf8)), | ||
_ => Err(DaftError::TypeError(format!( | ||
"Expects input to rstrip to be utf8, but received {data_field}", | ||
))), | ||
}, | ||
Err(e) => Err(e), | ||
}, | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input args, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series], _: &Expr) -> DaftResult<Series> { | ||
match inputs { | ||
[data] => data.utf8_rstrip(), | ||
_ => Err(DaftError::ValueError(format!( | ||
"Expected 1 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
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,10 @@ | ||
from __future__ import annotations | ||
|
||
from daft.expressions import col | ||
from daft.table import MicroPartition | ||
|
||
|
||
def test_utf8_lstrip(): | ||
table = MicroPartition.from_pydict({"col": ["\ta\t", None, "\nb\n", "\vc\t", "\td ", "e"]}) | ||
result = table.eval_expression_list([col("col").str.lstrip()]) | ||
assert result.to_pydict() == {"col": ["a\t", None, "b\n", "c\t", "d ", "e"]} |
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,10 @@ | ||
from __future__ import annotations | ||
|
||
from daft.expressions import col | ||
from daft.table import MicroPartition | ||
|
||
|
||
def test_utf8_rstrip(): | ||
table = MicroPartition.from_pydict({"col": ["\ta\t", None, "\nb\n", "\vc\t", "\td ", "e"]}) | ||
result = table.eval_expression_list([col("col").str.rstrip()]) | ||
assert result.to_pydict() == {"col": ["\ta", None, "\nb", "\vc", "\td", "e"]} |