You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ma...@apache.org on 2016/10/06 19:37:11 UTC

[3/4] phoenix git commit: Minimize life cycle of hooks

Minimize life cycle of hooks


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

Branch: refs/heads/calcite
Commit: 88dd213a958e7143ca4ca81c2e4d2fb2d367af3a
Parents: b41c0a8
Author: maryannxue <ma...@gmail.com>
Authored: Mon Oct 3 17:31:33 2016 -0700
Committer: maryannxue <ma...@gmail.com>
Committed: Mon Oct 3 17:31:33 2016 -0700

----------------------------------------------------------------------
 .../calcite/jdbc/PhoenixCalciteFactory.java     | 53 ------------
 .../phoenix/calcite/PhoenixPrepareImpl.java     | 88 ++++++++++++++++++++
 2 files changed, 88 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/88dd213a/phoenix-core/src/main/java/org/apache/calcite/jdbc/PhoenixCalciteFactory.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/calcite/jdbc/PhoenixCalciteFactory.java b/phoenix-core/src/main/java/org/apache/calcite/jdbc/PhoenixCalciteFactory.java
index 054d7eb..776a06e 100644
--- a/phoenix-core/src/main/java/org/apache/calcite/jdbc/PhoenixCalciteFactory.java
+++ b/phoenix-core/src/main/java/org/apache/calcite/jdbc/PhoenixCalciteFactory.java
@@ -35,24 +35,14 @@ import org.apache.calcite.jdbc.CalciteFactory;
 import org.apache.calcite.jdbc.Driver;
 import org.apache.calcite.linq4j.Enumerable;
 import org.apache.calcite.linq4j.Ord;
-import org.apache.calcite.prepare.Prepare.Materialization;
-import org.apache.calcite.rel.RelNode;
-import org.apache.calcite.runtime.Hook;
-import org.apache.calcite.runtime.Hook.Closeable;
 import org.apache.calcite.schema.SchemaPlus;
-import org.apache.calcite.tools.Program;
-import org.apache.calcite.tools.Programs;
-import org.apache.calcite.util.Holder;
 import org.apache.phoenix.calcite.PhoenixSchema;
-import org.apache.phoenix.calcite.rel.PhoenixRel;
 import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.exception.SQLExceptionInfo;
 import org.apache.phoenix.execute.RuntimeContext;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 
