Skip to content

[Bug]: signed integer division uses DOUBLE and corrupts exact relational results #28580

Closed

Description

@Ariznawlll

现象

MatrixOne main(fc621e3616d229c7a29d0c80e73ed8eb1997459c)将两个有符号整数的 / 运算推导为 DOUBLE。当值超过 2^53 后,不同的 BIGINT 会折叠为同一个浮点值,不仅 SELECT 数值不准确,还会改变过滤、分组、排序、Join 和 CTAS 的结果。

CREATE TABLE src(id INT PRIMARY KEY, s BIGINT, KEY k_s(s));
INSERT INTO src VALUES
  (1,9007199254740991),
  (2,9007199254740992),
  (3,9007199254740993),
  (4,9223372036854775807);

SELECT id,s/1,s/3 FROM src ORDER BY id;

MatrixOne 中,id=2 和 id=3 的 s/1 都是 9007199254740992.0,s/3 都是 3002399751580330.5。MySQL 8.0.46 使用 DECIMAL 精确运算,保留四个不同整数,并且 id=3 的 s/3 为 3002399751580331.0000。

该类型推导会进一步造成关系结果错误:

-- MatrixOne 错误命中 id=2 和 id=3;MySQL 只命中 id=3
SELECT id FROM src WHERE s/1=9007199254740993;

-- MatrixOne 合并为一个 count=2 的 group;MySQL 是两个 count=1 的 group
SELECT s/1 AS q,COUNT(*)
FROM src WHERE id IN (2,3) GROUP BY q;

CREATE TABLE rhs AS SELECT id,s FROM src WHERE id IN (2,3);
-- MatrixOne 产生 4 个匹配;MySQL 只有 (2,2)、(3,3)
SELECT a.id,b.id
FROM src a JOIN rhs b ON a.s/1=b.s/1
WHERE a.id IN (2,3);

CREATE TABLE dst AS
SELECT id,s/1 AS q FROM src WHERE id IN (2,3);

MatrixOne 的 dst.q 是 DOUBLE,两行均被持久化成 9007199254740992.0;MySQL 的 dst.q 是 DECIMAL(23,4),两行仍可区分。VIEW 元数据也分别为 DOUBLE 与 DECIMAL(23,4)。

覆盖结果还表明:

  • TINYINT/SMALLINT/INT/BIGINT 的 signed/signed / 均被推导为 DOUBLE;
  • BIGINT UNSIGNED / 整数 与 DECIMAL / 整数 在 MatrixOne 中走 DECIMAL 路径,能够保持精度;
  • 显式 DOUBLE 的控制组与 MatrixOne 当前 signed/signed 结果相同;
  • SUM(BIGINT)/整数 因 SUM 已返回 DECIMAL,能够保持精度;
  • 字面量与 PreparedStatement 都受 signed/signed 浮点化影响,因此并非只发生在协议参数路径。

期望

精确数值类型之间的 / 应使用能够表示输入整数域的 DECIMAL 结果,不能在没有显式近似数值操作数时转为 DOUBLE。过滤、分组、排序、Join、VIEW 和 CTAS 必须基于未丢失的精确商。

代码定位

  • pkg/sql/plan/function/type_check.go 的 fixedBinaryCastRule2 将 signed integer / signed integer 组合映射到 FLOAT64。
  • pkg/sql/plan/function/arithmetic.go 的 divFn 因此进入 T_float64 分支并执行近似除法。
  • 同一张 coercion 表对 UINT64/DECIMAL 组合选择 DECIMAL128,解释了相邻控制组精确而 signed/signed 路径丢失精度的差异。

复现信息

  • MatrixOne main:fc621e3616d229c7a29d0c80e73ed8eb1997459c
  • MySQL:8.0.46
  • 整数宽度、2^53±1、INT64_MAX、signed/unsigned/decimal/double、PreparedStatement、WHERE、GROUP BY、ORDER BY、JOIN、VIEW、CTAS 和聚合控制组重复执行 3 次,结果一致

Activity

  1. added this to the MO-202609 milestone on Sep 10, 2026
  2. added
    kind/bug
    on Sep 10, 2026
  3. added theissue type on Sep 10, 2026
  4. Ariznawlll commented on Sep 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充窗口执行影响。BIGINT / BIGINT 进入 DOUBLE 后,相邻整数会被视为同一个 window peer / partition key,不只是普通 SELECT 的显示精度不同。

    CREATE TABLE src(id INT PRIMARY 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;

    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 次一致。

  5. added
    severity/s0
    and removed on Sep 10, 2026
  6. aunjgr commented on Sep 15, 2026

    @aunjgr
    Contributor

    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.

  7. aunjgr commented on Sep 15, 2026

    @aunjgr
    Contributor

    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.
    • PostgreSQL integer division truncates toward zero; NUMERIC operands select decimal arithmetic instead: mathematical operators.

    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.

  8. assigned and unassigned on Sep 15, 2026
  9. Ariznawlll commented on Sep 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    关闭说明(按产品数值类型契约)

    已复核研发结论:有符号整数 / 当前解析为 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)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions