From 8ca4f9306763b5a18ffda27a07ab03cc77351e35 Mon Sep 17 00:00:00 2001 From: Mryange <59914473+Mryange@users.noreply.github.com> Date: Mon, 29 May 2023 19:51:12 +0800 Subject: [PATCH] [fix](DECIMALV3) Fix the error in DECIMALV3 when explicitly casting. (#19926) before mysql [test]>select cast(1 as DECIMALV3(16, 2)) / cast(3 as DECIMALV3(16, 2)); +-----------------------------------------------------------+ | CAST(1 AS DECIMALV3(16, 2)) / CAST(3 AS DECIMALV3(16, 2)) | +-----------------------------------------------------------+ | 0.00 | +-----------------------------------------------------------+ mysql [test]>select * from divtest; +------+------+ | id | val | +------+------+ | 3 | 5.00 | | 2 | 4.00 | | 1 | 3.00 | +------+------+ mysql [test]>select cast(1 as decimalv3(16,2)) / val from divtest; +-------------------------------------+ | CAST(1 AS DECIMALV3(16, 2)) / `val` | +-------------------------------------+ | 0 | | 0 | | 0 | +-------------------------------------+ after mysql [test]>select cast(1 as DECIMALV3(16, 2)) / cast(3 as DECIMALV3(16, 2)); +-----------------------------------------------------------+ | CAST(1 AS DECIMALV3(16, 2)) / CAST(3 AS DECIMALV3(16, 2)) | +-----------------------------------------------------------+ | 0.33 | +-----------------------------------------------------------+ mysql [test]>select cast(1 as decimalv3(16,2)) / val from divtest; +-------------------------------------+ | CAST(1 AS DECIMALV3(16, 2)) / `val` | +-------------------------------------+ | 0.250000 | | 0.200000 | | 0.333333 | +-------------------------------------+ This is because in the previous code, the constant 1.000 would be transformed into 1. remove "ReduceType --- .../apache/doris/analysis/ArithmeticExpr.java | 5 -- .../correctness/test_cast_as_decimalv3.out | 9 ++++ .../data/decimalv3/tpch_sf0.1_p1/sql/q14.out | 2 +- .../correctness/test_cast_as_decimalv3.groovy | 50 +++++++++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 regression-test/data/correctness/test_cast_as_decimalv3.out create mode 100644 regression-test/suites/correctness/test_cast_as_decimalv3.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ArithmeticExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ArithmeticExpr.java index 4bf92c6c4b..e1e5ebcbee 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ArithmeticExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ArithmeticExpr.java @@ -543,11 +543,6 @@ public class ArithmeticExpr extends Expr { @Override public void analyzeImpl(Analyzer analyzer) throws AnalysisException { if (VectorizedUtil.isVectorized()) { - for (Expr child : children) { - if (child instanceof DecimalLiteral && child.getType().isDecimalV3()) { - ((DecimalLiteral) child).tryToReduceType(); - } - } // bitnot is the only unary op, deal with it here if (op == Operator.BITNOT) { Type t = getChild(0).getType(); diff --git a/regression-test/data/correctness/test_cast_as_decimalv3.out b/regression-test/data/correctness/test_cast_as_decimalv3.out new file mode 100644 index 0000000000..fe80da1027 --- /dev/null +++ b/regression-test/data/correctness/test_cast_as_decimalv3.out @@ -0,0 +1,9 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select1 -- +0.33333333 +0.25000000 +0.20000000 + +-- !select2 -- +0.33 + diff --git a/regression-test/data/decimalv3/tpch_sf0.1_p1/sql/q14.out b/regression-test/data/decimalv3/tpch_sf0.1_p1/sql/q14.out index 20188f6434..b9410d3f79 100644 --- a/regression-test/data/decimalv3/tpch_sf0.1_p1/sql/q14.out +++ b/regression-test/data/decimalv3/tpch_sf0.1_p1/sql/q14.out @@ -1,4 +1,4 @@ -- This file is automatically generated. You should know what you did if you want to edit this -- !q14 -- -16.2838 +16.283855 diff --git a/regression-test/suites/correctness/test_cast_as_decimalv3.groovy b/regression-test/suites/correctness/test_cast_as_decimalv3.groovy new file mode 100644 index 0000000000..47515be645 --- /dev/null +++ b/regression-test/suites/correctness/test_cast_as_decimalv3.groovy @@ -0,0 +1,50 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_cast_as_decimalv3") { + sql """ DROP TABLE IF EXISTS divtest """ + sql """ + CREATE TABLE IF NOT EXISTS divtest ( + `id` int(11) , + `val` decimalv3(16,2) + ) + UNIQUE KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 10 + PROPERTIES ( + "enable_unique_key_merge_on_write" = "true", + "replication_num" = "1" + ); + """ + sql """ + set enable_nereids_planner=false,enable_fold_constant_by_be = false + """ + sql """ + INSERT INTO divtest VALUES(1,3.00) + """ + sql """ + INSERT INTO divtest VALUES(2,4.00) + """ + sql """ + INSERT INTO divtest VALUES(3,5.00) + """ + qt_select1 """ + select cast(1 as decimalv3(16,2)) / val from divtest order by id + """ + qt_select2 """ + select cast(1 as DECIMALV3(16, 2)) / cast(3 as DECIMALV3(16, 2)) + """ +} \ No newline at end of file