drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jacq...@apache.org
Subject [13/17] git commit: DRILL-1114: Fix a bug in resolving tables in schema where view is created
Date Mon, 21 Jul 2014 02:46:35 GMT
DRILL-1114: Fix a bug in resolving tables in schema where view is created

Store the current schema path where view is created within view and use that schema path when
expanding the view as part of query planning.


Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/7e561def
Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/7e561def
Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/7e561def

Branch: refs/heads/master
Commit: 7e561def4bceca83a302d85154274eec199cdd9c
Parents: d399861
Author: vkorukanti <venki.korukanti@gmail.com>
Authored: Tue Jul 15 08:10:13 2014 -0700
Committer: Jacques Nadeau <jacques@apache.org>
Committed: Sun Jul 20 16:49:19 2014 -0700

----------------------------------------------------------------------
 .../org/apache/drill/exec/dotdrill/View.java    | 18 +++++++-
 .../exec/planner/logical/DrillViewTable.java    |  4 +-
 .../exec/planner/sql/handlers/ViewHandler.java  | 13 +++++-
 .../org/apache/drill/jdbc/test/TestViews.java   | 43 +++++++++++++++++++-
 4 files changed, 69 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7e561def/exec/java-exec/src/main/java/org/apache/drill/exec/dotdrill/View.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/dotdrill/View.java b/exec/java-exec/src/main/java/org/apache/drill/exec/dotdrill/View.java
index 5a7c0f8..ef55d68 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/dotdrill/View.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/dotdrill/View.java
@@ -19,6 +19,7 @@ package org.apache.drill.exec.dotdrill;
 
 import java.util.List;
 
+import com.google.common.collect.ImmutableList;
 import org.apache.drill.exec.planner.types.RelDataTypeDrillImpl;
 import org.apache.drill.exec.planner.types.RelDataTypeHolder;
 import org.eigenbase.reltype.RelDataType;
