Skip to content

Commit

Permalink
[fix](decimal) round result of decimal divide
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktengg committed May 22, 2024
1 parent bb407ee commit cf1b955
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 89 deletions.
7 changes: 6 additions & 1 deletion be/src/vec/functions/divide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ struct DivideFloatingImpl {
template <typename Result = ResultType>
static inline Result apply(A a, B b, UInt8& is_null) {
is_null = b == 0;
return static_cast<Result>(a) / (b + is_null);
if constexpr ((std::is_integral_v<A> && std::is_integral_v<B>) ||
(std::is_same_v<A, wide::Int256> && std::is_same_v<B, wide::Int256>)) {
return static_cast<Result>(a + ((a ^ b) > 0 ? b / 2 : -b / 2)) / (b + is_null);
} else {
return static_cast<Result>(a) / (b + is_null);
}
}
};

Expand Down
186 changes: 98 additions & 88 deletions regression-test/data/datatype_p0/decimalv2/test_decimalv2_calc.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,122 +12,132 @@

-- !select_calc1 --
\N \N \N \N \N \N
1.10000 1.10000 2.20000 0.00000 1.2100000000 1.000000000
1.10000 1.10000 2.20000 0.00000 1.2100000000 1.000000000
1.20000 1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.30000 1.30000 2.60000 0.00000 1.6900000000 1.000000000
2.10000 2.10000 4.20000 0.00000 4.4100000000 1.000000000
2.10000 2.10000 4.20000 0.00000 4.4100000000 1.000000000
2.20000 2.20000 4.40000 0.00000 4.8400000000 1.000000000
1.10000 1.10000 2.200000000 0E-9 1.210000000 1.000000000
1.10000 1.10000 2.200000000 0E-9 1.210000000 1.000000000
1.20000 1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.30000 1.30000 2.600000000 0E-9 1.690000000 1.000000000
2.10000 2.10000 4.200000000 0E-9 4.410000000 1.000000000
2.10000 2.10000 4.200000000 0E-9 4.410000000 1.000000000
2.20000 2.20000 4.400000000 0E-9 4.840000000 1.000000000

-- !select_calc2 --
\N \N \N \N \N \N \N \N \N \N
1.10000 1.20000 2.30000 -0.10000 1.3200000000 0.916666666 2.30000 0.10000 1.3200000000 1.090909090
1.10000 1.40000 2.50000 -0.30000 1.5400000000 0.785714285 2.50000 0.30000 1.5400000000 1.272727272
1.20000 1.20000 2.40000 0.00000 1.4400000000 1.000000000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 1.20000 2.40000 0.00000 1.4400000000 1.000000000 2.40000 0.00000 1.4400000000 1.000000000
1.30000 1.20000 2.50000 0.10000 1.5600000000 1.083333333 2.50000 -0.10000 1.5600000000 0.923076923
2.10000 1.20000 3.30000 0.90000 2.5200000000 1.750000000 3.30000 -0.90000 2.5200000000 0.571428571
2.10000 1.20000 3.30000 0.90000 2.5200000000 1.750000000 3.30000 -0.90000 2.5200000000 0.571428571
2.20000 1.40000 3.60000 0.80000 3.0800000000 1.571428571 3.60000 -0.80000 3.0800000000 0.636363636
1.10000 1.20000 2.300000000 -0.100000000 1.320000000 0.916666667 2.300000000 0.100000000 1.320000000 1.090909091
1.10000 1.40000 2.500000000 -0.300000000 1.540000000 0.785714286 2.500000000 0.300000000 1.540000000 1.272727273
1.20000 1.20000 2.400000000 0E-9 1.440000000 1.000000000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 1.20000 2.400000000 0E-9 1.440000000 1.000000000 2.400000000 0E-9 1.440000000 1.000000000
1.30000 1.20000 2.500000000 0.100000000 1.560000000 1.083333333 2.500000000 -0.100000000 1.560000000 0.923076923
2.10000 1.20000 3.300000000 0.900000000 2.520000000 1.750000000 3.300000000 -0.900000000 2.520000000 0.571428571
2.10000 1.20000 3.300000000 0.900000000 2.520000000 1.750000000 3.300000000 -0.900000000 2.520000000 0.571428571
2.20000 1.40000 3.600000000 0.800000000 3.080000000 1.571428571 3.600000000 -0.800000000 3.080000000 0.636363636

