Skip to content

[Compatibility]: TRUNCATE retains DECIMAL input scale instead of the requested precision #28593

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues, including open and closed issues.

Branch Name

main

Commit ID

fc621e3

Other Environment Information

  • MatrixOne: locally built from the commit above (8.0.30-MatrixOne-v1.3.0)
  • MySQL baseline: 8.0.46
  • Verification date: 2026-09-10

Actual Behavior

For exact DECIMAL input and a nonnegative constant precision, MatrixOne's TRUNCATE(X,D) always retains the input DECIMAL precision and scale. MySQL reduces the result scale to min(input_scale,D) and removes the discarded fractional capacity from the precision.

This is visible in both returned text and persisted schema. For a DECIMAL(10,4) source:

expression       MatrixOne value/type    MySQL value/type
TRUNCATE(d,2)    12345.6700 DECIMAL(10,4) 12345.67 DECIMAL(8,2)
TRUNCATE(d,0)    12345.0000 DECIMAL(10,4) 12345    DECIMAL(6,0)
TRUNCATE(d,8)    12345.6789 DECIMAL(10,4) 12345.6789 DECIMAL(10,4)

The same mismatch spans MatrixOne's three exact-decimal representations:

  • DECIMAL(10,4) truncated to 2 remains DECIMAL(10,4) instead of DECIMAL(8,2).
  • DECIMAL(38,10) truncated to 5 remains DECIMAL(38,10) instead of DECIMAL(33,5).
  • DECIMAL(50,10) truncated to 5 remains DECIMAL(50,10) instead of DECIMAL(45,5).

CREATE TABLE ... AS SELECT persists those wider MatrixOne types, and a view exposes the same wider metadata for nonnegative D. This can change downstream client metadata, generated schemas, storage width, string output, and comparisons that depend on implicit type derivation even though the significant numeric digits agree.

The negative-D case is deliberately not used as the compatibility contract here because MySQL 8.0.46 exposes different metadata for that case through a view versus CTAS. The clear mismatch above uses only nonnegative constant precision, for which MySQL is consistent.

Expected Behavior

For exact DECIMAL input and nonnegative constant D, TRUNCATE(X,D) should derive a MySQL-compatible result type:

  • result scale = min(input scale, D);
  • integer-digit capacity is preserved;
  • no extra fractional zeroes remain after the requested truncation;
  • CTAS and view metadata reflect the derived type.

Steps to Reproduce

DROP DATABASE IF EXISTS truncate_decimal_type;
CREATE DATABASE truncate_decimal_type;
USE truncate_decimal_type;

CREATE TABLE src(
  id INT PRIMARY KEY,
  d64 DECIMAL(10,4),
  d128 DECIMAL(38,10),
  d256 DECIMAL(50,10)
);
INSERT INTO src VALUES
  (1,12345.6789,
     1234567890123456789012345678.1234567890,
     1234567890123456789012345678901234567890.1234567890),
  (2,-12345.6789,-1.1234567890,-1.1234567890);

SELECT id,
       CAST(TRUNCATE(d64,2) AS CHAR),
       CAST(TRUNCATE(d64,0) AS CHAR),
       CAST(TRUNCATE(d64,8) AS CHAR),
       CAST(TRUNCATE(d128,5) AS CHAR),
       CAST(TRUNCATE(d256,5) AS CHAR)
FROM src ORDER BY id;

CREATE TABLE dst AS
SELECT TRUNCATE(d64,2) AS d2,
       TRUNCATE(d64,0) AS d0,
       TRUNCATE(d64,8) AS d8,
       TRUNCATE(d128,5) AS d128_5,
       TRUNCATE(d256,5) AS d256_5
FROM src;

SHOW CREATE TABLE dst;
SELECT column_name,column_type,numeric_precision,numeric_scale
FROM information_schema.columns
WHERE table_schema=DATABASE() AND table_name='dst'
ORDER BY ordinal_position;

Additional information

The DECIMAL overloads for TRUNCATE in pkg/sql/plan/function/list_builtIn.go return parameters[0] unchanged for Decimal64, Decimal128, and Decimal256. The evaluator in func_binary.go can rescale when the result vector's scale differs, but the declared return type never supplies the constant D-derived scale, so it retains the input scale and appends zeroes.

Positive, negative, zero, D=0, 0<D<input_scale, D>input_scale, Decimal64/128/256, direct SELECT, CTAS, view, and information-schema paths were reproduced three times on the same latest-main build. Closed #28492 concerned rejection of dynamic or NULL precision arguments; this report concerns the result type and representation of accepted constant-precision calls.

Activity

  1. added this to the MO-202609 milestone on Sep 10, 2026
  2. added theissue type on Sep 10, 2026
  3. daviszhen commented on Sep 10, 2026

    @daviszhen
    Contributor

    A fix is available in #28625.

    Local Docker-isolated verification confirms TRUNCATE metadata for Decimal64, Decimal128, and Decimal256. CTAS now derives DECIMAL(8,2), DECIMAL(6,0), DECIMAL(10,4), DECIMAL(33,5), and DECIMAL(45,5) for the issue reproduction matrix, with matching displayed values. Plan and function package tests pass.

  4. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:bb358ea76aeb44a6eed5079616e7e75ba35e75b9
    • 部署方式:macOS arm64 本地 standalone,1 CN / 1 TN / 1 LogService
    • 测试日期:2026-09-14

    原问题验证

    对 DECIMAL(10,4)、DECIMAL(38,10)、DECIMAL(50,10) 执行常量 precision 的 TRUNCATE 并 CTAS。连续 3 轮 metadata 稳定为 DECIMAL(8,2)、DECIMAL(6,0)、DECIMAL(10,4)、DECIMAL(33,5)、DECIMAL(45,5),不再保留原始无效 fractional capacity。

    Happy Path 覆盖

    1. 覆盖 D=2、D=0 与大于输入 scale 的 D=8。
    2. 覆盖 Decimal64、Decimal128、Decimal256。
    3. 用 CTAS/information_schema 验证持久化结果类型和 scale。

    Unhappy Path 覆盖

    本 issue 的兼容性契约限定为非负常量 precision;未把 MySQL 在负 precision 上不同的 view/CTAS metadata 行为误标为本 issue 的验收要求。CTAS 后 schema 与数据均可读取。

    白盒验证

    修复 PR matrixone#28625 已合入当前 main;本轮验证公开 planner/CTAS 类型推导链路。

    回归测试

    已加入 motr#169:suites/14_issue_regression/t/issue_28579_28593_28595_28653_28657_28669_compatibility.test。定向用例连续 3/3 通过,共 150/150 statements。

    测试证据

    • 本地输出 SHA-256:6817c73d112d012ae784cbb9c0fc9b515a313e4fd1b1057f0abd7ea15125b9f6(3 轮一致)。
    • 回归 PR:motr#169。

    测试结论

    测试通过。

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