Skip to content

Commit 7a194ef

Browse files
authored
branch-3.1: [fix] (function) Support concat_ws multi array parameters apache#53084 apache#53835 (apache#54196)
picked from apache#53084 apache#53835
1 parent 434171d commit 7a194ef

File tree

6 files changed

+164
-5
lines changed

6 files changed

+164
-5
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
package org.apache.doris.nereids.rules.expression;
1919

2020
import org.apache.doris.nereids.rules.expression.check.CheckCast;
21+
import org.apache.doris.nereids.rules.expression.rules.ConcatWsMultiArrayToOne;
2122
import org.apache.doris.nereids.rules.expression.rules.ConvertAggStateCast;
2223
import org.apache.doris.nereids.rules.expression.rules.DigitalMaskingConvert;
2324
import org.apache.doris.nereids.rules.expression.rules.FoldConstantRule;
2425
import org.apache.doris.nereids.rules.expression.rules.InPredicateDedup;
2526
import org.apache.doris.nereids.rules.expression.rules.InPredicateExtractNonConstant;
2627
import org.apache.doris.nereids.rules.expression.rules.InPredicateToEqualToRule;
28+
import org.apache.doris.nereids.rules.expression.rules.LogToLn;
2729
import org.apache.doris.nereids.rules.expression.rules.MergeDateTrunc;
2830
import org.apache.doris.nereids.rules.expression.rules.NormalizeBinaryPredicatesRule;
2931
import org.apache.doris.nereids.rules.expression.rules.SimplifyArithmeticComparisonRule;
@@ -51,6 +53,8 @@ public class ExpressionNormalization extends ExpressionRewrite {
5153
InPredicateToEqualToRule.INSTANCE,
5254
SimplifyNotExprRule.INSTANCE,
5355
SimplifyArithmeticRule.INSTANCE,
56+
LogToLn.INSTANCE,
57+
ConcatWsMultiArrayToOne.INSTANCE,
5458
FoldConstantRule.INSTANCE,
5559
SimplifyCastRule.INSTANCE,
5660
DigitalMaskingConvert.INSTANCE,

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.doris.nereids.rules.expression.rules.DistinctPredicatesRule;
2525
import org.apache.doris.nereids.rules.expression.rules.ExtractCommonFactorRule;
2626
import org.apache.doris.nereids.rules.expression.rules.LikeToEqualRewrite;
27-
import org.apache.doris.nereids.rules.expression.rules.LogToLn;
2827
import org.apache.doris.nereids.rules.expression.rules.NullSafeEqualToEqual;
2928
import org.apache.doris.nereids.rules.expression.rules.SimplifyComparisonPredicate;
3029
import org.apache.doris.nereids.rules.expression.rules.SimplifyConflictCompound;
@@ -62,8 +61,7 @@ public class ExpressionOptimization extends ExpressionRewrite {
6261
TopnToMax.INSTANCE,
6362
NullSafeEqualToEqual.INSTANCE,
6463
LikeToEqualRewrite.INSTANCE,
65-
BetweenToEqual.INSTANCE,
66-
LogToLn.INSTANCE
64+
BetweenToEqual.INSTANCE
6765
)
6866
);
6967
private static final ExpressionRuleExecutor EXECUTOR = new ExpressionRuleExecutor(OPTIMIZE_REWRITE_RULES);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.rules.expression.rules;
19+
20+
import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
21+
import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
22+
import org.apache.doris.nereids.trees.expressions.Expression;
23+
import org.apache.doris.nereids.trees.expressions.functions.scalar.ArrayConcat;
24+
import org.apache.doris.nereids.trees.expressions.functions.scalar.ConcatWs;
25+
import org.apache.doris.nereids.types.ArrayType;
26+
27+
import com.google.common.collect.ImmutableList;
28+
29+
import java.util.List;
30+
31+
/**
32+
* ConcatWsTMultiArrayToOne, convert ConcatWs with multiple array arguments to a single array argument.
33+
* This rule is useful for optimizing queries that use ConcatWs with multiple array arguments,
34+
* allowing them to be processed more efficiently by combining the arrays into a single array argument.
35+
*/
36+
public class ConcatWsMultiArrayToOne implements ExpressionPatternRuleFactory {
37+
38+
public static final ConcatWsMultiArrayToOne INSTANCE = new ConcatWsMultiArrayToOne();
39+
40+
@Override
41+
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
42+
return ImmutableList.of(
43+
matchesType(ConcatWs.class).then(ConcatWsMultiArrayToOne::rewrite)
44+
);
45+
}
46+
47+
/** rewrite */
48+
public static Expression rewrite(ConcatWs cat) {
49+
if (cat.arity() >= 3 && cat.child(2).getDataType() instanceof ArrayType) {
50+
return new ConcatWs(cat.child(0),
51+
new ArrayConcat(cat.child(1), cat.children().subList(2, cat.arity()).toArray(new Expression[0])));
52+
} else {
53+
return cat;
54+
}
55+
}
56+
}

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/ConcatWs.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ public class ConcatWs extends ScalarFunction
4242
.args(VarcharType.SYSTEM_DEFAULT, ArrayType.of(VarcharType.SYSTEM_DEFAULT)),
4343
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT)
4444
.varArgs(VarcharType.SYSTEM_DEFAULT, VarcharType.SYSTEM_DEFAULT),
45-
FunctionSignature.ret(StringType.INSTANCE).varArgs(StringType.INSTANCE, StringType.INSTANCE)
46-
);
45+
FunctionSignature.ret(StringType.INSTANCE).varArgs(StringType.INSTANCE, StringType.INSTANCE),
46+
// This signature is for compatibility with the old implementation
47+
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT)
48+
.varArgs(VarcharType.SYSTEM_DEFAULT, ArrayType.of(VarcharType.SYSTEM_DEFAULT)),
49+
FunctionSignature.ret(StringType.INSTANCE)
50+
.varArgs(StringType.INSTANCE, ArrayType.of(StringType.INSTANCE)));
4751