-- !select_calc3 --
\N \N \N \N \N \N \N \N \N \N
1.10000 1.50000 2.60000 -0.40000 1.6500000000 0.733333333 2.60000 0.40000 1.6500000000 1.363636363
1.10000 1.56000 2.66000 -0.46000 1.7160000000 0.705128205 2.66000 0.46000 1.7160000000 1.418181818
1.20000 1.34400 2.54400 -0.14400 1.6128000000 0.892857142 2.54400 0.14400 1.6128000000 1.120000000
1.20000 1.66000 2.86000 -0.46000 1.9920000000 0.722891566 2.86000 0.46000 1.9920000000 1.383333333
1.30000 1.45600 2.75600 -0.15600 1.8928000000 0.892857142 2.75600 0.15600 1.8928000000 1.120000000
2.10000 1.30000 3.40000 0.80000 2.7300000000 1.615384615 3.40000 -0.80000 2.7300000000 0.619047619
2.10000 1.30000 3.40000 0.80000 2.7300000000 1.615384615 3.40000 -0.80000 2.7300000000 0.619047619
2.20000 1.21000 3.41000 0.99000 2.6620000000 1.818181818 3.41000 -0.99000 2.6620000000 0.550000000
1.10000 1.50000 2.600000000 -0.400000000 1.650000000 0.733333333 2.600000000 0.400000000 1.650000000 1.363636364
1.10000 1.56000 2.660000000 -0.460000000 1.716000000 0.705128205 2.660000000 0.460000000 1.716000000 1.418181818
1.20000 1.34400 2.544000000 -0.144000000 1.612800000 0.892857143 2.544000000 0.144000000 1.612800000 1.120000000
1.20000 1.66000 2.860000000 -0.460000000 1.992000000 0.722891566 2.860000000 0.460000000 1.992000000 1.383333333
1.30000 1.45600 2.756000000 -0.156000000 1.892800000 0.892857143 2.756000000 0.156000000 1.892800000 1.120000000
2.10000 1.30000 3.400000000 0.800000000 2.730000000 1.615384615 3.400000000 -0.800000000 2.730000000 0.619047619
2.10000 1.30000 3.400000000 0.800000000 2.730000000 1.615384615 3.400000000 -0.800000000 2.730000000 0.619047619
2.20000 1.21000 3.410000000 0.990000000 2.662000000 1.818181818 3.410000000 -0.990000000 2.662000000 0.550000000

