Skip to content

Commit 1bcf63a

Browse files
Fix(MCOL-4611): mod loses precision on huge narrow decimal (#3473)
1 parent 848f5bf commit 1bcf63a

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
DROP DATABASE IF EXISTS mcol_4611;
2+
CREATE DATABASE mcol_4611;
3+
USE mcol_4611;
4+
DROP TABLE IF EXISTS t1;
5+
CREATE TABLE t1 (a DECIMAL(18,0)) ENGINE=ColumnStore;
6+
INSERT INTO t1 VALUES (999999999999999999);
7+
SELECT a MOD 10000 FROM t1;
8+
a MOD 10000
9+
9999
10+
DROP TABLE IF EXISTS t1;
11+
CREATE TABLE t1 (a DECIMAL(18,0)) ENGINE=ColumnStore;
12+
INSERT INTO t1 VALUES (999999999999999999);
13+
SELECT RAND(9999), RAND(a MOD 10000) FROM t1;
14+
RAND(9999) RAND(a MOD 10000)
15+
0.7361292641015065 0.7361292641015065
16+
DROP TABLE IF EXISTS t1;
17+
DROP DATABASE mcol_4611;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- source ../include/have_columnstore.inc
2+
--disable_warnings
3+
DROP DATABASE IF EXISTS mcol_4611;
4+
--enable_warnings
5+
CREATE DATABASE mcol_4611;
6+
USE mcol_4611;
7+
8+
--disable_warnings
9+
DROP TABLE IF EXISTS t1;
10+
--enable_warnings
11+
CREATE TABLE t1 (a DECIMAL(18,0)) ENGINE=ColumnStore;
12+
INSERT INTO t1 VALUES (999999999999999999);
13+
SELECT a MOD 10000 FROM t1;
14+
15+
--disable_warnings
16+
DROP TABLE IF EXISTS t1;
17+
--enable_warnings
18+
CREATE TABLE t1 (a DECIMAL(18,0)) ENGINE=ColumnStore;
19+
INSERT INTO t1 VALUES (999999999999999999);
20+
SELECT RAND(9999), RAND(a MOD 10000) FROM t1;
21+
22+
--disable_warnings
23+
DROP TABLE IF EXISTS t1;
24+
--enable_warnings
25+
26+
--disable_warnings
27+
DROP DATABASE mcol_4611;
28+
--enable_warnings

utils/funcexp/func_mod.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ IDB_Decimal Func_mod::getDecimalVal(Row& row, FunctionParm& parm, bool& isNull,
106106
}
107107

108108
IDB_Decimal d = parm[0]->data()->getDecimalVal(row, isNull);
109-
int64_t value = d.value / pow(10.0, d.scale);
109+
int64_t value = d.value / static_cast<int64_t>(pow(10.0, d.scale));
110110
int lefto = d.value % (int)pow(10.0, d.scale);
111111

112112
int64_t mod = (value % div) * pow(10.0, d.scale) + lefto;

utils/funcexp/functor_real.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ class Func_mod : public Func_Real
381381
return static_cast<ModType>(integralRemainder.toTFloat128() + intAndFract.second);
382382
}
383383
}
384-
int64_t value = d.value / pow(10.0, d.scale);
384+
int64_t value = d.value / static_cast<int64_t>(pow(10.0, d.scale));
385385
return value % div;
386386
}
387387
};

0 commit comments

Comments
 (0)