Skip to content

Apache DataFusion SQL Query Engine

ARROW-11138: [Rust] [DataFusion] Add ltrim, rtrim to built-in functions

Dmitry Patsura4415929d26d4
diff --git a/rust/datafusion/src/logical_plan/expr.rs b/rust/datafusion/src/logical_plan/expr.rs
index 4aee03c4c1..66658e1bab 100644
--- a/rust/datafusion/src/logical_plan/expr.rs
+++ b/rust/datafusion/src/logical_plan/expr.rs
@@ -658,6 +658,8 @@ unary_scalar_expr!(Log2, log2);
unary_scalar_expr!(Log10, log10);
unary_scalar_expr!(Lower, lower);
unary_scalar_expr!(Trim, trim);
+unary_scalar_expr!(Ltrim, ltrim);
+unary_scalar_expr!(Rtrim, rtrim);
unary_scalar_expr!(Upper, upper);
/// returns the length of a string in bytes
diff --git a/rust/datafusion/src/logical_plan/mod.rs b/rust/datafusion/src/logical_plan/mod.rs
index 0d37da6f6a..9be3b9449e 100644
--- a/rust/datafusion/src/logical_plan/mod.rs
+++ b/rust/datafusion/src/logical_plan/mod.rs
@@ -36,8 +36,8 @@ pub use display::display_schema;
pub use expr::{
abs, acos, and, array, asin, atan, avg, binary_expr, case, ceil, col, concat, cos,
count, count_distinct, create_udaf, create_udf, exp, exprlist_to_fields, floor,
- length, lit, ln, log10, log2, lower, max, min, or, round, signum, sin, sqrt, sum,
- tan, trim, trunc, upper, when, Expr, Literal,
+ length, lit, ln, log10, log2, lower, ltrim, max, min, or, round, rtrim, signum, sin,
+ sqrt, sum, tan, trim, trunc, upper, when, Expr, Literal,
};
pub use extension::UserDefinedLogicalNode;
pub use operators::Operator;
diff --git a/rust/datafusion/src/physical_plan/functions.rs b/rust/datafusion/src/physical_plan/functions.rs
index 316586d44b..ff88ec7173 100644
--- a/rust/datafusion/src/physical_plan/functions.rs
+++ b/rust/datafusion/src/physical_plan/functions.rs
@@ -124,6 +124,10 @@ pub enum BuiltinScalarFunction {
Upper,
/// trim
Trim,
+ /// trim left
+ Ltrim,
+ /// trim right
+ Rtrim,
/// to_timestamp
ToTimestamp,
/// construct an array from columns
@@ -168,6 +172,8 @@ impl FromStr for BuiltinScalarFunction {
"concat" => BuiltinScalarFunction::Concat,
"lower" => BuiltinScalarFunction::Lower,
"trim" => BuiltinScalarFunction::Trim,
+ "ltrim" => BuiltinScalarFunction::Ltrim,
+ "rtrim" => BuiltinScalarFunction::Rtrim,
"upper" => BuiltinScalarFunction::Upper,
"to_timestamp" => BuiltinScalarFunction::ToTimestamp,
"date_trunc" => BuiltinScalarFunction::DateTrunc,
@@ -227,6 +233,26 @@ pub fn return_type(
));
}
}),
+ BuiltinScalarFunction::Ltrim => Ok(match arg_types[0] {
+ DataType::LargeUtf8 => DataType::LargeUtf8,
+ DataType::Utf8 => DataType::Utf8,
+ _ => {
+ // this error is internal as `data_types` should have captured this.
+ return Err(DataFusionError::Internal(
+ "The ltrim function can only accept strings.".to_string(),
+ ));
+ }
+ }),
+ BuiltinScalarFunction::Rtrim => Ok(match arg_types[0] {
+ DataType::LargeUtf8 => DataType::LargeUtf8,
+ DataType::Utf8 => DataType::Utf8,
+ _ => {
+ // this error is internal as `data_types` should have captured this.
+ return Err(DataFusionError::Internal(
+ "The rtrim function can only accept strings.".to_string(),
+ ));
+ }
+ }),
BuiltinScalarFunction::Trim => Ok(match arg_types[0] {
DataType::LargeUtf8 => DataType::LargeUtf8,
DataType::Utf8 => DataType::Utf8,
@@ -312,6 +338,22 @@ pub fn create_physical_expr(
other,
))),
},
+ BuiltinScalarFunction::Ltrim => |args| match args[0].data_type() {
+ DataType::Utf8 => Ok(Arc::new(string_expressions::ltrim::<i32>(args)?)),
+ DataType::LargeUtf8 => Ok(Arc::new(string_expressions::ltrim::<i64>(args)?)),
+ other => Err(DataFusionError::Internal(format!(
+ "Unsupported data type {:?} for function ltrim",
+ other,
+ ))),
+ },
+ BuiltinScalarFunction::Rtrim => |args| match args[0].data_type() {
+ DataType::Utf8 => Ok(Arc::new(string_expressions::rtrim::<i32>(args)?)),
+ DataType::LargeUtf8 => Ok(Arc::new(string_expressions::rtrim::<i64>(args)?)),
+ other => Err(DataFusionError::Internal(format!(
+ "Unsupported data type {:?} for function rtrim",
+ other,
+ ))),
+ },
BuiltinScalarFunction::Upper => |args| match args[0].data_type() {
DataType::Utf8 => Ok(Arc::new(string_expressions::upper::<i32>(args)?)),
DataType::LargeUtf8 => Ok(Arc::new(string_expressions::upper::<i64>(args)?)),
@@ -363,6 +405,12 @@ fn signature(fun: &BuiltinScalarFunction) -> Signature {
BuiltinScalarFunction::Trim => {
Signature::Uniform(1, vec![DataType::Utf8, DataType::LargeUtf8])
}
+ BuiltinScalarFunction::Ltrim => {
+ Signature::Uniform(1, vec![DataType::Utf8, DataType::LargeUtf8])
+ }
+ BuiltinScalarFunction::Rtrim => {
+ Signature::Uniform(1, vec![DataType::Utf8, DataType::LargeUtf8])
+ }
BuiltinScalarFunction::ToTimestamp => Signature::Uniform(1, vec![DataType::Utf8]),
BuiltinScalarFunction::DateTrunc => Signature::Exact(vec![
DataType::Utf8,
diff --git a/rust/datafusion/src/physical_plan/string_expressions.rs b/rust/datafusion/src/physical_plan/string_expressions.rs
index 328e24b8b9..c633aa874f 100644
--- a/rust/datafusion/src/physical_plan/string_expressions.rs
+++ b/rust/datafusion/src/physical_plan/string_expressions.rs
@@ -89,3 +89,5 @@ macro_rules! string_unary_function {
string_unary_function!(lower, to_ascii_lowercase);
string_unary_function!(upper, to_ascii_uppercase);
string_unary_function!(trim, trim);
+string_unary_function!(ltrim, trim_start);
+string_unary_function!(rtrim, trim_end);
diff --git a/rust/datafusion/src/prelude.rs b/rust/datafusion/src/prelude.rs
index c8a4804ae6..b607713c77 100644
--- a/rust/datafusion/src/prelude.rs
+++ b/rust/datafusion/src/prelude.rs
@@ -28,7 +28,7 @@
pub use crate::dataframe::DataFrame;
pub use crate::execution::context::{ExecutionConfig, ExecutionContext};
pub use crate::logical_plan::{
- array, avg, col, concat, count, create_udf, length, lit, lower, max, min, sum, trim,
- upper, JoinType, Partitioning,
+ array, avg, col, concat, count, create_udf, length, lit, lower, ltrim, max, min,
+ rtrim, sum, trim, upper, JoinType, Partitioning,
};
pub use crate::physical_plan::csv::CsvReadOptions;
diff --git a/rust/datafusion/tests/sql.rs b/rust/datafusion/tests/sql.rs
index 87b155a902..b810c428d5 100644
--- a/rust/datafusion/tests/sql.rs
+++ b/rust/datafusion/tests/sql.rs
@@ -1841,11 +1841,14 @@ async fn string_expressions() -> Result<()> {
,upper(NULL) AS upper_null
,trim(' tom ') AS trim
,trim(NULL) AS trim_null
+ ,ltrim(' tom ') AS trim_left
+ ,rtrim(' tom ') AS trim_right
";
let actual = execute(&mut ctx, sql).await;
let expected = vec![vec![
- "3", "NULL", "3", "NULL", "tom", "NULL", "TOM", "NULL", "tom", "NULL",
+ "3", "NULL", "3", "NULL", "tom", "NULL", "TOM", "NULL", "tom", "NULL", "tom ",
+ " tom",
]];
assert_eq!(expected, actual);
Ok(())