-- !select_calc4 --
\N \N \N \N \N \N \N \N \N \N
1.10000 1.23400 2.33400 -0.13400 1.3574000000 0.891410048 2.33400 0.13400 1.3574000000 1.121818181
1.10000 1.60000 2.70000 -0.50000 1.7600000000 0.687500000 2.70000 0.50000 1.7600000000 1.454545454
1.20000 1.32432 2.52432 -0.12432 1.5891840000 0.906125407 2.52432 0.12432 1.5891840000 1.103600000
1.20000 1.53465 2.73465 -0.33465 1.8415800000 0.781937249 2.73465 0.33465 1.8415800000 1.278875000
1.30000 1.34544 2.64544 -0.04544 1.7490720000 0.966226661 2.64544 0.04544 1.7490720000 1.034953846
2.10000 1.23400 3.33400 0.86600 2.5914000000 1.701782820 3.33400 -0.86600 2.5914000000 0.587619047
2.10000 1.23420 3.33420 0.86580 2.5918200000 1.701507049 3.33420 -0.86580 2.5918200000 0.587714285
2.20000 1.12300 3.32300 1.07700 2.4706000000 1.959038290 3.32300 -1.07700 2.4706000000 0.510454545
1.10000 1.23400 2.334000000 -0.134000000 1.357400000 0.891410049 2.334000000 0.134000000 1.357400000 1.121818182
1.10000 1.60000 2.700000000 -0.500000000 1.760000000 0.687500000 2.700000000 0.500000000 1.760000000 1.454545455
1.20000 1.32432 2.524320000 -0.124320000 1.589184000 0.906125408 2.524320000 0.124320000 1.589184000 1.103600000
1.20000 1.53465 2.734650000 -0.334650000 1.841580000 0.781937250 2.734650000 0.334650000 1.841580000 1.278875000
1.30000 1.34544 2.645440000 -0.045440000 1.749072000 0.966226662 2.645440000 0.045440000 1.749072000 1.034953846
2.10000 1.23400 3.334000000 0.866000000 2.591400000 1.701782820 3.334000000 -0.866000000 2.591400000 0.587619048
2.10000 1.23420 3.334200000 0.865800000 2.591820000 1.701507049 3.334200000 -0.865800000 2.591820000 0.587714286
2.20000 1.12300 3.323000000 1.077000000 2.470600000 1.959038290 3.323000000 -1.077000000 2.470600000 0.510454545

-- !select_calc5 --
\N \N \N \N \N
1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.20000 2.40000 0.00000 1.4400000000 1.000000000
1.40000 2.80000 0.00000 1.9600000000 1.000000000
1.40000 2.80000 0.00000 1.9600000000 1.000000000
1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.20000 2.400000000 0E-9 1.440000000 1.000000000
1.40000 2.800000000 0E-9 1.960000000 1.000000000
1.40000 2.800000000 0E-9 1.960000000 1.000000000

-- !select_calc6 --
\N \N \N \N \N \N \N \N \N \N
1.20000 1.30000 2.50000 -0.10000 1.5600000000 0.972289742 2.50000 0.10000 1.5600000000 1.083333333
1.20000 1.30000 2.50000 -0.10000 1.5600000000 0.972447325 2.50000 0.10000 1.5600000000 1.083333333
1.20000 1.34400 2.54400 -0.14400 1.6128000000 0.906125407 2.54400 0.14400 1.6128000000 1.120000000
1.20000 1.45600 2.65600 -0.25600 1.7472000000 0.891901534 2.65600 0.25600 1.7472000000 1.213333333
1.20000 1.56000 2.76000 -0.36000 1.8720000000 0.972447325 2.76000 0.36000 1.8720000000 1.300000000
1.20000 1.66000 2.86000 -0.46000 1.9920000000 0.781937249 2.86000 0.46000 1.9920000000 1.383333333
1.40000 1.21000 2.61000 0.19000 1.6940000000 1.246660730 2.61000 -0.19000 1.6940000000 0.864285714
1.40000 1.50000 2.90000 -0.10000 2.1000000000 0.875000000 2.90000 0.10000 2.1000000000 1.071428571
1.20000 1.30000 2.500000000 -0.100000000 1.560000000 0.972289742 2.500000000 0.100000000 1.560000000 1.083333333
1.20000 1.30000 2.500000000 -0.100000000 1.560000000 0.972447326 2.500000000 0.100000000 1.560000000 1.083333333
1.20000 1.34400 2.544000000 -0.144000000 1.612800000 0.906125408 2.544000000 0.144000000 1.612800000 1.120000000
1.20000 1.45600 2.656000000 -0.256000000 1.747200000 0.891901534 2.656000000 0.256000000 1.747200000 1.213333333
1.20000 1.56000 2.760000000 -0.360000000 1.872000000 0.972447326 2.760000000 0.360000000 1.872000000 1.300000000
1.20000 1.66000 2.860000000 -0.460000000 1.992000000 0.781937250 2.860000000 0.460000000 1.992000000 1.383333333
1.40000 1.21000 2.610000000 0.190000000 1.694000000 1.246660730 2.610000000 -0.190000000 1.694000000 0.864285714
1.40000 1.50000 2.900000000 -0.100000000 2.100000000 0.875000000 2.900000000 0.100000000 2.100000000 1.071428571

