Skip to content

Commit

Permalink
feat(query): expand trim/ltrim/rtrim
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Nov 11, 2024
1 parent f4e599f commit cf7752c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 102 deletions.
18 changes: 14 additions & 4 deletions src/query/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,12 +961,22 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
rule! {
TRIM
~ "("
~ #subexpr(0)
~ #subexpr(0) ~ ("," ~ #subexpr(0))?
~ ^")"
},
|(_, _, expr, _)| ExprElement::Trim {
expr: Box::new(expr),
trim_where: None,
|(_, _, expr, trim_str, _)| {
if let Some(trim_str) = trim_str {
let trim_str = trim_str.1;
ExprElement::Trim {
expr: Box::new(expr),
trim_where: Some((TrimWhere::Both, Box::new(trim_str))),
}
} else {
ExprElement::Trim {
expr: Box::new(expr),
trim_where: None,
}
}
},
);
let trim_from = map(
Expand Down
31 changes: 31 additions & 0 deletions src/query/functions/src/scalars/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,22 @@ pub fn register(registry: &mut FunctionRegistry) {
}),
);

registry.register_passthrough_nullable_2_arg::<StringType, StringType, StringType, _, _>(
"ltrim",
|_, _, _| FunctionDomain::Full,
vectorize_with_builder_2_arg::<StringType, StringType, StringType>(
|val, trim_str, output, _| {
if trim_str.is_empty() {
output.put_and_commit(val);
return;
}

output.put_and_commit(val.trim_start_matches(trim_str));
},
),
);


registry.register_passthrough_nullable_2_arg::<StringType, StringType, StringType, _, _>(
"trim_leading",
|_, _, _| FunctionDomain::Full,
Expand All @@ -445,6 +461,21 @@ pub fn register(registry: &mut FunctionRegistry) {
),
);

registry.register_passthrough_nullable_2_arg::<StringType, StringType, StringType, _, _>(
"rtrim",
|_, _, _| FunctionDomain::Full,
vectorize_with_builder_2_arg::<StringType, StringType, StringType>(
|val, trim_str, output, _| {
if trim_str.is_empty() {
output.put_and_commit(val);
return;
}

output.put_and_commit(val.trim_end_matches(trim_str));
},
),
);

registry.register_passthrough_nullable_2_arg::<StringType, StringType, StringType, _, _>(
"trim_trailing",
|_, _, _| FunctionDomain::Full,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,8 @@ Functions overloads:
35 lte FACTORY
0 ltrim(String) :: String
1 ltrim(String NULL) :: String NULL
2 ltrim(String, String) :: String
3 ltrim(String NULL, String NULL) :: String NULL
0 map(Array(Nothing), Array(Nothing)) :: Map(Nothing)
1 map(Array(Nothing) NULL, Array(Nothing) NULL) :: Map(Nothing) NULL
2 map(Array(T0), Array(T1)) :: Map(T0, T1)
Expand Down Expand Up @@ -3532,6 +3534,8 @@ Functions overloads:
1 rpad(String NULL, UInt64 NULL, String NULL) :: String NULL
0 rtrim(String) :: String
1 rtrim(String NULL) :: String NULL
2 rtrim(String, String) :: String
3 rtrim(String NULL, String NULL) :: String NULL
0 running_difference(Int64) :: Int64
1 running_difference(Int64 NULL) :: Int64 NULL
2 running_difference(Date) :: Int32
Expand Down
98 changes: 0 additions & 98 deletions tests/sqllogictests/suites/base/20+_others/20_0001_planner.test
Original file line number Diff line number Diff line change
Expand Up @@ -779,104 +779,6 @@ SELECT DISTINCT count(number %3) as c FROM numbers(10) group by number % 3 ORDE
3
4

query T
select trim(leading ' ' from ' abc')
----
abc


statement ok
select trim(leading ' ' from '')


statement ok
select trim(leading 'ab' from 'abab')


query T
select trim(leading 'ab' from 'abc')
----
c


query T
select trim(leading ' ' from NULL)
----
NULL


query T
select trim(leading NULL from 'aaa')
----
NULL


query T
select trim(trailing ' ' from 'abc ')
----
abc


statement ok
select trim(trailing ' ' from '')


statement ok
select trim(trailing 'ab' from 'abab')


query T
select trim(trailing 'ab' from 'cab')
----
c


query T
select trim(trailing ' ' from NULL)
----
NULL


query T
select trim(trailing NULL from 'aaa')
----
NULL


statement ok
select trim(both 'ab' from 'abab')


query T
select trim(both 'ab' from 'abcab')
----
c


query T
select trim(both ' ' from NULL)
----
NULL


query T
select trim(both NULL from 'aaa')
----
NULL


query T
select trim(' abc ')
----
abc


query T
select trim(NULL)
----
NULL


query I
select [1, 2, 3]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,120 @@ select trim_leading('aaabbaaa', '')
----
aaabbaaa

query T
select ltrim('aaabbaaa', 'aa')
----
abbaaa

query T
select trim_trailing('aaabbaaa', 'aa')
----
aaabba

query T
select rtrim('aaabbaaa', 'aa')
----
aaabba

query T
select trim_trailing('aaabbaaa', '')
----
aaabbaaa

query T
select trim(leading ' ' from ' abc')
----
abc


query T
select trim(leading ' ' from '')
----
(empty)

statement ok
select trim(leading 'ab' from 'abab')


query T
select trim(leading 'ab' from 'abc')
----
c


query T
select trim(leading ' ' from NULL)
----
NULL


query T
select trim(leading NULL from 'aaa')
----
NULL


query T
select trim(trailing ' ' from 'abc ')
----
abc


query T
select trim(trailing ' ' from '')
----
(empty)

query T
select trim(trailing 'ab' from 'abab')
----
(empty)

query T
select trim(trailing 'ab' from 'cab')
----
c


query T
select trim(trailing ' ' from NULL)
----
NULL


query T
select trim(trailing NULL from 'aaa')
----
NULL


query T
select trim(both 'ab' from 'abab')
----
(empty)

query T
select trim(both 'ab' from 'abcab')
----
c

query T
select trim('abcab', 'ab')
----
c

query T
select trim(both ' ' from NULL)
----
NULL


query T
select trim(both NULL from 'aaa')
----
NULL

query T
select trim(NULL)
----
NULL

0 comments on commit cf7752c

Please sign in to comment.