FrankChen021 commented on code in PR #5283:
URL: https://github.com/apache/calcite/pull/5283#discussion_r4335544613


##########
core/src/test/java/org/apache/calcite/test/RexShuttleTest.java:
##########
@@ -32,16 +32,136 @@
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.tools.RelBuilder;
 
+import com.google.common.collect.ImmutableList;
+
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.util.ArrayList;
+import java.util.List;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Unit tests for {@link RexShuttle}.
  */
 class RexShuttleTest {
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7809";>[CALCITE-7809]
+   * Reduce temporary object allocation in expression shuttles</a>. */

Review Comment:
   Updated the JIRA summary.



##########
core/src/test/java/org/apache/calcite/sql/util/SqlShuttleTest.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql.util;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import static java.util.Objects.requireNonNull;
+
+/** Unit tests for {@link SqlShuttle}. */
+class SqlShuttleTest {
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7809";>[CALCITE-7809]
+  * Reduce temporary object allocation in expression shuttles</a>. */

Review Comment:
   Updated the JIRA summary and fixed the indentation.



##########
core/src/test/java/org/apache/calcite/test/RexShuttleTest.java:
##########
@@ -32,16 +32,136 @@
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.tools.RelBuilder;
 
+import com.google.common.collect.ImmutableList;
+
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.util.ArrayList;
+import java.util.List;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Unit tests for {@link RexShuttle}.
  */
 class RexShuttleTest {
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7809";>[CALCITE-7809]
+   * Reduce temporary object allocation in expression shuttles</a>. */
+  @Test void testVisitListReusesUnchangedImmutableList() {
+    final RelDataType type = createIntegerType();
+    final ImmutableList<RexNode> operands = createInputRefs(type, 3);
+    final RexNode absent = new RexInputRef(3, type);
+    final boolean[] update = {false};
+
+    final List<RexNode> result =
+        new ListVisitingShuttle(absent, absent)
+            .visitListForTest(operands, update);
+
+    assertSame(operands, result);
+    assertFalse(update[0]);
+  }
+
+  @ParameterizedTest
+  @ValueSource(ints = {0, 1, 2})
+  void testVisitListCopiesOnFirstChange(int changedIndex) {

Review Comment:
   Added testVisitListCopiesOnMultipleChanges, which replaces the first and 
last operands in one traversal and asserts all three positions.



##########
core/src/test/java/org/apache/calcite/sql/util/SqlShuttleTest.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql.util;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import static java.util.Objects.requireNonNull;
+
+/** Unit tests for {@link SqlShuttle}. */
+class SqlShuttleTest {
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7809";>[CALCITE-7809]
+  * Reduce temporary object allocation in expression shuttles</a>. */
+  @Test void testUnchangedCallIsReused() {
+    final SqlLiteral[] operands = createLiterals();
+    final SqlCall call = createCall(operands);
+    final SqlLiteral absent = SqlLiteral.createExactNumeric("4", 
SqlParserPos.ZERO);
+
+    final SqlNode result =
+        requireNonNull(call.accept(new ReplacingSqlShuttle(absent, absent)));
+
+    assertSame(call, result);
+  }
+
+  @Test void testChangedOperandCopiesCall() {

Review Comment:
   Added testChangedOperandsCopyCall, which replaces operands 0 and 2 in one 
traversal and asserts all three result operands.



-- 
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]

Reply via email to