You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by za...@apache.org on 2021/07/29 11:23:46 UTC

[calcite] branch master updated: [CALCITE-4642] Use RelDataTypeSystem from Config in Planner (Nick Riasanovsky)

This is an automated email from the ASF dual-hosted git repository.

zabetak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 2f7f9bc  [CALCITE-4642] Use RelDataTypeSystem from Config in Planner (Nick Riasanovsky)
2f7f9bc is described below

commit 2f7f9bc43912d0666215f767a60684debed1c1b8
Author: Nicholas J Riasanovsky <ni...@bodo.ai>
AuthorDate: Tue Jun 8 02:19:57 2021 -0700

    [CALCITE-4642] Use RelDataTypeSystem from Config in Planner (Nick Riasanovsky)
    
    Close apache/calcite#2435
---
 .../org/apache/calcite/prepare/PlannerImpl.java    |  5 +-
 .../java/org/apache/calcite/tools/PlannerTest.java | 58 ++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/prepare/PlannerImpl.java b/core/src/main/java/org/apache/calcite/prepare/PlannerImpl.java
index 524e40f..fddafd0 100644
--- a/core/src/main/java/org/apache/calcite/prepare/PlannerImpl.java
+++ b/core/src/main/java/org/apache/calcite/prepare/PlannerImpl.java
@@ -76,6 +76,7 @@ public class PlannerImpl implements Planner, ViewExpander {
   private final @Nullable RelOptCostFactory costFactory;
   private final Context context;
   private final CalciteConnectionConfig connectionConfig;
+  private final RelDataTypeSystem typeSystem;
 
   /** Holds the trait definitions to be registered with planner. May be null. */
   private final @Nullable ImmutableList<RelTraitDef> traitDefs;
@@ -118,6 +119,7 @@ public class PlannerImpl implements Planner, ViewExpander {
     this.executor = config.getExecutor();
     this.context = config.getContext();
     this.connectionConfig = connConfig(context, parserConfig);
+    this.typeSystem = config.getTypeSystem();
     reset();
   }
 
@@ -176,9 +178,6 @@ public class PlannerImpl implements Planner, ViewExpander {
     }
     ensure(State.STATE_1_RESET);
 
-    RelDataTypeSystem typeSystem =
-        connectionConfig.typeSystem(RelDataTypeSystem.class,
-            RelDataTypeSystem.DEFAULT);
     typeFactory = new JavaTypeFactoryImpl(typeSystem);
     RelOptPlanner planner = this.planner = new VolcanoPlanner(costFactory, context);
     RelOptUtil.registerDefaultRules(planner,
diff --git a/core/src/test/java/org/apache/calcite/tools/PlannerTest.java b/core/src/test/java/org/apache/calcite/tools/PlannerTest.java
index f9622c4..1a70376 100644
--- a/core/src/test/java/org/apache/calcite/tools/PlannerTest.java
+++ b/core/src/test/java/org/apache/calcite/tools/PlannerTest.java
@@ -51,8 +51,10 @@ import org.apache.calcite.rel.rules.CoreRules;
 import org.apache.calcite.rel.rules.ProjectMergeRule;
 import org.apache.calcite.rel.rules.PruneEmptyRules;
 import org.apache.calcite.rel.rules.UnionMergeRule;
+import org.apache.calcite.rel.type.DelegatingTypeSystem;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
 import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.schema.impl.ScalarFunctionImpl;
 import org.apache.calcite.sql.SqlAggFunction;
@@ -1525,4 +1527,60 @@ class PlannerTest {
     final RelRoot root = planner.rel(validate);
     assertThat(toString(root.rel), matcher);
   }
+
+  /** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-4642">[CALCITE-4642]
+   * Checks that custom type systems can be registered in a planner by
+   * comparing options for converting unions of chars.</a>.
+   */
+  @Test void testCustomTypeSystem() throws Exception {
+    final String sql = "select Case when DEPTNO <> 30 then 'hi' else 'world' end from dept";
+    final String expectedVarying = "LogicalProject("
+        + "EXPR$0=["
+        + "CASE(<>($0, 30),"
+        + " 'hi':VARCHAR(5), "
+        + "'world':VARCHAR(5))])\n"
+        + "  LogicalValues("
+        + "tuples=[[{ 10, 'Sales' },"
+        + " { 20, 'Marketing' },"
+        + " { 30, 'Engineering' },"
+        + " { 40, 'Empty' }]])\n";
+    final String expectedDefault = ""
+        + "LogicalProject(EXPR$0=[CASE(<>($0, 30), 'hi   ', 'world')])\n"
+        + "  LogicalValues(tuples=[[{ 10, 'Sales      ' }, { 20, 'Marketing  ' }, { 30, 'Engineering' }, { 40, 'Empty      ' }]])\n";
+    assertValidPlan(sql, new VaryingTypeSystem(DelegatingTypeSystem.DEFAULT), is(expectedVarying));
+    assertValidPlan(sql, DelegatingTypeSystem.DEFAULT, is(expectedDefault));
+  }
+
+  /**
+   *  Asserts a Planner generates the correct plan using the provided
+   *  type system.
+   */
+  private void assertValidPlan(String sql, RelDataTypeSystem typeSystem,
+      Matcher<String> planMatcher)  throws SqlParseException,
+      ValidationException, RelConversionException {
+    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
+    final FrameworkConfig config = Frameworks.newConfigBuilder()
+        .defaultSchema(
+            CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.POST))
+        .typeSystem(typeSystem).build();
+    final Planner planner = Frameworks.getPlanner(config);
+    SqlNode parse = planner.parse(sql);
+    final SqlNode validate = planner.validate(parse);
+    final RelRoot root = planner.rel(validate);
+    assertThat(toString(root.rel), planMatcher);
+  }
+
+  /**
+   * Custom type system that converts union of chars to varchars.
+   */
+  private static class VaryingTypeSystem extends DelegatingTypeSystem {
+
+    VaryingTypeSystem(RelDataTypeSystem typeSystem) {
+      super(typeSystem);
+    }
+
+    @Override public boolean shouldConvertRaggedUnionTypesToVarying() {
+      return true;
+    }
+  }
 }