From d98abb12f984017d58084cbf37f2a2bc56bbd664 Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Tue, 17 Jan 2023 11:41:41 +0800 Subject: [PATCH] [fix](Nereids)set oepration type coercion is diff with legacy planner (#15982) --- .../plans/logical/LogicalSetOperation.java | 26 +++++++------------ .../data/nereids_syntax_p0/set_operation.out | 26 +++++++++++-------- .../nereids_syntax_p0/set_operation.groovy | 9 +++++++ 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java index 1e69cda49c..ef72488816 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java @@ -17,6 +17,7 @@ package org.apache.doris.nereids.trees.plans.logical; +import org.apache.doris.catalog.Type; import org.apache.doris.nereids.memo.GroupExpression; import org.apache.doris.nereids.properties.LogicalProperties; import org.apache.doris.nereids.trees.expressions.Cast; @@ -129,25 +130,16 @@ public abstract class LogicalSetOperation extends AbstractLogicalPlan implements List newRightOutpus = new ArrayList<>(); // Ensure that the output types of the left and right children are consistent and expand upward. for (int i = 0; i < resetNullableForLeftOutputs.size(); ++i) { - boolean hasPushed = false; Slot left = resetNullableForLeftOutputs.get(i); Slot right = child(1).getOutput().get(i); - if (TypeCoercionUtils.canHandleTypeCoercion(left.getDataType(), right.getDataType())) { - Optional tightestCommonType = - TypeCoercionUtils.findTightestCommonType(null, left.getDataType(), right.getDataType()); - if (tightestCommonType.isPresent()) { - Expression newLeft = TypeCoercionUtils.castIfNotSameType(left, tightestCommonType.get()); - Expression newRight = TypeCoercionUtils.castIfNotSameType(right, tightestCommonType.get()); - newLeftOutputs.add(newLeft); - newRightOutpus.add(newRight); - hasPushed = true; - } - } - - if (!hasPushed) { - newLeftOutputs.add(left); - newRightOutpus.add(right); - } + DataType compatibleType = DataType.convertFromCatalogDataType(Type.getAssignmentCompatibleType( + left.getDataType().toCatalogDataType(), + right.getDataType().toCatalogDataType(), + false)); + Expression newLeft = TypeCoercionUtils.castIfNotSameType(left, compatibleType); + Expression newRight = TypeCoercionUtils.castIfNotSameType(right, compatibleType); + newLeftOutputs.add(newLeft); + newRightOutpus.add(newRight); } List> resultExpressions = new ArrayList<>(); diff --git a/regression-test/data/nereids_syntax_p0/set_operation.out b/regression-test/data/nereids_syntax_p0/set_operation.out index 91b5c554a8..09b3124475 100644 --- a/regression-test/data/nereids_syntax_p0/set_operation.out +++ b/regression-test/data/nereids_syntax_p0/set_operation.out @@ -406,31 +406,31 @@ d d 3 3 9.0 3 9 -- !union30 -- -1.00 2.0 -1.01 2.0 -0.00 0.0 +1.0000 2.0000000 +1.0100 2.0000000 +0.0001 1E-7 -- !union31 -- 1 2 hell0 -- !union32 -- -1.0 2.0 +1.00000000 2.00000 -- !union33 -- -1.0 2.0 +1.00000000 2.00000 -- !union34 -- -1.0 2.0 -1.0 2.0 -1.0 2.0 +1.00000000 2.00000 +1.00000000 2.00000 +1.00000000 2.00000 -- !union35 -- -1.0 2.0 -1.0 2.0 +1.00000000 2.00000 +1.00000000 2.00000 -- !union36 -- -1.0 2.0 +1.00000000 2.00000 -- !union38 -- 2016-07-01 @@ -585,3 +585,7 @@ hell0 2020-05-25 2020-05-25 +-- !union44 -- +2020-05-25 +2020-05-25 00 + diff --git a/regression-test/suites/nereids_syntax_p0/set_operation.groovy b/regression-test/suites/nereids_syntax_p0/set_operation.groovy index d8820e95b5..bc2e86db2f 100644 --- a/regression-test/suites/nereids_syntax_p0/set_operation.groovy +++ b/regression-test/suites/nereids_syntax_p0/set_operation.groovy @@ -277,4 +277,13 @@ suite("test_nereids_set_operation") { """ qt_union43 """select '2020-05-25' day from test_table union all select day from test_table;""" + + qt_union44 """ + select * from + (select day from test_table + union all + select DATE_FORMAT(day, '%Y-%m-%d %H') dt_h from test_table + ) a + order by 1 + """ }