Skip to content

[Bug]: LAG/LEAD narrows defaults to the input type, truncating strings and rounding decimal values #29974

Closed

Description

@Ariznawlll

问题

LAG/LEAD 的返回类型只取第一个参数,并把第三个默认值强制转为该类型。合法的较长字符串默认值被截断;整数输入配小数默认值时小数被舍入。错误结果还会被 CTAS 持久化,View 也暴露过窄类型。

环境

官方 main 5a5501b3e1048f3d19d96824cc68a62b7f0fcb07,2026-10-12 固定基线;macOS arm64,本地源码构建单 CN/TN/LogService,etc/launch/launch.toml。对照 MySQL 8.0.46,Connector/J 8.3.0。

复现

CREATE TABLE wdefault(id INT PRIMARY KEY,s VARCHAR(10),v INT);
INSERT INTO wdefault VALUES(1,'a',100),(2,'b',10),(3,NULL,20);
SELECT id,
 LAG(s,1,'long default') OVER(ORDER BY id) lg,
 LEAD(s,1,'long default') OVER(ORDER BY id) ld,
 LAG(v,1,0.5) OVER(ORDER BY id) lv
FROM wdefault ORDER BY id;

MO 第一行 lg=long defau、lv=1,最后一行 ld=long defau。
MySQL 第一行 lg=long default、lv=0.5,最后一行 ld=long default。

CREATE TABLE wcopy AS
SELECT id,LAG(s,1,'long default') OVER(ORDER BY id) lg FROM wdefault;
SHOW COLUMNS FROM wcopy;
SELECT * FROM wcopy ORDER BY id;

MO lg 类型为 VARCHAR(10),已写入截断后的 long defau;MySQL 是 VARCHAR(12),完整保留 long default。

对照与稳定性

  • 最小查询连续 3 轮同样差异。更完整 fixture 含 UTF-8/NULL;LAG、LEAD、CTAS、View 均各 3 轮确认。
  • Connector/J client/server prepared 各 3 轮,默认参数在短字符串、长字符串、NULL、UTF-8 之间复用:长字符串同样被截断。
  • 对原值显式 CAST(s AS CHAR(32)) 后字符串完整;CAST(v AS DECIMAL(12,1)) 后默认值保持 0.5,两端一致。
  • 查询不修改源表;源表行数和值保持一致,会话后续查询正常。

代码定位

pkg/sql/plan/base_binder.go 的 case "lead", "lag" 将第三参强转为第一参类型;pkg/sql/plan/function/list_window.go 返回第一参类型,没有按输入与默认值推导可容纳两者的公共结果类型。

关联

已检查兼容性契约与 open/closed issue、PR;未发现本组合的现有单。#24402/#24424 修复的是 DECIMAL 默认值内部缩放错误;本单是强制窄化产生的数据丢失,不是该旧问题重复。#29925 是跨分区 NULL 默认值的错误,本单使用非 NULL 默认值且单分区。

本轮未添加正式用例;后续需要覆盖文本/二进制宽度、整数与小数公共类型、prepared 复用以及 CTAS/View 的值和元数据。

