Repository: phoenix
Updated Branches:
  refs/heads/master 3e4aec1bc -> 1e78d3b36


PHOENIX-3349 DROP TABLE and DROP SEQUENCE not working with schemas


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

Branch: refs/heads/master
Commit: 1e78d3b368b7aa8397224074f691ea2fed340e34
Parents: 3e4aec1
Author: Ankit Singhal <ankitsingha...@gmail.com>
Authored: Tue Oct 18 12:50:56 2016 +0530
Committer: Ankit Singhal <ankitsingha...@gmail.com>
Committed: Tue Oct 18 12:50:56 2016 +0530

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/UseSchemaIT.java | 39 ++++++++++++++++++++
 .../apache/phoenix/compile/SequenceManager.java |  3 ++
 .../apache/phoenix/schema/MetaDataClient.java   |  6 ++-
 3 files changed, 46 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e78d3b3/phoenix-core/src/it/java/org/apache/phoenix/end2end/UseSchemaIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UseSchemaIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UseSchemaIT.java
index c43fb0e..07ae77e 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UseSchemaIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UseSchemaIT.java
@@ -115,6 +115,45 @@ public class UseSchemaIT extends ParallelStatsDisabledIT {
         rs = conn.createStatement().executeQuery("select schema_name from " + 
testTable );
         assertTrue(rs.next());
         assertEquals(schema, rs.getString(1));
+        conn.createStatement().execute("DROP TABLE " + testTable);
+        conn.close();
+    }
+    
+    @Test
+    public void testSequences() throws Exception {
+        Properties props = new Properties();
+        String schema = generateUniqueName();
+        props.setProperty(QueryServices.SCHEMA_ATTRIB, schema);
+        props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, 
Boolean.toString(true));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.setAutoCommit(true);
+        String ddl = "CREATE SCHEMA IF NOT EXISTS " + schema;
+        conn.createStatement().execute(ddl);
+        String sequenceName = generateUniqueName();
+        ddl = "create SEQUENCE "+schema + "." + sequenceName + " START WITH 
100 INCREMENT BY 2 CACHE 10";
+        conn.createStatement().execute(ddl);
+        String query = "SELECT NEXT VALUE FOR "+schema + "." + sequenceName;
+        ResultSet rs = conn.createStatement().executeQuery(query);
+        assertTrue(rs.next());
+        assertEquals("100", rs.getString(1));
+        conn.createStatement().execute("DROP Sequence " + schema + "." + 
sequenceName);
+        
+        schema = generateUniqueName();
+        sequenceName = generateUniqueName();
+        ddl = "CREATE SCHEMA " + schema;
+        conn.createStatement().execute(ddl);
+        conn.createStatement().execute("use " + schema);
+        ddl = "create SEQUENCE "+ sequenceName + " START WITH 100 INCREMENT BY 
2 CACHE 10";
+        conn.createStatement().execute(ddl);
+        query = "SELECT NEXT VALUE FOR "+sequenceName;
+        rs = conn.createStatement().executeQuery(query);
+        assertTrue(rs.next());
+        assertEquals("100", rs.getString(1));
+        query = "SELECT CURRENT VALUE FOR "+sequenceName;
+        rs = conn.createStatement().executeQuery(query);
+        assertTrue(rs.next());
+        assertEquals("100", rs.getString(1));
+        conn.createStatement().execute("DROP Sequence " + sequenceName);
         conn.close();
     }
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e78d3b3/phoenix-core/src/main/java/org/apache/phoenix/compile/SequenceManager.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/compile/SequenceManager.java 
b/phoenix-core/src/main/java/org/apache/phoenix/compile/SequenceManager.java
index 5ec8cd2..4159be1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/SequenceManager.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/SequenceManager.java
@@ -124,6 +124,9 @@ public class SequenceManager {
         PName tenantName = statement.getConnection().getTenantId();
         String tenantId = tenantName == null ? null : tenantName.getString();
         TableName tableName = node.getTableName();
+        if (tableName.getSchemaName() == null && 
statement.getConnection().getSchema() != null) {
+            tableName = 
TableName.create(statement.getConnection().getSchema(), 
tableName.getTableName());
+        }
         int nSaltBuckets = 
statement.getConnection().getQueryServices().getSequenceSaltBuckets();
         ParseNode numToAllocateNode = node.getNumToAllocateNode();
         

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1e78d3b3/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java 
b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
index 2dccb32..285c8fa 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
@@ -1456,7 +1456,8 @@ public class MetaDataClient {
     public MutationState dropSequence(DropSequenceStatement statement) throws 
SQLException {
         Long scn = connection.getSCN();
         long timestamp = scn == null ? HConstants.LATEST_TIMESTAMP : scn;
-        String schemaName = statement.getSequenceName().getSchemaName();
+        String schemaName = connection.getSchema() != null && 
statement.getSequenceName().getSchemaName() == null
+                ? connection.getSchema() : 
statement.getSequenceName().getSchemaName();
         String sequenceName = statement.getSequenceName().getTableName();
         String tenantId = connection.getTenantId() == null ? null : 
connection.getTenantId().getString();
         try {
@@ -2391,7 +2392,8 @@ public class MetaDataClient {
     }
 
     public MutationState dropTable(DropTableStatement statement) throws 
SQLException {
-        String schemaName = statement.getTableName().getSchemaName();
+        String schemaName = connection.getSchema() != null && 
statement.getTableName().getSchemaName() == null
+                ? connection.getSchema() : 
statement.getTableName().getSchemaName();
         String tableName = statement.getTableName().getTableName();
         return dropTable(schemaName, tableName, null, 
statement.getTableType(), statement.ifExists(), statement.cascade());
     }

Reply via email to