xuzifu666 commented on code in PR #4400:
URL: https://github.com/apache/calcite/pull/4400#discussion_r2122536318
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2364,6 +2366,43 @@ private RexNode simplifyCeilFloor(RexCall e) {
ImmutableList.of(operand, e.getOperands().get(1)));
}
+ /** Simplify TRIM function by eliminating nested duplication.
+ *
+ * <p>Examples:
+ * <ul>
+ *
+ * <li>{@code trim(trim(' aa '))} returns {@code trim(' aa ')}
+ *
+ * <li>{@code trim(BOTH ' ' from trim(BOTH ' ' from ' aa '))}
+ * returns {@code trim(BOTH ' ' from ' aa ')}
+ *
+ * <li>{@code trim(LEADING 'a' from trim(BOTH ' ' from ' aa '))} does not
change
+ *
+ * </ul>
+ */
+ private RexNode simplifyTrim(RexCall e) {
+ if (e.getOperands().size() != 3) {
+ return e;
+ }
+
+ if (e.getOperands().get(2) instanceof RexCall) {
+ RexCall childNode = (RexCall) e.getOperands().get(2);
+ // only strings with the same trim method and deduplication will be
eliminated.
+ if (childNode.getKind() == SqlKind.TRIM
+ &&
simplify(e.operands.get(0)).equals(simplify(childNode.operands.get(0)))
+ &&
simplify(e.operands.get(1)).equals(simplify(childNode.operands.get(1)))) {
+ return simplifyTrim(childNode);
+ }
+ }
+
+ ArrayList<RexNode> rexNodes = new ArrayList<>();
+ for (RexNode node : e.operands) {
+ rexNodes.add(simplify(node));
Review Comment:
OK, had optimze it.
##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2364,6 +2366,43 @@ private RexNode simplifyCeilFloor(RexCall e) {
ImmutableList.of(operand, e.getOperands().get(1)));
}
+ /** Simplify TRIM function by eliminating nested duplication.
+ *
+ * <p>Examples:
+ * <ul>
+ *
+ * <li>{@code trim(trim(' aa '))} returns {@code trim(' aa ')}
+ *
+ * <li>{@code trim(BOTH ' ' from trim(BOTH ' ' from ' aa '))}
+ * returns {@code trim(BOTH ' ' from ' aa ')}
+ *
+ * <li>{@code trim(LEADING 'a' from trim(BOTH ' ' from ' aa '))} does not
change
+ *
+ * </ul>
+ */
+ private RexNode simplifyTrim(RexCall e) {
+ if (e.getOperands().size() != 3) {
+ return e;
+ }
+
+ if (e.getOperands().get(2) instanceof RexCall) {
+ RexCall childNode = (RexCall) e.getOperands().get(2);
+ // only strings with the same trim method and deduplication will be
eliminated.
+ if (childNode.getKind() == SqlKind.TRIM
+ &&
simplify(e.operands.get(0)).equals(simplify(childNode.operands.get(0)))
+ &&
simplify(e.operands.get(1)).equals(simplify(childNode.operands.get(1)))) {
+ return simplifyTrim(childNode);
+ }
+ }
+
+ ArrayList<RexNode> rexNodes = new ArrayList<>();
+ for (RexNode node : e.operands) {
+ rexNodes.add(simplify(node));
Review Comment:
OK, had optimzed it.
--
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]