This is an automated email from the ASF dual-hosted git repository.
zabetak pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 6e664e97fa [CALCITE-6883] Add Javadoc for RelRoot#isTrivial variants
and refactor related tests
6e664e97fa is described below
commit 6e664e97fa0dbc09cf48492fb3f2099b1442db2b
Author: Niels Pardon <[email protected]>
AuthorDate: Tue Mar 11 08:26:32 2025 +0100
[CALCITE-6883] Add Javadoc for RelRoot#isTrivial variants and refactor
related tests
Signed-off-by: Niels Pardon <[email protected]>
Close apache/calcite#4239
---
.../main/java/org/apache/calcite/rel/RelRoot.java | 56 ++++++++++
.../java/org/apache/calcite/rel/RelRootTest.java | 122 +++++++++++++++++++++
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 88 ---------------
3 files changed, 178 insertions(+), 88 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/rel/RelRoot.java
b/core/src/main/java/org/apache/calcite/rel/RelRoot.java
index fe15a39047..4eb495e70a 100644
--- a/core/src/main/java/org/apache/calcite/rel/RelRoot.java
+++ b/core/src/main/java/org/apache/calcite/rel/RelRoot.java
@@ -174,11 +174,60 @@ public RelNode project(boolean force) {
ImmutableSet.of());
}
+ /**
+ * Returns true if the field names defined in this RelRoot are the same as
the names of the
+ * embedded relation, otherwise false.
+ *
+ * <p>Positive example (same names):
+ *
+ * <blockquote><code>RelRoot: {
+ * rel: Project(empno)
+ * TableScan(EMP)
+ * fields: [0 -> empno]
+ * collation: []
+ * }</code></blockquote>
+ *
+ * <p>Negative example (different names):
+ *
+ * <blockquote><code>RelRoot: {
+ * rel: Project(empno)
+ * TableScan(EMP)
+ * fields: [0 -> empid]
+ * collation: []
+ * }</code></blockquote>
+ *
+ * @return true if the field names are the same as in the embedded relation,
otherwise false
+ */
public boolean isNameTrivial() {
final RelDataType inputRowType = rel.getRowType();
return fields.rightList().equals(inputRowType.getFieldNames());
}
+ /**
+ * Returns true if the embedded relation is either a DML relation or if the
field order defined
+ * in this RelRoot is the same as the field order of the embedded relation,
otherwise false.
+ *
+ * <p>Positive example (same order):
+ *
+ * <blockquote><code>RelRoot: {
+ * rel: Project(name, empno)
+ * TableScan(EMP)
+ * fields: [0 -> name, 1 -> empno]
+ * collation: []
+ * }</code></blockquote>
+ *
+ * <p>Negative example (different order):
+ *
+ * <blockquote><code>RelRoot: {
+ * rel: Project(name, empno)
+ * TableScan(EMP)
+ * fields: [0 -> empno, 1 -> name]
+ * collation: []
+ * }</code></blockquote>
+ *
+ * @return true if the embedded relation is a DML relation or if the field
names of the RelRoot
+ * are in the same order as in the embedded relation, otherwise false
+ */
public boolean isRefTrivial() {
if (SqlKind.DML.contains(kind)) {
// DML statements return a single count column.
@@ -190,6 +239,13 @@ public boolean isRefTrivial() {
return Mappings.isIdentity(fields.leftList(),
inputRowType.getFieldCount());
}
+ /**
+ * Returns true if the embedded relation has a single collation defined
which matches the
+ * collation of this RelRoot, otherwise false.
+ *
+ * @return true if the embedded relation has a single collation which
matches the collation
+ * of this RelRoot, otherwise false
+ */
public boolean isCollationTrivial() {
final List<RelCollation> collations = rel.getTraitSet()
.getTraits(RelCollationTraitDef.INSTANCE);
diff --git a/core/src/test/java/org/apache/calcite/rel/RelRootTest.java
b/core/src/test/java/org/apache/calcite/rel/RelRootTest.java
new file mode 100644
index 0000000000..9c13336cee
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/rel/RelRootTest.java
@@ -0,0 +1,122 @@
+/*
+ * 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.rel;
+
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
+import org.apache.calcite.rel.type.RelRecordType;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.test.CalciteAssert;
+import org.apache.calcite.test.RelBuilderTest;
+import org.apache.calcite.tools.FrameworkConfig;
+import org.apache.calcite.tools.Frameworks;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Tests for {@link RelRoot}.
+ */
+public class RelRootTest {
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6877">[CALCITE-6877]
+ * Generate LogicalProject in RelRoot.project() when mapping is not name
trivial</a>. */
+ @Test void testRelRootProjectForceNonNameTrivial() {
+ final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
+ final SchemaPlus defaultSchema =
+ CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR);
+ final FrameworkConfig frameworkConfig = RelBuilderTest.config()
+ .defaultSchema(defaultSchema)
+ .build();
+ final RelBuilder relBuilder = RelBuilder.create(frameworkConfig);
+ final RelNode inputRel = relBuilder.scan("emps")
+
.project(relBuilder.fields(Collections.singletonList("empid"))).build();
+
+ final List<RelDataTypeField> fields =
+ Collections.singletonList(
+ // rename empid to empno via RelRoot
+ new RelDataTypeFieldImpl("empno",
+ inputRel.getRowType().getFieldList().get(0).getIndex(),
+ inputRel.getRowType().getFieldList().get(0).getType()));
+
+ final RelRoot root = RelRoot.of(inputRel, new RelRecordType(fields),
SqlKind.SELECT);
+
+ // inner LogicalProject selects one field and RelRoot only has one field
+ assertThat(root.isRefTrivial(), is(true));
+
+ // inner LogicalProject has different field name than RelRoot
+ assertThat(root.isNameTrivial(), is(false));
+
+ final RelNode project = root.project();
+ assertThat(project, equalTo(inputRel));
+
+ // regular project() and force project() are different
+ final RelNode forceProject = root.project(true);
+ assertThat(forceProject, not(equalTo(project)));
+
+ // new LogicalProject on top of inputRel
+ assertThat(forceProject, instanceOf(LogicalProject.class));
+ assertThat(forceProject.getInput(0), equalTo(inputRel));
+
+ // new LogicalProject renames field
+ if (forceProject instanceof LogicalProject) {
+ assertThat(((LogicalProject)
forceProject).getNamedProjects().get(0).getValue(),
+ equalTo("empno"));
+ }
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6877">[CALCITE-6877]
+ * Generate LogicalProject in RelRoot.project() when mapping is not name
trivial</a>. */
+ @Test void testRelRootProjectForceNameTrivial() {
+ final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
+ final SchemaPlus defaultSchema =
+ CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR);
+ final FrameworkConfig frameworkConfig = RelBuilderTest.config()
+ .defaultSchema(defaultSchema)
+ .build();
+ final RelBuilder relBuilder = RelBuilder.create(frameworkConfig);
+ final RelNode inputRel = relBuilder.scan("emps")
+
.project(relBuilder.fields(Collections.singletonList("empid"))).build();
+
+ final RelRoot root = RelRoot.of(inputRel, SqlKind.SELECT);
+
+ // inner LogicalProject selects one field and RelRoot only has one field
+ assertThat(root.isRefTrivial(), is(true));
+
+ // inner LogicalProject has same field name as RelRoot
+ assertThat(root.isNameTrivial(), is(true));
+
+ final RelNode project = root.project();
+ assertThat(project, equalTo(inputRel));
+
+ // regular project() and force project() are the same
+ final RelNode forceProject = root.project(true);
+ assertThat(forceProject, equalTo(project));
+ }
+}
diff --git
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index a70324a8be..2e9c3648ec 100644
---
a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++
b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -27,14 +27,12 @@
import org.apache.calcite.rel.RelFieldCollation.Direction;
import org.apache.calcite.rel.RelFieldCollation.NullDirection;
import org.apache.calcite.rel.RelNode;
-import org.apache.calcite.rel.RelRoot;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.hint.HintPredicates;
import org.apache.calcite.rel.hint.HintStrategyTable;
import org.apache.calcite.rel.hint.RelHint;
import org.apache.calcite.rel.logical.LogicalAggregate;
import org.apache.calcite.rel.logical.LogicalFilter;
-import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.rules.AggregateJoinTransposeRule;
import org.apache.calcite.rel.rules.AggregateProjectMergeRule;
import org.apache.calcite.rel.rules.CoreRules;
@@ -44,18 +42,14 @@
import org.apache.calcite.rel.rules.PruneEmptyRules;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
-import org.apache.calcite.rel.type.RelDataTypeField;
-import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
-import org.apache.calcite.rel.type.RelRecordType;
import org.apache.calcite.runtime.FlatLists;
import org.apache.calcite.runtime.Hook;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlDialect.DatabaseProduct;
-import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.SqlWriter;
@@ -106,7 +100,6 @@
import java.math.BigDecimal;
import java.util.Collection;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -122,10 +115,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasToString;
-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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -9798,84 +9788,6 @@ private void checkLiteral2(String expression, String
expected) {
sql(sql).ok(expected);
}
-
- /** Test case for
- * <a
href="https://issues.apache.org/jira/browse/CALCITE-6877">[CALCITE-6877]
- * Generate LogicalProject in RelRoot.project() when mapping is not name
trivial</a>. */
- @Test void testRelRootProjectForceNonNameTrivial() {
- final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
- final SchemaPlus defaultSchema =
- CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR);
- final FrameworkConfig frameworkConfig = RelBuilderTest.config()
- .defaultSchema(defaultSchema)
- .build();
- final RelBuilder relBuilder = RelBuilder.create(frameworkConfig);
- final RelNode inputRel = relBuilder.scan("emps")
-
.project(relBuilder.fields(Collections.singletonList("empid"))).build();
-
- final List<RelDataTypeField> fields =
- Collections.singletonList(
- // rename empid to empno via RelRoot
- new RelDataTypeFieldImpl("empno",
- inputRel.getRowType().getFieldList().get(0).getIndex(),
- inputRel.getRowType().getFieldList().get(0).getType()));
-
- final RelRoot root = RelRoot.of(inputRel, new RelRecordType(fields),
SqlKind.SELECT);
-
- // inner LogicalProject selects one field and RelRoot only has one field
- assertTrue(root.isRefTrivial());
-
- // inner LogicalProject has different field name than RelRoot
- assertFalse(root.isNameTrivial());
-
- final RelNode project = root.project();
- assertEquals(inputRel, project);
-
- // regular project() and force project() are different
- final RelNode forceProject = root.project(true);
- assertNotEquals(project, forceProject);
-
- // new LogicalProject on top of inputRel
- assertInstanceOf(LogicalProject.class, forceProject);
- assertEquals(inputRel, forceProject.getInput(0));
-
- // new LogicalProject renames field
- if (forceProject instanceof LogicalProject) {
- assertEquals("empno",
- ((LogicalProject)
forceProject).getNamedProjects().get(0).getValue());
- }
- }
-
- /** Test case for
- * <a
href="https://issues.apache.org/jira/browse/CALCITE-6877">[CALCITE-6877]
- * Generate LogicalProject in RelRoot.project() when mapping is not name
trivial</a>. */
- @Test void testRelRootProjectForceNameTrivial() {
- final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
- final SchemaPlus defaultSchema =
- CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR);
- final FrameworkConfig frameworkConfig = RelBuilderTest.config()
- .defaultSchema(defaultSchema)
- .build();
- final RelBuilder relBuilder = RelBuilder.create(frameworkConfig);
- final RelNode inputRel = relBuilder.scan("emps")
-
.project(relBuilder.fields(Collections.singletonList("empid"))).build();
-
- final RelRoot root = RelRoot.of(inputRel, SqlKind.SELECT);
-
- // inner LogicalProject selects one field and RelRoot only has one field
- assertTrue(root.isRefTrivial());
-
- // inner LogicalProject has same field name as RelRoot
- assertTrue(root.isNameTrivial());
-
- final RelNode project = root.project();
- assertEquals(inputRel, project);
-
- // regular project() and force project() are the same
- final RelNode forceProject = root.project(true);
- assertEquals(project, forceProject);
- }
-
/** Test case for
* <a
href="https://issues.apache.org/jira/browse/CALCITE-6825">[CALCITE-6825]
* Add support for ALL, SOME, ANY in RelToSqlConverter</a>. */