Skip to content

Commit 4804912

Browse files
committed
Address Oliver's comments
1 parent 526d63a commit 4804912

File tree

5 files changed

+76
-76
lines changed

5 files changed

+76
-76
lines changed

core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,9 @@ Builder populate() {
653653

654654
map.put(SAFE_ADD,
655655
new SafeArithmeticImplementor(BuiltInMethod.SAFE_ADD.method));
656-
map.put(SAFE_DIVIDE,
657-
new SafeArithmeticImplementor(BuiltInMethod.SAFE_DIVIDE.method));
658-
map.put(SAFE_MULTIPLY,
656+
map.put(SAFE_DIVIDE,
657+
new SafeArithmeticImplementor(BuiltInMethod.SAFE_DIVIDE.method));
658+
map.put(SAFE_MULTIPLY,
659659
new SafeArithmeticImplementor(BuiltInMethod.SAFE_MULTIPLY.method));
660660
map.put(SAFE_NEGATE,
661661
new SafeArithmeticImplementor(BuiltInMethod.SAFE_MULTIPLY.method));

core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,11 +1733,11 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
17331733
SqlFunctionCategory.NUMERIC);
17341734

17351735
/** The "SAFE_DIVIDE(numeric1, numeric2)" function; equivalent to the {@code /} operator but
1736-
* returns null if an error occurs, such as division by zero. */
1736+
* returns null if an error occurs, such as overflow or division by zero. */
17371737
@LibraryOperator(libraries = {BIG_QUERY})
17381738
public static final SqlFunction SAFE_DIVIDE =
17391739
SqlBasicFunction.create("SAFE_DIVIDE",
1740-
ReturnTypes.DOUBLE_IF_INTEGER.orElse(ReturnTypes.QUOTIENT_FORCE_NULLABLE),
1740+
ReturnTypes.DOUBLE_IF_INTEGERS.orElse(ReturnTypes.QUOTIENT_FORCE_NULLABLE),
17411741
OperandTypes.NUMERIC_NUMERIC,
17421742
SqlFunctionCategory.NUMERIC);
17431743

core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,10 @@ public static SqlCall stripSeparator(SqlCall call) {
827827
DECIMAL_QUOTIENT.andThen(SqlTypeTransforms.TO_NULLABLE);
828828

829829
/**
830-
* Type-inference stratey whereby the result type of a call is
830+
* Type-inference strategy whereby the result type of a call is
831831
* {@link #DOUBLE} if both operands are integer types.
832832
*/
833-
public static final SqlReturnTypeInference DOUBLE_IF_INTEGER = opBinding -> {
833+
public static final SqlReturnTypeInference DOUBLE_IF_INTEGERS = opBinding -> {
834834
RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
835835
SqlTypeName type1 = opBinding.getOperandType(0).getSqlTypeName();
836836
SqlTypeName type2 = opBinding.getOperandType(1).getSqlTypeName();

site/_docs/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,7 @@ BigQuery's type system uses confusingly different names for types and functions:
28002800
| b o | RTRIM(string) | Returns *string* with all blanks removed from the end
28012801
| b | SAFE_ADD(numeric1, numeric2) | Returns *numeric1* + *numeric2*, or NULL on overflow
28022802
| b | SAFE_CAST(value AS type) | Converts *value* to *type*, returning NULL if conversion fails
2803-
| b | SAFE_DIVIDE(numeric1, numeric2) | Returns *numeric1* / *numeric2*, or NULL on overflow or if *numeric2* is zero
2803+
| b | SAFE_DIVIDE(numeric1, numeric2) | Returns *numeric1* / *numeric2*, or NULL on overflow or if *numeric2* is zero
28042804
| b | SAFE_MULTIPLY(numeric1, numeric2) | Returns *numeric1* * *numeric2*, or NULL on overflow
28052805
| b | SAFE_NEGATE(numeric) | Returns *numeric* * -1, or NULL on overflow
28062806
| b | SAFE_OFFSET(index) | Similar to `OFFSET` except null is returned if *index* is out of bounds

testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7408,76 +7408,76 @@ private static void checkIf(SqlOperatorFixture f) {
74087408
@Test void testSafeAddFunc() {
74097409
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.SAFE_ADD);
74107410
f0.checkFails("^safe_add(2, 3)^",
7411-
"No match found for function signature "
7412-
+ "SAFE_ADD\\(<NUMERIC>, <NUMERIC>\\)", false);
7411+
"No match found for function signature "
7412+
+ "SAFE_ADD\\(<NUMERIC>, <NUMERIC>\\)", false);
74137413
final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.BIG_QUERY);
74147414
// Basic test for each of the 9 2-permutations of BIGINT, DECIMAL, and FLOAT
74157415
f.checkScalar("safe_add(cast(20 as bigint), cast(20 as bigint))",
7416-
"40", "BIGINT");
7416+
"40", "BIGINT");
74177417
f.checkScalar("safe_add(cast(20 as bigint), cast(1.2345 as decimal(5,4)))",
7418-
"21.2345", "DECIMAL(19, 4)");
7418+
"21.2345", "DECIMAL(19, 4)");
74197419
f.checkScalar("safe_add(cast(1.2345 as decimal(5,4)), cast(20 as bigint))",
7420-
"21.2345", "DECIMAL(19, 4)");
7421-
f.checkScalar("safe_add(cast(1.2345 as decimal(5,4)), "
7422-
+ "cast(2.0 as decimal(2, 1)))", "3.2345", "DECIMAL(6, 4)");
7420+
"21.2345", "DECIMAL(19, 4)");
7421+
f.checkScalar("safe_add(cast(1.2345 as decimal(5,4)), cast(2.0 as decimal(2, 1)))",
7422+
"3.2345", "DECIMAL(6, 4)");
74237423
f.checkScalar("safe_add(cast(3 as double), cast(3 as bigint))",
7424-
"6.0", "DOUBLE");
7424+
"6.0", "DOUBLE");
74257425
f.checkScalar("safe_add(cast(3 as bigint), cast(3 as double))",
7426-
"6.0", "DOUBLE");
7426+
"6.0", "DOUBLE");
74277427
f.checkScalar("safe_add(cast(3 as double), cast(1.2345 as decimal(5, 4)))",
7428-
"4.2345", "DOUBLE");
7428+
"4.2345", "DOUBLE");
74297429
f.checkScalar("safe_add(cast(1.2345 as decimal(5, 4)), cast(3 as double))",
7430-
"4.2345", "DOUBLE");
7430+
"4.2345", "DOUBLE");
74317431
f.checkScalar("safe_add(cast(3 as double), cast(3 as double))",
7432-
"6.0", "DOUBLE");
7432+
"6.0", "DOUBLE");
74337433
// Tests for + and - Infinity
74347434
f.checkScalar("safe_add(cast('Infinity' as double), cast(3 as double))",
7435-
"Infinity", "DOUBLE");
7435+
"Infinity", "DOUBLE");
74367436
f.checkScalar("safe_add(cast('-Infinity' as double), cast(3 as double))",
7437-
"-Infinity", "DOUBLE");
7437+
"-Infinity", "DOUBLE");
74387438
f.checkScalar("safe_add(cast('-Infinity' as double), "
7439-
+ "cast('Infinity' as double))", "NaN", "DOUBLE");
7439+
+ "cast('Infinity' as double))", "NaN", "DOUBLE");
74407440
// Tests for NaN
74417441
f.checkScalar("safe_add(cast('NaN' as double), cast(3 as bigint))",
7442-
"NaN", "DOUBLE");
7442+
"NaN", "DOUBLE");
74437443
f.checkScalar("safe_add(cast('NaN' as double), cast(1.23 as decimal(3, 2)))",
7444-
"NaN", "DOUBLE");
7444+
"NaN", "DOUBLE");
74457445
f.checkScalar("safe_add(cast('NaN' as double), cast('Infinity' as double))",
7446-
"NaN", "DOUBLE");
7446+
"NaN", "DOUBLE");
74477447
f.checkScalar("safe_add(cast(3 as bigint), cast('NaN' as double))",
7448-
"NaN", "DOUBLE");
7448+
"NaN", "DOUBLE");
74497449
f.checkScalar("safe_add(cast(1.23 as decimal(3, 2)), cast('NaN' as double))",
7450-
"NaN", "DOUBLE");
7450+
"NaN", "DOUBLE");
74517451
// Overflow test for each pairing
74527452
f.checkNull("safe_add(cast(20 as bigint), "
7453-
+ "cast(9223372036854775807 as bigint))");
7453+
+ "cast(9223372036854775807 as bigint))");
74547454
f.checkNull("safe_add(cast(-20 as bigint), "
7455-
+ "cast(-9223372036854775807 as bigint))");
7455+
+ "cast(-9223372036854775807 as bigint))");
74567456
f.checkNull("safe_add(9, cast(9.999999999999999999e75 as DECIMAL(38, 19)))");
74577457
f.checkNull("safe_add(-9, cast(-9.999999999999999999e75 as DECIMAL(38, 19)))");
74587458
f.checkNull("safe_add(cast(9.999999999999999999e75 as DECIMAL(38, 19)), 9)");
74597459
f.checkNull("safe_add(cast(-9.999999999999999999e75 as DECIMAL(38, 19)), -9)");
74607460
f.checkNull("safe_add(cast(9.9e75 as DECIMAL(76, 0)), "
7461-
+ "cast(9.9e75 as DECIMAL(76, 0)))");
7461+
+ "cast(9.9e75 as DECIMAL(76, 0)))");
74627462
f.checkNull("safe_add(cast(-9.9e75 as DECIMAL(76, 0)), "
7463-
+ "cast(-9.9e75 as DECIMAL(76, 0)))");
7463+
+ "cast(-9.9e75 as DECIMAL(76, 0)))");
74647464
f.checkNull("safe_add(cast(1.7976931348623157e308 as double), "
7465-
+ "cast(9.9e7 as decimal(76, 0)))");
7465+
+ "cast(9.9e7 as decimal(76, 0)))");
74667466
f.checkNull("safe_add(cast(-1.7976931348623157e308 as double), "
7467-
+ "cast(-9.9e7 as decimal(76, 0)))");
7467+
+ "cast(-9.9e7 as decimal(76, 0)))");
74687468
f.checkNull("safe_add(cast(9.9e7 as decimal(76, 0)), "
7469-
+ "cast(1.7976931348623157e308 as double))");
7469+
+ "cast(1.7976931348623157e308 as double))");
74707470
f.checkNull("safe_add(cast(-9.9e7 as decimal(76, 0)), "
7471-
+ "cast(-1.7976931348623157e308 as double))");
7471+
+ "cast(-1.7976931348623157e308 as double))");
74727472
f.checkNull("safe_add(cast(1.7976931348623157e308 as double), cast(3 as bigint))");
74737473
f.checkNull("safe_add(cast(-1.7976931348623157e308 as double), "
7474-
+ "cast(-3 as bigint))");
7474+
+ "cast(-3 as bigint))");
74757475
f.checkNull("safe_add(cast(3 as bigint), cast(1.7976931348623157e308 as double))");
74767476
f.checkNull("safe_add(cast(-3 as bigint), "
7477-
+ "cast(-1.7976931348623157e308 as double))");
7477+
+ "cast(-1.7976931348623157e308 as double))");
74787478
f.checkNull("safe_add(cast(3 as double), cast(1.7976931348623157e308 as double))");
74797479
f.checkNull("safe_add(cast(-3 as double), "
7480-
+ "cast(-1.7976931348623157e308 as double))");
7480+
+ "cast(-1.7976931348623157e308 as double))");
74817481
// Check that null argument retuns null
74827482
f.checkNull("safe_add(cast(null as double), cast(3 as bigint))");
74837483
f.checkNull("safe_add(cast(3 as double), cast(null as bigint))");
@@ -7486,8 +7486,8 @@ private static void checkIf(SqlOperatorFixture f) {
74867486
@Test void testSafeDivideFunc() {
74877487
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.SAFE_DIVIDE);
74887488
f0.checkFails("^safe_divide(2, 3)^",
7489-
"No match found for function signature "
7490-
+ "SAFE_DIVIDE\\(<NUMERIC>, <NUMERIC>\\)", false);
7489+
"No match found for function signature "
7490+
+ "SAFE_DIVIDE\\(<NUMERIC>, <NUMERIC>\\)", false);
74917491
final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.BIG_QUERY);
74927492
// Basic test for each of the 9 2-permutations of BIGINT, DECIMAL, and FLOAT
74937493
f.checkScalar("safe_divide(cast(2 as bigint), cast(4 as bigint))",
@@ -7633,8 +7633,8 @@ private static void checkIf(SqlOperatorFixture f) {
76337633
@Test void testSafeNegateFunc() {
76347634
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.SAFE_NEGATE);
76357635
f0.checkFails("^safe_negate(2)^",
7636-
"No match found for function signature "
7637-
+ "SAFE_NEGATE\\(<NUMERIC>\\)", false);
7636+
"No match found for function signature "
7637+
+ "SAFE_NEGATE\\(<NUMERIC>\\)", false);
76387638
final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.BIG_QUERY);
76397639
f.checkScalar("safe_negate(cast(20 as bigint))", "-20",
76407640
"BIGINT");
@@ -7668,79 +7668,79 @@ private static void checkIf(SqlOperatorFixture f) {
76687668
@Test void testSafeSubtractFunc() {
76697669
final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.SAFE_SUBTRACT);
76707670
f0.checkFails("^safe_subtract(2, 3)^",
7671-
"No match found for function signature "
7672-
+ "SAFE_SUBTRACT\\(<NUMERIC>, <NUMERIC>\\)", false);
7671+
"No match found for function signature "
7672+
+ "SAFE_SUBTRACT\\(<NUMERIC>, <NUMERIC>\\)", false);
76737673
final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.BIG_QUERY);
76747674
// Basic test for each of the 9 2-permutations of BIGINT, DECIMAL, and FLOAT
76757675
f.checkScalar("safe_subtract(cast(20 as bigint), cast(20 as bigint))",
7676-
"0", "BIGINT");
7676+
"0", "BIGINT");
76777677
f.checkScalar("safe_subtract(cast(20 as bigint), cast(-1.2345 as decimal(5,4)))",
7678-
"21.2345", "DECIMAL(19, 4)");
7678+
"21.2345", "DECIMAL(19, 4)");
76797679
f.checkScalar("safe_subtract(cast(1.2345 as decimal(5,4)), cast(-20 as bigint))",
7680-
"21.2345", "DECIMAL(19, 4)");
7680+
"21.2345", "DECIMAL(19, 4)");
76817681
f.checkScalar("safe_subtract(cast(1.23 as decimal(3,2)), "
7682-
+ "cast(-2.0 as decimal(2, 1)))", "3.23", "DECIMAL(4, 2)");
7682+
+ "cast(-2.0 as decimal(2, 1)))", "3.23", "DECIMAL(4, 2)");
76837683
f.checkScalar("safe_subtract(cast(3 as double), cast(-3 as bigint))",
7684-
"6.0", "DOUBLE");
7684+
"6.0", "DOUBLE");
76857685
f.checkScalar("safe_subtract(cast(3 as bigint), cast(-3 as double))",
7686-
"6.0", "DOUBLE");
7686+
"6.0", "DOUBLE");
76877687
f.checkScalar("safe_subtract(cast(3 as double), cast(-1.2345 as decimal(5, 4)))",
7688-
"4.2345", "DOUBLE");
7688+
"4.2345", "DOUBLE");
76897689
f.checkScalar("safe_subtract(cast(1.2345 as decimal(5, 4)), cast(-3 as double))",
7690-
"4.2345", "DOUBLE");
7690+
"4.2345", "DOUBLE");
76917691
f.checkScalar("safe_subtract(cast(3 as double), cast(3 as double))",
7692-
"0.0", "DOUBLE");
7692+
"0.0", "DOUBLE");
76937693
// Tests for + and - Infinity
76947694
f.checkScalar("safe_subtract(cast('Infinity' as double), cast(3 as double))",
7695-
"Infinity", "DOUBLE");
7695+
"Infinity", "DOUBLE");
76967696
f.checkScalar("safe_subtract(cast('-Infinity' as double), cast(3 as double))",
7697-
"-Infinity", "DOUBLE");
7697+
"-Infinity", "DOUBLE");
76987698
f.checkScalar("safe_subtract(cast('Infinity' as double), "
7699-
+ "cast('Infinity' as double))", "NaN", "DOUBLE");
7699+
+ "cast('Infinity' as double))", "NaN", "DOUBLE");
77007700
// Tests for NaN
77017701
f.checkScalar("safe_subtract(cast('NaN' as double), cast(3 as bigint))",
7702-
"NaN", "DOUBLE");
7702+
"NaN", "DOUBLE");
77037703
f.checkScalar("safe_subtract(cast('NaN' as double), cast(1.23 as decimal(3, 2)))",
7704-
"NaN", "DOUBLE");
7704+
"NaN", "DOUBLE");
77057705
f.checkScalar("safe_subtract(cast('NaN' as double), cast('Infinity' as double))",
7706-
"NaN", "DOUBLE");
7706+
"NaN", "DOUBLE");
77077707
f.checkScalar("safe_subtract(cast(3 as bigint), cast('NaN' as double))",
7708-
"NaN", "DOUBLE");
7708+
"NaN", "DOUBLE");
77097709
f.checkScalar("safe_subtract(cast(1.23 as decimal(3, 2)), cast('NaN' as double))",
7710-
"NaN", "DOUBLE");
7710+
"NaN", "DOUBLE");
77117711
// Overflow test for each pairing
77127712
f.checkNull("safe_subtract(cast(20 as bigint), "
7713-
+ "cast(-9223372036854775807 as bigint))");
7713+
+ "cast(-9223372036854775807 as bigint))");
77147714
f.checkNull("safe_subtract(cast(-20 as bigint), "
7715-
+ "cast(9223372036854775807 as bigint))");
7715+
+ "cast(9223372036854775807 as bigint))");
77167716
f.checkNull("safe_subtract(9, cast(-9.999999999999999999e75 as DECIMAL(38, 19)))");
77177717
f.checkNull("safe_subtract(-9, cast(9.999999999999999999e75 as DECIMAL(38, 19)))");
77187718
f.checkNull("safe_subtract(cast(-9.999999999999999999e75 as DECIMAL(38, 19)), 9)");
77197719
f.checkNull("safe_subtract(cast(9.999999999999999999e75 as DECIMAL(38, 19)), -9)");
77207720
f.checkNull("safe_subtract(cast(-9.9e75 as DECIMAL(76, 0)), "
7721-
+ "cast(9.9e75 as DECIMAL(76, 0)))");
7721+
+ "cast(9.9e75 as DECIMAL(76, 0)))");
77227722
f.checkNull("safe_subtract(cast(9.9e75 as DECIMAL(76, 0)), "
7723-
+ "cast(-9.9e75 as DECIMAL(76, 0)))");
7723+
+ "cast(-9.9e75 as DECIMAL(76, 0)))");
77247724
f.checkNull("safe_subtract(cast(1.7976931348623157e308 as double), "
7725-
+ "cast(-9.9e7 as decimal(76, 0)))");
7725+
+ "cast(-9.9e7 as decimal(76, 0)))");
77267726
f.checkNull("safe_subtract(cast(-1.7976931348623157e308 as double), "
7727-
+ "cast(9.9e7 as decimal(76, 0)))");
7727+
+ "cast(9.9e7 as decimal(76, 0)))");
77287728
f.checkNull("safe_subtract(cast(9.9e7 as decimal(76, 0)), "
7729-
+ "cast(-1.7976931348623157e308 as double))");
7729+
+ "cast(-1.7976931348623157e308 as double))");
77307730
f.checkNull("safe_subtract(cast(-9.9e7 as decimal(76, 0)), "
7731-
+ "cast(1.7976931348623157e308 as double))");
7731+
+ "cast(1.7976931348623157e308 as double))");
77327732
f.checkNull("safe_subtract(cast(1.7976931348623157e308 as double), "
7733-
+ "cast(-3 as bigint))");
7733+
+ "cast(-3 as bigint))");
77347734
f.checkNull("safe_subtract(cast(-1.7976931348623157e308 as double), "
7735-
+ "cast(3 as bigint))");
7735+
+ "cast(3 as bigint))");
77367736
f.checkNull("safe_subtract(cast(3 as bigint), "
7737-
+ "cast(-1.7976931348623157e308 as double))");
7737+
+ "cast(-1.7976931348623157e308 as double))");
77387738
f.checkNull("safe_subtract(cast(-3 as bigint), "
7739-
+ "cast(1.7976931348623157e308 as double))");
7739+
+ "cast(1.7976931348623157e308 as double))");
77407740
f.checkNull("safe_subtract(cast(3 as double), "
7741-
+ "cast(-1.7976931348623157e308 as double))");
7741+
+ "cast(-1.7976931348623157e308 as double))");
77427742
f.checkNull("safe_subtract(cast(-3 as double), "
7743-
+ "cast(1.7976931348623157e308 as double))");
7743+
+ "cast(1.7976931348623157e308 as double))");
77447744
// Check that null argument retuns null
77457745
f.checkNull("safe_subtract(cast(null as double), cast(3 as bigint))");
77467746
f.checkNull("safe_subtract(cast(3 as double), cast(null as bigint))");

0 commit comments

Comments
 (0)