Skip to content

[Bug]: numeric RANGE window frame bound wider than the ORDER BY key's integer type errors instead of computing #30563

Closed

Description

@iamlinjunhong

Branch Name: main

Commit ID: 3fc0070

Environment:

  • MatrixOne 8.0.30-MatrixOne-v1.3.0 (main @ 3fc0070, 2026-10-18), macOS arm64 (Darwin), single standalone mo-service started from the repo's etc/launch configuration with all ports shifted by LOOM_TEST_PORT_OFFSET (frontend 27001), queried with the mysql client; no other config change.
  • Reference oracle: MySQL 8.0.46 (Homebrew [email protected]), fresh local instance on port 33306, same machine, identical SQL verbatim.

Actual Behavior:
For a window with a numeric RANGE frame whose ORDER BY expression is a signed integer column narrower than BIGINT (TINYINT / SMALLINT / INT), any constant frame bound that does not fit the ORDER BY key's own integer type aborts the whole query at plan time with:

ERROR 1690 (22003): data out of range: data type int8, value '200'

The plan binder narrows the frame-bound literal into the ORDER BY key's storage type and constant-folds that cast; the out-of-range cast raises moerr.ErrOutOfRange before execution ever starts. Verified failures:

  • TINYINT key + RANGE BETWEEN 200 PRECEDING AND CURRENT ROW -> ERROR 1690, "data type int8, value '200'"
  • TINYINT key + RANGE BETWEEN CURRENT ROW AND 200 FOLLOWING -> ERROR 1690, int8
  • TINYINT key + RANGE BETWEEN 100 PRECEDING AND 200 FOLLOWING -> ERROR 1690, int8 (the 100 fits, the 200 does not)
  • SMALLINT key + RANGE BETWEEN 70000 PRECEDING AND CURRENT ROW -> ERROR 1690, "data type int16, value '70000'"
  • INT key + RANGE BETWEEN 2147483648 PRECEDING AND CURRENT ROW -> ERROR 1690, "data type int32, value '2147483648'"
  • INT key + RANGE BETWEEN 9223372036854775807 PRECEDING AND CURRENT ROW -> ERROR 1690, "data type int32, value '9223372036854775807'"

COUNT, SUM and any window aggregate over such a frame are affected alike; it is not specific to one function.

Expected Behavior:
MySQL 8.0.46 (verified on the same machine, same SQL) evaluates numeric RANGE frame bounds in wider arithmetic (BIGINT domain), so k - 200 on TINYINT keys falls below every TINYINT and the frame degenerates into the full partition prefix (resp. suffix for FOLLOWING), with no error and no warnings:

  • tinyint k=1,2,3 + 200 PRECEDING: COUNT = 1,2,3 and SUM(v) = 10,30,60
  • tinyint k=1,2,3 + CURRENT ROW AND 200 FOLLOWING: COUNT = 3,2,1
  • tinyint k=1,2,3 + 100 PRECEDING AND 200 FOLLOWING: COUNT = 3,3,3
  • smallint + 70000 PRECEDING: COUNT = 1,2,3
  • int (with a duplicated id=2 peer) + 2147483648 PRECEDING: COUNT = 1,3,3,4,5,6
  • int + 9223372036854775807 PRECEDING: COUNT = 1,3,3,4,5,6

The expected fix follows the same principle PR #27578 applied to runtime extrema: numeric RANGE bounds must be compared in a domain wide enough for key +/- bound (e.g. int64/decimal), never narrowed into the ORDER BY key's storage type; out-of-domain start/end bounds then map to before/after-domain insertion points.

Steps to Reproduce:
Run the SQL from the repro field on both engines (six window queries; TINYINT/SMALLINT/INT keys with bounds 200, 70000, 2147483648, 9223372036854775807). MatrixOne fails each window query with ERROR 1690 (22003) before returning any rows; MySQL 8.0.46 returns the frame results listed above. The behavior is deterministic across runs and restarts.

