Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expr): implementation for char_length operation #2991

Merged
merged 4 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions e2e_test/batch/func.slt
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ select split_part('abc,def,ghi,jkl', ',', -2);
----
ghi


query I
select char_length('hello world');
----
11

query I
select char_length('abcdefghijklmnopqrstuvwxyz');
----
26
2 changes: 1 addition & 1 deletion proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ message ExprNode {
CEIL = 221;
FLOOR = 222;
TO_CHAR = 223;

MD5 = 224;
CHAR_LENGTH = 225;

// Boolean comparison
IS_TRUE = 301;
Expand Down
7 changes: 6 additions & 1 deletion src/expr/src/expr/expr_unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ pub fn new_unary_expr(
return_type,
ascii,
)),
(ProstType::CharLength, _, _) => Box::new(UnaryExpression::<Utf8Array, I32Array, _>::new(
child_expr,
return_type,
length_default,
)),
(ProstType::Neg, _, _) => {
gen_unary_atm_expr! { "Neg", child_expr, return_type, general_neg,
{
Expand Down Expand Up @@ -334,7 +339,7 @@ pub fn new_unary_expr(
}

pub fn new_length_default(expr_ia1: BoxedExpression, return_type: DataType) -> BoxedExpression {
Box::new(UnaryExpression::<Utf8Array, I64Array, _>::new(
Box::new(UnaryExpression::<Utf8Array, I32Array, _>::new(
expr_ia1,
return_type,
length_default,
Expand Down
2 changes: 1 addition & 1 deletion src/expr/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn build_from_prost(prost: &ExprNode) -> Result<BoxedExpression> {

match prost.get_expr_type()? {
Cast | Upper | Lower | Md5 | Not | IsTrue | IsNotTrue | IsFalse | IsNotFalse | IsNull
| IsNotNull | Neg | Ascii | Abs | Ceil | Floor | Round | BitwiseNot => {
| IsNotNull | Neg | Ascii | Abs | Ceil | Floor | Round | BitwiseNot | CharLength => {
build_unary_expr_prost(prost)
}
Equal | NotEqual | LessThan | LessThanOrEqual | GreaterThan | GreaterThanOrEqual | Add
Expand Down
4 changes: 2 additions & 2 deletions src/expr/src/vector_op/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use risingwave_common::error::Result;

#[inline(always)]
pub fn length_default(s: &str) -> Result<i64> {
Ok(s.chars().count() as i64)
pub fn length_default(s: &str) -> Result<i32> {
Ok(s.chars().count() as i32)
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ impl Binder {
inputs = Self::rewrite_two_bool_inputs(inputs)?;
ExprType::NotEqual
}
"char_length" => ExprType::CharLength,
"character_length" => ExprType::CharLength,
_ => {
return Err(ErrorCode::NotImplemented(
format!("unsupported function: {:?}", function_name),
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/expr/type_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ fn build_type_derive_map() -> HashMap<FuncSign, DataTypeName> {
T::Varchar,
);
}
for e in [E::Length, E::Ascii] {
for e in [E::Length, E::Ascii, E::CharLength] {
map.insert(FuncSign::new(e, vec![T::Varchar]), T::Int32);
}
map.insert(
Expand Down