Skip to content

[Bug]: unary minus silently wraps signed integer minimum values #28491

Closed

Description

@Ariznawlll

Environment

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

Problem

Unary minus keeps the input signed-integer type and performs unchecked same-width negation. Negating the minimum value of TINYINT, SMALLINT, INT, or BIGINT therefore wraps to the same negative value instead of producing a positive value in a wider domain or reporting overflow.

Reproduction

CREATE DATABASE unary_min;
USE unary_min;

CREATE TABLE t (
  ti TINYINT,
  si SMALLINT,
  i  INT,
  bi BIGINT
);

INSERT INTO t VALUES
  (-128, -32768, -2147483648, -9223372036854775808);

SELECT -ti, -si, -i, -bi FROM t;

MatrixOne returns the original negative minima:

-128, -32768, -2147483648, -9223372036854775808

For the first three columns MySQL 8.3 widens the expression and returns:

128, 32768, 2147483648

For a BIGINT column, MySQL reports error 1690 because the positive result is outside the BIGINT range. For an explicitly typed scalar, MySQL can expose the valid positive value in a widened exact domain:

SELECT -CAST('-9223372036854775808' AS SIGNED);
-- MySQL: 9223372036854775808
-- MatrixOne: -9223372036854775808

Consequences and coverage

The behavior was reproduced three times for constants/typed expressions, table columns, SQL prepared statements, derived expressions, and CTAS.

  • -bi > 0 is false and -bi = bi is true at the BIGINT minimum.
  • ORDER BY -bi orders using the wrapped negative value.
  • SUM(-bi) aggregates the wrapped value.
  • CTAS preserves the incorrect minimum values and infers the original narrow integer types.
  • DECIMAL, DOUBLE, neighboring signed values, and NULL controls behave correctly.
  • The server remains healthy after the queries.

Code analysis

UNARY_MINUS in pkg/sql/plan/function/list_operator.go declares the same return type as its input for every signed integer overload. operatorUnaryMinus in pkg/sql/plan/function/operatorSet.go then evaluates -v without an overflow check:

v, null := p1.GetValue(i)
if err := rs.Append(-v, null); err != nil {
    return err
}

For the minimum value of a two's-complement signed type, -v cannot be represented in the same type and wraps to v.

Duplicate check

#22734 covered binary arithmetic and SUM overflow and is closed. It did not cover the unary-minus overload or its same-width result typing, so this is filed separately rather than reopening that issue.

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 9, 2026
  4. assigned and unassigned on Sep 11, 2026
  5. slyunnn commented on Sep 14, 2026

    @slyunnn

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

    Verification Environment

    • MatrixOne source/runtime: local origin/main at bb358ea76aeb44a6eed5079616e7e75ba35e75b9 — fix(agg): coerce numeric bitwise aggregate inputs (#28779); mo-service was rebuilt from this commit and the service was restarted before verification.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy.
    • Endpoints: Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002; SELECT 1 succeeded on all three.
    • Client protocol: MySQL CLI over the MySQL protocol for direct SQL and MOTR verification.
    • Related fix: PR #28499, merge commit 438353c73228feda05805a9daf1d09188a288a57 (fix: prevent unary minus signed integer overflow).

    What Was Covered

    • OK — Original TINYINT, SMALLINT, and INT minimum values now evaluate to 128, 32768, and 2147483648 instead of wrapping.
    • OK — BIGINT neighbor -9223372036854775807 negates to 9223372036854775807.
    • OK — BIGINT minimum returns MySQL error 1690 rather than silently wrapping; a subsequent query succeeds.
    • OK — DECIMAL, DOUBLE, and NULL controls retain correct unary-minus behavior.
    • OK — Selection, ordering, SUM, SQL PREPARE / EXECUTE, and CTAS use widened narrow-integer results; CTAS columns are BIGINT.
    • Not covered — Binary prepared-statement protocol, high concurrency, spill/resource-limit, network-failure, and large-data paths; this bug is deterministic per-value result typing and overflow handling.

    Findings

    The original signed-minimum wraparound is no longer reproducible on current main. Narrow signed integer operands widen to BIGINT; negating the unrepresentable BIGINT minimum produces error 1690.

    MOTR / BVT Coverage

    • Added suites/14_issue_regression/t/issue_28491_unary_minus_overflow.test and its recorded result.
    • MOTR recording and replay through Proxy 6001 both passed: 22/22 statements.
    • Direct original and expanded checks ran concurrently with endpoint-specific databases through Proxy 6001, CN1 16001, and CN2 16002.
    • MOTR record/replay ran serially because its fixture database has shared setup and teardown.
    • Source MOTR commit: b50f7422954d6ccccff56275fdeb92edd6d36388; daily cherry-pick: 0803001dfdf2ae18e4937187eff8586ab175f862 in MOTR PR #160.

    Final Conclusion

    Fixed. Large-data regression is not required: unary-minus type widening and overflow checks are deterministic per value and do not depend on row count, block/batch boundaries, memory/spill, concurrency, data skew, network transfer, or resource scale. The issue remains open for maintainer disposition.

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