-- !select_calc7 --
\N \N \N \N \N \N \N \N \N \N
1.20000 1.23400 2.43400 -0.03400 1.4808000000 0.972447325 2.43400 0.03400 1.4808000000 1.028333333
1.20000 1.23400 2.43400 -0.03400 1.4808000000 0.972447325 2.43400 0.03400 1.4808000000 1.028333333
1.20000 1.23420 2.43420 -0.03420 1.4810400000 0.972289742 2.43420 0.03420 1.4810400000 1.028500000
1.20000 1.32432 2.52432 -0.12432 1.5891840000 0.906125407 2.52432 0.12432 1.5891840000 1.103600000
1.20000 1.34544 2.54544 -0.14544 1.6145280000 0.891901534 2.54544 0.14544 1.6145280000 1.121200000
1.20000 1.53465 2.73465 -0.33465 1.8415800000 0.781937249 2.73465 0.33465 1.8415800000 1.278875000
1.40000 1.12300 2.52300 0.27700 1.5722000000 1.246660730 2.52300 -0.27700 1.5722000000 0.802142857
1.40000 1.60000 3.00000 -0.20000 2.2400000000 0.875000000 3.00000 0.20000 2.2400000000 1.142857142
1.20000 1.23400 2.434000000 -0.034000000 1.480800000 0.972447326 2.434000000 0.034000000 1.480800000 1.028333333
1.20000 1.23400 2.434000000 -0.034000000 1.480800000 0.972447326 2.434000000 0.034000000 1.480800000 1.028333333
1.20000 1.23420 2.434200000 -0.034200000 1.481040000 0.972289742 2.434200000 0.034200000 1.481040000 1.028500000
1.20000 1.32432 2.524320000 -0.124320000 1.589184000 0.906125408 2.524320000 0.124320000 1.589184000 1.103600000
1.20000 1.34544 2.545440000 -0.145440000 1.614528000 0.891901534 2.545440000 0.145440000 1.614528000 1.121200000
1.20000 1.53465 2.734650000 -0.334650000 1.841580000 0.781937250 2.734650000 0.334650000 1.841580000 1.278875000
1.40000 1.12300 2.523000000 0.277000000 1.572200000 1.246660730 2.523000000 -0.277000000 1.572200000 0.802142857
1.40000 1.60000 3.000000000 -0.200000000 2.240000000 0.875000000 3.000000000 0.200000000 2.240000000 1.142857143

-- !select_calc8 --
\N \N \N \N \N
1.21000 2.42000 0.00000 1.4641000000 1.000000000
1.30000 2.60000 0.00000 1.6900000000 1.000000000
1.30000 2.60000 0.00000 1.6900000000 1.000000000
1.34400 2.68800 0.00000 1.8063360000 1.000000000
1.45600 2.91200 0.00000 2.1199360000 1.000000000
1.50000 3.00000 0.00000 2.2500000000 1.000000000
1.56000 3.12000 0.00000 2.4336000000 1.000000000
1.66000 3.32000 0.00000 2.7556000000 1.000000000
1.21000 2.420000000 0E-9 1.464100000 1.000000000
1.30000 2.600000000 0E-9 1.690000000 1.000000000
1.30000 2.600000000 0E-9 1.690000000 1.000000000
1.34400 2.688000000 0E-9 1.806336000 1.000000000
1.45600 2.912000000 0E-9 2.119936000 1.000000000
1.50000 3.000000000 0E-9 2.250000000 1.000000000
1.56000 3.120000000 0E-9 2.433600000 1.000000000
1.66000 3.320000000 0E-9 2.755600000 1.000000000