@@ -41,6 +42,9 @@ public class View {
   private String sql;
   private List<FieldType> fields;
 
+  /* Current schema when view is created (not the schema to which view belongs to) */
+  private List<String> workspaceSchemaPath;
+
   @JsonInclude(Include.NON_NULL)
   public static class FieldType {
     public final String name;
@@ -80,20 +84,27 @@ public class View {
     }
   }
 
-  public View(String name, String sql, RelDataType rowType){
+  public View(String name, String sql, RelDataType rowType, List<String> workspaceSchemaPath){
     this.name = name;
     this.sql = sql;
     fields = Lists.newArrayList();
     for(RelDataTypeField f : rowType.getFieldList()){
       fields.add(new FieldType(f.getName(), f.getType()));
     }
+    this.workspaceSchemaPath =
+        workspaceSchemaPath == null ? ImmutableList.<String>of() : ImmutableList.copyOf(workspaceSchemaPath);
   }
 
   @JsonCreator
-  public View(@JsonProperty("name") String name, @JsonProperty("sql") String sql, @JsonProperty("fields")
List<FieldType> fields){
+  public View(@JsonProperty("name") String name,
+              @JsonProperty("sql") String sql,
+              @JsonProperty("fields") List<FieldType> fields,
+              @JsonProperty("workspaceSchemaPath") List<String> workspaceSchemaPath){
     this.name = name;
     this.sql = sql;
     this.fields = fields;
+    this.workspaceSchemaPath =
+        workspaceSchemaPath == null ? ImmutableList.<String>of() : ImmutableList.copyOf(workspaceSchemaPath);
   }
 
   public RelDataType getRowType(RelDataTypeFactory factory){
@@ -140,6 +151,9 @@ public class View {
     return fields;
   }
 
+  public List<String> getWorkspaceSchemaPath() {
+    return workspaceSchemaPath;
+  }
 
 
 

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7e561def/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillViewTable.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillViewTable.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillViewTable.java
index 19fd7ba..e798d4e 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillViewTable.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillViewTable.java
@@ -36,11 +36,9 @@ public class DrillViewTable implements TranslatableTable, DrillViewInfoProvider
   static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillViewTable.class);
 
   private final View view;
-  private final List<String> path;
 
   public DrillViewTable(List<String> path, View view){
     this.view = view;
-    this.path = path;
   }
 
   @Override
@@ -56,7 +54,7 @@ public class DrillViewTable implements TranslatableTable, DrillViewInfoProvider
   @Override
   public RelNode toRel(ToRelContext context, RelOptTable relOptTable) {
     RelDataType rowType = relOptTable.getRowType();
-    RelNode rel = context.expandView(rowType, view.getSql(), path);
+    RelNode rel = context.expandView(rowType, view.getSql(), view.getWorkspaceSchemaPath());
 
     if (view.isDynamic()){
       return rel;

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7e561def/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
index ced07e5..ba70ae5 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/handlers/ViewHandler.java
@@ -20,6 +20,8 @@ package org.apache.drill.exec.planner.sql.handlers;
 import java.io.IOException;
 import java.util.List;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
 import net.hydromatic.optiq.SchemaPlus;
 import net.hydromatic.optiq.Table;
 import net.hydromatic.optiq.impl.ViewTable;
@@ -64,7 +66,8 @@ public abstract class ViewHandler extends AbstractSqlHandler{
       SqlCreateView createView = unwrap(sqlNode, SqlCreateView.class);
 
       try {
-        SchemaPlus schema = findSchema(context.getRootSchema(), context.getNewDefaultSchema(),
createView.getSchemaPath());
+        SchemaPlus defaultSchema = context.getNewDefaultSchema();
+        SchemaPlus schema = findSchema(context.getRootSchema(), defaultSchema, createView.getSchemaPath());
         AbstractSchema drillSchema = getDrillSchema(schema);
 
         String schemaPath = drillSchema.getFullSchemaName();
@@ -72,6 +75,12 @@ public abstract class ViewHandler extends AbstractSqlHandler{
           return DirectPlan.createDirectPlan(context, false, String.format("Current schema
'%s' is not a mutable schema. " +
               "Can't create views in this schema.", schemaPath));
 
+        // find current workspace schema path
+        List<String> workspaceSchemaPath = ImmutableList.of();
+        if (!isRootSchema(defaultSchema)) {
+          workspaceSchemaPath = getDrillSchema(defaultSchema).getSchemaPath();
+        }
+
         String viewSql = createView.getQuery().toString();
 
         SqlNode validatedQuery = planner.validate(createView.getQuery());
@@ -97,7 +106,7 @@ public abstract class ViewHandler extends AbstractSqlHandler{
           queryRowType = new DrillFixedRelDataTypeImpl(planner.getTypeFactory(), viewFieldNames);
         }
 
-        View view = new View(createView.getName(), viewSql, queryRowType);
+        View view = new View(createView.getName(), viewSql, queryRowType, workspaceSchemaPath);
 
         boolean replaced;
         if (drillSchema instanceof WorkspaceSchema) {

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7e561def/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestViews.java
----------------------------------------------------------------------
diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestViews.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestViews.java
index d21b56e..89d9573 100644
--- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestViews.java
+++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestViews.java
@@ -369,8 +369,6 @@ public class TestViews extends JdbcTestQueryBase {
 
           statement.executeQuery("drop view dfs_test.tmp.testview").close();
 
-          statement.executeQuery("drop view dfs_test.tmp.testview").close();
-
           statement.close();
           return null;
         } catch (Exception e) {
@@ -424,4 +422,45 @@ public class TestViews extends JdbcTestQueryBase {
       }
     });
   }
+
+  @Test
+  public void testViewResolvingTablesInWorkspaceSchema() throws Exception{
+    JdbcAssert.withNoDefaultSchema().withConnection(new Function<Connection, Void>()
{
+      public Void apply(Connection connection) {
+        try {
+          Statement statement = connection.createStatement();
+
+          // change default schema
+          statement.executeQuery("USE cp");
+
+          // create a view with full schema identifier
+          ResultSet resultSet =  statement.executeQuery(
+              "CREATE VIEW dfs_test.tmp.testViewResolvingTablesInWorkspaceSchema AS " +
+              "SELECT region_id, sales_city FROM `region.json`");
+          String result = JdbcAssert.toString(resultSet).trim();
+          resultSet.close();
+          String expected = "ok=true; summary=View 'testViewResolvingTablesInWorkspaceSchema'
" +
+              "created successfully in 'dfs_test.tmp' schema";
+          assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result,
expected),
+              expected.equals(result));
+
+          // query from view
+          resultSet = statement.executeQuery(
+              "SELECT region_id FROM dfs_test.tmp.testViewResolvingTablesInWorkspaceSchema
LIMIT 1");
+          result = JdbcAssert.toString(resultSet).trim();
+          resultSet.close();
+          expected = "region_id=0";
+          assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result,
expected),
+              expected.equals(result));
+
+          statement.executeQuery("drop view dfs_test.tmp.testViewResolvingTablesInWorkspaceSchema").close();
+
+          statement.close();
+          return null;
+        } catch (Exception e) {
+          throw new RuntimeException(e);
+        }
+      }
+    });
+  }
 }


Mime
View raw message