Skip to content

Commit

Permalink
[round](decimalv2) round decimalv2 to precision value (apache#22138)
Browse files Browse the repository at this point in the history
* [round](decimalv2) round decimalv2 to precision value

* update

* update`
  • Loading branch information
Gabriel39 authored Jul 24, 2023
1 parent 752cec9 commit a0463ea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions be/src/vec/functions/round.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class DecimalRoundingImpl {
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);
Expand All @@ -160,15 +161,15 @@ class DecimalRoundingImpl {
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;
}
Expand Down
9 changes: 9 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,10 @@
[['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'], ''],
Expand All @@ -1255,20 +1259,25 @@
[['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'], ''],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,15 @@
-- !query --
0.000 0.000 0.000

-- !query --
16.03

-- !query --
16.02

-- !query --
16.030000000

-- !query --
16.020000000

Original file line number Diff line number Diff line change
Expand Up @@ -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"); """
}

0 comments on commit a0463ea

Please sign in to comment.