Repository navigation
Description
Environment
- MatrixOne: latest
main, commitf0c31cd4b830be32442cf329e0a3fb08aa9c16c3 - 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: -9223372036854775808Consequences and coverage
The behavior was reproduced three times for constants/typed expressions, table columns, SQL prepared statements, derived expressions, and CTAS.
-bi > 0is false and-bi = biis true at theBIGINTminimum.ORDER BY -biorders 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, andNULLcontrols 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
fix: prevent unary minus signed integer overflow (#28499)
slyunnn commentedon Sep 14, 2026
Regression and extended verification for #28491 (2026-09-14)
Verification Environment
origin/mainatbb358ea76aeb44a6eed5079616e7e75ba35e75b9—fix(agg): coerce numeric bitwise aggregate inputs (#28779);mo-servicewas rebuilt from this commit and the service was restarted before verification.2 CN / 1 TN / 1 Log service / Proxy.127.0.0.1:6001, CN1127.0.0.1:16001, and CN2127.0.0.1:16002;SELECT 1succeeded on all three.438353c73228feda05805a9daf1d09188a288a57(fix: prevent unary minus signed integer overflow).What Was Covered
TINYINT,SMALLINT, andINTminimum values now evaluate to128,32768, and2147483648instead of wrapping.BIGINTneighbor-9223372036854775807negates to9223372036854775807.BIGINTminimum returns MySQL error1690rather than silently wrapping; a subsequent query succeeds.DECIMAL,DOUBLE, andNULLcontrols retain correct unary-minus behavior.SUM, SQLPREPARE/EXECUTE, and CTAS use widened narrow-integer results; CTAS columns areBIGINT.Findings
The original signed-minimum wraparound is no longer reproducible on current
main. Narrow signed integer operands widen toBIGINT; negating the unrepresentableBIGINTminimum produces error1690.MOTR / BVT Coverage
suites/14_issue_regression/t/issue_28491_unary_minus_overflow.testand its recorded result.6001both passed:22/22statements.6001, CN116001, and CN216002.b50f7422954d6ccccff56275fdeb92edd6d36388; daily cherry-pick:0803001dfdf2ae18e4937187eff8586ab175f862in 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.fix: prevent unary minus signed integer overflow (matrixorigin#28499)
fix: prevent unary minus signed integer overflow (matrixorigin#28499)
fix: prevent unary minus signed integer overflow (matrixorigin#28499)