-- !select_calc9 --
\N \N \N \N \N \N \N \N \N \N
1.21000 1.12300 2.33300 0.08700 1.3588300000 1.077471059 2.33300 -0.08700 1.3588300000 0.928099173
1.30000 1.23400 2.53400 0.06600 1.6042000000 1.053484602 2.53400 -0.06600 1.6042000000 0.949230769
1.30000 1.23420 2.53420 0.06580 1.6044600000 1.053313887 2.53420 -0.06580 1.6044600000 0.949384615
1.34400 1.32432 2.66832 0.01968 1.7798860800 1.014860456 2.66832 -0.01968 1.7798860800 0.985357142
1.45600 1.34544 2.80144 0.11056 1.9589606400 1.082173861 2.80144 -0.11056 1.9589606400 0.924065934
1.50000 1.60000 3.10000 -0.10000 2.4000000000 0.937500000 3.10000 0.10000 2.4000000000 1.066666666
1.56000 1.23400 2.79400 0.32600 1.9250400000 1.264181523 2.79400 -0.32600 1.9250400000 0.791025641
1.66000 1.53465 3.19465 0.12535 2.5475190000 1.081679861 3.19465 -0.12535 2.5475190000 0.924487951
1.21000 1.12300 2.333000000 0.087000000 1.358830000 1.077471060 2.333000000 -0.087000000 1.358830000 0.928099174
1.30000 1.23400 2.534000000 0.066000000 1.604200000 1.053484603 2.534000000 -0.066000000 1.604200000 0.949230769
1.30000 1.23420 2.534200000 0.065800000 1.604460000 1.053313888 2.534200000 -0.065800000 1.604460000 0.949384615
1.34400 1.32432 2.668320000 0.019680000 1.779886080 1.014860457 2.668320000 -0.019680000 1.779886080 0.985357143
1.45600 1.34544 2.801440000 0.110560000 1.958960640 1.082173861 2.801440000 -0.110560000 1.958960640 0.924065934
1.50000 1.60000 3.100000000 -0.100000000 2.400000000 0.937500000 3.100000000 0.100000000 2.400000000 1.066666667
1.56000 1.23400 2.794000000 0.326000000 1.925040000 1.264181524 2.794000000 -0.326000000 1.925040000 0.791025641
1.66000 1.53465 3.194650000 0.125350000 2.547519000 1.081679862 3.194650000 -0.125350000 2.547519000 0.924487952

-- !select_calc10 --
\N \N \N \N \N
1.12300 2.24600 0.00000 1.2611290000 1.000000000
1.23400 2.46800 0.00000 1.5227560000 1.000000000
1.23400 2.46800 0.00000 1.5227560000 1.000000000
1.23420 2.46840 0.00000 1.5232496400 1.000000000
1.32432 2.64864 0.00000 1.7538234624 1.000000000
1.34544 2.69088 0.00000 1.8102087936 1.000000000
1.53465 3.06930 0.00000 2.3551506225 1.000000000
1.60000 3.20000 0.00000 2.5600000000 1.000000000
1.12300 2.246000000 0E-9 1.261129000 1.000000000
1.23400 2.468000000 0E-9 1.522756000 1.000000000
1.23400 2.468000000 0E-9 1.522756000 1.000000000
1.23420 2.468400000 0E-9 1.523249640 1.000000000
1.32432 2.648640000 0E-9 1.753823463 1.000000000
1.34544 2.690880000 0E-9 1.810208794 1.000000000
1.53465 3.069300000 0E-9 2.355150623 1.000000000
1.60000 3.200000000 0E-9 2.560000000 1.000000000

-- !calc_overflow --
\N \N \N \N
1.21000 1.12300 1209.9999879000 1.122999887700
1.30000 1.23400 1299.9999870000 1.233999876600
1.30000 1.23420 1299.9999870000 1.234199876580
1.34400 1.32432 1343.9999865600 1.324319867568
1.45600 1.34544 1455.9999854400 1.345439865456
1.50000 1.60000 1499.9999850000 1.599999840000
1.56000 1.23400 1559.9999844000 1.233999876600
1.66000 1.53465 1659.9999834000 1.534649846535
1.21000 1.12300 1209.999987900 1.122999888
1.30000 1.23400 1299.999987000 1.233999877
1.30000 1.23420 1299.999987000 1.234199877
1.34400 1.32432 1343.999986560 1.324319868
1.45600 1.34544 1455.999985440 1.345439865
1.50000 1.60000 1499.999985000 1.599999840
1.56000 1.23400 1559.999984400 1.233999877
1.66000 1.53465 1659.999983400 1.534649847

