Repository navigation
evalengine: return an error when float arithmetic overflows - #22105
BahaaMattar wants to merge 2 commits into
Conversation
Adding, subtracting or multiplying DOUBLE values returned +Inf or -Inf when the result did not fit in a DOUBLE. MySQL fails the statement with ERROR 1690 (22003) "DOUBLE value is out of range" instead, and evalengine already does so for division. Make +, - and * return the same error, in the AST interpreter and in the compiled program. Fixes vitessio#22104 Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the change to return out-of-range errors for DOUBLE arithmetic overflow.
No open issues 🎉
What changed in this PR
This PR aligns evalengine’s DOUBLE addition, subtraction, and multiplication with MySQL by returning an out-of-range error instead of infinity when arithmetic overflows, addressing #22104.
Changes:
- Add shared overflow-checking helpers and propagate errors through interpreted and compiled evaluation.
- Add regression tests for overflow, mixed numeric operands, finite results, and constant folding.
| File | Description |
|---|---|
go/vt/vtgate/evalengine/compiler_asm.go |
Use checked float helpers in compiled arithmetic instructions. |
go/vt/vtgate/evalengine/arithmetic.go |
Detect non-finite arithmetic results and return DOUBLE range errors. |
go/vt/vtgate/evalengine/arithmetic_float_test.go |
Test interpreter/compiler behavior and translation-time constant overflow. |
Review effort: Balanced
mattlord
left a comment
There was a problem hiding this comment.
Thanks for this!
-
Blocking: I think that we should make the new error come only from the argument that a conditional returns. evalengine evaluates every argument of
CASEbefore choosing one, both in the interpreter (go/vt/vtgate/evalengine/expr_logical.go:587) and in the compiledCmpCase(go/vt/vtgate/evalengine/compiler_asm.go:539).IFis rewritten into aCASE(go/vt/vtgate/evalengine/translate_builtin.go:1203), andIFNULLandCOALESCEalso evaluate every argument. Until now, an overflowing argument that wasn't chosen became+Infand was thrown away. With this change, its error fails the whole expression. On this head, withcolumn0 = 10andcolumn1 = 1.7976931348623157e308,IF(column1 < 1e300, column1 * column0, column1),CASE WHEN column1 < 1e300 THEN column1 * 1e10 ELSE column1 ENDandIFNULL(column0, column1 * 10)all fail withDOUBLE value is out of range, in both the interpreter and the compiled program, whilemainreturns the chosen value. MySQL 8.0.46 and 8.4.9 also return the chosen value. Over DOUBLE columns,IF(v < 1e300, v * w, v)returns1.7976931348623157e308,IFNULL(w, v * 10)returns10, andIF(1, 0, 1e308 * 10)returns0. This reaches real queries wherever VTGate evaluates the expression itself. For example,select if(sum(col) > 1e300, null, sum(col) * 10) from useron a sharded keyspace plans a VTGateProjectionover the mergedSUM. Ifcolis a DOUBLE and the total is near the limit, the query now fails instead of returningNULL. That also contradicts "Results that still fit in a DOUBLE are unchanged" in the description. BIGINT overflow and float division already fail this way onmain:CASE WHEN i < 100 THEN i * 2 ELSE i ENDwithi = 9223372036854775807errors, while MySQL returnsi. So the root cause is the eager evaluation, and this PR extends it to+,-and*on floats, which are far more common. I think that we should makeCASE,IFNULLandCOALESCEevaluate only the argument that they return, either here or in a PR that lands first, and add the guarded forms above as test cases. What do you think? -
I think that we should also add some of these cases to the MySQL comparison corpus in
go/vt/vtgate/evalengine/testcases/cases.go, such as overflowing+,-and*on DOUBLE inputs and the guardedIF,CASEandIFNULLforms.TestCompilerReferencechecks that the interpreter and the compiled program agree on every case there, and the evalengine unit test job compares each case against a real MySQL. The guarded forms would then be checked against MySQL itself, not against expectations taken from its source. The integration harness tolerates a MySQLvalue is out of rangeerror when evalengine returns a value (go/vt/vtgate/evalengine/integration/fuzz_test.go:95), soTestFloatArithmeticOverflowwould still be what checks that the error is raised.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #22105 +/- ##
===========================================
+ Coverage 69.67% 79.37% +9.70%
===========================================
Files 1614 502 -1112
Lines 216793 91149 -125644
===========================================
- Hits 151044 72347 -78697
+ Misses 65749 18802 -46947
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
TestSelectTupleWithNonLiteralMembers built its +Inf member from 1e308*10. Float arithmetic that overflows is an out of range error now, so the constant no longer folds and the query goes to the shard instead of being evaluated by vtgate. Pass +Inf as a bind variable instead. The case still checks that a tuple with a non-finite float member is returned rather than panicked on, and it does not depend on which functions return +Inf: COT(0) and DEGREES(1e308) still do, but MySQL fails on both. Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
|
Thanks for the detailed review, and for checking it against MySQL 8.0 and 8.4. You are right: The unit test failures came from On the root cause, I agree it should be fixed before this lands. One thing I noticed: the compiler already aggregates the |
| l := env.vm.stack[env.vm.sp-2].(*evalFloat) | ||
| r := env.vm.stack[env.vm.sp-1].(*evalFloat) | ||
| l.f += r.f | ||
| l.f, env.vm.err = mathAdd_ff0(l.f, r.f) |
| return newEvalFloat(v1 + v2) | ||
| func mathAdd_ff(v1, v2 float64) (*evalFloat, error) { | ||
| result, err := mathAdd_ff0(v1, v2) | ||
| return newEvalFloat(result), err |
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| env := evalengine.NewExpressionEnv(t.Context(), nil, evalengine.NewEmptyVCursor(venv, time.UTC)) |
mattlord
left a comment
There was a problem hiding this comment.
Thanks for the update and for fixing the test. With the bind variable, TestSelectTupleWithNonLiteralMembers still panics if evalTuple.ToRawBytes goes back to NewValue. I checked that.
Yes, that order works for me. This head doesn't change the production code, so the earlier Blocking point still applies until the CASE change lands. Two thoughts on that PR. First, it can be tested on its own with forms that already fail on main, such as CASE WHEN i < 100 THEN i * 2 ELSE i END with i = 9223372036854775807, which MySQL evaluates to i. Second, a branch that isn't returned and fails has no runtime type for the interpreter to aggregate. I wonder if the interpreter could use the same static aggregation that CaseExpr.compile already does (go/vt/vtgate/evalengine/expr_logical.go:676), so that the two paths can't drift apart. What do you think?
|
Thanks for checking the bind-variable case. Agreed on both. The BIGINT form already fails on On the types, one constraint I found: the interpreter is what |
mattlord
left a comment
There was a problem hiding this comment.
Thanks, that matches what I see. IR has no typeof (go/vt/vtgate/evalengine/expr.go:35), only typedIR does, and UntypedExpr.typeof gets its type by compiling for the current runtime types. Either option sounds fine to me. I'd lean toward the one that leaves CASE with a single type aggregation for the interpreter and the compiler to share, so they can't drift apart. One small thing for that PR: the comment in CaseExpr.eval (go/vt/vtgate/evalengine/expr_logical.go:594) says that MySQL evaluates every branch of a CASE. That doesn't match MySQL 8.0 and 8.4, which return the chosen value when a branch that isn't returned overflows. So I think that comment should change along with the code.
CI is green at this head apart from the labels check. Until the CASE change lands, the earlier Blocking point still applies here.


Description
+,-and*on DOUBLE values returned+Infor-Infwhen the result does not fit in a DOUBLE. MySQL fails the statement withERROR 1690 (22003): DOUBLE value is out of rangeinstead (Item_func_plus,Item_func_minusandItem_func_mulend incheck_float_overflow), and evalengine already does the same for/(mathDiv_ff0).arithmetic.go: newmathAdd_ff0,mathSub_ff0andmathMul_ff0, shaped likemathDiv_ff0. They returndataOutOfRangeError(..., "DOUBLE", op)when the result is not finite.mathAdd_ff,mathSub_ffandmathMul_ffnow return that error to their callers.compiler_asm.go: the compiledAdd_ff,Sub_ffandMul_ffuse the same helpers, so the interpreter and the compiled program agree.1e308 * 10is now reported when it is translated, like the existing1e308 / 0.1.Results that still fit in a DOUBLE are unchanged, with one exception found in review: evalengine evaluates every argument of
CASE(and soIFandIFNULL) and ofCOALESCE, so an overflow in an argument that is not returned now fails the whole expression, while MySQL returns the chosen value. See the review discussion. This is a bug fix; I don't think it needs a backport.Motivation
How do you use Vitess? (pick one)
Did you run this change against a real Vitess deployment? No. I found this by reading the float helpers in
arithmetic.gonext to the division one, which already returns this error. I reproduced it with a unit test onmain:TestFloatArithmeticOverflowfails there (interpreter returned FLOAT64(+Inf), want an error containing "DOUBLE value is out of range", and the same for the compiler) and passes with this change. I did not have a MySQL server; the MySQL behaviour is taken from the MySQL 8.4 source (Item_func_plus::real_op,Item_func_minus::real_op,Item_func_mul::real_op).Related Issue(s)
Fixes #22104
Checklist
Deployment Notes
None. A query whose DOUBLE
+,-or*overflows inside vtgate now fails with the MySQL error instead of returning+Inf/-Inf.AI Disclosure
Developed with AI assistance; I reviewed and tested every change.