vlsi commented on code in PR #5283:
URL: https://github.com/apache/calcite/pull/5283#discussion_r4243062684
##########
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:
Every case changes exactly one operand, so nothing checks that a second
change keeps the first replacement. Could you add a case that replaces the
first and the last operand, keeps the middle one, and asserts all three
positions of the result?
##########
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:
Each test here changes at most one operand, so nothing checks that
`visitChild` keeps writing into the `clonedOperands` array the first change
allocated. A case that replaces `operands[0]` and `operands[2]` and asserts all
three operands of the result would cover it.
##########
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:
The JIRA summary is now "Avoid copying unchanged SQL and Rex operands".
```suggestion
* Avoid copying unchanged SQL and Rex operands</a>. */
```
##########
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:
The JIRA summary is now "Avoid copying unchanged SQL and Rex operands", and
this line is also indented one space short.
```suggestion
* Avoid copying unchanged SQL and Rex operands</a>. */
```
--
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]