This is an automated email from the ASF dual-hosted git repository.

morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new c4722069a06 [Fix](planner) fix 
ScalarType.getAssignmentCompatibleType() when deal boolean and decimal (#34435)
c4722069a06 is described below

commit c4722069a0672f4b459b8bbeeda895197b6f6ee1
Author: feiniaofeiafei <53502832+feiniaofeia...@users.noreply.github.com>
AuthorDate: Thu May 9 10:33:48 2024 +0800

    [Fix](planner) fix ScalarType.getAssignmentCompatibleType() when deal 
boolean and decimal (#34435)
    
    The legacy planner encounters issues when handling filters such as: 
c1(boolean type)=0.0(decimalv3).
    The literal 0.0 is interpreted as decimalv3(1,1), and the boolean type c1 
is coerced to decimalv3(1,1).
    decimalv3(1,1) can only retain values in the range [0,1), while the boolean 
true is represented as 1, exceeding the upper bound, thus causing an overflow 
problem.
    This pull request addresses this issue by considering the boolean type as 
decimalv3(1,0), making both c1 and 0.0 being cast to decimal(2,1).
    
    
    Co-authored-by: feiniaofeiafei <moail...@selectdb.com>
---
 .../java/org/apache/doris/catalog/ScalarType.java  |  8 +++-
 .../get_assignment_compatible_type.out             |  8 ++++
 .../get_assignment_compatible_type.groovy          | 52 ++++++++++++++++++++++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java 
b/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java
index 8e8a66849e2..12cf7af02ba 100644
--- a/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java
+++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java
@@ -1099,7 +1099,13 @@ public class ScalarType extends Type {
             if (t1.isFloatingPointType() || t2.isFloatingPointType()) {
                 return t1.isFloatingPointType() ? t1 : t2;
             } else if (t1.isBoolean() || t2.isBoolean()) {
-                return t1.isDecimalV3() ? t1 : t2;
+                return t1.isDecimalV3()
+                        ? ScalarType.createDecimalV3Type(
+                        Math.max(t1.precision - t1.scale, 1) + 
Math.max(t1.scale, 0),
+                        Math.max(t1.scale, 0)) :
+                        ScalarType.createDecimalV3Type(
+                        Math.max(t2.precision - t2.scale, 1) + 
Math.max(t2.scale, 0),
+                        Math.max(t2.scale, 0));
             }
         }
 
diff --git 
a/regression-test/data/datatype_p0/scalar_types/get_assignment_compatible_type.out
 
b/regression-test/data/datatype_p0/scalar_types/get_assignment_compatible_type.out
new file mode 100644
index 00000000000..9f8d9b546fc
--- /dev/null
+++ 
b/regression-test/data/datatype_p0/scalar_types/get_assignment_compatible_type.out
@@ -0,0 +1,8 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !test_sql --
+test_decimal_boolean_view      CREATE VIEW `test_decimal_boolean_view` COMMENT 
'VIEW' AS SELECT `id` AS `id`, `c1` AS `c1`, `c2` AS `c2` FROM 
`regression_test_datatype_p0_scalar_types`.`test_decimal_boolean` WHERE (0.0 = 
CAST(`c1` AS DECIMALV3(2, 1))) AND (CAST(`c2` AS DECIMALV3(6, 1)) = 1.0); 
utf8mb4 utf8mb4_0900_bin
+
+-- !test_union --
+0
+1
+
diff --git 
a/regression-test/suites/datatype_p0/scalar_types/get_assignment_compatible_type.groovy
 
b/regression-test/suites/datatype_p0/scalar_types/get_assignment_compatible_type.groovy
new file mode 100644
index 00000000000..afbf3746259
--- /dev/null
+++ 
b/regression-test/suites/datatype_p0/scalar_types/get_assignment_compatible_type.groovy
@@ -0,0 +1,52 @@
+// 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("get_assignment_compatible_type") {
+    sql 'set enable_nereids_planner=false'
+    sql "drop table if exists test_decimal_boolean"
+    sql """create table test_decimal_boolean (
+        id int,
+        c1 boolean,
+        c2 tinyint
+        ) duplicate key (`id`)
+        DISTRIBUTED BY HASH(`id`) BUCKETS 1
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2"
+        ); """
+    sql "insert into test_decimal_boolean values (1,1,1),(2,0,1);"
+    sql "sync"
+    sql "drop view if exists test_decimal_boolean_view;"
+    // 0.0 = c1 is the test point
+    sql "create view test_decimal_boolean_view as select id,c1,c2 from 
test_decimal_boolean where 0.0=c1 and c2 = 1.0;"
+    sql "select * from test_decimal_boolean_view;"
+    qt_test_sql "show create view test_decimal_boolean_view"
+
+    sql "drop table if exists test_decimal_boolean_union"
+    sql """create table test_decimal_boolean_union (
+        id int,
+        c1 boolean,
+        c2 decimalv3(1,1)
+        ) duplicate key (`id`)
+        DISTRIBUTED BY HASH(`id`) BUCKETS 1
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1"
+        ); """
+    sql "insert into test_decimal_boolean_union 
values(1,true,0.0),(1,false,0.0)"
+    qt_test_union "select c1 from test_decimal_boolean_union union select c2 
from test_decimal_boolean_union order by 1"
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to