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 2015/04/30 18:40:38 UTC

phoenix git commit: PHOENIX-1878Implement PhoenixSchema and PhoenixTable in Phoenix/Calcite Integration

Repository: phoenix
Updated Branches:
  refs/heads/calcite 5cf992ef6 -> 8e3f68a2d


PHOENIX-1878Implement PhoenixSchema and PhoenixTable in Phoenix/Calcite Integration


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

Branch: refs/heads/calcite
Commit: 8e3f68a2da1473d62e69db6396232e6214c06201
Parents: 5cf992e
Author: maryannxue <we...@intel.com>
Authored: Thu Apr 30 12:40:29 2015 -0400
Committer: maryannxue <we...@intel.com>
Committed: Thu Apr 30 12:40:29 2015 -0400

----------------------------------------------------------------------
 .../org/apache/phoenix/calcite/CalciteTest.java | 72 ++++++++++-------
 .../apache/phoenix/calcite/CalciteUtils.java    | 82 ++++++++++++++++++--
 .../apache/phoenix/calcite/PhoenixSchema.java   | 62 ++++++++++-----
 3 files changed, 163 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/8e3f68a2/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
index 6b7065b..bff6706 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteTest.java
@@ -647,6 +647,52 @@ public class CalciteTest extends BaseClientManagedTimeIT {
                .close();
     }
     
+    @Test public void testScalarSubquery() {
+        start().sql("select \"item_id\", name, (select max(quantity) sq \n"
+            + "from " + JOIN_ORDER_TABLE_FULL_NAME + " o where o.\"item_id\" = i.\"item_id\")\n"
+            + "from " + JOIN_ITEM_TABLE_FULL_NAME + " i")
+            .explainIs("PhoenixToEnumerableConverter\n" +
+                       "  PhoenixServerProject(item_id=[$0], NAME=[$1], EXPR$2=[$8])\n" +
+                       "    PhoenixServerJoin(condition=[=($0, $7)], joinType=[left], isSingleValueRhs=[true])\n" +
+                       "      PhoenixTableScan(table=[[phoenix, Join, ItemTable]])\n" +
+                       "      PhoenixServerAggregate(group=[{0}], SQ=[MAX($1)])\n" +
+                       "        PhoenixServerProject(item_id0=[$7], QUANTITY=[$4])\n" +
+                       "          PhoenixServerJoin(condition=[=($2, $7)], joinType=[inner])\n" +
+                       "            PhoenixTableScan(table=[[phoenix, Join, OrderTable]])\n" +
+                       "            PhoenixServerAggregate(group=[{0}])\n" +
+                       "              PhoenixServerProject(item_id=[$0])\n" +
+                       "                PhoenixTableScan(table=[[phoenix, Join, ItemTable]])\n")
+            .resultIs(new Object[][] {
+                    new Object[] {"0000000001", "T1", 1000}, 
+                    new Object[] {"0000000002", "T2", 3000}, 
+                    new Object[] {"0000000003", "T3", 5000}, 
+                    new Object[] {"0000000004", "T4", null}, 
+                    new Object[] {"0000000005", "T5", null}, 
+                    new Object[] {"0000000006", "T6", 4000}, 
+                    new Object[] {"invalid001", "INVALID-1", null}})
+            .close();;
+            start().sql("select \"item_id\", name, (select quantity sq \n"
+                    + "from " + JOIN_ORDER_TABLE_FULL_NAME + " o where o.\"item_id\" = i.\"item_id\")\n"
+                    + "from " + JOIN_ITEM_TABLE_FULL_NAME + " i where \"item_id\" < '0000000006'")
+                    .explainIs("PhoenixToEnumerableConverter\n" +
+                               "  PhoenixServerProject(item_id=[$0], NAME=[$1], EXPR$2=[$8])\n" +
+                               "    PhoenixServerJoin(condition=[=($0, $7)], joinType=[left], isSingleValueRhs=[true])\n" +
+                               "      PhoenixTableScan(table=[[phoenix, Join, ItemTable]], filter=[<($0, '0000000006')])\n" +
+                               "      PhoenixServerProject(item_id0=[$7], SQ=[$4])\n" +
+                               "        PhoenixServerJoin(condition=[=($2, $7)], joinType=[inner])\n" +
+                               "          PhoenixTableScan(table=[[phoenix, Join, OrderTable]])\n" +
+                               "          PhoenixServerAggregate(group=[{0}])\n" +
+                               "            PhoenixServerProject(item_id=[$0])\n" +
+                               "              PhoenixTableScan(table=[[phoenix, Join, ItemTable]], filter=[<($0, '0000000006')])\n")
+                    .resultIs(new Object[][] {
+                            new Object[] {"0000000001", "T1", 1000}, 
+                            new Object[] {"0000000002", "T2", 3000}, 
+                            new Object[] {"0000000003", "T3", 5000}, 
+                            new Object[] {"0000000004", "T4", null}, 
+                            new Object[] {"0000000005", "T5", null}})
+                    .close();;
+    }
+    
     @Test public void testConnectJoinHsqldb() {
         final Start start = new Start() {
             @Override
@@ -679,32 +725,6 @@ public class CalciteTest extends BaseClientManagedTimeIT {
                     new Object[] {1998, 5000, 365L}})
             .close();;
     }
