Skip to content

[Bug]: signed BIGINT Window RANGE bounds wrap at INT64 extrema and return empty frames #27574

Closed

Description

@Lundomn

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

249a458ba567b26d1818fb0496b449135cb05ef2

This was the current GitHub main head immediately before submission. The
runtime reproducer was also validated on PR #27568 head 131ca9c; that PR
changes the uint64 / BIT path, while the signed integer path remains
unchanged.

Other Environment Information

- Hardware parameters: local development machine
- OS type: macOS arm64
- Deployment: local standalone MatrixOne, direct CN SQL endpoint
- Client: MySQL text protocol at 127.0.0.1:16001
- Server version: 8.0.30-MatrixOne-v1.3.0
- Executable: mo-service-fix-27565-131ca9c (PR #27568 head)

Actual Behavior

For a signed BIGINT ORDER BY key, a finite Window RANGE bound silently
wraps when it crosses INT64_MAX or INT64_MIN. The wrapped search key is
placed at the opposite end of the signed domain, so a frame that must contain
its current row becomes empty.

Both directions are affected:

  • RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING at
    9223372036854775807 returns COUNT(*) = 0, SUM(v) = NULL.
  • RANGE BETWEEN 1 PRECEDING AND CURRENT ROW at
    -9223372036854775808 returns COUNT(*) = 0, SUM(v) = NULL.

The minimal queries reproduced identically in three consecutive runs.

A 500,010-row confirmation used 23,810 independent partitions of 21 rows.
Each partition contained one INT64_MIN key, nineteen zero keys, and one
INT64_MAX key. An independent reference found:

INT64_MAX, CURRENT ROW ... 1 FOLLOWING:     23,810 wrong rows
INT64_MIN, 1 PRECEDING ... CURRENT ROW:     23,810 wrong rows
ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING:        0 wrong rows

Two complete scale repetitions returned identical counts. Zero-offset
RANGE, interior signed keys, and ROWS controls return correct results.

This is different from #27565 / PR #27568, which cover BIGINT UNSIGNED and
BIT. It is also different from #27350, which concerns temporal Window bounds
that fail with error 1690. This signed defect reproduces on the #27568 PR-head
executable itself.

Expected Behavior

A finite Window RANGE frame includes its current row. A mathematical upper
bound above INT64_MAX is beyond every key in an ascending partition, and a
lower bound below INT64_MIN is before every key. Neither bound should wrap
to the opposite endpoint.

For both boundary rows, the expected result is:

COUNT(*) = 1
SUM(v)   = current row's v

Steps to Reproduce

CREATE DATABASE mo_i64_range_overflow_repro;
USE mo_i64_range_overflow_repro;

CREATE TABLE t (
  id INT PRIMARY KEY,
  k BIGINT NOT NULL,
  v INT NOT NULL
);

INSERT INTO t VALUES
  (1, -9223372036854775808, 10),
  (2, 0, 20),
  (3, 9223372036854775807, 30);

SELECT id, k,
       COUNT(*) OVER (
         ORDER BY k
         RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING
       ) AS c,
       SUM(v) OVER (
         ORDER BY k
         RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING
       ) AS s
FROM t
ORDER BY id;

SELECT id, k,
       COUNT(*) OVER (
         ORDER BY k
         RANGE BETWEEN 1 PRECEDING AND CURRENT ROW
       ) AS c,
       SUM(v) OVER (
         ORDER BY k
         RANGE BETWEEN 1 PRECEDING AND CURRENT ROW
       ) AS s
FROM t
ORDER BY id;

Actual results at the affected rows:

FOLLOWING query:
id  k                    c  s
3   9223372036854775807  0  NULL

PRECEDING query:
id  k                     c  s
1   -9223372036854775808  0  NULL

Possible Root Cause

pkg/sql/colexec/window/window.go, in the signed integer branches of
searchLeft and searchRight, computes frame search keys with unchecked
fixed-width arithmetic:

col[rowIdx] + c
col[rowIdx] - c

At signed extrema these operations wrap before genericSearchLeft /
genericSearchRight runs. Current GitHub main still contains this arithmetic
in the int8, int16, int32, and int64 branches.

The out-of-domain bound should map to the correct before-domain or after-domain
insertion boundary, with ASC/DESC handled explicitly, rather than manufacturing
a wrapped key. PR #27568's explicit out-of-domain handling for uint64 is a
close structural precedent.

Suggested regression coverage:

  • signed integer minima/maxima for T_int8, T_int16, T_int32, and T_int64
  • ASC and DESC
  • PRECEDING and FOLLOWING
  • zero and nonzero offsets
  • NULL peers and multi-row peer groups
  • COUNT, SUM, and empty-frame-sensitive aggregates

Activity

  1. self-assigned this
    on Aug 25, 2026
  2. added a commit that references this issue on Aug 25, 2026
    bc6c842
  3. reopened this on Sep 7, 2026
  4. Lundomn commented on Sep 7, 2026

    @Lundomn
    ContributorAuthor

    QA 转测(2026-09-07)

    • 修复 PR:PR #27578(已 merged)
    • 根因与修复范围:Window RANGE 边界在 TINYINT/SMALLINT/INT/BIGINT 极值处发生有符号算术回绕,导致 ASC/DESC 的越界搜索边界错误。
    • 开发侧验证:window 单测、race、go vet、colexec 测试通过;包覆盖率 84.2%,变更语句 52/52;BVT 重复 3 次及 500,010 行边界回归重复 2 次通过。
    • QA 请重点覆盖:各有符号整数类型、PRECEDING/FOLLOWING、最小/最大值、溢出/下溢、ASC/DESC、空 frame 与 NULL peer。

    请在本 Issue 记录实际测试版本/环境、通过或失败结论、未覆盖项及补测要求。按流程,QA 明确通过后再关闭 Issue;如发现失败或回归请保持 open 并反馈复现信息。

  5. Ariznawlll commented on Sep 7, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • commit:bcd5ed833b5400fe95f322e4028adb2bde3de0a4
    • 部署方式:本地 standalone MatrixOne,MySQL 协议端口 6001
    • 测试日期:2026-09-07

    原问题验证

    1. 创建 signed BIGINT 主键表,插入 -9223372036854775808、0、9223372036854775807 三个边界值。
    2. 执行 RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING,再执行 RANGE BETWEEN 1 PRECEDING AND CURRENT ROW。
    3. 连续执行 3 轮,核对每个边界行的窗口计数/求和,并清理数据库。

    Happy Path 覆盖

    1. 两种方向的窗口帧均分别返回计数 1、求和 10/20/30;最小值和最大值未发生溢出或空帧。
    2. 边界行、0 值和普通值的结果均保持一致,连接健康检查通过。

    Unhappy Path 覆盖

    1. signed BIGINT 两端极值作为 RANGE 运算边界执行 3/3 轮,未观察到 wrap、错误空结果或 panic。
    2. 每轮完成数据库清理,无残留表或会话异常。

    白盒验证

    不适用。本轮验证公开窗口 SQL 语义,未使用内部故障注入。

    回归测试

    已加入 MOTR PR #145:suites/14_issue_regression/t/issue_27574_signed_bigint_range.test。

    测试证据

    测试结论

    测试通过。

  6. added 3 commits that reference this issue on Sep 17, 2026
    5dce691
    35768bb
    ea1de2f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions