Skip to content

Commit

Permalink
Determine precision and scale of quotient in smarter way
Browse files Browse the repository at this point in the history
  • Loading branch information
katzyn committed Feb 18, 2023
1 parent 8835170 commit e6ce81e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions h2/src/docsrc/html/changelog.html
Expand Up @@ -21,6 +21,10 @@ <h1>Change Log</h1>

<h2>Next Version (unreleased)</h2>
<ul>
<li>Issue #3731: Division result exceeds numeric precision constraint
</li>
<li>PR #3718: Add test coverage for JDK 17
</li>
<li>Issue #3580: TestCrashAPI: NPE in ParserUtil.getTokenType() (called by Parser.readIfDataType1())
</li>
<li>PR #3709: Update copyright years and fix building of documentation
Expand Down
7 changes: 7 additions & 0 deletions h2/src/main/org/h2/expression/BinaryOperation.java
Expand Up @@ -5,6 +5,7 @@
*/
package org.h2.expression;

import org.h2.engine.Constants;
import org.h2.engine.SessionLocal;
import org.h2.expression.IntervalOperation.IntervalOpType;
import org.h2.expression.function.DateTimeFunction;
Expand Down Expand Up @@ -214,6 +215,12 @@ private void optimizeNumeric(TypeInfo leftType, TypeInfo rightType) {
// 10^rightScale, so add rightScale to its precision and adjust the
// result to the changes in scale.
precision = leftPrecision + rightScale - leftScale + scale;
// If precision is too large, reduce it together with scale
if (precision > Constants.MAX_NUMERIC_PRECISION) {
long sub = Math.min(precision - Constants.MAX_NUMERIC_PRECISION, scale);
precision -= sub;
scale -= sub;
}
break;
}
default:
Expand Down
12 changes: 12 additions & 0 deletions h2/src/test/org/h2/test/scripts/datatypes/numeric.sql
Expand Up @@ -206,3 +206,15 @@ DROP TABLE TEST;

SET MODE Regular;
> ok

CREATE TABLE TEST(A NUMERIC(100000), B NUMERIC(100)) AS VALUES (1E99999, 1E99);
> ok

SELECT CHAR_LENGTH(CAST(A / B AS VARCHAR)) FROM TEST;
>> 99901

SELECT CHAR_LENGTH(CAST(A / CAST(B AS NUMERIC(200, 100)) AS VARCHAR)) FROM TEST;
>> 99901

DROP TABLE TEST;
> ok

0 comments on commit e6ce81e

Please sign in to comment.