Additional information:
Root cause chain (main @ 3fc0070):

  1. pkg/sql/plan/window_binder.go:995 - for RANGE frames the frame-bound target type is chosen as the ORDER BY expression's own type (typ = &w.OrderBy[0].Expr.Typ). The ROWS branch just above instead binds as T_uint64 (window_binder.go:987), which is why huge ROWS offsets such as 9223372036854775807 ROWS PRECEDING work fine.
  2. pkg/sql/plan/window_binder.go:1028 and :1041 - the start/end bound expressions are bound through makeFrameConstValue(..., typ).
  3. pkg/sql/plan/window_binder.go:1312 - makeWindowFrameConstValue inserts a narrowing cast via appendCastBeforeExpr(bindCtx, e, *typ), turning e.g. an int64 literal 200 into an int8 cast.
  4. pkg/sql/plan/window_binder.go:1317-1324 - the cast is constant-folded immediately (NewExpressionExecutor + Eval on EmptyForConstFoldBatch); casting 200 -> int8 raises moerr.ErrOutOfRange, which reaches the client as MySQL error 1690 ("data out of range: data type %s, %s", pkg/common/moerr/error.go:469).

No existing test or BVT codifies this rejection: test/distributed/cases/window/window_frame_order.sql covers inverted frames and INTERVAL-typed bounds on numeric ordering, but nothing pins numeric bounds that exceed the ORDER BY key's integer domain.

Verified unaffected / consistent (same session, same data):

  • In-domain bounds are identical (tinyint + 100 PRECEDING -> 1,2,3 on both engines).
  • BIGINT key + 9223372036854775807 PRECEDING works on both (the bound fits int64).
  • ROWS frames with huge offsets work on both (uint64 bound path).
  • DECIMAL(10,0) and FLOAT keys + 2147483648 bounds work on MatrixOne (wider/float domains, matches MySQL).
  • INT UNSIGNED keys are out of scope: MySQL itself rejects 2147483648 PRECEDING there with its own 1690 ("BIGINT UNSIGNED value is out of range in '(k - 2147483648)'"), so the engines' unsigned domains are separate.

Prior art checked, none covers this defect:

Workaround (verified on MatrixOne): widen the ORDER BY key, e.g. ORDER BY CAST(k AS BIGINT) RANGE BETWEEN 200 PRECEDING AND CURRENT ROW, or use a ROWS frame. Queries that MySQL runs keep failing without it, so severity is major rather than critical.

Suggested regression coverage: TINYINT/SMALLINT/INT keys x {PRECEDING, FOLLOWING} x bounds {type_max+1, 2^31, INT64_MAX} compared against MySQL, plus in-domain and BIGINT-key controls.

Full reproduction, exactly as Loom ran it
-- Both engines run this verbatim. MatrixOne fails every window query below at
-- plan time with ERROR 1690 (22003); MySQL 8.0.46 returns the frames.

CREATE DATABASE loomwin;
USE loomwin;

CREATE TABLE ts1 (k TINYINT, v INT);
INSERT INTO ts1 VALUES (1,10),(2,20),(3,30);

-- 1. tinyint key, 200 PRECEDING (200 does not fit int8)
SELECT k,
       COUNT(*) OVER (ORDER BY k RANGE BETWEEN 200 PRECEDING AND CURRENT ROW) AS c,
       SUM(v)    OVER (ORDER BY k RANGE BETWEEN 200 PRECEDING AND CURRENT ROW) AS s
FROM ts1 ORDER BY k;

-- 2. tinyint key, CURRENT ROW AND 200 FOLLOWING
SELECT k,
       COUNT(*) OVER (ORDER BY k RANGE BETWEEN CURRENT ROW AND 200 FOLLOWING) AS c
FROM ts1 ORDER BY k;

-- 3. tinyint key, 100 PRECEDING AND 200 FOLLOWING (100 fits int8, 200 does not)
SELECT k,
       COUNT(*) OVER (ORDER BY k RANGE BETWEEN 100 PRECEDING AND 200 FOLLOWING) AS c
FROM ts1 ORDER BY k;