-import com.google.common.base.Function;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 public class PhoenixCalciteFactory extends CalciteFactory {
@@ -121,51 +111,11 @@ public class PhoenixCalciteFactory extends CalciteFactory {
     private static class PhoenixCalciteConnection extends CalciteConnectionImpl {
         private final Map<Meta.StatementHandle, ImmutableList<RuntimeContext>> runtimeContextMap =
                 new ConcurrentHashMap<Meta.StatementHandle, ImmutableList<RuntimeContext>>();
-        private final List<Closeable> hooks = Lists.newArrayList();
         
         public PhoenixCalciteConnection(Driver driver, AvaticaFactory factory, String url,
                 Properties info, final CalciteSchema rootSchema,
                 JavaTypeFactory typeFactory) {
             super(driver, factory, url, info, rootSchema, typeFactory);
-
-            hooks.add(Hook.PARSE_TREE.add(new Function<Object[], Object>() {
-                @Override
-                public Object apply(Object[] input) {
-                    for (CalciteSchema schema : rootSchema.getSubSchemaMap().values()) {
-                        if (schema.schema instanceof PhoenixSchema) {
-                            ((PhoenixSchema) schema.schema).clear();
-                            for (CalciteSchema subSchema : schema.getSubSchemaMap().values()) {
-                                ((PhoenixSchema) subSchema.schema).clear();
-                            }
-                        }
-                    }
-                    return null;
-                }
-            }));
-
-            hooks.add(Hook.TRIMMED.add(new Function<RelNode, Object>() {
-                @Override
-                public Object apply(RelNode root) {
-                    for (CalciteSchema schema : rootSchema.getSubSchemaMap().values()) {
-                        if (schema.schema instanceof PhoenixSchema) {
-                            ((PhoenixSchema) schema.schema).defineIndexesAsMaterializations();
-                            for (CalciteSchema subSchema : schema.getSubSchemaMap().values()) {
-                                ((PhoenixSchema) subSchema.schema).defineIndexesAsMaterializations();
-                            }
-                        }
-                    }
-                    return null;
-                }
-            }));
-
-            hooks.add(Hook.PROGRAM.add(new Function<org.apache.calcite.util.Pair<List<Materialization>, Holder<Program>>, Object>() {
-                @Override
-                public Object apply(
-                        org.apache.calcite.util.Pair<List<Materialization>, Holder<Program>> input) {
-                    input.getValue().set(Programs.standard(PhoenixRel.METADATA_PROVIDER));
-                    return null;
-                }
-            }));
         }
 
         public <T> Enumerable<T> enumerable(Meta.StatementHandle handle,
@@ -280,9 +230,6 @@ public class PhoenixCalciteFactory extends CalciteFactory {
                 public void call(PhoenixConnection conn) throws SQLException {
                     conn.close();
                 }});
-            for (Closeable hook : hooks) {
-                hook.close();
-            }
             super.close();
         }
         

http://git-wip-us.apache.org/repos/asf/phoenix/blob/88dd213a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
index 2d6a84c..b5461ce 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
@@ -1,5 +1,6 @@
 package org.apache.phoenix.calcite;
 
+import java.lang.reflect.Type;
 import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.List;
@@ -7,16 +8,22 @@ import java.util.Map;
 
 import org.apache.calcite.adapter.enumerable.EnumerableRules;
 import org.apache.calcite.jdbc.CalcitePrepare;
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.linq4j.Queryable;
 import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.plan.RelOptCostFactory;
 import org.apache.calcite.plan.RelOptPlanner;
 import org.apache.calcite.plan.RelOptRule;
 import org.apache.calcite.prepare.CalcitePrepareImpl;
+import org.apache.calcite.prepare.Prepare.Materialization;
+import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.logical.LogicalSort;
 import org.apache.calcite.rel.rules.JoinCommuteRule;
 import org.apache.calcite.rel.rules.SortProjectTransposeRule;
 import org.apache.calcite.rel.rules.SortUnionTransposeRule;
 import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.runtime.Hook;
+import org.apache.calcite.runtime.Hook.Closeable;
 import org.apache.calcite.schema.SchemaPlus;
 import org.apache.calcite.sql.SqlColumnDefInPkConstraintNode;
 import org.apache.calcite.sql.SqlColumnDefNode;
@@ -30,6 +37,9 @@ import org.apache.calcite.sql.SqlOptionNode;
 import org.apache.calcite.sql.parser.SqlParser;
 import org.apache.calcite.sql.parser.SqlParserPos;
 import org.apache.calcite.sql.parser.SqlParserUtil;
+import org.apache.calcite.tools.Program;
+import org.apache.calcite.tools.Programs;
+import org.apache.calcite.util.Holder;
 import org.apache.calcite.util.NlsString;
 import org.apache.hadoop.hbase.util.Pair;
 import org.apache.phoenix.calcite.parse.SqlCreateIndex;
@@ -82,6 +92,7 @@ import org.apache.phoenix.schema.PTable.IndexType;
 import org.apache.phoenix.schema.PTableType;
 import org.apache.phoenix.schema.SortOrder;
 
+import com.google.common.base.Function;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Lists;
@@ -151,6 +162,83 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl {
         return planner;
     }
 
+    public <T> CalciteSignature<T> prepareQueryable(
+            Context context,
+            Queryable<T> queryable) {
+        List<Closeable> hooks = addHooks(
+                context.getRootSchema(),
+                context.config().materializationsEnabled());
+        try {
+            return super.prepareQueryable(context, queryable);
+        } finally {
+            for (Closeable hook : hooks) {
+                hook.close();
+            }
+        }
+    }
+
+    public <T> CalciteSignature<T> prepareSql(
+            Context context,
+            Query<T> query,
+            Type elementType,
+            long maxRowCount) {
+        List<Closeable> hooks = addHooks(
+                context.getRootSchema(),
+                context.config().materializationsEnabled());
+        try {
+            return super.prepareSql(context, query, elementType, maxRowCount);
+        } finally {
+            for (Closeable hook : hooks) {
+                hook.close();
+            }
+        }
+    }
+    
+    private List<Closeable> addHooks(final CalciteSchema rootSchema, boolean materializationEnabled) {
+        final List<Closeable> hooks = Lists.newArrayList();
+
+        hooks.add(Hook.PARSE_TREE.add(new Function<Object[], Object>() {
+            @Override
+            public Object apply(Object[] input) {
+                for (CalciteSchema schema : rootSchema.getSubSchemaMap().values()) {
+                    if (schema.schema instanceof PhoenixSchema) {
+                        ((PhoenixSchema) schema.schema).clear();
+                        for (CalciteSchema subSchema : schema.getSubSchemaMap().values()) {
+                            ((PhoenixSchema) subSchema.schema).clear();
+                        }
+                    }
+                }
+                return null;
+            }
+        }));
+
+        hooks.add(Hook.TRIMMED.add(new Function<RelNode, Object>() {
+            @Override
+            public Object apply(RelNode root) {
+                for (CalciteSchema schema : rootSchema.getSubSchemaMap().values()) {
+                    if (schema.schema instanceof PhoenixSchema) {
+                        ((PhoenixSchema) schema.schema).defineIndexesAsMaterializations();
+                        for (CalciteSchema subSchema : schema.getSubSchemaMap().values()) {
+                            ((PhoenixSchema) subSchema.schema).defineIndexesAsMaterializations();
+                        }
+                    }
+                }
+                return null;
+            }
+        }));
+
+        hooks.add(Hook.PROGRAM.add(new Function<org.apache.calcite.util.Pair<List<Materialization>, Holder<Program>>, Object>() {
+            @Override
+            public Object apply(
+                    org.apache.calcite.util.Pair<List<Materialization>, Holder<Program>> input) {
+                input.getValue().set(Programs.standard(PhoenixRel.METADATA_PROVIDER));
+                return null;
+            }
+        }));
+        
+        return hooks;
+    }
+
     @Override
     public void executeDdl(Context context, SqlNode node) {
         try {