You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2015/06/10 00:08:06 UTC

[2/7] incubator-calcite git commit: Separate TableFactory from suggested table name, so one TableFactory can be used for several tables

Separate TableFactory from suggested table name, so one TableFactory can be used for several tables

Make TableFactory.createTable return Table, not TableEntry; the client now needs to choose a name and register it in a schema.


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/3aec3f82
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/3aec3f82
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/3aec3f82

Branch: refs/heads/master
Commit: 3aec3f8208828eca530685b4e1eef16db6146f9d
Parents: 8b53dc4
Author: Julian Hyde <jh...@apache.org>
Authored: Thu Jun 4 21:27:25 2015 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Fri Jun 5 10:10:33 2015 -0700

----------------------------------------------------------------------
 .../org/apache/calcite/jdbc/CalciteSchema.java  | 17 +++--
 .../materialize/MaterializationService.java     | 71 +++++++-------------
 2 files changed, 37 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/3aec3f82/core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java b/core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java
index 081ed85..3ce35b0 100644
--- a/core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java
+++ b/core/src/main/java/org/apache/calcite/jdbc/CalciteSchema.java
@@ -138,6 +138,11 @@ public class CalciteSchema {
     return rootSchema;
   }
 
+  /** Creates a TableEntryImpl with no SQLs. */
+  private TableEntryImpl tableEntry(String name, Table table) {
+    return new TableEntryImpl(this, name, table, ImmutableList.<String>of());
+  }
+
   /** Defines a table within this schema. */
   public TableEntry add(String tableName, Table table) {
     return add(tableName, table, ImmutableList.<String>of());
@@ -273,7 +278,7 @@ public class CalciteSchema {
       if (implicitTableCache.get(now).contains(tableName)) {
         final Table table = schema.getTable(tableName);
         if (table != null) {
-          return new TableEntryImpl(this, tableName, table, ImmutableList.<String>of());
+          return tableEntry(tableName, table);
         }
       }
       return null;
@@ -292,7 +297,7 @@ public class CalciteSchema {
       if (tableName2 != null) {
         final Table table = schema.getTable(tableName2);
         if (table != null) {
-          return new TableEntryImpl(this, tableName2, table, ImmutableList.<String>of());
+          return tableEntry(tableName2, table);
         }
       }
       return null;
@@ -454,14 +459,14 @@ public class CalciteSchema {
         if (function instanceof TableMacro) {
           assert function.getParameters().isEmpty();
           final Table table = ((TableMacro) function).apply(ImmutableList.of());
-          return new TableEntryImpl(this, tableName, table, ImmutableList.<String>of());
+          return tableEntry(tableName, table);
         }
       }
       for (Function function : schema.getFunctions(tableName)) {
         if (function instanceof TableMacro
             && function.getParameters().isEmpty()) {
           final Table table = ((TableMacro) function).apply(ImmutableList.of());
-          return new TableEntryImpl(this, tableName, table, ImmutableList.<String>of());
+          return tableEntry(tableName, table);
         }
       }
     } else {
@@ -471,7 +476,7 @@ public class CalciteSchema {
         if (function instanceof TableMacro) {
           assert function.getParameters().isEmpty();
           final Table table = ((TableMacro) function).apply(ImmutableList.of());
-          return new TableEntryImpl(this, tableName, table, ImmutableList.<String>of());
+          return tableEntry(tableName, table);
         }
       }
       final NavigableSet<String> set =
@@ -482,7 +487,7 @@ public class CalciteSchema {
               && function.getParameters().isEmpty()) {
             final Table table =
                 ((TableMacro) function).apply(ImmutableList.of());
-            return new TableEntryImpl(this, tableName, table, ImmutableList.<String>of());
+            return tableEntry(tableName, table);
           }
         }
       }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/3aec3f82/core/src/main/java/org/apache/calcite/materialize/MaterializationService.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/materialize/MaterializationService.java b/core/src/main/java/org/apache/calcite/materialize/MaterializationService.java
index 8776271..b594cca 100644
--- a/core/src/main/java/org/apache/calcite/materialize/MaterializationService.java
+++ b/core/src/main/java/org/apache/calcite/materialize/MaterializationService.java
@@ -87,24 +87,23 @@ public class MaterializationService {
       };
 
   private final MaterializationActor actor = new MaterializationActor();
+  private final DefaultTableFactory tableFactory = new DefaultTableFactory();
 
   private MaterializationService() {
   }
 
   /** Defines a new materialization. Returns its key. */
   public MaterializationKey defineMaterialization(final CalciteSchema schema,
-                                                  TileKey tileKey, String viewSql,
-                                                  List<String> viewSchemaPath,
-                                                  final String suggestedTableName, boolean create) {
-    TableFactory tableFactory =
-        new MaterializationService.DefaultTableFactory(suggestedTableName);
-    return defineMaterialization(schema, tileKey, viewSql, viewSchemaPath, tableFactory, create);
+      TileKey tileKey, String viewSql, List<String> viewSchemaPath,
+      final String suggestedTableName, boolean create) {
+    return defineMaterialization(schema, tileKey, viewSql, viewSchemaPath,
+        suggestedTableName, tableFactory, create);
   }
 
   /** Defines a new materialization. Returns its key. */
   public MaterializationKey defineMaterialization(final CalciteSchema schema,
       TileKey tileKey, String viewSql, List<String> viewSchemaPath,
-      final TableFactory tableFactory, boolean create) {
+      String suggestedTableName, TableFactory tableFactory, boolean create) {
     final MaterializationActor.QueryKey queryKey =
         new MaterializationActor.QueryKey(viewSql, schema, viewSchemaPath);
     final MaterializationKey existingKey = actor.keyBySql.get(queryKey);
@@ -120,8 +119,12 @@ public class MaterializationService {
     CalciteSchema.TableEntry tableEntry = schema.getTableBySql(viewSql);
     RelDataType rowType = null;
     if (tableEntry == null) {
-      tableEntry = tableFactory.createTable(schema, viewSql, viewSchemaPath);
-      rowType = tableEntry.getTable().getRowType(connection.getTypeFactory());
+      Table table = tableFactory.createTable(schema, viewSql, viewSchemaPath);
+      final String tableName = Schemas.uniqueTableName(schema,
+          Util.first(suggestedTableName, "m"));
+      tableEntry = schema.add(tableName, table, ImmutableList.of(viewSql));
+      Hook.CREATE_MATERIALIZATION.run(tableName);
+      rowType = table.getRowType(connection.getTypeFactory());
     }
 
     if (rowType == null) {
@@ -164,13 +167,14 @@ public class MaterializationService {
   public Pair<CalciteSchema.TableEntry, TileKey> defineTile(Lattice lattice,
       ImmutableBitSet groupSet, List<Lattice.Measure> measureList,
       CalciteSchema schema, boolean create, boolean exact) {
-    TableFactory defaultTableFactory = new DefaultTableFactory("m" + groupSet);
-    return defineTile(lattice, groupSet, measureList, schema, create, exact, defaultTableFactory);
+    return defineTile(lattice, groupSet, measureList, schema, create, exact,
+        "m" + groupSet, tableFactory);
   }
 
   public Pair<CalciteSchema.TableEntry, TileKey> defineTile(Lattice lattice,
-        ImmutableBitSet groupSet, List<Lattice.Measure> measureList,
-        CalciteSchema schema, boolean create, boolean exact, TableFactory tableFactory) {
+      ImmutableBitSet groupSet, List<Lattice.Measure> measureList,
+      CalciteSchema schema, boolean create, boolean exact,
+      String suggestedTableName, TableFactory tableFactory) {
     MaterializationKey materializationKey;
     final TileKey tileKey =
         new TileKey(lattice, groupSet, ImmutableList.copyOf(measureList));
@@ -259,11 +263,9 @@ public class MaterializationService {
         new TileKey(lattice, groupSet, ImmutableList.copyOf(measureSet));
 
     final String sql = lattice.sql(groupSet, newTileKey.measures);
-
-    System.out.println(sql);
     materializationKey =
         defineMaterialization(schema, newTileKey, sql, schema.path(null),
-            tableFactory, true);
+            suggestedTableName, tableFactory, true);
     if (materializationKey != null) {
       final CalciteSchema.TableEntry tableEntry =
           checkValid(materializationKey);
@@ -335,31 +337,20 @@ public class MaterializationService {
   }
 
   /**
-   * TableFactory Interface defines functions required by
-   * MaterializationService to create tables that represent
-   * a materialized view.
+   * Creates tables that represent a materialized view.
    */
   public interface TableFactory {
-    CalciteSchema.TableEntry createTable(final CalciteSchema schema,
-                                         final String viewSql,
-                                         final List<String> viewSchemaPath);
-    String getTableName(CalciteSchema schema);
+    Table createTable(CalciteSchema schema, String viewSql,
+        List<String> viewSchemaPath);
   }
 
   /**
-   * Default implementation of TableFactory.
-   * Creates a table using CloneSchema.
+   * Default implementation of {@link TableFactory}.
+   * Creates a table using {@link CloneSchema}.
    */
   public static class DefaultTableFactory implements TableFactory {
-    final String tableName;
-
-    public DefaultTableFactory(final String tableName) {
-      this.tableName = tableName;
-    }
-
-    public CalciteSchema.TableEntry createTable(final CalciteSchema schema,
-                                                final String viewSql,
-                                                final List<String> viewSchemaPath) {
+    public Table createTable(CalciteSchema schema, String viewSql,
+        List<String> viewSchemaPath) {
       final CalciteConnection connection =
           CalciteMetaImpl.connect(schema.root(), null);
       final ImmutableMap<CalciteConnectionProperty, String> map =
@@ -367,7 +358,7 @@ public class MaterializationService {
               "false");
       final CalcitePrepare.CalciteSignature<Object> calciteSignature =
           Schemas.prepare(connection, schema, viewSchemaPath, viewSql, map);
-      Table table = CloneSchema.createCloneTable(connection.getTypeFactory(),
+      return CloneSchema.createCloneTable(connection.getTypeFactory(),
           RelDataTypeImpl.proto(calciteSignature.rowType),
           Lists.transform(calciteSignature.columns,
               new Function<ColumnMetaData, ColumnMetaData.Rep>() {
@@ -400,16 +391,6 @@ public class MaterializationService {
               return calciteSignature.enumerable(dataContext).iterator();
             }
           });
-
-      final String tableName = this.getTableName(schema);
-      CalciteSchema.TableEntry tableEntry = schema.add(tableName, table,
-          ImmutableList.of(viewSql));
-      Hook.CREATE_MATERIALIZATION.run(tableName);
-      return tableEntry;
-    }
-
-    public String getTableName(CalciteSchema schema) {
-      return Schemas.uniqueTableName(schema, Util.first(tableName, "m"));
     }
   }
 }