-    
-    @Test public void testScalarSubquery() {
-        start().sql("select \"item_id\", name, (select max(quantity) sq \n"
-            + "from " + JOIN_ORDER_TABLE_FULL_NAME + " o where o.\"item_id\" = i.\"item_id\")\n"
-            + "from " + JOIN_ITEM_TABLE_FULL_NAME + " i")
-            .explainIs("PhoenixToEnumerableConverter\n" +
-                       "  PhoenixServerProject(item_id=[$0], NAME=[$1], EXPR$2=[$8])\n" +
-                       "    PhoenixServerJoin(condition=[=($0, $7)], joinType=[left], isSingleValueRhs=[true])\n" +
-                       "      PhoenixTableScan(table=[[phoenix, Join, ItemTable]])\n" +
-                       "      PhoenixServerAggregate(group=[{0}], SQ=[MAX($1)])\n" +
-                       "        PhoenixServerProject(item_id0=[$7], QUANTITY=[$4])\n" +
-                       "          PhoenixServerJoin(condition=[=($2, $7)], joinType=[inner])\n" +
-                       "            PhoenixTableScan(table=[[phoenix, Join, OrderTable]])\n" +
-                       "            PhoenixServerAggregate(group=[{0}])\n" +
-                       "              PhoenixServerProject(item_id=[$0])\n" +
-                       "                PhoenixTableScan(table=[[phoenix, Join, ItemTable]])\n")
-            .resultIs(new Object[][] {
-                    new Object[] {"0000000001", "T1", 1000}, 
-                    new Object[] {"0000000002", "T2", 3000}, 
-                    new Object[] {"0000000003", "T3", 5000}, 
-                    new Object[] {"0000000004", "T4", null}, 
-                    new Object[] {"0000000005", "T5", null}, 
-                    new Object[] {"0000000006", "T6", 4000}, 
-                    new Object[] {"invalid001", "INVALID-1", null}})
-            .close();;
-    }
 
     @Test public void testConnectUsingModel() throws Exception {
         final Start start = new Start() {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/8e3f68a2/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
index ab2bf21..1b2e4b4 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
@@ -55,21 +55,80 @@ public class CalciteUtils {
 
 			@Override
 			public Expression newExpression(RexNode node, Implementor implementor) {
-				RexCall call = (RexCall) node;
-				List<Expression> children = Lists.newArrayListWithExpectedSize(call.getOperands().size());
-				for (RexNode op : call.getOperands()) {
-					Expression child = getFactory(op).newExpression(op, implementor);
-					children.add(child);
-				}
 				ImmutableBytesWritable ptr = new ImmutableBytesWritable();
 				try {
-					return ComparisonExpression.create(CompareOp.EQUAL, children, ptr);
+					return ComparisonExpression.create(CompareOp.EQUAL, convertChildren((RexCall) node, implementor), ptr);
 				} catch (SQLException e) {
 					throw new RuntimeException(e);
 				}
 			}
 			
 		});
+        EXPRESSION_MAP.put(SqlKind.NOT_EQUALS, new ExpressionFactory() {
+
+            @Override
+            public Expression newExpression(RexNode node, Implementor implementor) {
+                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+                try {
+                    return ComparisonExpression.create(CompareOp.NOT_EQUAL, convertChildren((RexCall) node, implementor), ptr);
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            
+        });
+        EXPRESSION_MAP.put(SqlKind.GREATER_THAN, new ExpressionFactory() {
+
+            @Override
+            public Expression newExpression(RexNode node, Implementor implementor) {
+                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+                try {
+                    return ComparisonExpression.create(CompareOp.GREATER, convertChildren((RexCall) node, implementor), ptr);
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            
+        });
+        EXPRESSION_MAP.put(SqlKind.GREATER_THAN_OR_EQUAL, new ExpressionFactory() {
+
+            @Override
+            public Expression newExpression(RexNode node, Implementor implementor) {
+                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+                try {
+                    return ComparisonExpression.create(CompareOp.GREATER_OR_EQUAL, convertChildren((RexCall) node, implementor), ptr);
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            
+        });
+        EXPRESSION_MAP.put(SqlKind.LESS_THAN, new ExpressionFactory() {
+
+            @Override
+            public Expression newExpression(RexNode node, Implementor implementor) {
+                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+                try {
+                    return ComparisonExpression.create(CompareOp.LESS, convertChildren((RexCall) node, implementor), ptr);
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            
+        });
+        EXPRESSION_MAP.put(SqlKind.LESS_THAN_OR_EQUAL, new ExpressionFactory() {
+
+            @Override
+            public Expression newExpression(RexNode node, Implementor implementor) {
+                ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+                try {
+                    return ComparisonExpression.create(CompareOp.LESS_OR_EQUAL, convertChildren((RexCall) node, implementor), ptr);
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            
+        });
 		EXPRESSION_MAP.put(SqlKind.LITERAL, new ExpressionFactory() {
 
 			@Override
@@ -138,6 +197,15 @@ public class CalciteUtils {
             }
         });
     }
+    
+    private static List<Expression> convertChildren(RexCall call, Implementor implementor) {
+        List<Expression> children = Lists.newArrayListWithExpectedSize(call.getOperands().size());
+        for (RexNode op : call.getOperands()) {
+            Expression child = getFactory(op).newExpression(op, implementor);
+            children.add(child);
+        }
+        return children;
+    }
 
 	public static Expression toExpression(RexNode node, Implementor implementor) {
 		ExpressionFactory eFactory = getFactory(node);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/8e3f68a2/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 f995f19..816c156 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
@@ -1,15 +1,15 @@
 package org.apache.phoenix.calcite;
 
-import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.SetMultimap;
+import com.google.common.collect.Sets;
 
 import org.apache.calcite.linq4j.tree.Expression;
 import org.apache.calcite.schema.*;
 import org.apache.phoenix.compile.ColumnResolver;
 import org.apache.phoenix.compile.FromCompiler;
 import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
 import org.apache.phoenix.parse.ColumnDef;
 import org.apache.phoenix.parse.NamedTableNode;
 import org.apache.phoenix.parse.TableName;
@@ -17,7 +17,9 @@ import org.apache.phoenix.schema.MetaDataClient;
 import org.apache.phoenix.schema.TableRef;
 
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.DriverManager;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.*;
 
@@ -27,25 +29,48 @@ import java.util.*;
 public class PhoenixSchema implements Schema {
     public static final Factory FACTORY = new Factory();
     
-    private final String schemaName;
-    private final PhoenixConnection pc;
+    protected final String schemaName;
+    protected final PhoenixConnection pc;
     protected final MetaDataClient client;
     
-    // TODO to be removed after PHOENIX-1878.
-    private static final SetMultimap<String, String> tableCache;
-    static {
-        tableCache = HashMultimap.<String, String> create();
-        tableCache.put("", "ATABLE");
-        tableCache.put("Join", "ItemTable");
-        tableCache.put("Join", "SupplierTable");
-        tableCache.put("Join", "CustomerTable");
-        tableCache.put("Join", "OrderTable");
-    }
+    protected final Set<String> subSchemaNames;
+    protected final Set<String> tableNames;
     
     private PhoenixSchema(String name, PhoenixConnection pc) {
         this.schemaName = name;
         this.pc = pc;
         this.client = new MetaDataClient(pc);
+        this.subSchemaNames = Sets.newHashSet();
+        this.tableNames = Sets.newHashSet();
+        if (schemaName == null) {
+            loadSubSchemaNames();
+        }
+        loadTableNames();
+    }
+    
+    private void loadSubSchemaNames() {
+        try {
+            DatabaseMetaData md = pc.getMetaData();
+            ResultSet rs = md.getSchemas();
+            while (rs.next()) {
+                String schemaName = rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM);
+                this.subSchemaNames.add(schemaName == null ? "" : schemaName);
+            }
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    
+    private void loadTableNames() {
+        try {
+            DatabaseMetaData md = pc.getMetaData();
+            ResultSet rs = md.getTables(null, schemaName == null ? "" : schemaName, null, null);
+            while (rs.next()) {
+                this.tableNames.add(rs.getString(PhoenixDatabaseMetaData.TABLE_NAME));
+            }
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private static Schema create(SchemaPlus parentSchema, Map<String, Object> operand) {
@@ -83,7 +108,7 @@ public class PhoenixSchema implements Schema {
 
     @Override
     public Set<String> getTableNames() {
-        return tableCache.get(schemaName == null ? "" : schemaName);
+        return tableNames;
     }
 
     @Override
@@ -98,7 +123,7 @@ public class PhoenixSchema implements Schema {
 
     @Override
     public Schema getSubSchema(String name) {
-        if (schemaName != null || !tableCache.containsKey(name))
+        if (!subSchemaNames.contains(name))
             return null;
         
         return new PhoenixSchema(name, pc);
@@ -106,10 +131,7 @@ public class PhoenixSchema implements Schema {
 
     @Override
     public Set<String> getSubSchemaNames() {
-        if (schemaName != null)
-            return Collections.emptySet();
-        
-        return tableCache.keySet();
+        return subSchemaNames;
     }
 
     @Override