Activity

  1. added this to the MO-202610 milestone on Oct 12, 2026
  2. added theissue type on Oct 12, 2026
  3. added
    kind/bug
    on Oct 12, 2026
  4. added
    severity/s0
    and removed on Oct 17, 2026
  5. assigned and unassigned on Oct 17, 2026
  6. XuPeng-SH commented on Oct 17, 2026

    @XuPeng-SH
    Contributor

    Actual post-merge functional verification of #30115, with separate main and release results.

    main b111224b0c624773389b390cd3cf1223126b6ee2: reported behavior fixed in the executed scope. Linux/amd64, Go1.27.1, CPU/CGo test binaries built with the repository wrapper. An independent MySQL-protocol overlay replays the original table/rows/query and asserts:

    • first LAG string = long default, first decimal default = 0.5; existing 100/10 values remain exact;
    • CTAS retains full text and VARCHAR(12) metadata; View returns the decimal values;
    • prepared default reuse across long/NULL/UTF-8 values;
    • 65,536-byte MEDIUMTEXT survives mixed defaults and CTAS; BOOL defaults do not turn 42 into a boolean; numeric 0.5 and string '0.5' retain distinct window identities.

    Normal and race independent original-SQL runs pass all three selected issue subcases, including this one. Commands:

    .agents/skills/mo-dev/scripts/mo-cgo-test -p 1 -count=1 -short -tags matrixone_test -timeout=12m -json -overlay main-original-overlay.json -run '^TestAuditRound1450OriginalSQL$' ./pkg/tests/sqlintegration
    # Repeat with -race, same source and assertions.

    Additional owning-package verification: 13 complete owner packages, 40,594 pass events, 0 failures/11 explicit skips; complete SQL integration 54 roots/391 pass events, no failures/skips. The selected SQL/race consumer set passes 82 events. Window owning package passes all 532 events normal and race. This is not whole-repository/GPU certification.

    4.2-dev ac27db31696c0394ab1b2787b7b0a8f4933f088f, Go1.26.4: original public SQL still fails. The same independent source and expected rows run via an overlay in ./pkg/tests/issues:

    actual first row: 1 | long defau | b | 1
    expected:         1 | long default | b | 0.5
    

    The old clean main d00ece1f55fa696414bea2ac312d0d27e7967c8d is an executed negative control and reproduces the same narrowing. No product source or result assertion was edited for these probes.

    This remains a partial branch fix. Assignment events show ck89119 unassigned and slyunnn assigned at 2026-10-17T12:47:59Z. Returning to ck89119, the evidenced developer before QA, for the still-failing release scope; not guessing an owner or treating main-only repair as issue-wide completion.

    An initial race attempt failed before entering SQL due to audit processes using different temporary-directory port registries. Those raw logs are retained as environment failures. The passing normal/race evidence above uses one shared runtime lease directory; no skips/retries were added to product or test assertions. MySQL reference server/GPU/live mixed-binary rollout were not executed.

  7. assigned and unassigned on Oct 18, 2026
  8. ck89119 commented on Oct 18, 2026

    @ck89119
    Contributor

    no need to backport to 4.2-dev

  9. XuPeng-SH commented on Oct 19, 2026

    @XuPeng-SH
    Contributor

    补充本轮更高边界的 main 公共 SQL 验证,保持现有测试负责人 slyunnn。

    2026-10-19,本轮新构建 native CGO,main完整SHA 2ad1d7e5c2baafdee0b8fa097ce02ca32feb3a7b,Linux/amd64、Go1.27.1,真实两 CN/MySQL TCP。LAG/LEAD 的长默认值、decimal 0.5、CTAS varchar(12) metadata、VIEW、prepared 短→长→Unicode→短复用均通过;额外用65536字节 MEDIUMTEXT 与 JSON 默认值验证 fallback CTAS,在 strict 和 permissive sql_mode 下均保留完整内容,无截断。

    TestAudit21NumericWindowContracts 的此子场景两个 CN 都PASS,同命令的 NTILE 输入/prepare复用和浮点 BIT_COUNT 对照也PASS,整体exit0。原始严格断言与日志:pr-audit-continue21/public_numeric_test.go、numeric-overlay.json、logs/public-numeric.{log,exit}。该证据属于本轮main,不把历史4.2或作者说明当本轮结果。

  10. slyunnn commented on Oct 21, 2026

    @slyunnn

    Regression and extended verification for #29974 (2026-10-21)

    Verification Environment

    • Freshly fetched MatrixOne origin/main: e47f45ca6548e198e73ecb242521047bfd449850 — fix(python-sdk): redact create_table diagnostics and gate the success log (#30678).
    • Clean source built with make build TYPECHECK=1, Go 1.27.1, Linux/arm64. The running binary reports e47f45ca65.
    • Full isolated local topology: 2 CN / 1 TN / 1 Log service / 1 Proxy. Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002 were connected and used for actual original and extended queries.
    • Clients: MOTR/PyMySQL text protocol; Go database/sql + go-sql-driver/mysql with interpolateParams=false; Connector/J 8.3.0 with verified ClientPreparedStatement and ServerPreparedStatement modes. MySQL 8.0.46 was used for the Java original-query, metadata and prepared-reuse control.

    What Was Covered

    • OK — Original three-row query: first LAG default is the complete long default, numeric default remains 0.5, and the final LEAD default is untruncated. Existing values and source NULL remain correct.
    • OK — CTAS and direct-expression View values and metadata: VARCHAR(12) and DECIMAL(11,1); JDBC string width, numeric precision and scale were asserted.
    • OK — LAG / LEAD binary prepared reuse with short, long, NULL, UTF-8 and short-again defaults; decimal defaults changed between positive, negative, NULL and larger fractional values. Binary defaults preserve NUL and FF bytes.
    • OK — Zero and out-of-partition offsets, empty input, same-type decimal and Boolean controls, unchanged source rows and reusable connections.
    • OK — 65,535 / 65,536-byte MEDIUMTEXT with JSON fallback survives CTAS in strict and permissive SQL modes with full content equality.
    • OK — A common DECIMAL domain exceeding precision 65 is rejected with 1690; failed CTAS leaves no catalog table.
    • OK — Native protocol checks passed 37/37 per endpoint, 111/111 overall. Connector/J passed 114/114 checks on MatrixOne and 38/38 against MySQL.
    • OK — Isolated endpoint fixtures ran concurrently. SQL and protocol cases use separate databases and passed MOTR -j 2; this is test scheduling, not an engine-parallel performance claim. Cleanup left no issue databases.
    • Not covered — Full window BVT group, production/TKE deployment and mixed-version upgrade. The developer explicitly states that a 4.2-dev backport is not required; that branch is outside this acceptance scope.

    Findings

    Related fix: #30115, merge commit 728c6863579518bff9e0967bc2cddb0ef319a83e — fix(sql): reconcile LAG/LEAD default values into a common result type with the value argument.

    The original narrowing no longer reproduces on the tested main. Values and defaults are reconciled into a common result type rather than narrowing defaults to the input column type. No new product failure was observed in the executed scope.

    MOTR / BVT Coverage

    • Independent branch: test/issue-29974-lag-lead-default-type.
    • Source commit: 61b25424d98c918cfe0c2cc613d19bfa0df80216, pushed to MOTR over SSH; remote commit and branch tracking verified.
    • SQL/result: suites/14_issue_regression/{t,r}/issue_29974_lag_lead_default_type.{test,result}; native prepared checks are in the companion Go protocol script and launcher.
    • Final SQL recording passed 19/19 statements. Final parallel replay passed 2/2 cases, including after cherry-pick into daily PR #251, commit fa226d5a8b494d7575f66edce8a860e4e6895c65.
    • Existing window_lag_lead_default_type.sql coverage was inspected. The entire product BVT group was not rerun. The Java compatibility helper was executed locally; the committed MOTR additions contain SQL and Go coverage. MatrixOne service source is unchanged.

    Final Conclusion

    Fixed on the tested latest main, within the developer-confirmed main-only acceptance scope.

    Large-row-count regression is not required: this is a common-type and result-width contract. Three source rows plus explicit 65,535 / 65,536-byte boundaries cover the relevant states without a larger dataset or long-running load test.

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