xiedeyantu commented on code in PR #4400:
URL: https://github.com/apache/calcite/pull/4400#discussion_r2122468746
##########
core/src/test/java/org/apache/calcite/test/RexImplicationCheckerTest.java:
##########
@@ -360,6 +361,97 @@ public class RexImplicationCheckerTest {
hasToString("2014"));
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7042">[CALCITE-7042]
+ * Eliminate nested TRIM calls, exploiting the fact that TRIM is
idempotent</a>. */
+ @Test void testSimplifyIdempotentFunctions() {
Review Comment:
I think we should pass a test case like the following, even if the third
parameter in trim is not a trim expression, we should continue to recursively
simplify. The floor I used may not be very appropriate, but I just want to
express this idea.
```
@Test void testSimplifyIdempotentFunctions() {
final Fixture f = new Fixture();
RexLiteral trimBoth = f.rexBuilder.makeLiteral("BOTH");
RexLiteral trimed = f.rexBuilder.makeLiteral("a");
final RexNode literalTs =
f.timestampLiteral(new TimestampString("2010-10-10 00:00:00"));
final RexNode innerFloorCall =
f.rexBuilder.makeCall(SqlStdOperatorTable.FLOOR, literalTs,
f.rexBuilder.makeFlag(TimeUnitRange.YEAR));
final RexNode outerFloorCall =
f.rexBuilder.makeCall(SqlStdOperatorTable.FLOOR, innerFloorCall,
f.rexBuilder.makeFlag(TimeUnitRange.YEAR));
RexCall floorSimplifiedExpr =
(RexCall) f.simplify.simplifyPreservingType(outerFloorCall,
RexUnknownAs.UNKNOWN, true);
assertThat(floorSimplifiedExpr,
hasToString("FLOOR(2010-10-10 00:00:00, FLAG(YEAR))"));
RelDataType varcharType =
f.typeFactory.createSqlType(SqlTypeName.VARCHAR);
RexCall floorSimplifiedExpr2 =
(RexCall)
f.simplify.simplifyPreservingType(f.rexBuilder.makeCast(varcharType,
outerFloorCall),
RexUnknownAs.UNKNOWN, true);
assertThat(floorSimplifiedExpr2,
hasToString("CAST(FLOOR(2010-10-10 00:00:00, FLAG(YEAR))):VARCHAR
NOT NULL"));
RexCall innerTrimCall =
(RexCall) f.rexBuilder.makeCall(SqlStdOperatorTable.TRIM, trimBoth,
trimed,
f.rexBuilder.makeCast(varcharType, outerFloorCall));
RexCall outerTrimCall =
(RexCall) f.rexBuilder.makeCall(SqlStdOperatorTable.TRIM, trimBoth,
trimed, innerTrimCall);
RexCall trimSimplifiedCall =
(RexCall) f.simplify.simplifyPreservingType(outerTrimCall,
RexUnknownAs.UNKNOWN, true);
// We will get this Currently
// assertThat(trimSimplifiedCall,
hasToString("TRIM('BOTH', 'a', CAST(FLOOR(FLOOR(2010-10-10 00:00:00,
"
+ "FLAG(YEAR)), FLAG(YEAR))):VARCHAR NOT NULL)"));
// I think We hope to get like this
assertThat(trimSimplifiedCall,
hasToString("TRIM('BOTH', 'a', CAST(FLOOR(2010-10-10 00:00:00, "
+ "FLAG(YEAR))):VARCHAR NOT NULL)"));
}
```
--
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]