This is an automated email from the ASF dual-hosted git repository.
mrhhsg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 7f41367e2f0 [opt](function) Make coalesce support more types (#56430)
7f41367e2f0 is described below
commit 7f41367e2f03f5c863d5faf9f23a32bd6fa31cb0
Author: Jerry Hu <[email protected]>
AuthorDate: Tue Oct 14 14:12:58 2025 +0800
[opt](function) Make coalesce support more types (#56430)
### What problem does this PR solve?
Add support for types:
1. JSON
2. Array
3. MAP
4. Struct
Doc: https://github.com/apache/doris-website/pull/2793
Related PR: #xxx
Problem Summary:
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR
should merge into -->
---
be/src/vec/functions/function_coalesce.cpp | 17 +++--
.../conditional_functions/test_coalesce.out | 28 +++++++
.../conditional_functions/test_coalesce.groovy | 88 ++++++++++++++++++++++
3 files changed, 127 insertions(+), 6 deletions(-)
diff --git a/be/src/vec/functions/function_coalesce.cpp
b/be/src/vec/functions/function_coalesce.cpp
index 6e247367313..7636c480103 100644
--- a/be/src/vec/functions/function_coalesce.cpp
+++ b/be/src/vec/functions/function_coalesce.cpp
@@ -123,10 +123,15 @@ public:
result_column = remove_nullable(result_type)->create_column();
}
- // because now the string types does not support random position
writing,
- // so insert into result data have two methods, one is for string
types, one is for others type remaining
- bool is_string_result = result_column->is_column_string();
- if (is_string_result) {
+ // because now follow below types does not support random position
writing,
+ // so insert into result data have two methods, one is for these
types, one is for others type remaining
+ bool cannot_random_write =
+ result_column->is_column_string() ||
+ result_type->get_primitive_type() == PrimitiveType::TYPE_MAP ||
+ result_type->get_primitive_type() ==
PrimitiveType::TYPE_STRUCT ||
+ result_type->get_primitive_type() == PrimitiveType::TYPE_ARRAY
||
+ result_type->get_primitive_type() == PrimitiveType::TYPE_JSONB;
+ if (cannot_random_write) {
result_column->reserve(input_rows_count);
}
@@ -193,7 +198,7 @@ public:
}
}
- if (!is_string_result) {
+ if (!cannot_random_write) {
//if not string type, could check one column firstly,
//and then fill the not null value in result column,
//this method may result in higher CPU cache
@@ -203,7 +208,7 @@ public:
}
}
- if (is_string_result) {
+ if (cannot_random_write) {
//if string type, should according to the record results, fill in
result one by one,
for (size_t row = 0; row < input_rows_count; ++row) {
if (null_map_data[row]) { //should be null
diff --git
a/regression-test/data/query_p0/sql_functions/conditional_functions/test_coalesce.out
b/regression-test/data/query_p0/sql_functions/conditional_functions/test_coalesce.out
index dc4c2d4a5b4..c9c03d536a9 100644
---
a/regression-test/data/query_p0/sql_functions/conditional_functions/test_coalesce.out
+++
b/regression-test/data/query_p0/sql_functions/conditional_functions/test_coalesce.out
@@ -813,3 +813,31 @@ false 1 1989 1001 11011902 123.123
true 1989-03-21 1989-03-21T13:00 wangjuoo4 0.
-- !coalesce10 --
1
+-- !coalesce11 --
+{"k":"v"}
+
+-- !coalesce_json --
+1 {"k":123}
+2 {"k": 0}
+3 {"k":789}
+
+-- !coalesce_map --
+1 {"k":123}
+2 {"k":0}
+3 {"k":789}
+
+-- !coalesce_array --
+1 [123, 456]
+2 [0]
+3 [789]
+
+-- !coalesce_array2 --
+1 [123, 456]
+2 \N
+3 [789]
+
+-- !coalesce_struct --
+1 {"name":"Alice", "age":30}
+2 {"name":"Charlie", "age":18}
+3 {"name":"Bob", "age":25}
+
diff --git
a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_coalesce.groovy
b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_coalesce.groovy
index b1402d8ec9a..e859e1f7823 100644
---
a/regression-test/suites/query_p0/sql_functions/conditional_functions/test_coalesce.groovy
+++
b/regression-test/suites/query_p0/sql_functions/conditional_functions/test_coalesce.groovy
@@ -33,4 +33,92 @@ suite("test_coalesce", "query,p0") {
qt_coalesce8 "select * from ${tableName2} where coalesce(k1, k2) in (1,
null) order by 1, 2, 3, 4"
qt_coalesce9 "select * from ${tableName1} where coalesce(k1, null) in (1,
null) order by 1, 2, 3, 4, 5, 6"
qt_coalesce10 "select coalesce(1, null)"
+ qt_coalesce11 """ select coalesce(null, json_parse('{"k": "v"}')); """
+
+ test {
+ sql """
+ select coalesce(null,to_json('{"k": 123}'), map("a", 123));
+ """
+ exception " Can not find the compatibility function"
+ }
+
+ sql "drop table if exists `json_test`;"
+ sql """
+ create table `json_test` (
+ id int,
+ value json
+ ) properties('replication_num' = '1');
+ """
+
+ sql """
+ insert into `json_test` values
+ (1, '{"k": 123}'),
+ (2, null),
+ (3, '{"k": 789}');
+ """
+
+ qt_coalesce_json """
+ select id, coalesce(value, '{"k": 0}') from `json_test` order by 1;
+ """
+
+ sql "drop table if exists `map_test`;"
+ sql """
+ create table `map_test` (
+ id int,
+ value map<string, int>
+ ) properties('replication_num' = '1');
+ """
+
+ sql """
+ insert into `map_test` values
+ (1, '{"k": 123}'),
+ (2, null),
+ (3, '{"k": 789}');
+ """
+
+ qt_coalesce_map """
+ select id, coalesce(value, '{"k": 0}') from `map_test` order by 1;
+ """
+
+ sql "drop table if exists `array_test`;"
+ sql """
+ create table `array_test` (
+ id int,
+ value array<int>
+ ) properties('replication_num' = '1');
+ """
+
+ sql """
+ insert into `array_test` values
+ (1, '[123, 456]'),
+ (2, null),
+ (3, '[789]');
+ """
+
+ qt_coalesce_array """
+ select id, coalesce(value, '[0]') from `array_test` order by 1;
+ """
+
+ qt_coalesce_array2 """
+ select id, coalesce(value, value) from `array_test` order by 1;
+ """
+
+ sql "drop table if exists `struct_test`;"
+ sql """
+ create table `struct_test` (
+ id int,
+ value STRUCT<name: VARCHAR(10), age: INT>
+ ) properties('replication_num' = '1');
+ """
+
+ sql """
+ insert into `struct_test` values
+ (1, STRUCT("Alice", 30)),
+ (2, null),
+ (3, STRUCT("Bob", 25));
+ """
+
+ qt_coalesce_struct """
+ select id, coalesce(value, STRUCT("Charlie", 18)) from `struct_test`
order by 1;
+ """
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]