drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jacq...@apache.org
Subject [15/17] git commit: DRILL-1136: Check for existance of view before creating one when no OR REPLACE clause is provided
Date Mon, 21 Jul 2014 02:46:37 GMT
DRILL-1136: Check for existance of view before creating one when no OR REPLACE clause is provided


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

Branch: refs/heads/master
Commit: fffd0d38af93aee16e0103a3133306c0eb07092e
Parents: 7e561de
Author: vkorukanti <venki.korukanti@gmail.com>
Authored: Tue Jul 15 09:02:46 2014 -0700
Committer: Jacques Nadeau <jacques@apache.org>
Committed: Sun Jul 20 16:49:19 2014 -0700

----------------------------------------------------------------------
 .../exec/planner/sql/handlers/ViewHandler.java  |  4 ++
 .../exec/store/dfs/WorkspaceSchemaFactory.java  |  5 +++
 .../apache/drill/exec/sql/TestViewSupport.java  |  2 +-
 .../org/apache/drill/jdbc/test/TestViews.java   | 47 ++++++++++++++++++++
 4 files changed, 57 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/fffd0d38/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 ba70ae5..b6bb7a1 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
@@ -110,6 +110,10 @@ public abstract class ViewHandler extends AbstractSqlHandler{
 
         boolean replaced;
         if (drillSchema instanceof WorkspaceSchema) {
+          WorkspaceSchema workspaceSchema = (WorkspaceSchema) drillSchema;
+          if (!createView.getReplace() && workspaceSchema.viewExists(view.getName()))
{
+            return DirectPlan.createDirectPlan(context, false, "View with given name already
exists in current schema");
+          }
           replaced = ((WorkspaceSchema) drillSchema).createView(view);
         }else{
           return DirectPlan.createDirectPlan(context, false, "Schema provided was not a workspace
schema.");

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/fffd0d38/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
index 1d2bf91..54781b4 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/store/dfs/WorkspaceSchemaFactory.java
@@ -174,6 +174,11 @@ public class WorkspaceSchemaFactory implements ExpandingConcurrentMap.MapValueFa
       return replaced;
     }
 
+    public boolean viewExists(String viewName) throws Exception {
+      Path viewPath = getViewPath(viewName);
+      return fs.getUnderlying().exists(viewPath);
+    }
+
     public void dropView(String viewName) throws IOException {
       fs.getUnderlying().delete(getViewPath(viewName), false);
       if(knownViews != null) knownViews.delete(viewName);

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/fffd0d38/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
index 585e931..b745baa 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestViewSupport.java
@@ -27,7 +27,7 @@ public class TestViewSupport extends BaseTestQuery{
   public void referToSchemaInsideAndOutsideView() throws Exception {
     String use = "use dfs_test.tmp;";
     String selectInto = "create table monkey as select c_custkey, c_regionkey from cp.`tpch/customer.parquet`";
-    String createView = "create view myMonkeyView as select c_custkey, c_regionkey from monkey";
+    String createView = "create or replace view myMonkeyView as select c_custkey, c_regionkey
from monkey";
     String selectInside = "select * from myMonkeyView;";
     String use2 = "use cp;";
     String selectOutside = "select * from dfs_test.tmp.myMonkeyView;";

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/fffd0d38/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 89d9573..2cbba34 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
@@ -463,4 +463,51 @@ public class TestViews extends JdbcTestQueryBase {
       }
     });
   }
+
+  @Test
+  public void testCreateViewWhenViewAlreadyExists() throws Exception{
+    JdbcAssert.withFull("dfs_test.tmp").withConnection(new Function<Connection, Void>()
{
+      public Void apply(Connection connection) {
+        try {
+          Statement statement = connection.createStatement();
+
+          // create a view
+          ResultSet resultSet =  statement.executeQuery(
+              "CREATE VIEW testCreateViewWhenViewAlreadyExists AS SELECT region_id, sales_city
FROM cp.`region.json`");
+          String result = JdbcAssert.toString(resultSet).trim();
+          resultSet.close();
+          String expected = "ok=true; summary=View 'testCreateViewWhenViewAlreadyExists'
" +
+              "created successfully in 'dfs_test.tmp' schema";
+          assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result,
expected),
+              expected.equals(result));
+
+          // try to create the view with same name
+          resultSet =  statement.executeQuery(
+              "CREATE VIEW testCreateViewWhenViewAlreadyExists AS SELECT region_id FROM cp.`region.json`");
+          result = JdbcAssert.toString(resultSet).trim();
+          resultSet.close();
+          expected = "ok=false; summary=View with given name already exists in current schema";
+          assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result,
expected),
+              expected.equals(result));
+
+          // try creating the view with same name but with a OR REPLACE clause
+          resultSet =  statement.executeQuery(
+              "CREATE OR REPLACE VIEW testCreateViewWhenViewAlreadyExists AS SELECT region_id
FROM cp.`region.json`");
+          result = JdbcAssert.toString(resultSet).trim();
+          resultSet.close();
+          expected = "ok=true; summary=View 'testCreateViewWhenViewAlreadyExists' " +
+              "replaced successfully in 'dfs_test.tmp' schema";
+          assertTrue(String.format("Generated string:\n%s\ndoes not match:\n%s", result,
expected),
+              expected.equals(result));
+
+          statement.executeQuery("drop view dfs_test.tmp.testCreateViewWhenViewAlreadyExists").close();
+
+          statement.close();
+          return null;
+        } catch (Exception e) {
+          throw new RuntimeException(e);
+        }
+      }
+    });
+  }
 }


Mime
View raw message