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(frontend): Bind concat_ws expression #2589

Merged
merged 13 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
30 changes: 30 additions & 0 deletions e2e_test/v2/batch/func.slt
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,33 @@ drop table t;

statement ok
drop table b;

query T
select concat_ws(',', 'a', 'b');
----
a,b

query T
select concat_ws(',', NULL, 'b');
----
b

query T
select concat_ws(',', 1, 1.01, 'A', true, NULL);
----
1,1.01,A,true

statement ok
create table t (v1 varchar, v2 smallint, v3 int, v4 decimal, v5 real, v6 double, v7 bool, v8 varchar);

statement ok
insert into t values (',', 1, 2, 3.01, 4, 5.01, true, NULL);

query T
select concat_ws(v1, v2, v3, v4, v5, v6, v7, v8) from t;
----
1,2,3.01,4,5.01,true


statement ok
drop table t;
1 change: 1 addition & 0 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl Binder {
inputs = Self::rewrite_nullif_to_case_when(inputs)?;
ExprType::Case
}
"concat_ws" => ExprType::ConcatWs,
"coalesce" => ExprType::Coalesce,
"round" => {
inputs = Self::rewrite_round_args(inputs);
Expand Down
24 changes: 24 additions & 0 deletions src/frontend/src/expr/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ impl FunctionCall {
}
align_types(inputs.iter_mut())
}
ExprType::ConcatWs => {
if inputs.len() < 2 {
return Err(ErrorCode::BindError(
"ConcatWs function must contain at least 2 arguments".into(),
)
.into());
}

if inputs[0].return_type() != DataType::Varchar {
return Err(ErrorCode::BindError(
"ConcatWs function must have text as first argument".into(),
)
.into());
}

// subsequent inputs can be any type, they are cast into varchars with
// explicit_cast.
inputs = inputs
.into_iter()
.map(|input| input.cast_explicit(DataType::Varchar))
.collect::<Result<Vec<_>>>()?;
Copy link
Contributor

@xiangjinwu xiangjinwu May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                inputs = inputs
                    .into_iter()
                    .enumerate()
                    .map(|(i, input)| match i {
                        // 0-th arg must be string
                        0 => input.cast_implicit(DataType::Varchar),
                        // subsequent can be any type
                        _ => input.cast_explicit(DataType::Varchar),
                    })
                    .collect::<Result<Vec<_>>>()?;

This would also allow the e2e test cases that use literal null as separator.

(Basically, when pg doc says it accepts type "T", it always means accepting any types implicitly castable to "T".)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I was wondering how to patch that.


Ok(DataType::Varchar)
}
_ => infer_type(
func_type,
inputs.iter().map(|expr| expr.return_type()).collect(),
Expand Down
26 changes: 26 additions & 0 deletions src/frontend/test_runner/tests/testdata/expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,29 @@
create table t (v1 int);
select coalesce(1,'a') from t;
binder_error: 'Bind error: types Int32 and Varchar cannot be matched'
- sql: |
create table t (v1 varchar);
select concat_ws(v1, 1) as expr from t;
batch_plan: |
BatchExchange { order: [], dist: Single }
BatchProject { exprs: [ConcatWs($0, 1:Int32::Varchar)] }
BatchScan { table: t, columns: [v1] }
stream_plan: |
StreamMaterialize { columns: [expr, _row_id#0(hidden)], pk_columns: [_row_id#0] }
StreamProject { exprs: [ConcatWs($0, 1:Int32::Varchar), $1] }
StreamTableScan { table: t, columns: [v1, _row_id#0], pk_indices: [1] }
- sql: |
create table t (v1 varchar);
select concat_ws(v1, 1.2) from t;
batch_plan: |
BatchExchange { order: [], dist: Single }
BatchProject { exprs: [ConcatWs($0, 1.2:Decimal::Varchar)] }
BatchScan { table: t, columns: [v1] }
- sql: |
create table t (v1 int);
select concat_ws(v1, 1.2) from t;
binder_error: 'Bind error: ConcatWs function must have text as first argument'
- sql: |
create table t (v1 int);
select concat_ws() from t;
binder_error: 'Bind error: ConcatWs function must contain at least 2 arguments'