Skip to content

Commit ec31a80

Browse files
authored
[fix](function)width_bucket did not enforce that the fourth argument must be a constant. (#60643)
The implementation of width_bucket requires the fourth argument to be constant but did not enforce this; a regular column can be passed in, however width_bucket will only use the first row. before ```sql mysql> SELECT k1, v1, v2, v3, width_bucket(v1, date('2023-11-18'), date('2027-11-18'), v4) AS w FROM width_bucket_test ORDER BY k1; +------+------------+-----------+--------+------+ | k1 | v1 | v2 | v3 | w | +------+------------+-----------+--------+------+ | 1 | 2022-11-18 | 290000 | 290000 | 0 | | 2 | 2023-11-18 | 320000 | 320000 | 1 | | 3 | 2024-11-18 | 399999.99 | 399999 | 1 | | 4 | 2025-11-18 | 400000 | 400000 | 1 | | 5 | 2026-11-18 | 470000 | 470000 | 1 | | 6 | 2027-11-18 | 510000 | 510000 | 2 | | 7 | 2028-11-18 | 610000 | 610000 | 2 | | 8 | NULL | NULL | NULL | NULL | +------+------------+-----------+--------+------+ ``` now ```sql mysql> SELECT k1, v1, v2, v3, width_bucket(v1, date('2023-11-18'), date('2027-11-18'), v4) AS w FROM width_bucket_test ORDER BY k1; ERROR 1105 (HY000): errCode = 2, detailMessage = The fourth argument of WidthBucket must be a constant. ```
1 parent a396f34 commit ec31a80

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

be/src/vec/functions/function_width_bucket.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class FunctionWidthBucket : public IFunction {
6868
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
6969
ColumnPtr max_value_ptr =
7070
block.get_by_position(arguments[2]).column->convert_to_full_column_if_const();
71-
ColumnPtr num_buckets_ptr =
72-
block.get_by_position(arguments[3]).column->convert_to_full_column_if_const();
71+
ColumnPtr num_buckets_ptr = block.get_by_position(arguments[3]).column;
7372
int64_t num_buckets = num_buckets_ptr->get_int(0);
7473

7574
if (num_buckets <= 0) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.doris.nereids.trees.expressions.functions.scalar;
1919

2020
import org.apache.doris.catalog.FunctionSignature;
21+
import org.apache.doris.nereids.exceptions.AnalysisException;
2122
import org.apache.doris.nereids.trees.expressions.Expression;
2223
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
2324
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
@@ -74,6 +75,18 @@ public WidthBucket withChildren(List<Expression> children) {
7475
return new WidthBucket(getFunctionParams(children));
7576
}
7677

78+
@Override
79+
public void checkLegalityBeforeTypeCoercion() {
80+
if (!child(3).isLiteral()) {
81+
throw new AnalysisException("The fourth argument of WidthBucket must be a constant.");
82+
}
83+
}
84+
85+
@Override
86+
public void checkLegalityAfterRewrite() {
87+
checkLegalityBeforeTypeCoercion();
88+
}
89+
7790
@Override
7891
public List<FunctionSignature> getSignatures() {
7992
return SIGNATURES;

regression-test/suites/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@ suite("test_width_bucket_function", "arrow_flight_sql") {
9494
sql "select width_bucket(4, 0, 8, 0)"
9595
exception "buckets must be a positive integer value"
9696
}
97+
98+
99+
test {
100+
sql "SELECT k1, width_bucket(v2, 200000, 600000, k1) FROM ${tableName2} ORDER BY k1"
101+
exception "The fourth argument of WidthBucket must be a constant"
102+
}
97103
}

0 commit comments

Comments
 (0)