Skip to content

[Compatibility]: VAR_* and STDDEV_* reject MySQL numeric-convertible character and temporal operands #28229

Closed

Description

@Ariznawlll

问题

MatrixOne 的 VAR_POP、VAR_SAMP、STDDEV_POP、STDDEV_SAMP(以及别名 VARIANCE、STD、STDDEV)只接受原生数值类型。VARCHAR、DATE、DATETIME 列在函数绑定阶段被拒绝,因此相同 SQL 无法从 MySQL 迁移。

MySQL 8.3 会按其数值转换规则处理这些类型:数值字符串转为对应数值,日期和日期时间转为数字形式后计算。该行为同时适用于普通聚合、窗口聚合和 prepared statement。

环境

  • MatrixOne: official main
  • commit: e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • comparison: MySQL 8.3

复现

create database stat_agg_type_repro;
use stat_agg_type_repro;

create table t(
  id int primary key,
  s varchar(20),
  d date,
  ts datetime
);

insert into t values
  (1, '1', '2024-01-01', '2024-01-01 01:02:03'),
  (2, '2', '2024-01-02', '2024-01-02 02:03:04'),
  (3, '3', '2024-01-03', '2024-01-03 03:04:05');

select var_pop(s), var_samp(s), stddev_pop(s), stddev_samp(s) from t;
select var_pop(d), var_samp(d), stddev_pop(d), stddev_samp(d) from t;
select var_pop(ts), var_samp(ts), stddev_pop(ts), stddev_samp(ts) from t;

select id,
       var_pop(s) over(order by id rows between unbounded preceding and current row)
from t order by id;

prepare p from 'select var_pop(s), stddev_samp(s) from t';
execute p;

MatrixOne 对前三组查询分别返回:

invalid argument aggregate function var_pop, bad value [VARCHAR]
invalid argument aggregate function var_pop, bad value [DATE]
invalid argument aggregate function var_pop, bad value [DATETIME]

窗口查询以及 PREPARE 也在绑定阶段返回相同类型错误。

MySQL 8.3 的数值字符串查询结果为:

VAR_POP(s) = 0.6666666666666666
VAR_SAMP(s) = 1
STDDEV_POP(s) = 0.816496580927726
STDDEV_SAMP(s) = 1

日期、日期时间、窗口和 prepared statement 查询也正常执行,并遵循 MySQL 的隐式数值转换规则。

代码定位

pkg/sql/plan/function/list_agg.go 中四个统计聚合均使用 SumSupportedTypes 做固定类型检查;字符和时间类型在这里被拒绝,没有进入 MySQL 兼容的数值转换路径。

期望

这些统计聚合对 MySQL 可数值转换的字符和时间类型采用与 MySQL 一致的转换规则,普通聚合、窗口聚合和 prepared statement 的行为保持一致。

