[round](decimalv2) round decimalv2 to precision value (#22138)

* [round](decimalv2) round decimalv2 to precision value

* update

* update`
This commit is contained in:
Gabriel
2023-07-25 03:29:48 +08:00
committed by GitHub
parent 752cec9e19
commit a0463ea047
4 changed files with 36 additions and 3 deletions

View File

@ -151,6 +151,7 @@ private:
public:
static NO_INLINE void apply(const Container& in, UInt32 in_scale, Container& out,
Int16 out_scale) {
constexpr bool is_decimalv2 = IsDecimalV2<T>;
Int16 scale_arg = in_scale - out_scale;
if (scale_arg > 0) {
size_t scale = int_exp10(scale_arg);
@ -160,15 +161,15 @@ public:
NativeType* __restrict p_out = reinterpret_cast<NativeType*>(out.data());
if (out_scale < 0) {
size_t target_scale = int_exp10(-out_scale);
while (p_in < end_in) {
Op::compute(p_in, scale, p_out, target_scale);
Op::compute(p_in, scale, p_out,
is_decimalv2 ? int_exp10(9 - out_scale) : int_exp10(-out_scale));
++p_in;
++p_out;
}
} else {
while (p_in < end_in) {
Op::compute(p_in, scale, p_out, 1);
Op::compute(p_in, scale, p_out, is_decimalv2 ? scale : 1);
++p_in;
++p_out;
}

View File

@ -1242,6 +1242,10 @@ visible_functions = {
[['floor', 'dfloor'], 'DOUBLE', ['DOUBLE'], ''],
[['round', 'dround'], 'DOUBLE', ['DOUBLE'], ''],
[['round_bankers'], 'DOUBLE', ['DOUBLE'], ''],
[['ceil', 'ceiling', 'dceil'], 'DECIMALV2', ['DECIMALV2'], ''],
[['floor', 'dfloor'], 'DECIMALV2', ['DECIMALV2'], ''],
[['round', 'dround'], 'DECIMALV2', ['DECIMALV2'], ''],
[['round_bankers'], 'DECIMALV2', ['DECIMALV2'], ''],
[['ceil', 'ceiling', 'dceil'], 'DECIMAL32', ['DECIMAL32'], ''],
[['floor', 'dfloor'], 'DECIMAL32', ['DECIMAL32'], ''],
[['round', 'dround'], 'DECIMAL32', ['DECIMAL32'], ''],
@ -1255,20 +1259,25 @@ visible_functions = {
[['round', 'dround'], 'DECIMAL128', ['DECIMAL128'], ''],
[['round_bankers'], 'DECIMAL128', ['DECIMAL128'], ''],
[['round', 'dround'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['round', 'dround'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['round', 'dround'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['round', 'dround'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['round', 'dround'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['round_bankers', 'round_bankers'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['round_bankers', 'round_bankers'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['round_bankers'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['round_bankers'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['round_bankers'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['floor', 'dfloor'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['ceil', 'dceil'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],
[['truncate'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
[['truncate'], 'DECIMALV2', ['DECIMALV2', 'INT'], ''],
[['truncate'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
[['truncate'], 'DECIMAL64', ['DECIMAL64', 'INT'], ''],
[['truncate'], 'DECIMAL128', ['DECIMAL128', 'INT'], ''],

View File

@ -116,3 +116,15 @@
-- !query --
0.000 0.000 0.000
-- !query --
16.03
-- !query --
16.02
-- !query --
16.030000000
-- !query --
16.020000000

View File

@ -139,4 +139,15 @@
qt_query """ select cast(round(sum(d1), -2) as decimalv3(27, 3)), cast(round(sum(d2), -2) as decimalv3(27, 3)), cast(round(sum(d3), -2) as decimalv3(27, 3)) from ${tableName3} """
qt_query """ select cast(round(sum(d1), -4) as decimalv3(27, 3)), cast(round(sum(d2), -4) as decimalv3(27, 3)), cast(round(sum(d3), -4) as decimalv3(27, 3)) from ${tableName3} """
sql """ ADMIN SET FRONTEND CONFIG ("enable_decimal_conversion" = "false"); """
sql """ ADMIN SET FRONTEND CONFIG ("disable_decimalv2" = "false"); """
sql """ set experimental_enable_nereids_planner=false; """
sql """ DROP TABLE IF EXISTS `test_decimalv2` """
sql """ CREATE TABLE `test_decimalv2` ( id int, decimal_col DECIMAL(19,5)) ENGINE=OLAP duplicate KEY (id) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ( "replication_allocation" = "tag.location.default: 1"); """
sql """ insert into test_decimalv2 values (1, 16.025); """
qt_query """ select round(decimal_col,2) from test_decimalv2; """
qt_query """ select truncate(decimal_col,2) from test_decimalv2; """
qt_query """ select ceil(decimal_col,2) from test_decimalv2; """
qt_query """ select floor(decimal_col,2) from test_decimalv2; """
sql """ ADMIN SET FRONTEND CONFIG ("enable_decimal_conversion" = "true"); """
}