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

jacktengg pushed a commit to branch TEBU-2.0.10-poc
in repository https://gitbox.apache.org/repos/asf/doris.git

commit f20eff2409cbb909858e96795a7944299815cc2f
Author: TengJianPing <18241664+jackte...@users.noreply.github.com>
AuthorDate: Fri May 24 11:33:34 2024 +0800

    [fix](decimalv2) fix scale of decimalv2 to string (#35222)
---
 be/src/vec/data_types/data_type_decimal.cpp        |  14 ++-
 be/src/vec/data_types/data_type_decimal.h          |  33 ++++++-
 be/src/vec/data_types/data_type_factory.cpp        |   6 +-
 be/test/vec/data_types/from_string_test.cpp        |   6 +-
 .../data_types/serde/data_type_serde_csv_test.cpp  |   4 +-
 .../data_types/serde/data_type_serde_text_test.cpp |  32 +++++--
 .../java/org/apache/doris/qe/SessionVariable.java  |   6 --
 .../decimalv2/test_decimalv2_cast_to_string.out    |  46 +++++++++
 .../decimalv3/test_decimalv3_cast_to_string.out    |  26 ++++++
 .../decimalv2/test_decimalv2_cast_to_string.groovy | 103 +++++++++++++++++++++
 .../decimalv3/test_decimalv3_cast_to_string.groovy |  68 ++++++++++++++
 11 files changed, 318 insertions(+), 26 deletions(-)

diff --git a/be/src/vec/data_types/data_type_decimal.cpp 
b/be/src/vec/data_types/data_type_decimal.cpp
index 23c4f2baeb1..e44913351a9 100644
--- a/be/src/vec/data_types/data_type_decimal.cpp
+++ b/be/src/vec/data_types/data_type_decimal.cpp
@@ -61,8 +61,13 @@ std::string DataTypeDecimal<T>::to_string(const IColumn& 
column, size_t row_num)
     ColumnPtr ptr = result.first;
     row_num = result.second;
 
-    auto value = assert_cast<const ColumnType&>(*ptr).get_element(row_num);
-    return value.to_string(scale);
+    if constexpr (!IsDecimalV2<T>) {
+        auto value = assert_cast<const ColumnType&>(*ptr).get_element(row_num);
+        return value.to_string(scale);
+    } else {
+        auto value = (DecimalV2Value)assert_cast<const 
ColumnType&>(*ptr).get_element(row_num);
+        return value.to_string(get_format_scale());
+    }
 }
 
 template <typename T>
@@ -77,9 +82,8 @@ void DataTypeDecimal<T>::to_string(const IColumn& column, 
size_t row_num,
         auto str = value.to_string(scale);
         ostr.write(str.data(), str.size());
     } else {
-        DecimalV2Value value =
-                (DecimalV2Value)assert_cast<const 
ColumnType&>(*ptr).get_element(row_num);
-        auto str = value.to_string(scale);
+        auto value = (DecimalV2Value)assert_cast<const 
ColumnType&>(*ptr).get_element(row_num);
+        auto str = value.to_string(get_format_scale());
         ostr.write(str.data(), str.size());
     }
 }
diff --git a/be/src/vec/data_types/data_type_decimal.h 
b/be/src/vec/data_types/data_type_decimal.h
index 008060014b9..e3749fce024 100644
--- a/be/src/vec/data_types/data_type_decimal.h
+++ b/be/src/vec/data_types/data_type_decimal.h
@@ -21,8 +21,6 @@
 #pragma once
 #include <fmt/format.h>
 #include <gen_cpp/Types_types.h>
-#include <stddef.h>
-#include <stdint.h>
 
 #include <algorithm>
 #include <cmath>
@@ -138,12 +136,28 @@ public:
 
     static constexpr size_t max_precision() { return 
max_decimal_precision<T>(); }
 
