Repository: phoenix
Updated Branches:
  refs/heads/4.x-cdh5.15 505551251 -> f8836f7a2


PHOENIX-4971 Drop index will execute successfully using Incorrect name of 
parent tables


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

Branch: refs/heads/4.x-cdh5.15
Commit: ce3c451fc6e3dfd598b2de302901f5d1195bc3e3
Parents: 5055512
Author: Jaanai <cloud.pos...@gmail.com>
Authored: Sat Nov 24 17:22:49 2018 +0000
Committer: Pedro Boado <pbo...@apache.org>
Committed: Tue Nov 27 15:20:58 2018 +0000

----------------------------------------------------------------------
 .../java/org/apache/phoenix/end2end/ViewIT.java | 76 ++++++++++----------
 .../phoenix/end2end/index/DropMetadataIT.java   | 23 +++++-
 .../phoenix/end2end/index/IndexMetadataIT.java  |  5 +-
 .../coprocessor/MetaDataEndpointImpl.java       |  2 +-
 .../phoenix/exception/SQLExceptionCode.java     |  2 +
 .../apache/phoenix/schema/MetaDataClient.java   | 16 +++++
 6 files changed, 83 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ce3c451f/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
index 090ccaa..6318dca 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java
@@ -59,6 +59,7 @@ import org.apache.hadoop.hbase.DoNotRetryIOException;
 import org.apache.hadoop.hbase.HColumnDescriptor;
 import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.Admin;
 import org.apache.hadoop.hbase.client.HBaseAdmin;
 import org.apache.hadoop.hbase.client.Scan;
 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
@@ -908,60 +909,61 @@ public class ViewIT extends SplitSystemCatalogIT {
         props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, 
Boolean.TRUE.toString());
 
         try (Connection conn = DriverManager.getConnection(getUrl(), props);
-                HBaseAdmin admin =
-                        
conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
+                Admin admin = 
conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
 
             conn.createStatement().execute("CREATE SCHEMA " + NS);
 
             // test for a view that is in non-default schema
-            HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(NS, 
TBL));
-            desc.addFamily(new HColumnDescriptor(CF));
-            admin.createTable(desc);
+            {
+                HTableDescriptor desc = new 
HTableDescriptor(TableName.valueOf(NS, TBL));
+                desc.addFamily(new HColumnDescriptor(CF));
+                admin.createTable(desc);
 
-            String view1 = NS + "." + TBL;
-            conn.createStatement().execute(
-                "CREATE VIEW " + view1 + " (PK VARCHAR PRIMARY KEY, " + CF + 
".COL VARCHAR)");
+                String view1 = NS + "." + TBL;
+                conn.createStatement().execute(
+                        "CREATE VIEW " + view1 + " (PK VARCHAR PRIMARY KEY, " 
+ CF + ".COL VARCHAR)");
 
-            assertTrue(QueryUtil
-                    .getExplainPlan(
+                assertTrue(QueryUtil.getExplainPlan(
                         conn.createStatement().executeQuery("explain select * 
from " + view1))
-                    .contains(NS + ":" + TBL));
+                        .contains(NS + ":" + TBL));
 
-            
+                conn.createStatement().execute("DROP VIEW " + view1);
+            }
+
+            // test for a view whose name contains a dot (e.g. "AAA.BBB") in 
default schema (for backward compatibility)
+            {
+                HTableDescriptor desc = new 
HTableDescriptor(TableName.valueOf(NS + "." + TBL));
+                desc.addFamily(new HColumnDescriptor(CF));
+                admin.createTable(desc);
 
-            // test for a view whose name contains a dot (e.g. "AAA.BBB") in 
default schema (for
-            // backward compatibility)
-            desc = new HTableDescriptor(TableName.valueOf(NS + "." + TBL));
-            desc.addFamily(new HColumnDescriptor(CF));
-            admin.createTable(desc);
+                String view2 = "\"" + NS + "." + TBL + "\"";
+                conn.createStatement().execute(
+                        "CREATE VIEW " + view2 + " (PK VARCHAR PRIMARY KEY, " 
+ CF + ".COL VARCHAR)");
 
-            String view2 = "\"" + NS + "." + TBL + "\"";
-            conn.createStatement().execute(
-                "CREATE VIEW " + view2 + " (PK VARCHAR PRIMARY KEY, " + CF + 
".COL VARCHAR)");
+                assertTrue(QueryUtil
+                        .getExplainPlan(
+                                conn.createStatement().executeQuery("explain 
select * from " + view2))
+                        .contains(NS + "." + TBL));
 
-            assertTrue(QueryUtil
-                    .getExplainPlan(
-                        conn.createStatement().executeQuery("explain select * 
from " + view2))
-                    .contains(NS + "." + TBL));
+                conn.createStatement().execute("DROP VIEW " + view2);
+            }
 
             // test for a view whose name contains a dot (e.g. "AAA.BBB") in 
non-default schema
-            desc = new HTableDescriptor(TableName.valueOf(NS, NS + "." + TBL));
-            desc.addFamily(new HColumnDescriptor(CF));
-            admin.createTable(desc);
+            {
+                HTableDescriptor desc = new 
HTableDescriptor(TableName.valueOf(NS, NS + "." + TBL));
+                desc.addFamily(new HColumnDescriptor(CF));
+                admin.createTable(desc);
 
-            String view3 = NS + ".\"" + NS + "." + TBL + "\"";
-            conn.createStatement().execute(
-                "CREATE VIEW " + view3 + " (PK VARCHAR PRIMARY KEY, " + CF + 
".COL VARCHAR)");
+                String view3 = NS + ".\"" + NS + "." + TBL + "\"";
+                conn.createStatement().execute(
+                        "CREATE VIEW " + view3 + " (PK VARCHAR PRIMARY KEY, " 
+ CF + ".COL VARCHAR)");
 
-            assertTrue(QueryUtil
-                    .getExplainPlan(
+                assertTrue(QueryUtil.getExplainPlan(
                         conn.createStatement().executeQuery("explain select * 
from " + view3))
-                    .contains(NS + ":" + NS + "." + TBL));
-            
-            conn.createStatement().execute("DROP VIEW " + view1);
-            conn.createStatement().execute("DROP VIEW " + view2);
-            conn.createStatement().execute("DROP VIEW " + view3);
+                        .contains(NS + ":" + NS + "." + TBL));
 
+                conn.createStatement().execute("DROP VIEW " + view3);
+            }
             conn.createStatement().execute("DROP SCHEMA " + NS);
         }
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ce3c451f/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropMetadataIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropMetadataIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropMetadataIT.java
index b92ed8d..3c670c5 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropMetadataIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/DropMetadataIT.java
@@ -18,9 +18,12 @@
 package org.apache.phoenix.end2end.index;
 
 import static org.apache.phoenix.util.TestUtil.HBASE_NATIVE_SCHEMA_NAME;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.SQLException;
 import java.util.Properties;
 
 import org.apache.hadoop.hbase.HColumnDescriptor;
@@ -29,6 +32,7 @@ import org.apache.hadoop.hbase.client.HBaseAdmin;
 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.end2end.ParallelStatsDisabledIT;
+import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.util.PropertiesUtil;
@@ -56,7 +60,24 @@ public class DropMetadataIT extends ParallelStatsDisabledIT {
         String url = QueryUtil.getConnectionUrl(props, config, PRINCIPAL);
         return DriverManager.getConnection(url, props);
     }
-    
+
+    @Test
+    public void testDropIndexTableHasSameNameWithDataTable() {
+        String tableName = generateUniqueName();
+        String indexName = "IDX_" + tableName;
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            String createTable = "CREATE TABLE " + tableName + "  (id varchar 
not null primary key, col integer)";
+            conn.createStatement().execute(createTable);
+            String createIndex = "CREATE INDEX " + indexName + " on " + 
tableName + "(col)";
+            conn.createStatement().execute(createIndex);
+            String dropIndex = "DROP INDEX " + indexName + " on " + indexName;
+            conn.createStatement().execute(dropIndex);
+            fail("should not execute successfully");
+        } catch (SQLException e) {
+            assertTrue(SQLExceptionCode.PARENT_TABLE_NOT_FOUND.getErrorCode() 
== e.getErrorCode());
+        }
+    }
+
     @Test
     public void testDropViewKeepsHTable() throws Exception {
         Connection conn = getConnection();

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ce3c451f/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexMetadataIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexMetadataIT.java
 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexMetadataIT.java
index f797b70..bb7e6ad 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexMetadataIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexMetadataIT.java
@@ -284,9 +284,10 @@ public class IndexMetadataIT extends 
ParallelStatsDisabledIT {
             try {
                 conn.createStatement().execute("DROP INDEX " + indexName + "1 
ON " + diffTableNameInSameSchema);
                 fail("Should have realized index " + indexName + "1 is not on 
the table");
-            } catch (TableNotFoundException ignore) {
-                
+            } catch (SQLException e) {
+                assertTrue(e.getErrorCode() == 
SQLExceptionCode.PARENT_TABLE_NOT_FOUND.getErrorCode());
             }
+
             ddl = "DROP TABLE " + INDEX_DATA_SCHEMA + 
QueryConstants.NAME_SEPARATOR + indexDataTable;
             conn.createStatement().execute(ddl);
             

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ce3c451f/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
index 5562340..b3b9185 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
@@ -2435,7 +2435,7 @@ public class MetaDataEndpointImpl extends 
MetaDataProtocol implements Coprocesso
                             .create(Bytes.toString(viewSchemaName), 
Bytes.toString(viewName));
                     try {
                         client.dropTable(
-                                new DropTableStatement(viewTableName, 
PTableType.VIEW, false, true, true));
+                                new DropTableStatement(viewTableName, 
PTableType.VIEW, true, true, true));
                     }
                     catch (TableNotFoundException e) {
                         logger.info("Ignoring view "+viewTableName+" as it has 
already been dropped");

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ce3c451f/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java 
b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
index d557714..5bffed5 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
@@ -185,6 +185,8 @@ public enum SQLExceptionCode {
      INVALID_REPLAY_AT(533, "42910", "Value of REPLAY_AT cannot be less than 
zero."),
      UNEQUAL_SCN_AND_BUILD_INDEX_AT(534, "42911", "If both specified, values 
of CURRENT_SCN and BUILD_INDEX_AT must be equal."),
      ONLY_INDEX_UPDATABLE_AT_SCN(535, "42912", "Only an index may be updated 
when the BUILD_INDEX_AT property is specified"),
+     PARENT_TABLE_NOT_FOUND(536, "42913", "Can't drop the index because the 
parent table in the DROP statement is incorrect."),
+
      /**
      * HBase and Phoenix specific implementation defined sub-classes.
      * Column family related exceptions.

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ce3c451f/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 3b68247..4c8af0b 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
@@ -23,6 +23,7 @@ import static org.apache.hadoop.hbase.HColumnDescriptor.TTL;
 import static 
org.apache.phoenix.coprocessor.BaseScannerRegionObserver.ANALYZE_TABLE;
 import static 
org.apache.phoenix.coprocessor.BaseScannerRegionObserver.RUN_UPDATE_STATS_ASYNC_ATTRIB;
 import static 
org.apache.phoenix.exception.SQLExceptionCode.INSUFFICIENT_MULTI_TENANT_COLUMNS;
+import static 
org.apache.phoenix.exception.SQLExceptionCode.PARENT_TABLE_NOT_FOUND;
 import static 
org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.APPEND_ONLY_SCHEMA;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ARG_POSITION;
 import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ARRAY_SIZE;
@@ -3122,8 +3123,23 @@ public class MetaDataClient {
             connection.setAutoCommit(wasAutoCommit);
         }
     }
+
     MutationState dropTable(String schemaName, String tableName, String 
parentTableName, PTableType tableType,
             boolean ifExists, boolean cascade, boolean 
skipAddingParentColumns) throws SQLException {
+        // Checking the parent table whether exists
+        String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
+        try {
+            PTable ptable = connection.getTable(new 
PTableKey(connection.getTenantId(), fullTableName));
+            if (parentTableName != null 
&&!parentTableName.equals(ptable.getParentTableName().getString())) {
+                throw new SQLExceptionInfo.Builder(PARENT_TABLE_NOT_FOUND)
+                        
.setSchemaName(schemaName).setTableName(tableName).build().buildException();
+            }
+        } catch (TableNotFoundException e) {
+            if (!ifExists) {
+                throw e;
+            }
+        }
+
         connection.rollback();
         boolean wasAutoCommit = connection.getAutoCommit();
         PName tenantId = connection.getTenantId();

Reply via email to