Repository: phoenix
Updated Branches:
  refs/heads/calcite 4060f3bcd -> ed39c7d53


Compilation error fix


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/ed39c7d5
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/ed39c7d5
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/ed39c7d5

Branch: refs/heads/calcite
Commit: ed39c7d533d4f2aec58dbd64367d99da3c5393bb
Parents: 4060f3b
Author: maryannxue <wei....@intel.com>
Authored: Mon Nov 9 16:11:14 2015 -0500
Committer: maryannxue <wei....@intel.com>
Committed: Mon Nov 9 16:11:14 2015 -0500

----------------------------------------------------------------------
 .../apache/phoenix/calcite/PhoenixSchema.java   | 60 ++++++++++++++------
 .../calcite/jdbc/PhoenixPrepareImpl.java        | 18 ++++--
 2 files changed, 55 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ed39c7d5/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
index bc2d424..a6c10b7 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixSchema.java
@@ -40,6 +40,12 @@ import java.util.Set;
 
 /**
  * Implementation of Calcite's {@link Schema} SPI for Phoenix.
+ * 
+ * TODO
+ * 1) change this to non-caching mode??
+ * 2) how to deal with define indexes and views since they require a 
CalciteSchema
+ *    instance??
+ *
  */
 public class PhoenixSchema implements Schema {
     public static final Factory FACTORY = new Factory();
@@ -47,23 +53,21 @@ public class PhoenixSchema implements Schema {
     
     protected final String name;
     protected final String schemaName;
-    protected final SchemaPlus parentSchema;
     protected final PhoenixConnection pc;
     protected final MetaDataClient client;
-    protected final CalciteSchema calciteSchema;
     
     protected final Set<String> subSchemaNames;
     protected final Map<String, PTable> tableMap;
+    protected final Map<String, ViewDef> viewDefMap;
     protected final Map<String, Function> functionMap;
     
-    private PhoenixSchema(String name, SchemaPlus parentSchema, String 
schemaName, PhoenixConnection pc) {
+    private PhoenixSchema(String name, String schemaName, PhoenixConnection 
pc) {
         this.name = name;
         this.schemaName = schemaName;
-        this.parentSchema = parentSchema;
         this.pc = pc;
         this.client = new MetaDataClient(pc);
-        this.calciteSchema = new 
CalciteSchema(CalciteSchema.from(parentSchema), this, name);
         this.tableMap = Maps.<String, PTable> newHashMap();
+        this.viewDefMap = Maps.<String, ViewDef> newHashMap();
         this.functionMap = Maps.<String, Function> newHashMap();
         loadTables();
         this.subSchemaNames = schemaName == null ? 
@@ -110,7 +114,7 @@ public class PhoenixSchema implements Schema {
                 } else {
                     String viewSql = 
rs.getString(PhoenixDatabaseMetaData.VIEW_STATEMENT);
                     String viewType = 
rs.getString(PhoenixDatabaseMetaData.VIEW_TYPE);
-                    functionMap.put(tableName, 
ViewTable.viewMacro(calciteSchema.plus(), viewSql, calciteSchema.path(null), 
viewType.equals(ViewType.UPDATABLE.name())));
+                    viewDefMap.put(tableName, new ViewDef(viewSql, 
viewType.equals(ViewType.UPDATABLE.name())));
                 }
             }
         } catch (SQLException e) {
@@ -118,7 +122,7 @@ public class PhoenixSchema implements Schema {
         }
     }
 
-    private static Schema create(String name, SchemaPlus parentSchema, 
Map<String, Object> operand) {
+    private static Schema create(String name, Map<String, Object> operand) {
         String url = (String) operand.get("url");
         final Properties properties = new Properties();
         for (Map.Entry<String, Object> entry : operand.entrySet()) {
@@ -130,7 +134,7 @@ public class PhoenixSchema implements Schema {
                 DriverManager.getConnection(url, properties);
             final PhoenixConnection phoenixConnection =
                 connection.unwrap(PhoenixConnection.class);
-            return new PhoenixSchema(name, parentSchema, null, 
phoenixConnection);
+            return new PhoenixSchema(name, null, phoenixConnection);
         } catch (ClassNotFoundException e) {
             throw new RuntimeException(e);
         } catch (SQLException e) {
@@ -152,12 +156,12 @@ public class PhoenixSchema implements Schema {
     @Override
     public Collection<Function> getFunctions(String name) {
         Function func = functionMap.get(name);
-        return func == null ? Collections.<Function>emptyList() : 
ImmutableList.of(functionMap.get(name));
+        return func == null ? Collections.<Function>emptyList() : 
ImmutableList.of(func);
     }
 
     @Override
     public Set<String> getFunctionNames() {
-        return functionMap.keySet();
+        return viewDefMap.keySet();
     }
 
     @Override
@@ -165,7 +169,7 @@ public class PhoenixSchema implements Schema {
         if (!subSchemaNames.contains(name))
             return null;
         
-        return new PhoenixSchema(name, calciteSchema.plus(), name, pc);
+        return new PhoenixSchema(name, name, pc);
     }
 
     @Override
@@ -188,14 +192,24 @@ public class PhoenixSchema implements Schema {
         return false;
     }
     
-    public void defineIndexesAsMaterializations() {
+    public void initFunctionMap(CalciteSchema calciteSchema) {
+        for (Map.Entry<String, ViewDef> entry : viewDefMap.entrySet()) {
+            ViewDef viewDef = entry.getValue();
+            Function func = ViewTable.viewMacro(
+                    calciteSchema.plus(), viewDef.viewSql,
+                    calciteSchema.path(null), viewDef.updatable);
+            functionMap.put(entry.getKey(), func);
+        }
+    }
+    
+    public void defineIndexesAsMaterializations(CalciteSchema calciteSchema) {
         List<String> path = calciteSchema.path(null);
         for (Map.Entry<String, PTable> entry : tableMap.entrySet()) {
             final String tableName = entry.getKey();
             final PTable table = entry.getValue();
             if (!isUnorderedTableName(tableName)) {
                 for (PTable index : table.getIndexes()) {
-                    addMaterialization(table, index, path);
+                    addMaterialization(table, index, path, calciteSchema);
                 }
             }
         }
@@ -203,12 +217,13 @@ public class PhoenixSchema implements Schema {
             final String tableName = entry.getKey();
             final PTable table = entry.getValue();
             if (isUnorderedTableName(tableName)) {
-                addUnorderedAsMaterialization(tableName, table, path);
+                addUnorderedAsMaterialization(tableName, table, path, 
calciteSchema);
             }
         }
     }
     
-    protected void addMaterialization(PTable table, PTable index, List<String> 
path) {
+    protected void addMaterialization(PTable table, PTable index, List<String> 
path,
+            CalciteSchema calciteSchema) {
         StringBuffer sb = new StringBuffer();
         sb.append("SELECT");
         for (int i = PhoenixTable.getStartingColumnPosition(index); i < 
index.getColumns().size(); i++) {
@@ -224,7 +239,8 @@ public class PhoenixSchema implements Schema {
                 calciteSchema, null, sb.toString(), path, 
index.getTableName().getString(), true, true);        
     }
     
-    protected void addUnorderedAsMaterialization(String tableName, PTable 
table, List<String> path) {
+    protected void addUnorderedAsMaterialization(String tableName, PTable 
table, List<String> path,
+            CalciteSchema calciteSchema) {
         StringBuffer sb = new StringBuffer();
         sb.append("SELECT * FROM ")
             .append("\"")
@@ -237,6 +253,16 @@ public class PhoenixSchema implements Schema {
     private boolean isUnorderedTableName(String tableName) {
         return tableName.endsWith(UNORDERED_SUFFIX);
     }
+    
+    private static class ViewDef {
+        final String viewSql;
+        final boolean updatable;
+        
+        ViewDef(String viewSql, boolean updatable) {
+            this.viewSql = viewSql;
+            this.updatable = updatable;
+        }
+    }
 
     /** Schema factory that creates a
      * {@link org.apache.phoenix.calcite.PhoenixSchema}.
@@ -263,7 +289,7 @@ public class PhoenixSchema implements Schema {
      */
     public static class Factory implements SchemaFactory {
         public Schema create(SchemaPlus parentSchema, String name, Map<String, 
Object> operand) {
-            return PhoenixSchema.create(name, parentSchema, operand);
+            return PhoenixSchema.create(name, operand);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ed39c7d5/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
index dd3c9bf..677b966 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java
@@ -80,12 +80,18 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl {
         planner.addRule(PhoenixInnerSortRemoveRule.INSTANCE);
         planner.addRule(PhoenixOrderedAggregateRule.INSTANCE);
         
-        if (prepareContext.config().materializationsEnabled()) {
-            for (CalciteSchema subSchema : 
prepareContext.getRootSchema().getSubSchemaMap().values()) {
-                if (subSchema.schema instanceof PhoenixSchema) {
-                    ((PhoenixSchema) 
subSchema.schema).defineIndexesAsMaterializations();
-                    for (CalciteSchema phoenixSubSchema : 
subSchema.getSubSchemaMap().values()) {
-                        ((PhoenixSchema) 
phoenixSubSchema.schema).defineIndexesAsMaterializations();
+        for (CalciteSchema schema : 
prepareContext.getRootSchema().getSubSchemaMap().values()) {
+            if (schema.schema instanceof PhoenixSchema) {
+                PhoenixSchema phoenixSchema = (PhoenixSchema) schema.schema;
+                phoenixSchema.initFunctionMap(schema);
+                if (prepareContext.config().materializationsEnabled()) {
+                    phoenixSchema.defineIndexesAsMaterializations(schema);
+                }
+                for (CalciteSchema subSchema : 
schema.getSubSchemaMap().values()) {
+                    PhoenixSchema phoenixSubSchema = (PhoenixSchema) 
subSchema.schema;
+                    phoenixSubSchema.initFunctionMap(subSchema);
+                    if (prepareContext.config().materializationsEnabled()) {
+                        
phoenixSubSchema.defineIndexesAsMaterializations(subSchema);
                     }
                 }
             }

Reply via email to