|
24 | 24 | import org.apache.calcite.rex.RexSimplify; |
25 | 25 | import org.apache.calcite.rex.RexUnknownAs; |
26 | 26 | import org.apache.calcite.sql.SqlKind; |
| 27 | +import org.apache.calcite.sql.SqlOperator; |
27 | 28 | import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
28 | 29 | import org.apache.calcite.sql.type.SqlTypeName; |
29 | 30 | import org.apache.calcite.util.DateString; |
|
35 | 36 |
|
36 | 37 | import org.junit.jupiter.api.Test; |
37 | 38 |
|
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
| 41 | + |
38 | 42 | import static org.apache.calcite.test.RexImplicationCheckerFixtures.Fixture; |
39 | 43 |
|
40 | 44 | import static org.hamcrest.CoreMatchers.is; |
| 45 | +import static org.hamcrest.CoreMatchers.not; |
41 | 46 | import static org.hamcrest.MatcherAssert.assertThat; |
42 | 47 | import static org.hamcrest.Matchers.hasToString; |
43 | 48 |
|
@@ -361,6 +366,73 @@ public class RexImplicationCheckerTest { |
361 | 366 | hasToString("2014")); |
362 | 367 | } |
363 | 368 |
|
| 369 | + /** Test case for |
| 370 | + * <a href="https://issues.apache.org/jira/browse/CALCITE-7122">[CALCITE-7122] |
| 371 | + * Eliminate nested calls for idempotent unary functions UPPER/LOWER/ABS/INITCAP</a>. */ |
| 372 | + @Test void testSimplifyIdempotentUnaryFunctions() { |
| 373 | + // Test that: |
| 374 | + // upper(upper(x)) is simplied to upper(x) |
| 375 | + // lower(upper(x)) is simplied to lower(x) |
| 376 | + // initcap(upper(x)) is simplied to initcap(x) |
| 377 | + final Fixture f = new Fixture(); |
| 378 | + List<SqlOperator> testOeratorList = |
| 379 | + Arrays.asList(SqlStdOperatorTable.UPPER, |
| 380 | + SqlStdOperatorTable.LOWER, |
| 381 | + SqlStdOperatorTable.INITCAP); |
| 382 | + for (SqlOperator operator : testOeratorList) { |
| 383 | + RexCall innerCall = |
| 384 | + (RexCall) f.rexBuilder |
| 385 | + .makeCall(operator, f.rexBuilder.makeLiteral("Calcite Test")); |
| 386 | + RexCall outerCall = |
| 387 | + (RexCall) f.rexBuilder.makeCall(operator, innerCall); |
| 388 | + RexCall simplifiedInnerCall = |
| 389 | + (RexCall) f.simplify.simplifyPreservingType(outerCall, |
| 390 | + RexUnknownAs.UNKNOWN, true); |
| 391 | + |
| 392 | + assertThat(((RexLiteral) simplifiedInnerCall.getOperands().get(0)) |
| 393 | + .getValue(), |
| 394 | + is(((RexLiteral) innerCall.getOperands().get(0)).getValue())); |
| 395 | + } |
| 396 | + |
| 397 | + // Test that abs(abs(x)) is simplied to abs(x) |
| 398 | + RelDataType intType = f.rexBuilder.getTypeFactory().createSqlType(SqlTypeName.INTEGER); |
| 399 | + RexCall innerCall = |
| 400 | + (RexCall) f.rexBuilder |
| 401 | + .makeCall(SqlStdOperatorTable.ABS, f.rexBuilder.makeLiteral(12, intType)); |
| 402 | + RexCall outerCall = |
| 403 | + (RexCall) f.rexBuilder |
| 404 | + .makeCall(SqlStdOperatorTable.ABS, innerCall); |
| 405 | + RexCall simplifiedInnerCall = |
| 406 | + (RexCall) f.simplify.simplifyPreservingType(outerCall, |
| 407 | + RexUnknownAs.UNKNOWN, true); |
| 408 | + |
| 409 | + assertThat(((RexLiteral) simplifiedInnerCall.getOperands().get(0)).getValue(), |
| 410 | + is(((RexLiteral) innerCall.getOperands().get(0)).getValue())); |
| 411 | + |
| 412 | + // Test that max(abs(abs(x))) is simplied to max(abs(x)) |
| 413 | + RexCall maxCall = |
| 414 | + (RexCall) f.rexBuilder |
| 415 | + .makeCall(SqlStdOperatorTable.MAX, outerCall); |
| 416 | + simplifiedInnerCall = |
| 417 | + (RexCall) f.simplify.simplifyPreservingType(maxCall, |
| 418 | + RexUnknownAs.UNKNOWN, true); |
| 419 | + assertThat(simplifiedInnerCall.getOperator(), is(SqlStdOperatorTable.MAX)); |
| 420 | + assertThat(simplifiedInnerCall.getOperands().get(0), is(innerCall)); |
| 421 | + assertThat(simplifiedInnerCall.getOperands().get(0), not(outerCall)); |
| 422 | + |
| 423 | + // negative test |
| 424 | + innerCall = (RexCall) f.rexBuilder |
| 425 | + .makeCall(SqlStdOperatorTable.UPPER, f.rexBuilder.makeLiteral("Calcite Test")); |
| 426 | + outerCall = (RexCall) f.rexBuilder.makeCall(SqlStdOperatorTable.LOWER, innerCall); |
| 427 | + simplifiedInnerCall = (RexCall) f.simplify |
| 428 | + .simplifyPreservingType(outerCall, RexUnknownAs.UNKNOWN, true); |
| 429 | + |
| 430 | + assertThat(simplifiedInnerCall.getOperands().get(0), |
| 431 | + not(((RexLiteral) innerCall.getOperands().get(0)).getValue())); |
| 432 | + assertThat(((RexCall) simplifiedInnerCall.getOperands().get(0)).getOperands().get(0), |
| 433 | + is(innerCall.getOperands().get(0))); |
| 434 | + } |
| 435 | + |
364 | 436 | /** Test case for |
365 | 437 | * <a href="https://issues.apache.org/jira/browse/CALCITE-7042">[CALCITE-7042] |
366 | 438 | * Eliminate nested TRIM calls, exploiting the fact that TRIM is idempotent</a>. */ |
|
0 commit comments