-- !div_round --
\N 3.000000000 \N
-2.000000000 -3.000000000 0.666666667
-2.000000000 0E-9 \N
-2.000000000 3.000000000 -0.666666667
2.000000000 \N \N
2.000000000 -3.000000000 -0.666666667
2.000000000 0E-9 \N
2.000000000 3.000000000 0.666666667

Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,83 @@
76891560.464178000 5 0.464178000 0.464178000
277170831.851350000 7 0.851350000 0.851350000

-- !div_round1 --
\N 3.000 \N
-2.000 -3.000 0.6666667
-2.000 0.000 \N
-2.000 3.000 -0.6666667
2.000 \N \N
2.000 -3.000 -0.6666667
2.000 0.000 \N
2.000 3.000 0.6666667

-- !div_round2 --
\N 3.000 \N
-2.000 -3.000 0.6666667
-2.000 0.000 \N
-2.000 3.000 -0.6666667
2.000 \N \N
2.000 -3.000 -0.6666667
2.000 0.000 \N
2.000 3.000 0.6666667

-- !div_round3 --
\N 3.000000000 \N
-2.000000000 -3.000000000 0.6666666667
-2.000000000 0E-9 \N
-2.000000000 3.000000000 -0.6666666667
2.000000000 \N \N
2.000000000 -3.000000000 -0.6666666667
2.000000000 0E-9 \N
2.000000000 3.000000000 0.6666666667

-- !div_round4 --
\N 3.000000000 \N
-2.000000000 -3.000000000 0.6666666666667
-2.000000000 0E-9 \N
-2.000000000 3.000000000 -0.6666666666667
2.000000000 \N \N
2.000000000 -3.000000000 -0.6666666666667
2.000000000 0E-9 \N
2.000000000 3.000000000 0.6666666666667

-- !div_round5 --
\N 3 \N
-2 -3 0.6666666666666666
-2 0 \N
-2 3 -0.6666666666666666
2 \N \N
2 -3 -0.6666666666666666
2 0 \N
2 3 0.6666666666666666

-- !div_round6 --
\N 3 \N
-2 -3 0.6667
-2 0 \N
-2 3 -0.6667
2 \N \N
2 -3 -0.6667
2 0 \N
2 3 0.6667

-- !div_round7 --
\N 3 \N
-2 -3 0.6666666666666666
-2 0 \N
-2 3 -0.6666666666666666
2 \N \N
2 -3 -0.6666666666666666
2 0 \N
2 3 0.6666666666666666

-- !div_round8 --
\N 3 \N
-2 -3 0.6667
-2 0 \N
-2 3 -0.6667
2 \N \N
2 -3 -0.6667
2 0 \N
2 3 0.6667

Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ suite("test_decimalv2_calc", "nonConcurrent") {
from ${table1} order by 1, 2, 3, 4;
"""

sql """
drop table if exists decimalv2_div_round;
"""
sql """
create table decimalv2_div_round(k1 decimalv2(27,9), k2 decimalv2(27,9)) distributed by hash(k1) properties("replication_num"="1");
"""
sql """
insert into decimalv2_div_round values (2, 3), (2, -3), (-2, 3), (-2, -3), (2, 0), (-2, 0), (null, 3), (2, null);
"""
qt_div_round """
select k1, k2, k1 / k2 from decimalv2_div_round order by 1, 2;
"""

sql """
admin set frontend config("enable_decimal_conversion" = "true");
"""
Expand Down
Loading

0 comments on commit cf1b955

Please sign in to comment.