You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: signed integer division uses DOUBLE and corrupts exact relational results #28580
-- MatrixOne 错误命中 id=2 和 id=3;MySQL 只命中 id=3SELECT id FROM src WHERE s/1=9007199254740993;
-- MatrixOne 合并为一个 count=2 的 group;MySQL 是两个 count=1 的 groupSELECT s/1AS q,COUNT(*)
FROM src WHERE id IN (2,3) GROUP BY q;
CREATETABLErhsASSELECT id,s FROM src WHERE id IN (2,3);
-- MatrixOne 产生 4 个匹配;MySQL 只有 (2,2)、(3,3)SELECTa.id,b.idFROM src a JOIN rhs b ONa.s/1=b.s/1WHEREa.idIN (2,3);
CREATETABLEdstASSELECT id,s/1AS q FROM src WHERE id IN (2,3);
CREATETABLEsrc(id INTPRIMARY KEY,s BIGINT,v INT);
INSERT INTO src VALUES
(1,9007199254740991,10),
(2,9007199254740992,20),
(3,9007199254740993,30),
(4,9007199254740994,40);
SELECT id,s,
RANK() OVER(ORDER BY s/1) AS r,
DENSE_RANK() OVER(ORDER BY s/1) AS dr
FROM src ORDER BY id;
SELECT id,s,
COUNT(*) OVER(
ORDER BY s/1 RANGE BETWEEN CURRENT ROW AND CURRENT ROW
) AS peers,
SUM(v) OVER(
ORDER BY s/1 RANGE BETWEEN CURRENT ROW AND CURRENT ROW
) AS peer_sum
FROM src ORDER BY id;
SELECT id,s,
COUNT(*) OVER(PARTITION BY s/1) AS members,
SUM(v) OVER(PARTITION BY s/1) AS member_sum
FROM src ORDER BY id;
RANGE CURRENT ROW 将两行作为 peers,peers=2、peer_sum=50;
PARTITION BY s/1 将两行放入同一 partition,members=2、member_sum=50。
MySQL 8.0.46 保持 DECIMAL 精度,四行 rank 分别为 1、2、3、4,每个 RANGE peer group 和 partition 都只有一行。
将排序/分区表达式显式改为 CAST(s AS UNSIGNED)/1 或 CAST(s AS DECIMAL(30,0))/1 后,MatrixOne 四行也能正确区分;ROWS BETWEEN CURRENT ROW AND CURRENT ROW 配合 ORDER BY s/1,id 仍逐行计算。以上窗口、控制组在 main@fc621e3616 连续执行 3 次一致。
Decision: keep the current arithmetic type-inference rules. We will not change integer / to DECIMAL merely to reproduce MySQL's results.
When an expression resolves to DOUBLE, floating-point rounding is part of its semantics. Adjacent BIGINT inputs above 2^53 can produce the same DOUBLE quotient. GROUP BY, JOIN, ordering, window peers/partitions, and CTAS must consistently consume that computed DOUBLE value—not the original integer or an ideal mathematical quotient. The examples here demonstrate the precision boundary of the chosen result type; they do not establish separate relational-execution bugs or data corruption.
Consistency means applying the resolved type's semantics across literals, columns, prepared execution, and relational operators. It does not require different input types, including signed and unsigned integers, to infer the same result type. We are retaining those existing promotion rules as well.
Applications requiring decimal precision should select an appropriate DECIMAL operand type and account for its scale and rounding. DECIMAL division is not universally exact either.
A bug would be inconsistent execution of the same resolved type, incorrect floating-point evaluation, or an optimization that changes these semantics. A difference from MySQL's choice of result type is not, by itself, a bug. The rounding shown in this report is accepted behavior.
For comparison, other databases deliberately choose different division contracts:
Database
Integer / integer
Value of 5 / 2
DECIMAL/NUMERIC / DECIMAL/NUMERIC
DuckDB
Floating-point division
2.5
Also floating-point division
Snowflake
Fixed-point NUMBER division
2.5, with a derived decimal scale
Fixed-point division with derived precision/scale and rounding
PostgreSQL
Integer division, truncating toward zero
2
NUMERIC division with decimal precision
Official references:
DuckDB explicitly defines integer / as floating-point division and provides // for integer division: numeric operators. It also explicitly uses approximate floating-point arithmetic for DECIMAL division: numeric types.
Snowflake's INTEGER/BIGINT names are aliases for NUMBER(38,0): numeric types. Its fixed-point division derives output scale from the numerator, normally adding six fractional digits for scale-zero operands, and rounds beyond the output scale: division rules.
This is a comparison of documented contracts, not a claim that we executed a new cross-database benchmark. Result display formatting may vary by client.
MO's choice of DOUBLE for signed-integer / is not intrinsically wrong. DuckDB provides a direct precedent; Snowflake and PostgreSQL demonstrate that there is no single cross-database result-type convention. MySQL's DECIMAL choice is not the correctness specification for every database.
For the reported BIGINT values, floating-point conversion can collapse adjacent integers above 2^53. Once the expression has a DOUBLE result, grouping equal results together, matching them in a join, treating them as window peers, and storing that DOUBLE through CTAS are consistent consequences of the chosen contract. Calling these consequences “data corruption” conflates a different arithmetic domain with a broken implementation.
Conversely, PostgreSQL preserves those large integers in s / 1, but its 5 / 2 is 2, not 2.5. Snowflake preserves the example values using fixed-point arithmetic, but still applies finite precision and rounding; DECIMAL is not an exact rational representation for expressions such as 1 / 3.
We will retain MO's current type rules and accept the rounding inherent in floating-point results. Different operand types may legitimately select different arithmetic domains. The correctness requirement is consistent evaluation of the resolved type across binding, execution, optimization, prepared statements, and relational operators—not identical results to MySQL. A demonstrated violation of that contract is actionable; the precision loss shown here, by itself, is not.
Activity
Ariznawlll commentedon Sep 10, 2026
补充窗口执行影响。
BIGINT / BIGINT进入 DOUBLE 后,相邻整数会被视为同一个 window peer / partition key,不只是普通 SELECT 的显示精度不同。MatrixOne 将
9007199254740992/1和9007199254740993/1都计算为9007199254740992.0,因此:RANK/DENSE_RANK都是2/2,后一行没有新 rank;RANGE CURRENT ROW将两行作为 peers,peers=2、peer_sum=50;PARTITION BY s/1将两行放入同一 partition,members=2、member_sum=50。MySQL 8.0.46 保持 DECIMAL 精度,四行 rank 分别为 1、2、3、4,每个 RANGE peer group 和 partition 都只有一行。
将排序/分区表达式显式改为
CAST(s AS UNSIGNED)/1或CAST(s AS DECIMAL(30,0))/1后,MatrixOne 四行也能正确区分;ROWS BETWEEN CURRENT ROW AND CURRENT ROW配合ORDER BY s/1,id仍逐行计算。以上窗口、控制组在main@fc621e3616连续执行 3 次一致。aunjgr commentedon Sep 15, 2026
Decision: keep the current arithmetic type-inference rules. We will not change integer
/to DECIMAL merely to reproduce MySQL's results.When an expression resolves to DOUBLE, floating-point rounding is part of its semantics. Adjacent BIGINT inputs above 2^53 can produce the same DOUBLE quotient. GROUP BY, JOIN, ordering, window peers/partitions, and CTAS must consistently consume that computed DOUBLE value—not the original integer or an ideal mathematical quotient. The examples here demonstrate the precision boundary of the chosen result type; they do not establish separate relational-execution bugs or data corruption.
Consistency means applying the resolved type's semantics across literals, columns, prepared execution, and relational operators. It does not require different input types, including signed and unsigned integers, to infer the same result type. We are retaining those existing promotion rules as well.
Applications requiring decimal precision should select an appropriate DECIMAL operand type and account for its scale and rounding. DECIMAL division is not universally exact either.
A bug would be inconsistent execution of the same resolved type, incorrect floating-point evaluation, or an optimization that changes these semantics. A difference from MySQL's choice of result type is not, by itself, a bug. The rounding shown in this report is accepted behavior.
aunjgr commentedon Sep 15, 2026
For comparison, other databases deliberately choose different division contracts:
/integer5 / 2/DECIMAL/NUMERICOfficial references:
/as floating-point division and provides//for integer division: numeric operators. It also explicitly uses approximate floating-point arithmetic for DECIMAL division: numeric types.NUMBER(38,0): numeric types. Its fixed-point division derives output scale from the numerator, normally adding six fractional digits for scale-zero operands, and rounds beyond the output scale: division rules.This is a comparison of documented contracts, not a claim that we executed a new cross-database benchmark. Result display formatting may vary by client.
MO's choice of DOUBLE for signed-integer
/is not intrinsically wrong. DuckDB provides a direct precedent; Snowflake and PostgreSQL demonstrate that there is no single cross-database result-type convention. MySQL's DECIMAL choice is not the correctness specification for every database.For the reported BIGINT values, floating-point conversion can collapse adjacent integers above 2^53. Once the expression has a DOUBLE result, grouping equal results together, matching them in a join, treating them as window peers, and storing that DOUBLE through CTAS are consistent consequences of the chosen contract. Calling these consequences “data corruption” conflates a different arithmetic domain with a broken implementation.
Conversely, PostgreSQL preserves those large integers in
s / 1, but its5 / 2is 2, not 2.5. Snowflake preserves the example values using fixed-point arithmetic, but still applies finite precision and rounding; DECIMAL is not an exact rational representation for expressions such as1 / 3.We will retain MO's current type rules and accept the rounding inherent in floating-point results. Different operand types may legitimately select different arithmetic domains. The correctness requirement is consistent evaluation of the resolved type across binding, execution, optimization, prepared statements, and relational operators—not identical results to MySQL. A demonstrated violation of that contract is actionable; the precision loss shown here, by itself, is not.
Ariznawlll commentedon Sep 18, 2026
关闭说明(按产品数值类型契约)
已复核研发结论:有符号整数
/当前解析为DOUBLE是 MatrixOne 明确保留的数值类型规则,不承诺 MySQL 的DECIMAL除法结果。2^53,DOUBLE的有限精度可能折叠相邻整数;由此产生的过滤、分组、排序、Join、窗口 peer 分组、VIEW 和 CTAS 结果,是在同一已解析DOUBLE数值域中一致执行的后果,不构成数据损坏。DECIMAL(p,s);需要截断或特定舍入规则时,也应显式表达该规则。DECIMAL、unsigned 或显式近似数值等其他操作数类型可以按各自规则选择不同数值域,不能反推 signed/signed/应采用 MySQL 的DECIMAL规则。以下仍属于独立且可追踪的正确性问题:同一已解析类型在 literal、PreparedStatement、优化、执行、过滤、分组、Join、VIEW 或 CTAS 间出现值/类型不一致;显式
DECIMAL或 unsigned 路径违反其自身类型规则;或发生 panic、会话异常、原子性破坏。该兼容性限制已记录在文档 PR:
研发产品决策:
据此关闭本 issue(won't fix / documented compatibility limitation)。