-    DataTypeDecimal(UInt32 precision = 27, UInt32 scale = 9) : 
precision(precision), scale(scale) {
+    DataTypeDecimal(UInt32 precision = 27, UInt32 scale = 9,
+                    UInt32 arg_original_precision = UINT32_MAX,
+                    UInt32 arg_original_scale = UINT32_MAX)
+            : precision(precision),
+              scale(scale),
+              original_precision(arg_original_precision),
+              original_scale(arg_original_scale) {
         check_type_precision(precision);
         check_type_scale(scale);
+        if (UINT32_MAX != original_precision) {
+            check_type_precision(original_precision);
+        }
+        if (UINT32_MAX != original_scale) {
+            check_type_scale(scale);
+        }
     }
 
-    DataTypeDecimal(const DataTypeDecimal& rhs) : precision(rhs.precision), 
scale(rhs.scale) {}
+    DataTypeDecimal(const DataTypeDecimal& rhs)
+            : precision(rhs.precision),
+              scale(rhs.scale),
+              original_precision(rhs.original_precision),
+              original_scale(rhs.original_scale) {}
 
     const char* get_family_name() const override { return "Decimal"; }
     std::string do_get_name() const override;
@@ -241,6 +255,9 @@ public:
 
     [[nodiscard]] UInt32 get_precision() const override { return precision; }
     [[nodiscard]] UInt32 get_scale() const override { return scale; }
+    [[nodiscard]] UInt32 get_format_scale() const {
+        return UINT32_MAX == original_scale ? scale : original_scale;
+    }
     T get_scale_multiplier() const { return get_scale_multiplier(scale); }
 
     T whole_part(T x) const {
@@ -315,6 +332,14 @@ public:
 private:
     const UInt32 precision;
     const UInt32 scale;
+
+    // For decimalv2 only, record the original(schema) precision and scale.
+    // UINT32_MAX means original precision and scale are unknown.
+    // Decimalv2 will be converted to Decimal(27, 9) in memory when doing any 
calculations,
+    // but when casting decimalv2 to string, it's better to keep the presion 
and
+    // scale of it's original value in schema.
+    UInt32 original_precision = UINT32_MAX;
+    UInt32 original_scale = UINT32_MAX;
 };
 
 template <typename T, typename U>
diff --git a/be/src/vec/data_types/data_type_factory.cpp 
b/be/src/vec/data_types/data_type_factory.cpp
index 50b52d98fe3..410c11ab63f 100644
--- a/be/src/vec/data_types/data_type_factory.cpp
+++ b/be/src/vec/data_types/data_type_factory.cpp
@@ -176,7 +176,8 @@ DataTypePtr DataTypeFactory::create_data_type(const 
TypeDescriptor& col_desc, bo
         nested = std::make_shared<vectorized::DataTypeBitMap>();
         break;
     case TYPE_DECIMALV2:
-        nested = 
std::make_shared<vectorized::DataTypeDecimal<vectorized::Decimal128>>(27, 9);
+        nested = 
std::make_shared<vectorized::DataTypeDecimal<vectorized::Decimal128>>(
+                27, 9, col_desc.precision, col_desc.scale);
         break;
     case TYPE_QUANTILE_STATE:
         nested = std::make_shared<vectorized::DataTypeQuantileStateDouble>();
@@ -382,7 +383,8 @@ DataTypePtr 
DataTypeFactory::_create_primitive_data_type(const FieldType& type,
         result = std::make_shared<vectorized::DataTypeBitMap>();
         break;
     case FieldType::OLAP_FIELD_TYPE_DECIMAL:
-        result = 
std::make_shared<vectorized::DataTypeDecimal<vectorized::Decimal128>>(27, 9);
+        result = 
std::make_shared<vectorized::DataTypeDecimal<vectorized::Decimal128>>(
+                27, 9, precision, scale);
         break;
     case FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE:
         result = std::make_shared<vectorized::DataTypeQuantileStateDouble>();
diff --git a/be/test/vec/data_types/from_string_test.cpp 
b/be/test/vec/data_types/from_string_test.cpp
index 594b67c7b11..9410dd99690 100644
--- a/be/test/vec/data_types/from_string_test.cpp
+++ b/be/test/vec/data_types/from_string_test.cpp
@@ -159,7 +159,11 @@ TEST(FromStringTest, ScalaWrapperFieldVsDataType) {
             DataTypePtr data_type_ptr;
             int precision = 0;
             int scale = 0;
-            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL32) {
+            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL) {
+                data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 27, 9);
+                precision = 27;
+                scale = 9;
+            } else if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL32) {
                 // decimal32(7, 2)
                 data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 9, 2);
                 precision = 9;
diff --git a/be/test/vec/data_types/serde/data_type_serde_csv_test.cpp 
b/be/test/vec/data_types/serde/data_type_serde_csv_test.cpp
index 82e30e4ca53..52a0b0a7b08 100644
--- a/be/test/vec/data_types/serde/data_type_serde_csv_test.cpp
+++ b/be/test/vec/data_types/serde/data_type_serde_csv_test.cpp
@@ -125,7 +125,9 @@ TEST(CsvSerde, ScalaDataTypeSerdeCsvTest) {
         for (auto type_pair : arithmetic_scala_field_types) {
             auto type = std::get<0>(type_pair);
             DataTypePtr data_type_ptr;
-            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL32) {
+            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL) {
+                data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 27, 9);
+            } else if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL32) {
                 // decimal32(7, 2)
                 data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 9, 2);
             } else if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL64) {
diff --git a/be/test/vec/data_types/serde/data_type_serde_text_test.cpp 
b/be/test/vec/data_types/serde/data_type_serde_text_test.cpp
index 652354ea487..09a98819d8b 100644
--- a/be/test/vec/data_types/serde/data_type_serde_text_test.cpp
+++ b/be/test/vec/data_types/serde/data_type_serde_text_test.cpp
@@ -125,7 +125,9 @@ TEST(TextSerde, ScalaDataTypeSerdeTextTest) {
         for (auto type_pair : arithmetic_scala_field_types) {
             auto type = std::get<0>(type_pair);
             DataTypePtr data_type_ptr;
-            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL32) {
+            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL) {
+                data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 27, 9);
+            } else if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL32) {
                 // decimal32(7, 2)
                 data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 9, 2);
             } else if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL64) {
@@ -363,8 +365,12 @@ TEST(TextSerde, ComplexTypeSerdeTextTest) {
         // array type
         for (auto type_pair : nested_field_types) {
             auto type = std::get<0>(type_pair);
-            DataTypePtr nested_data_type_ptr =
-                    DataTypeFactory::instance().create_data_type(type, 0, 0);
+            DataTypePtr nested_data_type_ptr;
+            if (type == FieldType::OLAP_FIELD_TYPE_DECIMAL) {
+                nested_data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 27, 9);
+            } else {
+                nested_data_type_ptr = 
DataTypeFactory::instance().create_data_type(type, 0, 0);
+            }
             DataTypePtr array_data_type_ptr = make_nullable(
                     
std::make_shared<DataTypeArray>(make_nullable(nested_data_type_ptr)));
 
@@ -524,8 +530,14 @@ TEST(TextSerde, ComplexTypeSerdeTextTest) {
             auto value_type = std::get<1>(type_pair);
             DataTypePtr nested_key_type_ptr =
                     DataTypeFactory::instance().create_data_type(key_type, 0, 
0);
-            DataTypePtr nested_value_type_ptr =
-                    DataTypeFactory::instance().create_data_type(value_type, 
0, 0);
+            DataTypePtr nested_value_type_ptr;
+            if (value_type == FieldType::OLAP_FIELD_TYPE_DECIMAL) {
+                nested_value_type_ptr =
+                        
DataTypeFactory::instance().create_data_type(value_type, 27, 9);
+            } else {
+                nested_value_type_ptr =
+                        
DataTypeFactory::instance().create_data_type(value_type, 0, 0);
+            }
             DataTypePtr map_data_type_ptr = 
make_nullable(std::make_shared<DataTypeMap>(
                     make_nullable(nested_key_type_ptr), 
make_nullable(nested_value_type_ptr)));
 
@@ -608,8 +620,14 @@ TEST(TextSerde, ComplexTypeSerdeTextTest) {
             auto value_type = std::get<1>(type_pair);
             DataTypePtr nested_key_type_ptr =
                     DataTypeFactory::instance().create_data_type(key_type, 0, 
0);
-            DataTypePtr nested_value_type_ptr =
-                    DataTypeFactory::instance().create_data_type(value_type, 
0, 0);
+            DataTypePtr nested_value_type_ptr;
+            if (value_type == FieldType::OLAP_FIELD_TYPE_DECIMAL) {
+                nested_value_type_ptr =
+                        
DataTypeFactory::instance().create_data_type(value_type, 27, 9);
+            } else {
+                nested_value_type_ptr =
+                        
DataTypeFactory::instance().create_data_type(value_type, 0, 0);
+            }
             DataTypePtr map_data_type_ptr = std::make_shared<DataTypeMap>(
                     make_nullable(nested_key_type_ptr), 
make_nullable(nested_value_type_ptr));
 
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
index 56743ba04c5..32863a06254 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
@@ -721,8 +721,6 @@ public class SessionVariable implements Serializable, 
Writable {
     public String defaultStorageEngine = "olap";
     @VariableMgr.VarAttr(name = DEFAULT_TMP_STORAGE_ENGINE)
     public String defaultTmpStorageEngine = "olap";
-    @VariableMgr.VarAttr(name = DIV_PRECISION_INCREMENT)
-    public int divPrecisionIncrement = 4;
 
     // -1 means unset, BE will use its config value
     @VariableMgr.VarAttr(name = MAX_SCAN_KEY_NUM)
@@ -1951,10 +1949,6 @@ public class SessionVariable implements Serializable, 
Writable {
         this.storageEngine = storageEngine;
     }
 
-    public int getDivPrecisionIncrement() {
-        return divPrecisionIncrement;
-    }
-
     public int getMaxScanKeyNum() {
         return maxScanKeyNum;
     }
diff --git 
a/regression-test/data/datatype_p0/decimalv2/test_decimalv2_cast_to_string.out 
b/regression-test/data/datatype_p0/decimalv2/test_decimalv2_cast_to_string.out
new file mode 100644
index 00000000000..dca5f03143d
--- /dev/null
+++ 
b/regression-test/data/datatype_p0/decimalv2/test_decimalv2_cast_to_string.out
@@ -0,0 +1,46 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !cast1 --
+\N     \N
+-999999999999999999.999999999  -999999999999999999.999999999
+-999999999999999999.000000000  -999999999999999999.000000000
+-1.123000000   -1.123000000
+-1.000000000   -1.000000000
+-0.100000000   -0.100000000
+-1E-9  -0.000000001
+0E-9   0.000000000
+1E-9   0.000000001
+0.100000000    0.100000000
+1.000000000    1.000000000
+1.123000000    1.123000000
+999999999999999999.000000000   999999999999999999.000000000
+999999999999999999.999999999   999999999999999999.999999999
+
+-- !cast2 --
+\N     \N
+-9999999.999   -9999999.999
+-9999999.001   -9999999.001
+-9999999.000   -9999999.000
+-1.123 -1.123
+-1.123 -1.123
+-1.100 -1.100
+-1.000 -1.000
+-0.100 -0.100
+-0.001 -0.001
+0.000  0.000
+0.001  0.001
+0.100  0.100
+1.000  1.000
+1.100  1.100
+1.123  1.123
+1.123  1.123
+9999999.000    9999999.000
+9999999.001    9999999.001
+9999999.999    9999999.999
+
+-- !join1 --
+1.123  1.1230
+
+-- !cast_join1 --
+
+-- !cast_join2 --
+
diff --git 
a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast_to_string.out 
b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast_to_string.out
new file mode 100644
index 00000000000..c4622cdcb42
--- /dev/null
+++ 
b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast_to_string.out
@@ -0,0 +1,26 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !cast1 --
+\N     \N
+-99999999999999999999999999999999.999999       
-99999999999999999999999999999999.999999
+-99999999999999999999999999999999.000001       
-99999999999999999999999999999999.000001
+-99999999999999999999999999999999.000000       
-99999999999999999999999999999999.000000
+-1.123000      -1.123000
+-1.000000      -1.000000
+-0.100000      -0.100000
+-0.000001      -0.000001
+0.000000       0.000000
+0.000001       0.000001
+0.100000       0.100000
+1.000000       1.000000
+1.123000       1.123000
+99999999999999999999999999999999.000000        
99999999999999999999999999999999.000000
+99999999999999999999999999999999.000001        
99999999999999999999999999999999.000001
+99999999999999999999999999999999.999999        
99999999999999999999999999999999.999999
+
+-- !join1 --
+1.123  1.1230
+
+-- !cast_join1 --
+
+-- !cast_join2 --
+
diff --git 
a/regression-test/suites/datatype_p0/decimalv2/test_decimalv2_cast_to_string.groovy
 
b/regression-test/suites/datatype_p0/decimalv2/test_decimalv2_cast_to_string.groovy
new file mode 100644
index 00000000000..28c7de8aed0
--- /dev/null
+++ 
b/regression-test/suites/datatype_p0/decimalv2/test_decimalv2_cast_to_string.groovy
@@ -0,0 +1,103 @@
+// 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_decimalv2_cast_to_string", "nonConcurrent") {
+    def config_row = sql """ ADMIN SHOW FRONTEND CONFIG LIKE 
'disable_decimalv2'; """
+    String old_value1 = config_row[0][1]
+    config_row = sql """ ADMIN SHOW FRONTEND CONFIG LIKE 
'enable_decimal_conversion'; """
+    String old_value2 = config_row[0][1]
+
+    sql """
+        admin set frontend config("enable_decimal_conversion" = "false");
+    """
+    sql """
+        admin set frontend config("disable_decimalv2" = "false");
+    """
+
+    sql """
+        drop table if exists decimalv2_cast_to_string_test;
+    """
+    sql """
+        create table decimalv2_cast_to_string_test (k1 decimalv2(27,9)) 
distributed by hash(k1) properties("replication_num"="1");
+    """
+    sql """
+        insert into decimalv2_cast_to_string_test  values (null), (0), (1), 
(1.123), (0.1), (0.000000001), (999999999999999999), 
(999999999999999999.999999999);
+    """
+    sql """
+        insert into decimalv2_cast_to_string_test  values (-1), (-1.123), 
(-0.1), (-0.000000001), (-999999999999999999), (-999999999999999999.999999999);
+    """
+
+    qt_cast1 """
+        select k1, cast(k1 as varchar) from decimalv2_cast_to_string_test 
order by 1;
+    """
+
+    sql """
+        drop table if exists decimalv2_cast_to_string_test2;
+    """
+    sql """
+        create table decimalv2_cast_to_string_test2 (k1 decimalv2(10,3)) 
distributed by hash(k1) properties("replication_num"="1");
+    """
+    sql """
+        insert into decimalv2_cast_to_string_test2 values (null), (0), (0.1), 
(0.001), (1), (1.1), (1.123), (1.123456789), (9999999), (9999999.001), 
(9999999.999);
+    """
+    sql """
+        insert into decimalv2_cast_to_string_test2 values (-0.1), (-0.001), 
(-1), (-1.1), (-1.123), (-1.123456789), (-9999999), (-9999999.001), 
(-9999999.999);
+    """
+
+    qt_cast2 """
+        select k1, cast(k1 as varchar) from decimalv2_cast_to_string_test2 
order by 1;
+    """
+
+    sql """
+        drop table if exists decimalv2_cast_to_string_join_test_l;
+    """
+    sql """
+        drop table if exists decimalv2_cast_to_string_join_test_r;
+    """
+    sql """
+        create table decimalv2_cast_to_string_join_test_l(k1 decimalv2(10, 3)) 
distributed by hash(k1) properties("replication_num"="1");
+    """
+    sql """
+        create table decimalv2_cast_to_string_join_test_r(kk1 decimalv2(10, 
4)) distributed by hash(kk1) properties("replication_num"="1");
+    """
+    sql """
+        insert into decimalv2_cast_to_string_join_test_l values (1.123);
+    """
+    sql """
+        insert into decimalv2_cast_to_string_join_test_r values (1.123);
+    """
+    qt_join1 """
+        select * from decimalv2_cast_to_string_join_test_l, 
decimalv2_cast_to_string_join_test_r where k1 = kk1 order by 1, 2;
+    """
+    // scale is different, cast result will not equal
+    qt_cast_join1 """
+        select k1, cast(k1 as char(16)) k1cast, kk1, cast(kk1 as char(16)) 
kk1cast
+            from decimalv2_cast_to_string_join_test_l, 
decimalv2_cast_to_string_join_test_r
+        where cast(k1 as char(16)) = cast(kk1 as char(16)) order by 1, 2;
+    """
+    qt_cast_join2 """
+        select k1, cast(k1 as varchar) k1cast, kk1, cast(kk1 as varchar) 
kk1cast
+            from decimalv2_cast_to_string_join_test_l, 
decimalv2_cast_to_string_join_test_r
+        where cast(k1 as varchar) = cast(kk1 as varchar) order by 1, 2;
+    """
+
+    // restore disable_decimalv2 to old_value
+    sql """ ADMIN SET FRONTEND CONFIG ("disable_decimalv2" = "${old_value1}"); 
"""
+
+    // restore enable_decimal_conversion to old_value
+    sql """ ADMIN SET FRONTEND CONFIG ("enable_decimal_conversion" = 
"${old_value2}"); """
+}
\ No newline at end of file
diff --git 
a/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_cast_to_string.groovy
 
b/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_cast_to_string.groovy
new file mode 100644
index 00000000000..57d900d0d6d
--- /dev/null
+++ 
b/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_cast_to_string.groovy
@@ -0,0 +1,68 @@
+// 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_decimalv3_cast_to_string") {
+    sql """
+        drop table if exists decimalv3_cast_to_string_test;
+    """
+    sql """
+        create table decimalv3_cast_to_string_test (k1 decimalv3(38,6)) 
distributed by hash(k1) properties("replication_num"="1");
+    """
+    sql """
+        insert into decimalv3_cast_to_string_test values (null), (0), (1), 
(1.123), (0.1), (0.000001), (99999999999999999999999999999999), 
("99999999999999999999999999999999.000001"), 
("99999999999999999999999999999999.999999");
+    """
+    sql """
+        insert into decimalv3_cast_to_string_test values (-1), (-1.123), 
(-0.1), (-0.000001), (-99999999999999999999999999999999), 
("-99999999999999999999999999999999.000001"), 
("-99999999999999999999999999999999.999999");
+    """
+
+    qt_cast1 """
+        select k1, cast(k1 as varchar) from decimalv3_cast_to_string_test 
order by 1;
+    """
+
+    sql """
+        drop table if exists decimalv3_cast_to_string_join_test_l;
+    """
+    sql """
+        drop table if exists decimalv3_cast_to_string_join_test_r;
+    """
+    sql """
+        create table decimalv3_cast_to_string_join_test_l(k1 decimalv3(10, 3)) 
distributed by hash(k1) properties("replication_num"="1");
+    """
+    sql """
+        create table decimalv3_cast_to_string_join_test_r(kk1 decimalv3(10, 
4)) distributed by hash(kk1) properties("replication_num"="1");
+    """
+    sql """
+        insert into decimalv3_cast_to_string_join_test_l values (1.123);
+    """
+    sql """
+        insert into decimalv3_cast_to_string_join_test_r values (1.123);
+    """
+    qt_join1 """
+        select * from decimalv3_cast_to_string_join_test_l, 
decimalv3_cast_to_string_join_test_r where k1 = kk1 order by 1, 2;
+    """
+    // scale is different, cast result will not equal
+    qt_cast_join1 """
+        select k1, cast(k1 as char(16)) k1cast, kk1, cast(kk1 as char(16)) 
kk1cast
+            from decimalv3_cast_to_string_join_test_l, 
decimalv3_cast_to_string_join_test_r
+        where cast(k1 as char(16)) = cast(kk1 as char(16)) order by 1, 2;
+    """
+    qt_cast_join2 """
+        select k1, cast(k1 as varchar) k1cast, kk1, cast(kk1 as varchar) 
kk1cast
+            from decimalv3_cast_to_string_join_test_l, 
decimalv3_cast_to_string_join_test_r
+        where cast(k1 as varchar) = cast(kk1 as varchar) order by 1, 2;
+    """
+}
\ 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