以上最小场景在两个引擎上均重复执行 3 次,结果稳定。

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. added
    severity/s0
    and removed on Sep 5, 2026
  6. jiangxinmeng1 commented on Sep 14, 2026

    @jiangxinmeng1
    Contributor

    fixed by #28509

  7. slyunnn commented on Sep 14, 2026

    @slyunnn

    Regression and extended verification for #28229 (2026-09-14)

    Verification Environment

    • MatrixOne: latest local `origin/main` at `be30a70fe50eac4e8c78e5117fdd69179ba853e0` — `fix(agg): support decimal256 variance aggregates (fix(agg): support decimal256 variance aggregates #28841)`.
    • Associated fix: PR #28509, `fix: coerce temporal and string variance operands`; merge commit `fffcacc813c9e3b30afb64f18e96e7b21e7ca71e`.
    • Topology: one Log Service, one TN, two CN services, and one Proxy, all from the locally built latest `origin/main` runtime.
    • Endpoints: Proxy `127.0.0.1:6001`, direct CN1 `127.0.0.1:16001`, and direct CN2 `127.0.0.1:16002`.
    • Client protocol: MySQL text protocol through the `mysql` CLI and MOTR; SQL `PREPARE` / `EXECUTE` was exercised on the real frontend path.
    • Compatibility reference: MySQL `8.3.0` in a temporary localhost-only Docker container.

    What Was Covered

    • OK — The original ordinary-aggregate reproduction for `VARCHAR`, `DATE`, and `DATETIME` completed without the prior binding error for `VAR_POP`, `VAR_SAMP`, `STDDEV_POP`, or `STDDEV_SAMP`.
    • OK — `VARCHAR` results matched MySQL `8.3.0`: `VAR_POP=0.6666666666666666`, `VAR_SAMP=1`, `STDDEV_POP=0.816496580927726`, and `STDDEV_SAMP=1`.
    • OK — Happy paths cover all four functions plus `VARIANCE`, `STD`, and `STDDEV` aliases on numeric strings.
    • OK — Temporal coercion covers `DATE`, `TIME`, `DATETIME`, and `TIMESTAMP`.
    • OK — Window aggregation covers running `VAR_POP` on `VARCHAR` and `STDDEV_SAMP` on `DATETIME`.
    • OK — SQL `PREPARE` / `EXECUTE` covers string `VAR_POP` and timestamp `STDDEV_SAMP`.
    • OK — Boundary and unhappy paths cover empty input, `NULL`-only input, a singleton, and a non-numeric string; no binding failure occurred and the numeric-conversion results match MySQL.
    • OK — Native `INT` remains a control equivalent to numeric-string input.
    • OK — The recorded MOTR case was replayed through Proxy `6001`, CN1 `16001`, and CN2 `16002`.
    • Not covered — MySQL binary prepared-statement protocol (`COM_STMT_PREPARE` / `COM_STMT_EXECUTE`) was not added because the issue is resolved at SQL function binding and the SQL `PREPARE` / `EXECUTE` frontend path directly exercises that contract.
    • Not covered — No additional client drivers or server versions beyond MySQL `8.3.0` were used.

    Findings

    The previous binding-time rejection of MySQL numeric-convertible string and temporal operands is resolved in the latest `main`. Ordinary aggregates, windows, and SQL prepared execution now reach the numeric-coercion path.

    MySQL `8.3.0` comparison matched all stable assertions. For the selected large `DATETIME` / `TIMESTAMP` `VAR_POP` input, MatrixOne and MySQL differ only in an insignificant final floating-point display digit before rounding (`...666624` versus `...666600` at `DECIMAL(24,6)`); the MOTR temporal `VAR_POP` assertion is therefore stabilized at `DECIMAL(24,3)`, while exact deterministic window assertions retain `6` decimals. This is not the original binding/coercion failure.

    No MatrixOne service source was modified.

    MOTR / BVT Coverage

    Added `suites/14_issue_regression/t/issue_28229_variance_coercion.test` and `suites/14_issue_regression/r/issue_28229_variance_coercion.result`.

    • Recording through Proxy `6001`: `21/21` statements passed.
    • Normal replay through Proxy `6001`: `21/21` statements passed.
    • Repeated replay through Proxy `6001`: `42/42` statements passed.
    • Direct replay through CN1 `16001`: `21/21` statements passed.
    • Direct replay through CN2 `16002`: `21/21` statements passed.
    • Focused MatrixOne test: `./.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s ./pkg/sql/plan/function` passed.
    • Independent MOTR branch: `test/issue-28229-variance-coercion`, commit `05daa8d4cafe974d692c226eff39a9227fade22c`.
    • Daily aggregation: cherry-picked as `9682687f9abaffe410d2e85a1e0d699fc265532b` into MOTR PR #160. No issue-specific PR was created.

    Final Conclusion

    Fixed. The original MySQL-compatibility binding regression is fixed and the regression coverage is recorded, replayed, pushed, and included in the designated daily MOTR PR.

    A large-data regression is not required: this is deterministic per-value binding and coercion behavior, independent of row count, block/batch boundaries, memory or spill state, concurrency, skew, network transfer, and resource scale.

  8. slyunnn commented on Sep 14, 2026

    @slyunnn

    Follow-up precision compatibility issue filed: #28852.\n\n#28229 remains fixed for the original binding/coercion regression; #28852 tracks the observable DATETIME / TIMESTAMP VAR_POP difference from MySQL 8.3.0 after CAST(... AS DECIMAL(24,6)).

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