diff --git a/be/src/vec/functions/function_string.h b/be/src/vec/functions/function_string.h index 9fc8e8702d75aa3..c05e7deb56be4f1 100644 --- a/be/src/vec/functions/function_string.h +++ b/be/src/vec/functions/function_string.h @@ -3107,6 +3107,8 @@ class FunctionRandomBytes : public IFunction { return std::make_shared(); } + bool use_default_implementation_for_constants() const final { return false; } + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, size_t result, size_t input_rows_count) const override { auto res = ColumnString::create(); @@ -3114,24 +3116,28 @@ class FunctionRandomBytes : public IFunction { auto& res_chars = res->get_chars(); res_offsets.resize(input_rows_count); - ColumnPtr argument_column = - block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); - const auto* length_col = assert_cast(argument_column.get()); + auto [arg_col, arg_const] = unpack_if_const(block.get_by_position(arguments[0]).column); + const auto* length_col = assert_cast(arg_col.get()); + + if (arg_const) { + res_chars.reserve(input_rows_count * (length_col->get_element(0) + 2)); + } std::vector random_bytes; std::random_device rd; std::mt19937 gen(rd()); + std::uniform_int_distribution distribution(0, 255); for (size_t i = 0; i < input_rows_count; ++i) { - if (length_col->get_element(i) < 0) [[unlikely]] { + size_t index = index_check_const(i, arg_const); + if (length_col->get_element(index) < 0) [[unlikely]] { return Status::InvalidArgument("argument {} of function {} at row {} was invalid.", - length_col->get_element(i), name, i); + length_col->get_element(index), name, index); } - random_bytes.resize(length_col->get_element(i)); + random_bytes.resize(length_col->get_element(index)); - std::uniform_int_distribution distribution(0, 255); for (auto& byte : random_bytes) { - byte = distribution(gen); + byte = distribution(gen) & 0xFF; } std::ostringstream oss; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Random.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Random.java index 5045d85c9194213..449cf42e6d0e032 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Random.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Random.java @@ -90,11 +90,6 @@ public boolean nullable() { } } - @Override - public boolean foldable() { - return false; - } - /** * withChildren. */ diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/RandomBytes.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/RandomBytes.java index f5906670f86b52c..36b56bca7528f49 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/RandomBytes.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/RandomBytes.java @@ -20,6 +20,7 @@ import org.apache.doris.catalog.FunctionSignature; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.functions.Nondeterministic; import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable; import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; import org.apache.doris.nereids.types.IntegerType; @@ -35,7 +36,7 @@ * ScalarFunction 'random_bytes'. This class is generated by GenerateFunction. */ public class RandomBytes extends ScalarFunction - implements ExplicitlyCastableSignature, PropagateNullable { + implements ExplicitlyCastableSignature, PropagateNullable, Nondeterministic { public static final List SIGNATURES = ImmutableList.of( FunctionSignature.ret(StringType.INSTANCE).args(IntegerType.INSTANCE), diff --git a/regression-test/data/nereids_function_p0/scalar_function/R.out b/regression-test/data/nereids_function_p0/scalar_function/R.out index df315a2624e26b2..e90736971184c1c 100644 --- a/regression-test/data/nereids_function_p0/scalar_function/R.out +++ b/regression-test/data/nereids_function_p0/scalar_function/R.out @@ -1125,6 +1125,3 @@ string3 string3 string3 --- !sql_random_bytes -- -\N - diff --git a/regression-test/data/nereids_p0/datetime_functions/test_date_function.out b/regression-test/data/nereids_p0/datetime_functions/test_date_function.out deleted file mode 100644 index b2ed2aae64a393e..000000000000000 --- a/regression-test/data/nereids_p0/datetime_functions/test_date_function.out +++ /dev/null @@ -1,605 +0,0 @@ --- This file is automatically generated. You should know what you did if you want to edit this --- !sql -- -2019-07-31T22:21:03 - --- !sql -- -2019-07-31T22:21:03 - --- !sql -- -2019-08-01T06:21:03 - --- !sql -- -2019-08-01T06:21:03 - --- !sql -- -\N - --- !sql -- -\N - --- !sql -- -\N - --- !sql -- -\N - --- !sql1 -- -1 2019-08-01T13:21:03 Asia/Shanghai Asia/Shanghai 2019-08-01T13:21:03 -2 2019-08-01T13:21:03 Asia/Singapore Asia/Shanghai 2019-08-01T13:21:03 -3 2019-08-01T13:21:03 Asia/Taipei Asia/Shanghai 2019-08-01T13:21:03 -4 2019-08-02T13:21:03 Australia/Queensland Asia/Shanghai 2019-08-02T11:21:03 -5 2019-08-02T13:21:03 Australia/Lindeman Asia/Shanghai 2019-08-02T11:21:03 -6 2019-08-03T13:21:03 America/Aruba Asia/Shanghai 2019-08-04T01:21:03 -7 2019-08-03T13:21:03 America/Blanc-Sablon Asia/Shanghai 2019-08-04T01:21:03 -8 2019-08-04T13:21:03 America/Dawson Africa/Lusaka 2019-08-04T22:21:03 -9 2019-08-04T13:21:03 America/Creston Africa/Lusaka 2019-08-04T22:21:03 -10 2019-08-05T13:21:03 Asia/Shanghai Asia/Shanghai 2019-08-05T13:21:03 -11 2019-08-05T13:21:03 Asia/Shanghai Asia/Singapore 2019-08-05T13:21:03 -12 2019-08-05T13:21:03 Asia/Shanghai Asia/Taipei 2019-08-05T13:21:03 -13 2019-08-06T13:21:03 Asia/Shanghai Australia/Queensland 2019-08-06T15:21:03 -14 2019-08-06T13:21:03 Asia/Shanghai Australia/Lindeman 2019-08-06T15:21:03 -15 2019-08-07T13:21:03 Asia/Shanghai America/Aruba 2019-08-07T01:21:03 -16 2019-08-07T13:21:03 Asia/Shanghai America/Blanc-Sablon 2019-08-07T01:21:03 -17 2019-08-08T13:21:03 Africa/Lusaka America/Dawson 2019-08-08T04:21:03 -18 2019-08-08T13:21:03 Africa/Lusaka America/Creston 2019-08-08T04:21:03 - --- !sql2 -- -2019-08-01T13:21:03 2019-08-01T13:21:03 2019-08-01T13:21:03 - --- !sql3 -- -2019-08-02T11:21:03 2019-08-02T11:21:03 2019-08-02T11:21:03 - --- !sql4 -- -2019-08-04T22:21:03 2019-08-04T22:21:03 2019-08-04T22:21:03 - --- !sql_vec1 -- -1 2019-08-01T13:21:03 Asia/Shanghai Asia/Shanghai 2019-08-01T13:21:03 -2 2019-08-01T13:21:03 Asia/Singapore Asia/Shanghai 2019-08-01T13:21:03 -3 2019-08-01T13:21:03 Asia/Taipei Asia/Shanghai 2019-08-01T13:21:03 -4 2019-08-02T13:21:03 Australia/Queensland Asia/Shanghai 2019-08-02T11:21:03 -5 2019-08-02T13:21:03 Australia/Lindeman Asia/Shanghai 2019-08-02T11:21:03 -6 2019-08-03T13:21:03 America/Aruba Asia/Shanghai 2019-08-04T01:21:03 -7 2019-08-03T13:21:03 America/Blanc-Sablon Asia/Shanghai 2019-08-04T01:21:03 -8 2019-08-04T13:21:03 America/Dawson Africa/Lusaka 2019-08-04T22:21:03 -9 2019-08-04T13:21:03 America/Creston Africa/Lusaka 2019-08-04T22:21:03 -10 2019-08-05T13:21:03 Asia/Shanghai Asia/Shanghai 2019-08-05T13:21:03 -11 2019-08-05T13:21:03 Asia/Shanghai Asia/Singapore 2019-08-05T13:21:03 -12 2019-08-05T13:21:03 Asia/Shanghai Asia/Taipei 2019-08-05T13:21:03 -13 2019-08-06T13:21:03 Asia/Shanghai Australia/Queensland 2019-08-06T15:21:03 -14 2019-08-06T13:21:03 Asia/Shanghai Australia/Lindeman 2019-08-06T15:21:03 -15 2019-08-07T13:21:03 Asia/Shanghai America/Aruba 2019-08-07T01:21:03 -16 2019-08-07T13:21:03 Asia/Shanghai America/Blanc-Sablon 2019-08-07T01:21:03 -17 2019-08-08T13:21:03 Africa/Lusaka America/Dawson 2019-08-08T04:21:03 -18 2019-08-08T13:21:03 Africa/Lusaka America/Creston 2019-08-08T04:21:03 - --- !sql_vec2 -- -2019-08-01T13:21:03 2019-08-01T13:21:03 2019-08-01T13:21:03 - --- !sql_vec3 -- -2019-08-02T11:21:03 2019-08-02T11:21:03 2019-08-02T11:21:03 - --- !sql_vec4 -- -2019-08-04T22:21:03 2019-08-04T22:21:03 2019-08-04T22:21:03 - --- !sql -- -2012-11-30T23:59:59 - --- !sql -- -2011-01-30T23:59:59 - --- !sql -- -2010-12-02T23:59:59 - --- !sql -- -2010-12-01T01:59:59 - --- !sql -- -2010-12-01T00:01:59 - --- !sql -- -2010-12-01T00:00:01 - --- !sql -- -22:23:00 - --- !sql -- -4th 00 Thu 04 10 Oct 277 - --- !sql -- -22 22 10 10:23:00 PM 22:23:00 00 6 - --- !sql -- -1998 52 - --- !sql -- -01 - --- !sql -- -%01 - --- !sql -- -2009-10-04 - --- !sql -- -2008-11-30T23:59:59 - --- !sql -- -2010-09-30T23:59:59 - --- !sql -- -2010-11-28T23:59:59 - --- !sql -- -2010-11-30T21:59:59 - --- !sql -- -2010-11-30T23:57:59 - --- !sql -- -2010-11-30T23:59:57 - --- !sql -- -1 - --- !sql -- --31 - --- !sql -- -16 - --- !sql -- -31 - --- !sql -- -29 - --- !sql -- -Saturday - --- !sql -- -31 - --- !sql -- -3 - --- !sql -- -3 - --- !sql -- -34 - --- !sql -- -34 - --- !sql -- -2000-07-03 - --- !sql -- -0000-01-01 - --- !sql -- -2007-12-01 00:30:19 - --- !sql -- -2007-12-01 00:30:19 - --- !sql -- -2007-12-01 - --- !sql -- -2007-12-01 00:30:19 - --- !sql -- -\N - --- !sql -- -23 - --- !sql -- -0 - --- !sql -- -2021-01-01 2021-04-10 2022-02-04 - --- !sql -- -59 - --- !sql -- -0 - --- !sql -- -1 - --- !sql -- -1 - --- !sql -- -February - --- !sql -- -February - --- !sql -- -59 - --- !sql -- -0 - --- !sql -- -2014-12-21T12:34:56 - --- !sql -- -2014-12-21T12:34:56 - --- !sql -- -2014-12-21T12:34:56 - --- !sql -- -2004-10-18 - --- !sql -- -2020-09-01T00:00 - --- !sql -- -2020-01-01T00:00 - --- !sql -- -2020-04-01T00:00 - --- !sql -- -2020-02-03T00:00 - --- !sql -- -2020-04-09T00:00 - --- !sql -- -08:00:00 - --- !sql -- -00:00:09 - --- !sql -- -\N - --- !sql -- -2020-01-02T00:00 - --- !sql -- -2019-02-02T00:00 - --- !sql -- -2019-01-09T00:00 - --- !sql -- -2019-01-03T00:00 - --- !sql -- -2019-01-02T01:00 - --- !sql -- -2019-01-02T00:01 - --- !sql -- -2019-01-02T00:00:01 - --- !sql -- -3 - --- !sql -- --1 - --- !sql -- -128885 - --- !sql -- -7689600 - --- !sql -- -2136 - --- !sql -- -89 - --- !sql -- -12 - --- !sql -- -733321 - --- !sql -- -749027 - --- !sql -- -1196389819 - --- !sql -- -1196389819 - --- !sql -- -1196389819 - --- !sql -- -0 - --- !sql -- -0 - --- !sql -- -27 - --- !sql -- -1 - --- !sql -- -1 - --- !sql -- -8 - --- !sql -- -1987 - --- !sql -- -2050 - --- !sql -- -0000-08-01T13:21:03 0 -2019-08-01T13:21:03 2019 -9999-08-01T13:21:03 9999 - --- !sql -- -202052 - --- !sql -- -202026 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -198912 - --- !sql -- -1 2022-08-01 -2 2022-08-01 -4 2022-08-01 - --- !sql -- - --- !sql -- -3 2022-07-31 - --- !sql -- -1 2022-08-01 -2 2022-08-01 -4 2022-08-01 - --- !sql -- -1 2022-08-01 -2 2022-08-01 -3 2022-07-31 -4 2022-08-01 - --- !sql -- -1 2022-08-01 -2 2022-08-01 -4 2022-08-01 - --- !sql -- -2 2022-08-01 00:00:00 - --- !sql -- -1 2022-08-01 17:00:31 -4 2022-08-01 00:00:01 - --- !sql -- -3 2022-07-31 23:59:59 - --- !sql -- -1 2022-08-01 17:00:31 -2 2022-08-01 00:00:00 -4 2022-08-01 00:00:01 - --- !sql -- -2 2022-08-01 00:00:00 -3 2022-07-31 23:59:59 - --- !sql -- -2 2022-08-01 00:00:00 - --- !sql -- -1 2022-08-01 17:00:31 - --- !sql -- -true - --- !sql -- -true - --- !sql -- -2022 31 4 - --- !sql -- -\N - --- !sql -- -2022-07-12T20:00:45 - --- !sql -- -2018-04-02T15:03:28 - --- !sql -- -19 19 21 22 23 24 25 26 - --- !sql -- -19 19 21 22 23 24 25 26 - --- !sql -- -2020-08-01T13:21:03 - --- !sql -- -2019-09-01T13:21:03 - --- !sql -- -2019-08-08T13:21:03 - --- !sql -- -2019-08-02T13:21:03 - --- !sql -- -2019-08-01T14:21:03 - --- !sql -- -2019-08-01T13:22:03 - --- !sql -- -2019-08-01T13:21:04 - --- !sql -- -2018-08-01T13:21:03 - --- !sql -- -2019-07-01T13:21:03 - --- !sql -- -2019-07-25T13:21:03 - --- !sql -- -2019-07-31T13:21:03 - --- !sql -- -2019-08-01T12:21:03 - --- !sql -- -2019-08-01T13:20:03 - --- !sql -- -2019-08-01T13:21:02 - --- !sql -- -\N - --- !sql -- -\N - --- !sql -- -2020-08-01T13:21:03.111 - --- !sql -- -2019-09-01T13:21:03.111 - --- !sql -- -2019-08-08T13:21:03.111 - --- !sql -- -2019-08-02T13:21:03.111 - --- !sql -- -2019-08-01T14:21:03.111 - --- !sql -- -2019-08-01T13:22:03.111 - --- !sql -- -2019-08-01T13:21:04.111 - --- !sql -- -2018-08-01T13:21:03.111 - --- !sql -- -2019-07-01T13:21:03.111 - --- !sql -- -2019-07-25T13:21:03.111 - --- !sql -- -2019-07-31T13:21:03.111 - --- !sql -- -2019-08-01T12:21:03.111 - --- !sql -- -2019-08-01T13:20:03.111 - --- !sql -- -2019-08-01T13:21:02.111 - --- !sql -- -2020-08-01T13:21:03.111111 - --- !sql -- -2019-09-01T13:21:03.111111 - --- !sql -- -2019-08-08T13:21:03.111111 - --- !sql -- -2019-08-02T13:21:03.111111 - --- !sql -- -2019-08-01T14:21:03.111111 - --- !sql -- -2019-08-01T13:22:03.111111 - --- !sql -- -2019-08-01T13:21:04.111111 - --- !sql -- -2018-08-01T13:21:03.111111 - --- !sql -- -2019-07-01T13:21:03.111111 - --- !sql -- -2019-07-25T13:21:03.111111 - --- !sql -- -2019-07-31T13:21:03.111111 - --- !sql -- -2019-08-01T12:21:03.111111 - --- !sql -- -2019-08-01T13:20:03.111111 - --- !sql -- -2019-08-01T13:21:02.111111 - --- !sql -- -\N \N \N \N -2000-02-29 2000-02-29 2000-02-29 2000-02-29 -2022-01-31 2022-01-31 2022-01-31 2022-01-31 -2022-02-28 2022-02-28 2022-02-28 2022-02-28 - --- !sql -- -\N \N -2000-02-29 2000-02-29 -2022-01-31 2022-01-31 -2022-02-28 2022-02-28 - --- !sql -- -\N \N \N \N -1970-01-01 1970-01-01 1970-01-01 1970-01-01 -2000-01-31 2000-01-31 2000-01-31 2000-01-31 -2021-12-27 2021-12-27 2021-12-27 2021-12-27 -2022-02-28 2022-02-28 2022-02-28 2022-02-28 - diff --git a/regression-test/data/query_p0/sql_functions/string_functions/test_string_function.out b/regression-test/data/query_p0/sql_functions/string_functions/test_string_function.out index 69ce99277fa878b..0aae3a220647839 100644 Binary files a/regression-test/data/query_p0/sql_functions/string_functions/test_string_function.out and b/regression-test/data/query_p0/sql_functions/string_functions/test_string_function.out differ diff --git a/regression-test/suites/nereids_function_p0/scalar_function/R.groovy b/regression-test/suites/nereids_function_p0/scalar_function/R.groovy index 2a00feeafdfadd4..bf21154192b6555 100644 --- a/regression-test/suites/nereids_function_p0/scalar_function/R.groovy +++ b/regression-test/suites/nereids_function_p0/scalar_function/R.groovy @@ -107,10 +107,4 @@ suite("nereids_scalar_fn_R") { qt_sql_rtrim_Varchar_Varchar_notnull "select rtrim(kvchrs1, '1') from fn_test_not_nullable order by kvchrs1" qt_sql_rtrim_String_String "select rtrim(kstr, '1') from fn_test order by kstr" qt_sql_rtrim_String_String_notnull "select rtrim(kstr, '1') from fn_test_not_nullable order by kstr" - sql "SELECT random_bytes(7);" - qt_sql_random_bytes "SELECT random_bytes(null);" - test { - sql " select random_bytes(-1); " - exception "argument -1 of function random_bytes at row 0 was invalid" - } } diff --git a/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy b/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy index f8fe485f96732e9..e2606b11909b36c 100644 --- a/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy @@ -361,4 +361,23 @@ suite("test_string_function", "arrow_flight_sql") { qt_strcmp1 """ select strcmp('a', 'abc'); """ qt_strcmp2 """ select strcmp('abc', 'abc'); """ qt_strcmp3 """ select strcmp('abcd', 'abc'); """ + + sql "SELECT random_bytes(7);" + qt_sql_random_bytes "SELECT random_bytes(null);" + test { + sql " select random_bytes(-1); " + exception "argument -1 of function random_bytes at row 0 was invalid" + } + def some_result = sql """ SELECT random_bytes(10) a FROM numbers("number" = "10") """ + assertTrue(some_result[0][0] != some_result[1][0], "${some_result[0][0]} should different with ${some_result[1][0]}") + sql "select random_bytes(k1) from test_function_ngram_search;" + + explain { + sql("""select/*+SET_VAR(enable_fold_constant_by_be=true)*/ random_bytes(10) from numbers("number" = "10");""") + contains "final projections: random_bytes(10)" + } + explain { + sql("""select/*+SET_VAR(enable_fold_constant_by_be=true)*/ random(10) from numbers("number" = "10");""") + contains "final projections: random(10)" + } }