-- 4. smallint key, 70000 PRECEDING (does not fit int16)
CREATE TABLE ss1 (k SMALLINT, v INT);
INSERT INTO ss1 VALUES (1,10),(2,20),(3,30);
SELECT k,
       COUNT(*) OVER (ORDER BY k RANGE BETWEEN 70000 PRECEDING AND CURRENT ROW) AS c
FROM ss1 ORDER BY k;

-- 5. int key, 2147483648 PRECEDING (int32 max + 1)
CREATE TABLE ipv (id INT, v INT);
INSERT INTO ipv VALUES (1,10),(2,20),(2,21),(3,30),(4,40),(5,50);
SELECT id,
       COUNT(*) OVER (ORDER BY id RANGE BETWEEN 2147483648 PRECEDING AND CURRENT ROW) AS c
FROM ipv ORDER BY id;

-- 6. int key, 9223372036854775807 PRECEDING (int64 max)
SELECT id,
       COUNT(*) OVER (ORDER BY id RANGE BETWEEN 9223372036854775807 PRECEDING AND CURRENT ROW) AS c
FROM ipv ORDER BY id;

-- Controls that behave identically on both engines:
-- a. in-domain bound: tinyint + 100 PRECEDING -> c = 1,2,3 on both
-- b. bigint key + 9223372036854775807 PRECEDING -> prefix frames on both
-- c. ROWS frame + 9223372036854775807 PRECEDING -> prefix frames on both
-- d. workaround: ORDER BY CAST(k AS BIGINT) + 200 PRECEDING -> c = 1,2,3 on MO too

Found by Loom bug hunt (module: custom-691c28503e; tests: unit, black-box, end-to-end)

Activity

  1. added
    kind/bug
    on Oct 19, 2026
  2. added
    ai-easy
    on Oct 25, 2026
  3. Ariznawlll commented on Oct 26, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main

    • commit:5fe08a3307e246e1ab9951ab690b50557af0f509;隔离构建的 mo-service 构建信息为此完整 SHA 且 vcs.modified=false

    • 部署方式:macOS arm64;独立数据目录,1 LogService、1 TN、2 CN(26101/26102)及本地 MatrixOne Proxy(26001),无共享 TKE

    • 测试日期:2026-10-26

    • 对应研发修复:PR #30973 已 MERGED,merge commit 在上述官方 main SHA 祖先链中。

    原问题验证

    原始 TINYINT 键 200 PRECEDING 的 COUNT 为 1/2/3、SUM 为 10/30/60;200 FOLLOWING 为 3/2/1;100 PRECEDING AND 200 FOLLOWING 为 3/3/3。SMALLINT+70000 为 1/2/3;INT 含重复键 2,2147483648 与 9223372036854775807 两种 PRECEDING 的 COUNT 都是 1/3/3/4/5/6。三组新库 3/3,MOTR 重复后 9/9,均不再报 1690。

    Happy Path 覆盖

    1. TINYINT 域内 100 PRECEDING 和 ORDER BY CAST(k AS BIGINT) 对照均为 1/2/3。2. COUNT/SUM 与 peer 计数精确断言。

    Unhappy Path 覆盖

    1. 超过 TINYINT/SMALLINT/INT 列域的合法正数 frame bound 未被错误地窄化;本问题没有无效输入验收要求。

    白盒验证

    本次以真实 MySQL 协议和 SQL 产品路径验收;未额外运行 Go UT/-race,不以代码审查代替运行结果。

    回归测试

    已加入 MOTR #278 的 script/14_issue_regression/issue_20261026_recent_assigned_sql.py 和配对 .out。本地 MOTR 定向 --repeat 3 成功 3/3;每次脚本在三组独立数据库重复本 issue 断言,因此本 issue 共 9/9。完整 MOTR suite 与远端 CI 未运行,不能声称全套通过。

    测试证据

    MOTR #278 的回归脚本;定向运行目录 /private/tmp/mo-qa-new-20261026-ROza73/motr-278/var/log/run-1792982597562506000;脚本以精确值/行数断言,失败即退出非零。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions