From db766bb073a5c71a6fda114287898b78304b3d22 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Thu, 6 Apr 2023 13:51:50 +0800 Subject: [PATCH] [fix](planner) decimalv2 castTo decimalv2 should change type directly (#18297) --- .../java/org/apache/doris/analysis/Expr.java | 7 ++ .../query_p0/aggregate/select_distinct.out | 4 + .../query_p0/aggregate/select_distinct.groovy | 77 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 regression-test/data/query_p0/aggregate/select_distinct.out create mode 100644 regression-test/suites/query_p0/aggregate/select_distinct.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java index d4ad469142..f59994f8a5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java @@ -1435,6 +1435,13 @@ public abstract class Expr extends TreeNode implements ParseNode, Cloneabl && (this.type.isStringType() || this.type.isHllType())) { return this; } + + if (targetType.getPrimitiveType() == PrimitiveType.DECIMALV2 + && this.type.getPrimitiveType() == PrimitiveType.DECIMALV2) { + this.type = targetType; + return this; + } + // Preconditions.checkState(PrimitiveType.isImplicitCast(type, targetType), // "cast %s to %s", this.type, targetType); // TODO(zc): use implicit cast diff --git a/regression-test/data/query_p0/aggregate/select_distinct.out b/regression-test/data/query_p0/aggregate/select_distinct.out new file mode 100644 index 0000000000..8c6c3f37f1 --- /dev/null +++ b/regression-test/data/query_p0/aggregate/select_distinct.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !distinct_decimal_cast -- +1 5.300000000 + diff --git a/regression-test/suites/query_p0/aggregate/select_distinct.groovy b/regression-test/suites/query_p0/aggregate/select_distinct.groovy new file mode 100644 index 0000000000..6456158bda --- /dev/null +++ b/regression-test/suites/query_p0/aggregate/select_distinct.groovy @@ -0,0 +1,77 @@ +// 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("select_distinct") { + sql """DROP TABLE IF EXISTS decimal_a;""" + sql """DROP TABLE IF EXISTS decimal_b;""" + sql """DROP TABLE IF EXISTS decimal_c;""" + + sql """ + CREATE TABLE IF NOT EXISTS `decimal_a` ( + `id` int(11) NOT NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + + sql """ + CREATE TABLE IF NOT EXISTS `decimal_b` ( + `id` int(11) NOT NULL, + `age` decimal(11, 3) NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + + sql """ + CREATE TABLE IF NOT EXISTS `decimal_c` ( + `id` int(11) NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 64 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + """ + + sql """insert into decimal_a values(1);""" + sql """insert into decimal_b values (1, 5.3);""" + sql """insert into decimal_c values(1);""" + + qt_distinct_decimal_cast """ + select distinct + decimal_a.id, + case + when decimal_b.age >= 0 then decimal_b.age + when decimal_b.age >= 0 then floor(decimal_b.age/365) + end + from + decimal_a + inner join decimal_b on decimal_a.id =decimal_b.id + left join decimal_c on decimal_a.id=decimal_c.id; + """ + + sql """DROP TABLE IF EXISTS decimal_a;""" + sql """DROP TABLE IF EXISTS decimal_b;""" + sql """DROP TABLE IF EXISTS decimal_c;""" +}