Skip to content

[Compatibility]: PERIOD_ADD and PERIOD_DIFF mishandle exact DECIMAL arguments #28496

Closed

Description

@Ariznawlll

Environment

  • MatrixOne: latest main, commit f0c31cd4b830be32442cf329e0a3fb08aa9c16c3
  • MySQL comparison: 8.3.0

Problem

PERIOD_ADD and PERIOD_DIFF truncate exact DECIMAL arguments instead of applying MySQL's exact numeric-to-integer rounding. Values at or above a .5 boundary therefore select a different period or month count. SQL prepared statements take a third path and reject the same DECIMAL values outright.

Reproduction

SELECT PERIOD_ADD(CAST(200801.5 AS DECIMAL(7,1)), 1);
SELECT PERIOD_ADD(200801, CAST(1.5 AS DECIMAL(3,1)));
SELECT PERIOD_ADD(200801, CAST(-1.5 AS DECIMAL(3,1)));

SELECT PERIOD_DIFF(CAST(200802.5 AS DECIMAL(7,1)), 200801);
SELECT PERIOD_DIFF(200802, CAST(200801.5 AS DECIMAL(7,1)));

MatrixOne returns:

200802
200802
200712
1
1

MySQL 8.3 returns:

200803
200803
200711
2
0

The difference comes from converting 200801.5 to period 200801 instead of 200802, and month counts 1.5/-1.5 to 1/-1 instead of 2/-2.

Prepared-statement path

PREPARE p FROM 'SELECT PERIOD_ADD(?, ?)';
SET @period = CAST(200801.5 AS DECIMAL(7,1));
SET @months = CAST(1.5 AS DECIMAL(3,1));
EXECUTE p USING @period, @months;

MatrixOne rejects this with invalid argument cast to int, bad value 200801.5; MySQL returns 200804 after rounding both exact arguments. PERIOD_DIFF has the same prepared-parameter rejection.

Coverage

All relevant paths were repeated three times:

  • DECIMAL constants below, at, and above the .5 boundary;
  • positive and negative DECIMAL month counts;
  • DECIMAL columns with valid YYMM/YYYYMM periods;
  • SQL prepared statements;
  • CTAS, which persists the incorrect PERIOD_ADD and PERIOD_DIFF results.

Integer and NULL controls behave normally, and .4 controls match MySQL because truncation and rounding happen to choose the same integer. The server remains healthy after all runs.

Code analysis

The function registrations in pkg/sql/plan/function/list_builtIn.go provide only signed integer, unsigned integer, and FLOAT64 overload combinations—there is no DECIMAL overload.

Both PeriodAdd and PeriodDiff in pkg/sql/plan/function/func_binary.go convert the floating execution path with direct truncation:

return int64(val), null // Truncate decimal part

Prepared parameter resolution instead selects an integer cast and rejects the fractional DECIMAL text. A dedicated exact DECIMAL conversion path is needed to keep constant, column, and prepared behavior aligned.

Activity

  1. added this to the MO-202609 milestone on Sep 9, 2026
  2. added theissue type on Sep 9, 2026
  3. added
    severity/s0
    and removed on Sep 14, 2026
  4. assigned and unassigned on Sep 27, 2026
  5. slyunnn commented on Sep 28, 2026

    @slyunnn

    Regression and extended verification for #28496 (2026-09-28)

    Verification Environment

    • Source: latest origin/main at 9b7c2c23dba737232d975d66bd04628199e1a436 — fix(tae): reconcile replay promotion merge state before write (#29421).
    • Runtime: Linux arm64 mo-service built from that exact source; its embedded build information reports branch main and commit 9b7c2c23db.
    • Topology: 1 Log service / 1 TN / 2 CN / 1 Proxy.
    • Endpoints: Proxy 6001, CN1 16001, and CN2 16002 were all connected successfully. Original and SQL PREPARE regressions used the MySQL text protocol through each endpoint; binary prepared coverage used Go's MySQL driver and COM_STMT_PREPARE / COM_STMT_EXECUTE on all three endpoints.
    • Compatibility reference: local MySQL 8.3.0 using the MySQL text protocol.
    • Execution was sequential; this is a scalar argument-binding issue and has no engine-parallel requirement.
    • Associated fix: PR #29164, merged as 61461c12a9e7088b5176fb477a7d40823e75cbef (refactor: migrate numeric integer parameters). It migrates both PERIOD_ADD and PERIOD_DIFF parameters to the shared signed INT64 integer-argument contract introduced by PR #28989, d9609d742b9000b54279f44e34295630b7238282.

    What Was Covered

    • OK Original scalar reproduction: PERIOD_ADD with fractional DECIMAL period/month arguments and PERIOD_DIFF with fractional DECIMAL periods returned 200803, 200803, 200711, 2, and 0, matching MySQL 8.3.0 on Proxy, CN1, and CN2.
    • OK Boundary controls below and at the rounding boundary: .4 and .5 exact DECIMAL values produced the same results as MySQL 8.3.0 on all three endpoints.
    • OK Negative fractional month count -1.5 rounded through the shared integer contract and returned 200711.
    • OK NULL controls for both functions returned NULL on all three endpoints.
    • OK DECIMAL table-column inputs, including positive, negative, .4, .5, and NULL rows, matched MySQL 8.3.0 on Proxy, CN1, and CN2.
    • OK CTAS persisted the corrected PERIOD_ADD and PERIOD_DIFF values on all three endpoints.
    • OK Same-session SQL PREPARE / EXECUTE with DECIMAL variables returned 200804 for PERIOD_ADD and 1 for PERIOD_DIFF on all three endpoints.
    • OK Binary COM_STMT_PREPARE / COM_STMT_EXECUTE with parameters cast to DECIMAL returned the expected results and NULL propagation on Proxy, CN1, and CN2.
    • Not covered Explicit FLOAT / DOUBLE conversion behavior is outside this exact-DECIMAL issue and was not used to judge the fix.

    Findings

    Both arguments of PERIOD_ADD and PERIOD_DIFF now use the shared exact integer-argument coercion before selecting the canonical INT64 executor. The previous fractional DECIMAL truncation and SQL prepared-statement rejection are not reproducible on the latest main runtime. All endpoints remained healthy after verification.

    MOTR / BVT Coverage

    No new MOTR case or commit was needed. Existing distributed BVT coverage in test/distributed/cases/function/integer_parameter_coercion.sql exercises numeric integer consumers, including PERIOD_ADD, DECIMAL columns, SQL prepared execution, NULL, and overflow controls. Latest-main targeted planner/function tests TestIntegerArgumentAdditionalSignatures and TestIntegerArgumentPreparedRuntimeCandidates passed.

    Large-data regression is not required: this behavior is determined by scalar exact DECIMAL conversion and overload binding, independent of row count, block/batch boundaries, memory/spill, concurrency, data skew, network transfer, or resource scale. Increasing data volume would not cover an additional relevant execution boundary.

    Final Conclusion

    Fixed. The original and extended exact-DECIMAL PERIOD_ADD / PERIOD_DIFF scenarios match MySQL 8.3.0 through Proxy, CN1, and CN2, including SQL and binary prepared execution.

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

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions