-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IGNITE-23192 Sql. Arithmetic operations failed with "out of range" exception #4422
base: main
Are you sure you want to change the base?
Conversation
@@ -519,7 +519,7 @@ public async Task TestCustomDecimalScale() | |||
await using var resultSet = await Client.Sql.ExecuteAsync(null, "select cast((10 / ?) as decimal(20, 5))", 3m); | |||
IIgniteTuple res = await resultSet.SingleAsync(); | |||
|
|||
Assert.AreEqual(3.33333m, res[0]); | |||
Assert.AreEqual(3.3m, res[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's should be either 3.00000 or 3.33333, but not 3.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -217,7 +217,7 @@ void testExecuteAsyncDdlDml() { | |||
assertEquals(10, rows.size()); | |||
assertEquals("hello 1", rows.get(1).stringValue(0)); | |||
assertEquals(1, rows.get(1).intValue(1)); | |||
assertEquals(2, rows.get(1).intValue(2)); | |||
assertEquals(2, rows.get(1).longValue(2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertEquals(2, rows.get(1).longValue(2)); | |
assertEquals(2L, rows.get(1).longValue(2)); |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -519,7 +519,7 @@ public async Task TestCustomDecimalScale() | |||
await using var resultSet = await Client.Sql.ExecuteAsync(null, "select cast((10 / ?) as decimal(20, 5))", 3m); | |||
IIgniteTuple res = await resultSet.SingleAsync(); | |||
|
|||
Assert.AreEqual(3.33333m, res[0]); | |||
Assert.AreEqual(3.3m, res[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be fixed soon
} | ||
|
||
private static Stream<Arguments> decimalOverflows() { | ||
return Stream.of( | ||
// BIGINT | ||
arguments(SqlTypeName.BIGINT, "SELECT 9223372036854775807 + 1", EMPTY_PARAM), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you decide to remove such cases? It looks as valid testcases
// arguments("SELECT -32768::SMALLINT/-1::SMALLINT", "32768"), | ||
// arguments("SELECT -128::TINYINT/-1::TINYINT", "128") | ||
|
||
arguments("SELECT CAST(-? AS BIGINT)/-1", "9223372036854775808", "9223372036854775808"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the test doesn't fail with overflow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i not understand why it need to fail ? Overflow need to operate with type, i didn`t see ant type here, did i miss smth ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most of the cases here should fail due to overflow, it looks incorrect now
modules/sql-engine/src/integrationTest/sql/types/integer/cast_to_integer.test
Show resolved
Hide resolved
|
||
/** Returns the multiply of its arguments, extending result type for overflow avoidance. */ | ||
public static BigDecimal multiply(BigDecimal x, BigDecimal y) { | ||
int maxPrecision = Commons.cluster().getTypeFactory().getTypeSystem().getMaxPrecision(SqlTypeName.DECIMAL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standard (see 6.29) says for the case the following:
The precision of the result of multiplication is implementation-defined, and the scale is S1 + S2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add more tests to the patch:
- planning tests to make sure derived result type for math operation is as expected for every type pair (you may extend
NumericBinaryOperationsTypeCoercionTest
fro this) - test on changes in TypeSystem
- execution test covering all math operation for all type pairs
if (isIntType(type1) && isIntType(type2)) { | ||
boolean typesNullability = type1.isNullable() || type2.isNullable(); | ||
|
||
switch (type1.getSqlTypeName()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of copy-pasting all these switch cases it's better to do following:
- get least restrictive type among arguments (
typeFactory.leastRestrictive(List.of(type1, type2))
) - get next type for the result type from p1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes in type systems must be covered IgniteTypeSystemTest
test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
append test, IgniteTypeSystemTest#deriveType
Pass the original address to `NettyClientConnection` to avoid using `channel.remoteAddress()`, which can return null on disconnect. We want have the address for logging and debugging purposes even after disconnect.
…pirationTime may unbox a null value and cause NPE (apache#4425)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I append tests into IgniteTypeSystemTest
- Fix IgniteTypeSystem issue
tests of IgniteTypeSystem was in p2. We need planner tests as well
@@ -519,7 +519,7 @@ public async Task TestCustomDecimalScale() | |||
await using var resultSet = await Client.Sql.ExecuteAsync(null, "select cast((10 / ?) as decimal(20, 5))", 3m); | |||
IIgniteTuple res = await resultSet.SingleAsync(); | |||
|
|||
Assert.AreEqual(3.33333m, res[0]); | |||
Assert.AreEqual(3.00000m, res[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain why the result has changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i can, previously we call SqlFunctions#divide() and it implementation is a bit diff from current:
calcite : b0.divide(b1, MathContext.DECIMAL64)
current: x.divide(y, RoundingMode.HALF_DOWN)
i can change it like calcite but test "test_correlated_any_all.test" will fail in such a case and as for me this is not hack from my side, check: we create accumulator MinMaxAccumulator with RelDataType as a parameter, but also if scale of such a type is == 0 input data still can has some fractional data which brings errors
...c/integrationTest/java/org/apache/ignite/internal/runner/app/client/ItThinClientSqlTest.java
Show resolved
Hide resolved
arguments(SqlTypeName.INTEGER, "SELECT -2147483648/-1", EMPTY_PARAM), | ||
arguments(SqlTypeName.INTEGER, "select CAST(9223372036854775807.5 + 9223372036854775807.5 AS INTEGER)", | ||
EMPTY_PARAM), | ||
arguments(SqlTypeName.INTEGER, "SELECT -CAST(? AS INTEGER)", -2147483648), | ||
|
||
// SMALLINT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it make sense to have similar test cases for all 4 types ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
derived type for SMALLINT and TINYINT tests without explicit casting is INTEGER so - no overflow will raised here
|
||
@ParameterizedTest | ||
@MethodSource("decimalOpTypeExtension") | ||
public void testCalcOpDynParamOverflow(String expr, String expect, Object param) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you name this test 'overflow' when no overflow is expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
arguments("SELECT CAST(-? AS BIGINT) * -1", "9223372036854775808", "9223372036854775808"), | ||
arguments("SELECT CAST(-? AS INTEGER) * -1", "2147483648", "2147483648"), | ||
arguments("SELECT CAST(-? AS SMALLINT) * -1", "32768", "32768"), | ||
arguments("SELECT CAST(-? AS TINYINT) * -1", "128", "128") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it make sense to add test cases not only with cast from string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other tests are moved into "cast_to_integer.test"
return typeFactory.createTypeWithNullability( | ||
typeFactory.createSqlType( | ||
SqlTypeName.DECIMAL, | ||
typeFactory.getTypeSystem().getMaxPrecision(SqlTypeName.DECIMAL), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it make sense to use minimal yet sufficient precision rather then maximum possible value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I change it and some tests will fail, for example "select1.test", if it not "MaxPrecision(SqlTypeName.DECIMAL)" results will return with non zero scale "177.00000" vs "177" and it not expected, i can`t found the problem for a shot time, probably we need additional issue here, wdyt ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will return with non zero scale "177.00000" vs "177" and it not expected
why do you say "it's not expected"? it's literally what you've changed in this patch :) Before this patch chain of math operation results in a least restrictive type among operands, now result type is bumped every time. Given an expression like this (a+b+c+d+e)/5
(from the first failed statement from mentioned script), before this patch final operation was (int) / (int)
which results in another int. Now it's (decimal) / (int)
, and to know resulting type you should check org.apache.calcite.rel.type.RelDataTypeSystem#deriveDecimalDivideType
...ql-engine/src/test/java/org/apache/ignite/internal/sql/engine/type/IgniteTypeSystemTest.java
Outdated
Show resolved
Hide resolved
@korlov42 planner tests extended, check NumericBinaryOperationsTypeCoercionTest -> mathResultMatcher |
Arguments.of(NativeTypes.INT32, NativeTypes.INT64, NativeTypes.INT64), | ||
|
||
Arguments.of(NativeTypes.INT64, NativeTypes.INT8, | ||
NativeTypes.decimalOf(typeSystem.getMaxPrecision(SqlTypeName.DECIMAL), 0)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's introduce constant for DECIMAL_MAX_0
} | ||
|
||
|
||
private static Stream<Arguments> deriveMultTypeArguments() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
argument for multiplication and addition seems to be identical. Does it make sense leave only one set of arguments?
return typeFactory.createTypeWithNullability( | ||
typeFactory.createSqlType( | ||
SqlTypeName.DECIMAL, | ||
typeFactory.getTypeSystem().getMaxPrecision(SqlTypeName.DECIMAL), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will return with non zero scale "177.00000" vs "177" and it not expected
why do you say "it's not expected"? it's literally what you've changed in this patch :) Before this patch chain of math operation results in a least restrictive type among operands, now result type is bumped every time. Given an expression like this (a+b+c+d+e)/5
(from the first failed statement from mentioned script), before this patch final operation was (int) / (int)
which results in another int. Now it's (decimal) / (int)
, and to know resulting type you should check org.apache.calcite.rel.type.RelDataTypeSystem#deriveDecimalDivideType
Thank you for submitting the pull request.
To streamline the review process of the patch and ensure better code quality
we ask both an author and a reviewer to verify the following:
The Review Checklist
- There is a single JIRA ticket related to the pull request.
- The web-link to the pull request is attached to the JIRA ticket.
- The JIRA ticket has the Patch Available state.
- The description of the JIRA ticket explains WHAT was made, WHY and HOW.
- The pull request title is treated as the final commit message. The following pattern must be used: IGNITE-XXXX Change summary where XXXX - number of JIRA issue.
Notes