Skip to content

[Bug]: JSON_OBJECTAGG rejects YEAR and truncates TIME/DATETIME fractional seconds #28889

Closed

Description

@Ariznawlll

Description

JSON_OBJECTAGG does not use the typed temporal conversion used by the other JSON constructors. A YEAR value is rejected, and valid TIME/DATETIME values lose their fractional seconds. The altered values are returned and persisted through normal aggregation, window aggregation, CTAS, views, prepared statements, parallel execution, and aggregate spill.

Environment

  • Branch: main
  • Commit: 0c3a04f390adaf6281fd592a49778ea5b1155e67
  • Deployment: local standalone MatrixOne, 1 CN / 1 TN / 1 LogService
  • Reference: local MySQL 8.0.45

Steps to reproduce

CREATE DATABASE json_objectagg_temporal_repro;
USE json_objectagg_temporal_repro;

CREATE TABLE t (
  k VARCHAR(16),
  y YEAR,
  tm0 TIME,
  tm6 TIME(6),
  dt0 DATETIME,
  dt6 DATETIME(6)
);
INSERT INTO t VALUES
  ('a', 2024, '12:34:56', '12:34:56.123456',
   '2024-02-29 12:34:56', '2024-02-29 12:34:56.123456'),
  ('b', NULL, NULL, NULL, NULL, NULL);

SELECT JSON_OBJECTAGG(k, y) FROM t;
SELECT JSON_OBJECTAGG(k, tm0), JSON_OBJECTAGG(k, tm6),
       JSON_OBJECTAGG(k, dt0), JSON_OBJECTAGG(k, dt6)
FROM t;

MatrixOne result

The YEAR query is rejected:

ERROR 20301 (HY000): invalid input: unsupported type for json aggregate: YEAR

The remaining query returns:

{"a":"12:34:56","b":null}
{"a":"12:34:56","b":null}
{"a":"2024-02-29 12:34:56","b":null}
{"a":"2024-02-29 12:34:56","b":null}

Both declared scale 0 and scale 6 collapse to the same JSON strings. For scale 6 this silently removes .123456.

MySQL 8.0.45 result

{"a":2024,"b":null}
{"a":"12:34:56.000000","b":null}
{"a":"12:34:56.123456","b":null}
{"a":"2024-02-29 12:34:56.000000","b":null}
{"a":"2024-02-29 12:34:56.123456","b":null}

JSON_OBJECT and the current JSON_ARRAYAGG implementation in MatrixOne also retain the scale-6 value, so the loss is specific to the object aggregate value-conversion path.

Scope and repeatability

  • The scalar aggregate, ordered window aggregate, CTAS result, view result, and prepared execution reproduce the same conversion on every run.
  • A 160,000-row / 40,000-group test returns all 40,000 complete JSON objects, but zero objects retain the TIME(6) or DATETIME(6) expected value.
  • That scale test reproduces 3/3 with max_dop=1 and max_dop=8, both resident and with agg_spill_mem=65536; the low-threshold runs enter multi-level aggregate spill and complete with the same altered values.
  • NULL values remain JSON null and object membership is complete; the problem is the non-NULL temporal atom conversion.

Code analysis

pkg/sql/plan/function/list_agg.go admits every non-binary value type for JSON_OBJECTAGG. In pkg/sql/colexec/aggexec/jsonagg2.go, both the normal jsonObjectAggExec.BatchFill path and the allocation-accounted spill path call the generic buildValueByteJson / appendJSONAggregateValue functions directly. Those functions do not handle YEAR, and they format TIME/DATETIME with String(), which drops declared fractional precision.

The recently merged JSON_ARRAYAGG fix routes TIME, DATETIME, and YEAR through jsonvalue.FromVector; the corresponding object-aggregate route is still absent.

Expected behavior

JSON_OBJECTAGG should accept YEAR values and preserve temporal scale and fractional content consistently with MatrixOne's other JSON constructors and MySQL 8. The corrected conversion must be shared by normal, accounted/spill, merge, window, CTAS/view, and prepared execution paths.