4852
/**
4953
* constructor with 2 or more arguments.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !concat_ws_ArrayWithNullElement --
3+
a-b-css-d-g-f-s
4+
5+
-- !concat_ws_ArrayWithEmptyString --
6+
a---css-d-
7+
8+
-- !concat_ws_WithEmptyArray --
9+
a-b-css-d
10+
11+
-- !concat_ws_SeparatorSpecial --
12+
x|y|m|n|p
13+
14+
-- !concat_ws_SeparatorEmpty --
15+
abcd
16+
17+
-- !concat_ws_ArrayWithNumber --
18+
1-2-3-4-5-6
19+
20+
-- !concat_ws_WithNullArray --
21+
22+
23+
-- !concat_ws_SingleArray --
24+
x,y,z
25+
26+
-- !concat_ws_ArrayAllNull --
27+
a-b
28+
29+
-- !concat_ws_MixedTypeElement --
30+
a|123|456|b
31+
32+
-- !concat_ws_chinese --
33+
你好,世界,Doris,Nereids,测试
34+
35+
-- !concat_ws_insert_1 --
36+
a-b-css-d
37+
x-y-z
38+
你好-世界-Doris-Nereids
39+
40+
-- !log_function --
41+
2.0 3.0 3.0 3.0000000000000004 2.9999999999999996
42+
43+
-- !log_function_with_null --
44+
\N \N \N \N \N
45+
46+
-- !log_function_wiht_one_argument --
47+
4.605170185988092 2.0794415416798357 3.295836866004329 4.8283137373023015 6.907755278982137
48+
49+
-- !log_function_with_null_and_one_argument --
50+
\N \N 0.0 2.302585092994046
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("nereids_scalar_fn_concat_ws_and_log") {
19+
20+
sql 'set enable_fold_constant_by_be=true'
21+
qt_concat_ws_ArrayWithNullElement "select concat_ws('-',['a','b'],['css',null,'d'],['g','f'],['s'])"
22+
23+
24+
qt_concat_ws_ArrayWithEmptyString "select concat_ws('-',['a',''],['','css'],['d',''])"
25+
qt_concat_ws_WithEmptyArray "select concat_ws('-',['a','b'],[],['css','d'],[])"
26+
qt_concat_ws_SeparatorSpecial "select concat_ws('|',['x','y'],['m',null,'n'],['p'])"
27+
28+
qt_concat_ws_SeparatorEmpty "select concat_ws('',['a','b'],['c',null],['d'])"
29+
30+
qt_concat_ws_ArrayWithNumber "select concat_ws('-',['1','2'],['3',null,'4'],['5','6'])"
31+
qt_concat_ws_WithNullArray "select concat_ws('-',['a'],null,['b','c'])"
32+
qt_concat_ws_SingleArray "select concat_ws(',',['x','y','z'])"
33+
qt_concat_ws_ArrayAllNull "select concat_ws('-',['a'],[null,null],['b'])"
34+
qt_concat_ws_MixedTypeElement "select concat_ws('|',['a','123'],['456',null,'b'])"
35+
qt_concat_ws_chinese "select concat_ws(',',['你好','世界'],['Doris',null,'Nereids'],['测试'])"
36+
37+
sql "DROP TABLE IF EXISTS test_concat_ws_1"
38+
sql "CREATE TABLE test_concat_ws_1 (id INT, a ARRAY<VARCHAR>, b ARRAY<VARCHAR>) ENGINE=OLAP DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1')"
39+
sql "INSERT INTO test_concat_ws_1 VALUES (1, ['a','b'], ['css',null,'d']), (2, ['x',null], ['y','z']),(3,['你好','世界'],['Doris',null,'Nereids'])"
40+
qt_concat_ws_insert_1 "SELECT concat_ws('-', a, b) FROM test_concat_ws_1 ORDER BY id"
41+
42+
43+
qt_log_function "SELECT log(10, 100), log(2, 8), log(3, 27), log(5, 125), log(10, 1000)"
44+
qt_log_function_with_null "SELECT log(10, NULL), log(NULL, 100), log(NULL, NULL), log(10, 0), log(0, 10)"
45+
qt_log_function_wiht_one_argument "SELECT log(100), log(8), log(27), log(125), log(1000)"
46+
qt_log_function_with_null_and_one_argument "SELECT log(NULL), log(0), log(1), log(10)"
47+
}

0 commit comments

Comments
 (0)