mihaibudiu commented on code in PR #5192:
URL: https://github.com/apache/calcite/pull/5192#discussion_r3815732378
##########
linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java:
##########
@@ -1717,6 +1717,94 @@ public void checkBlockBuilder(boolean optimizing, String
expected) {
+ ".add(\"1\").build()"));
}
+ /** Test cases for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7728">[CALCITE-7728]
+ * Linq4j can simplify expressions without regards for 'safety'</a>.
+ *
+ * <p>Checks {@link Expressions#mayThrow} for all possible expressions */
+ @Test void testMayThrow() {
+ final ParameterExpression i = Expressions.parameter(int.class, "i");
+ final ParameterExpression j = Expressions.parameter(int.class, "j");
+ final ParameterExpression o = Expressions.parameter(Object.class, "o");
+ final ParameterExpression str = Expressions.parameter(String.class, "str");
+ final ParameterExpression box = Expressions.parameter(Integer.class,
"box");
+ final ParameterExpression a = Expressions.parameter(int[].class, "a");
+ final ParameterExpression all = Expressions.parameter(AllType.class,
"all");
+
+ // Reading a variable or a constant
+ assertMayThrow(i, false);
+ assertMayThrow(ONE, false);
+
+ // Java arithmetic wraps around; comparison, bit manipulation
+ assertMayThrow(Expressions.add(i, j), false);
+ assertMayThrow(Expressions.multiply(i, j), false);
+ assertMayThrow(Expressions.negate(i), false);
+ assertMayThrow(Expressions.lessThan(i, j), false);
+ assertMayThrow(Expressions.leftShift(i, j), false);
+ assertMayThrow(
+ Expressions.andAlso(Expressions.lessThan(i, j),
+ Expressions.equal(i, j)), false);
+ assertMayThrow(Expressions.typeIs(o, String.class), false);
+ assertMayThrow(Expressions.add(str, str), false);
+ assertMayThrow(Expressions.equal(str, o), false);
+
+ // An operator unboxes its operands, and a null box raises
+ // NullPointerException
+ assertMayThrow(Expressions.add(box, i), true);
+ assertMayThrow(Expressions.negate(box), true);
+ assertMayThrow(Expressions.lessThan(box, i), true);
+ assertMayThrow(Expressions.equal(box, i), true);
+
+ // Division may divide by zero; checked arithmetic may overflow
+ assertMayThrow(Expressions.divide(i, j), true);
+ assertMayThrow(Expressions.modulo(i, j), true);
+ assertMayThrow(Expressions.addChecked(i, j), true);
+ assertMayThrow(Expressions.negateChecked(i), true);
+
+ // An operand that may throw infects the whole expression
+ assertMayThrow(Expressions.add(ONE, Expressions.divide(ONE, i)), true);
+ assertMayThrow(
+ Expressions.condition(Expressions.lessThan(i, j),
+ Expressions.divide(ONE, i), ONE), true);
+
+ // Reading an array element, and the length of an array
+ assertMayThrow(Expressions.arrayIndex(a, i), true);
+ assertMayThrow(Expressions.field(a, "length"), true);
+
+ // Reading an instance field; a static field has no target to be null
+ assertMayThrow(Expressions.field(all, "i"), true);
+ assertMayThrow(Expressions.field(null, Integer.class, "MAX_VALUE"), false);
+
+ // Calling a method or a constructor, and creating an array
+ assertMayThrow(Expressions.call(o, "toString"), true);
+ assertMayThrow(Expressions.new_(Object.class), true);
+ assertMayThrow(Expressions.newArrayBounds(int.class, 1, i), true);
+ assertMayThrow(Expressions.newArrayInit(int.class, ONE, TWO), true);
+
+ // A cast that cannot fail: a primitive conversion, boxing, or a widening
+ // reference conversion
+ assertMayThrow(Expressions.convert_(i, long.class), false);
+ assertMayThrow(Expressions.convert_(i, Integer.class), false);
+ assertMayThrow(Expressions.convert_(str, Object.class), false);
+
+ // A cast that may raise ClassCastException, and unboxing
Review Comment:
I will fix this and squash the commits at the same time.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]