Activity

  1. added this to the MO-202609 milestone on Sep 15, 2026
  2. added theissue type on Sep 15, 2026
  3. jiangxinmeng1 commented on Oct 21, 2026

    @jiangxinmeng1
    Contributor

    fixed by #28932

  4. Ariznawlll commented on Oct 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:63a964a82fbaae4759cfabb45655e0185bc0979c
    • 修复:fix remaining date/time compatibility issues #28932 已 MERGED,merge commit 0c3f7e035d90fbcf205b3164c560eda2df38335e;GitHub compare 确认它是上述 main 的祖先(ahead 399、behind 0)。
    • 部署方式:macOS arm64 本机隔离数据目录,1 CN / 1 TN / 1 LogService,MySQL TCP 127.0.0.1:46001;未使用共享 TKE。
    • 测试日期:2026-10-21。

    原问题验证

    通过真实 MySQL 协议执行 JSON_OBJECTAGG,分别输入 YEAR、TIME(0)、TIME(6)、DATETIME(0)、DATETIME(6) 和 SQL NULL。独立预期与实际值均一致,连续 3/3:YEAR 为 {"a":2024,"b":null};TIME(0) 为 12:34:56.000000,TIME(6) 为 12:34:56.123456;DATETIME(0) 为 2024-02-29 12:34:56.000000,DATETIME(6) 为 2024-02-29 12:34:56.123456。未出现原报告的 YEAR 拒绝或微秒截断。

    按报告规模创建 160,000 行、40,000 个分组,每组 4 个键(包含 JSON null)。max_dop=1/8 × agg_spill_mem=0/65536 四组设置分别运行 3 次,共 12/12;每次分组数、对象键数、YEAR、NULL、TIME(6)、DATETIME(6) 的校验计数均为 40000。低阈值的 EXPLAIN ANALYZE 实际显示聚合 SpillRows=471360、SpillSize=52.81 MiB,不是仅设置阈值而未触发 spill。

    Happy Path 覆盖

    1. 标量 JSON_OBJECT 和 JSON_ARRAYAGG 对照值正确;JSON_OBJECTAGG 的 CTAS、view、SQL PREPARE/EXECUTE、窗口聚合路径均保留 YEAR 与 TIME/DATETIME 微秒,3/3。
    2. 对象中 SQL NULL 值编码为 JSON null;零精度与六位小数精度分别校验,3/3。
    3. 每轮创建独立数据库、核对原表 2 行、最终删除;规模测试同样清理独立数据库。

    Unhappy Path 覆盖

    1. JSON_OBJECTAGG(NULL,tm6) 连续 3/3 返回 ERROR 20301: invalid input: json_objectagg key cannot be NULL;随后原表仍为 2 行、合法聚合仍返回 12:34:56.123456,确认错误后会话和数据未受损。

    白盒验证

    本轮未重复运行修复 PR 的 Go UT;验收以隔离部署上的公开 SQL 路径、原规模分组以及实际 spill 为准。修复 PR 自述的 Go 测试不计入本轮独立测试次数。

    回归测试

    • 已向唯一汇总 PR motr#247 追加成对用例 t/issue_28889_json_objectagg_temporal.test 与 r/issue_28889_json_objectagg_temporal.result,PR head d8173ae5。预期值由 SQL/JSON 语义独立确定,逐项核对 golden 全为预期的 1、非法键为 20301。
    • 隔离实例上该 MOTR case --repeat 3:3/3、57/57 语句通过;完整 suite 与集成 CI 本轮未执行,PR 尚未合入,后续继续跟踪。该轻量回归不替代上述已独立完成的 160k/40k spill 验收。

    测试证据

    • 修复 PR #28932 与 MOTR PR #247。
    • 本地可复核脚本与输出:/tmp/mo-28889-qa-jUbdgJ/verify_core.py(3/3 原路径、对照、错误恢复)、/tmp/mo-28889-qa-jUbdgJ/verify_scale.py(12/12 规模组合及 EXPLAIN ANALYZE 的 52.81 MiB spill)、/tmp/mo-28889-qa-jUbdgJ/motr-reports/(MOTR 3/3)。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions