diff --git a/be/src/runtime/fold_constant_executor.cpp b/be/src/runtime/fold_constant_executor.cpp index be0db2b117..aebda95c9f 100644 --- a/be/src/runtime/fold_constant_executor.cpp +++ b/be/src/runtime/fold_constant_executor.cpp @@ -29,6 +29,7 @@ #include "runtime/memory/mem_tracker.h" #include "runtime/runtime_state.h" #include "runtime/thread_context.h" +#include "vec/common/string_ref.h" #include "vec/data_types/data_type_number.h" #include "vec/exprs/vexpr.h" #include "vec/exprs/vexpr_context.h" @@ -82,7 +83,10 @@ Status FoldConstantExecutor::fold_constant_vexpr(const TFoldConstantParams& para expr_result.set_success(false); } else { expr_result.set_success(true); - auto string_ref = column_ptr->get_data_at(0); + StringRef string_ref; + if (!ctx->root()->type().is_complex_type()) { + string_ref = column_ptr->get_data_at(0); + } result = _get_result((void*)string_ref.data, string_ref.size, ctx->root()->type(), column_ptr, column_type); } @@ -208,7 +212,7 @@ string FoldConstantExecutor::_get_result(void* src, size_t size, const TypeDescr return std::string(buf, pos - buf - 1); } case TYPE_DECIMALV2: { - return reinterpret_cast(src)->to_string(); + return reinterpret_cast(src)->to_string(type.scale); } case TYPE_DECIMAL32: case TYPE_DECIMAL64: diff --git a/be/src/vec/data_types/data_type.h b/be/src/vec/data_types/data_type.h index 33b77130d0..724b8556d7 100644 --- a/be/src/vec/data_types/data_type.h +++ b/be/src/vec/data_types/data_type.h @@ -228,6 +228,9 @@ public: */ virtual bool only_null() const { return false; } + /* the data type create from type_null, NULL literal*/ + virtual bool is_null_literal() const { return false; } + /** If this data type cannot be wrapped in Nullable data type. */ virtual bool can_be_inside_nullable() const { return false; } diff --git a/be/src/vec/data_types/data_type_array.cpp b/be/src/vec/data_types/data_type_array.cpp index c232de9d78..70af66dec5 100644 --- a/be/src/vec/data_types/data_type_array.cpp +++ b/be/src/vec/data_types/data_type_array.cpp @@ -137,11 +137,6 @@ void DataTypeArray::to_string(const IColumn& column, size_t row_num, BufferWrita ostr.write("'", 1); nested->to_string(nested_column, i, ostr); ostr.write("'", 1); - } else if (which.is_decimal()) { - DecimalV2Value decimal_value; - get_decimal_value(nested_column, decimal_value, i); - std::string decimal_str = decimal_value.to_string(); - ostr.write(decimal_str.c_str(), decimal_str.size()); } else { nested->to_string(nested_column, i, ostr); } @@ -171,10 +166,6 @@ std::string DataTypeArray::to_string(const IColumn& column, size_t row_num) cons str += "'"; str += nested->to_string(nested_column, i); str += "'"; - } else if (which.is_decimal()) { - DecimalV2Value decimal_value; - get_decimal_value(nested_column, decimal_value, i); - str += decimal_value.to_string(); } else { str += nested->to_string(nested_column, i); } diff --git a/be/src/vec/data_types/data_type_factory.cpp b/be/src/vec/data_types/data_type_factory.cpp index 1b9b6dbece..de893ab92b 100644 --- a/be/src/vec/data_types/data_type_factory.cpp +++ b/be/src/vec/data_types/data_type_factory.cpp @@ -168,6 +168,9 @@ DataTypePtr DataTypeFactory::create_data_type(const TypeDescriptor& col_desc, bo // Just Mock A NULL Type in Vec Exec Engine case TYPE_NULL: nested = std::make_shared(); + const_cast( + reinterpret_cast(*nested)) + .set_null_literal(true); break; case TYPE_ARRAY: DCHECK(col_desc.children.size() == 1); diff --git a/be/src/vec/data_types/data_type_nullable.h b/be/src/vec/data_types/data_type_nullable.h index 03afd0ed88..8ca6174a76 100644 --- a/be/src/vec/data_types/data_type_nullable.h +++ b/be/src/vec/data_types/data_type_nullable.h @@ -96,6 +96,7 @@ public: Status from_string(ReadBuffer& rb, IColumn* column) const override; const DataTypePtr& get_nested_type() const { return nested_data_type; } + bool is_null_literal() const override { return nested_data_type->is_null_literal(); } private: DataTypePtr nested_data_type; diff --git a/be/src/vec/data_types/data_type_number_base.h b/be/src/vec/data_types/data_type_number_base.h index 7f4a4c85ba..da12e9a82c 100644 --- a/be/src/vec/data_types/data_type_number_base.h +++ b/be/src/vec/data_types/data_type_number_base.h @@ -117,6 +117,11 @@ public: void to_string(const IColumn& column, size_t row_num, BufferWritable& ostr) const override; std::string to_string(const IColumn& column, size_t row_num) const override; Status from_string(ReadBuffer& rb, IColumn* column) const override; + bool is_null_literal() const override { return _is_null_literal; } + void set_null_literal(bool flag) { _is_null_literal = flag; } + +private: + bool _is_null_literal = false; }; } // namespace doris::vectorized diff --git a/be/src/vec/functions/function_cast.h b/be/src/vec/functions/function_cast.h index d754d80e20..77be12e144 100644 --- a/be/src/vec/functions/function_cast.h +++ b/be/src/vec/functions/function_cast.h @@ -1647,7 +1647,7 @@ private: const auto& from_nested = from_type; const auto& to_nested = to_type; - if (from_type->only_null()) { + if (from_type->only_null() || from_type->is_null_literal()) { if (!to_nested->is_nullable()) { return create_unsupport_wrapper("Cannot convert NULL to a non-nullable type"); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnBE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnBE.java index 284313035b..da64459e7f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnBE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/FoldConstantRuleOnBE.java @@ -46,6 +46,7 @@ import org.apache.doris.thrift.TFoldConstantParams; import org.apache.doris.thrift.TNetworkAddress; import org.apache.doris.thrift.TPrimitiveType; import org.apache.doris.thrift.TQueryGlobals; +import org.apache.doris.thrift.TQueryOptions; import com.google.common.collect.Maps; import org.apache.logging.log4j.LogManager; @@ -167,8 +168,13 @@ public class FoldConstantRuleOnBE extends AbstractExpressionRewriteRule { queryGlobals.setTimeZone(context.getSessionVariable().getTimeZone()); } + TQueryOptions tQueryOptions = new TQueryOptions(); + tQueryOptions.setRepeatMaxNum(context.getSessionVariable().repeatMaxNum); + TFoldConstantParams tParams = new TFoldConstantParams(paramMap, queryGlobals); tParams.setVecExec(VectorizedUtil.isVectorized()); + tParams.setQueryOptions(tQueryOptions); + tParams.setQueryId(context.queryId()); Future future = BackendServiceProxy.getInstance().foldConstantExpr(brpcAddress, tParams); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java index 8559db951c..ef4f26c5e0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java @@ -237,7 +237,7 @@ public abstract class Literal extends Expression implements LeafExpression, Comp } else if (targetType.isIntegerType()) { return Literal.of(Double.valueOf(desc).intValue()); } else if (targetType.isBigIntType()) { - return Literal.of(Double.valueOf(desc).longValue()); + return Literal.of(Long.valueOf(desc)); } else if (targetType.isLargeIntType()) { return Literal.of(new BigDecimal(desc).toBigInteger()); } else if (targetType.isFloatType()) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java index 08872008be..bddf0005fa 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java @@ -36,6 +36,7 @@ import org.apache.doris.catalog.ScalarType; import org.apache.doris.catalog.Type; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.LoadException; +import org.apache.doris.common.util.DebugUtil; import org.apache.doris.common.util.TimeUtils; import org.apache.doris.common.util.VectorizedUtil; import org.apache.doris.proto.InternalService; @@ -420,9 +421,11 @@ public class FoldConstantsRule implements ExprRewriteRule { } } else { + LOG.warn("failed_fold_context.queryId(): " + DebugUtil.printId(context.queryId())); LOG.warn("failed to get const expr value from be: {}", result.getStatus().getErrorMsgsList()); } } catch (Exception e) { + LOG.warn("failed_fold_context.queryId(): " + DebugUtil.printId(context.queryId())); LOG.warn("failed to get const expr value from be: {}", e.getMessage()); } return resultMap; diff --git a/regression-test/data/export/test_array_export.out b/regression-test/data/export/test_array_export.out index fd4b9978aa..922de66ec9 100644 --- a/regression-test/data/export/test_array_export.out +++ b/regression-test/data/export/test_array_export.out @@ -1,5 +1,5 @@ -- This file is automatically generated. You should know what you did if you want to edit this -- !select_default -- -1 [1, 2, 3] [32767, 32768, 32769] [65534, 65535, 65536] ['a', 'b', 'c'] ['hello', 'world'] [2022-07-13] [2022-08-15 12:30:00] [0.331111, 0.672222] [3.141592, 0.878787] [4.2222, 5.5555, 6.67] -2 [4, 5, 6] [32767, 32768, 32769] [65534, 65535, 65536] ['d', 'e', 'f'] ['good', 'luck'] [2022-07-13] [2022-08-15 15:59:59] [0.333336, 0.666677] [3.141592, 0.878787] [4.22222, 5.5555555, 6.6666777] +1 [1, 2, 3] [32767, 32768, 32769] [65534, 65535, 65536] ['a', 'b', 'c'] ['hello', 'world'] [2022-07-13] [2022-08-15 12:30:00] [0.331111, 0.672222] [3.141592, 0.878787] [4.222200000, 5.555500000, 6.670000000] +2 [4, 5, 6] [32767, 32768, 32769] [65534, 65535, 65536] ['d', 'e', 'f'] ['good', 'luck'] [2022-07-13] [2022-08-15 15:59:59] [0.333336, 0.666677] [3.141592, 0.878787] [4.222220000, 5.555555500, 6.666677700] diff --git a/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out b/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out index a830fb4f27..410a0dca01 100644 --- a/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out +++ b/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out @@ -17,3 +17,6 @@ -- !sql -- 20 +-- !sql_null_cast_bitmap -- +true + diff --git a/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out b/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out index 8a2ea768f0..53aecd35b2 100644 --- a/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out +++ b/regression-test/data/query_p0/sql_functions/array_functions/test_array_with_scale_type.out @@ -20,26 +20,26 @@ 2022-12-02T23:23:24.999 -- !select -- -22.000000000 +22 -- !select -- -22.990000000 +22.990 -- !select -- -22.990000000 +22.990000 -- !select -- 22.678000000 23.678000000 -- !select -- -22.000000000 +22 -- !select -- -22.990000000 +22.990 -- !select -- -22.990000000 +22.990000 -- !select -- 33.678900000 @@ -50,8 +50,8 @@ [23.678] -- !select -- -[24.99, 25.99] -[24.99, 25.99] +[24.990, 25.990] +[24.990, 25.990] -- !select -- [24.990, 25.990] diff --git a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out index a830fb4f27..410a0dca01 100644 --- a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out +++ b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out @@ -17,3 +17,6 @@ -- !sql -- 20 +-- !sql_null_cast_bitmap -- +true + diff --git a/regression-test/suites/datatype_p0/decimalv3/test_predicate.groovy b/regression-test/suites/datatype_p0/decimalv3/test_predicate.groovy index 62c28fa928..429f98b94a 100644 --- a/regression-test/suites/datatype_p0/decimalv3/test_predicate.groovy +++ b/regression-test/suites/datatype_p0/decimalv3/test_predicate.groovy @@ -39,9 +39,9 @@ suite("test_predicate") { (1.2,1.2,1.3), (1.5,1.2,1.3) """ - qt_select1 "SELECT CAST((CASE WHEN (TRUE IS NOT NULL) THEN '1.2' ELSE '1.2' END) AS FLOAT) = CAST(1.2 AS decimal(2,1))" + qt_select1 "SELECT /*+ SET_VAR(enable_fold_constant_by_be = false) */ CAST((CASE WHEN (TRUE IS NOT NULL) THEN '1.2' ELSE '1.2' END) AS FLOAT) = CAST(1.2 AS decimal(2,1))" - qt_select2 "SELECT 1 FROM ${table1} WHERE CAST((CASE WHEN (TRUE IS NOT NULL) THEN '1.2' ELSE '1.2' END) AS FLOAT) = CAST(1.2 AS decimal(2,1));" + qt_select2 "SELECT /*+ SET_VAR(enable_fold_constant_by_be = false) */ 1 FROM ${table1} WHERE CAST((CASE WHEN (TRUE IS NOT NULL) THEN '1.2' ELSE '1.2' END) AS FLOAT) = CAST(1.2 AS decimal(2,1));" qt_select3 "SELECT * FROM ${table1} WHERE k1 != 1.1 ORDER BY k1" sql "drop table if exists ${table1}" } diff --git a/regression-test/suites/export/test_array_export.groovy b/regression-test/suites/export/test_array_export.groovy index 5794eade82..0c8906f110 100644 --- a/regression-test/suites/export/test_array_export.groovy +++ b/regression-test/suites/export/test_array_export.groovy @@ -70,7 +70,7 @@ suite("test_array_export", "export") { `k8` ARRAY NOT NULL COMMENT "", `k9` ARRAY NOT NULL COMMENT "", `k10` ARRAY NOT NULL COMMENT "", - `k11` ARRAY NULL COMMENT "" + `k11` ARRAY NULL COMMENT "" ) ENGINE=OLAP DUPLICATE KEY(`k1`) DISTRIBUTED BY HASH(`k1`) BUCKETS 1 diff --git a/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy b/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy index 97a46f6b42..b10a7e5bb9 100644 --- a/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy +++ b/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy @@ -24,25 +24,6 @@ suite("test_cast_function") { qt_sql """ select cast ("0.0000031417" as datetime) """ qt_sql """ select cast (NULL AS CHAR(1)); """ qt_sql """ select cast ('20190101' AS CHAR(2)); """ - - test { - sql """ - select - ref_0.`k0` as c1 - from - `test_query_db`.`baseall` as ref_0 - where - cast( - case - when BITMAP_EMPTY() is NULL then null - else null - end as bitmap - ) is NULL - """ - check{result, exception, startTime, endTime -> - assertTrue(exception != null) - logger.info(exception.message) - } - } + qt_sql_null_cast_bitmap """ select cast (case when BITMAP_EMPTY() is NULL then null else null end as bitmap) is NULL; """ } diff --git a/regression-test/suites/query_p0/sql_functions/array_functions/test_array_with_scale_type.groovy b/regression-test/suites/query_p0/sql_functions/array_functions/test_array_with_scale_type.groovy index 3ba6b6f851..9bc37196d1 100644 --- a/regression-test/suites/query_p0/sql_functions/array_functions/test_array_with_scale_type.groovy +++ b/regression-test/suites/query_p0/sql_functions/array_functions/test_array_with_scale_type.groovy @@ -47,18 +47,18 @@ suite("test_array_with_scale_type") { qt_select "select array_max(array(cast ('2022-12-02 22:23:24.999999' as datetimev2(3))))" qt_select "select array_max(c_array_datetimev2) from ${tableName}" - qt_select "select array_min(array(cast (22.99 as decimal)))" - qt_select "select array_min(array(cast (22.99 as decimal(10,3))))" - qt_select "select array_min(array(cast (22.99 as decimal(10,6))))" + qt_select "select array_min(array(cast (22.99 as decimalv3)))" + qt_select "select array_min(array(cast (22.99 as decimalv3(10,3))))" + qt_select "select array_min(array(cast (22.99 as decimalv3(10,6))))" qt_select "select array_min(c_array_decimal) from ${tableName}" - qt_select "select array_max(array(cast (22.99 as decimal)))" - qt_select "select array_max(array(cast (22.99 as decimal(10,3))))" - qt_select "select array_max(array(cast (22.99 as decimal(10,6))))" + qt_select "select array_max(array(cast (22.99 as decimalv3)))" + qt_select "select array_max(array(cast (22.99 as decimalv3(10,3))))" + qt_select "select array_max(array(cast (22.99 as decimalv3(10,6))))" qt_select "select array_max(c_array_decimal) from ${tableName}" qt_select "select array(c_decimal) from ${tableName}" - qt_select "select array(cast (24.99 as decimal(10,3)),cast (25.99 as decimal(10,3))) from ${tableName}" - qt_select "select array(cast (24.99 as decimal(10,3)),cast (25.99 as decimal(10,3)))" + qt_select "select array(cast (24.99 as decimalv3(10,3)),cast (25.99 as decimalv3(10,3))) from ${tableName}" + qt_select "select array(cast (24.99 as decimalv3(10,3)),cast (25.99 as decimalv3(10,3)))" qt_select "select array(c_decimalv3) from ${tableName}" qt_select "select array(cast (24.99 as decimalv3(10,3)),cast (25.99 as decimalv3(10,3))) from ${tableName}" diff --git a/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy index db3c958709..8dbec6ee91 100644 --- a/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy @@ -23,21 +23,6 @@ suite("test_cast_function") { qt_sql """ select cast (NULL AS CHAR(1)); """ qt_sql """ select cast ('20190101' AS CHAR(2)); """ - test { - sql """ - select - ref_0.`k0` as c1 - from - `test_query_db`.`baseall` as ref_0 - where - cast( - case - when BITMAP_EMPTY() is NULL then null - else null - end as bitmap - ) is NULL - """ - exception "Conversion from UInt8 to BitMap is not supported" - } + qt_sql_null_cast_bitmap """ select cast (case when BITMAP_EMPTY() is NULL then null else null end as bitmap) is NULL; """ }