You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2014/05/27 21:50:48 UTC

git commit: PHOENIX-1004 'drop index' should delete index data from local index table (Rajeshbabu)

Repository: incubator-phoenix
Updated Branches:
  refs/heads/local-index 16ad19025 -> a56780189


PHOENIX-1004 'drop index' should delete index data from local index table (Rajeshbabu)


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

Branch: refs/heads/local-index
Commit: a56780189cc263f9d0a340fc9eb1102c533bcad9
Parents: 16ad190
Author: James Taylor <jt...@salesforce.com>
Authored: Tue May 27 12:51:56 2014 -0700
Committer: James Taylor <jt...@salesforce.com>
Committed: Tue May 27 12:51:56 2014 -0700

----------------------------------------------------------------------
 .../phoenix/end2end/index/LocalIndexIT.java     | 36 ++++++++++++++++++++
 .../phoenix/compile/IndexStatementRewriter.java |  6 ++++
 .../apache/phoenix/index/IndexMaintainer.java   | 26 ++++++++++----
 .../apache/phoenix/schema/MetaDataClient.java   | 10 +++---
 4 files changed, 67 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/a5678018/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
index 16743e4..c4e935d 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java
@@ -310,4 +310,40 @@ public class LocalIndexIT extends BaseIndexIT {
         assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + INDEX_TABLE_NAME + "2" + " ['a']",QueryUtil.getExplainPlan(rs1));
         conn1.close();
     }
+
+    @Test
+    public void testDropLocalIndexShouldDeleteDataFromLocalIndexTable() throws Exception {
+        createBaseTable(DATA_TABLE_NAME, null, "('e','i','o')");
+        Connection conn1 = DriverManager.getConnection(getUrl());
+        try {
+            conn1.createStatement().execute("UPSERT INTO " + DATA_TABLE_NAME + " values('b',1,2,'z')");
+            conn1.createStatement().execute("UPSERT INTO " + DATA_TABLE_NAME + " values('f',1,2,'a')");
+            conn1.createStatement().execute("UPSERT INTO " + DATA_TABLE_NAME + " values('j',2,4,'a')");
+            conn1.createStatement().execute("UPSERT INTO " + DATA_TABLE_NAME + " values('q',3,1,'c')");
+            conn1.commit();
+            conn1.createStatement().execute("CREATE LOCAL INDEX " + INDEX_TABLE_NAME + " ON " + DATA_TABLE_NAME + "(v1)");
+            conn1.createStatement().execute("DROP INDEX " + INDEX_TABLE_NAME + " ON " + DATA_TABLE_NAME);
+            HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
+            HTable indexTable = new HTable(admin.getConfiguration() ,TableName.valueOf(MetaDataUtil.getLocalIndexTableName(DATA_TABLE_NAME)));
+            Pair<byte[][], byte[][]> startEndKeys = indexTable.getStartEndKeys();
+            byte[][] startKeys = startEndKeys.getFirst();
+            byte[][] endKeys = startEndKeys.getSecond();
+            // No entry should be present in local index table after drop index.
+            for (int i = 0; i < startKeys.length; i++) {
+                Scan s = new Scan();
+                s.setStartRow(startKeys[i]);
+                s.setStopRow(endKeys[i]);
+                ResultScanner scanner = indexTable.getScanner(s);
+                int count = 0;
+                for(Result r:scanner){
+                    count++;
+                }
+                scanner.close();
+                assertEquals(0, count);
+            }
+            indexTable.close();
+        } finally {
+            conn1.close();
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/a5678018/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
index 68a1218..b6acc65 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/IndexStatementRewriter.java
@@ -96,6 +96,12 @@ public class IndexStatementRewriter extends ParseNodeRewriter {
 
         String indexColName = IndexUtil.getIndexColumnName(dataCol);
         // Same alias as before, but use the index column name instead of the data column name
+        // TODO: add dataColRef as an alternate ColumnParseNode in the case that the index
+        // ColumnParseNode cannot be resolved. When this occurs, add the dataColRef to a list
+        // on the StatementContext to indicate that a join back to the data table is necessary.
+        // At ExpressionCompiler.resolveColumn(), test if the table is a local index, and only
+        // then use the alternate. How will the values be resolved on the client? Need to have
+        // a special cf? Or append the values to the PK?
         ParseNode indexColNode = new ColumnParseNode(tName, node.isCaseSensitive() ? '"' + indexColName + '"' : indexColName, node.getAlias());
         PDataType indexColType = IndexUtil.getIndexColumnDataType(dataCol);
         PDataType dataColType = dataColRef.getColumn().getDataType();

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/a5678018/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java b/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
index a3a8791..749bbb0 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/index/IndexMaintainer.java
@@ -137,16 +137,28 @@ public class IndexMaintainer implements Writable, Iterable<ColumnReference> {
      * @param ptr bytes pointer to hold returned serialized value
      */
     public static void serialize(PTable dataTable, ImmutableBytesWritable ptr) {
-        Iterator<PTable> indexes = nonDisabledIndexIterator(dataTable.getIndexes().iterator());
-        if (dataTable.isImmutableRows() || !indexes.hasNext()) {
+        List<PTable> indexes = dataTable.getIndexes();
+        serialize(dataTable, ptr, indexes);
+    }
+
+    /**
+     * For client-side to serialize all IndexMaintainers for a given table
+     * @param dataTable data table
+     * @param ptr bytes pointer to hold returned serialized value
+     * @param indexes indexes to serialize
+     */
+    public static void serialize(PTable dataTable, ImmutableBytesWritable ptr,
+            List<PTable> indexes) {
+        Iterator<PTable> indexesItr = nonDisabledIndexIterator(indexes.iterator());
+        if (dataTable.isImmutableRows() || !indexesItr.hasNext()) {
             ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
             return;
         }
         int nIndexes = 0;
         int estimatedSize = dataTable.getRowKeySchema().getEstimatedByteSize() + 2;
-        while (indexes.hasNext()) {
+        while (indexesItr.hasNext()) {
             nIndexes++;
-            PTable index = indexes.next();
+            PTable index = indexesItr.next();
             estimatedSize += index.getIndexMaintainer(dataTable).getEstimatedByteSize();
         }
         TrustedByteArrayOutputStream stream = new TrustedByteArrayOutputStream(estimatedSize + 1);
@@ -156,9 +168,9 @@ public class IndexMaintainer implements Writable, Iterable<ColumnReference> {
             WritableUtils.writeVInt(output, nIndexes * (dataTable.getBucketNum() == null ? 1 : -1));
             // Write out data row key schema once, since it's the same for all index maintainers
             dataTable.getRowKeySchema().write(output);
-            indexes = nonDisabledIndexIterator(dataTable.getIndexes().iterator());
-            while (indexes.hasNext()) {
-                    indexes.next().getIndexMaintainer(dataTable).write(output);
+            indexesItr = nonDisabledIndexIterator(indexes.iterator());
+            while (indexesItr.hasNext()) {
+                    indexesItr.next().getIndexMaintainer(dataTable).write(output);
             }
         } catch (IOException e) {
             throw new RuntimeException(e); // Impossible

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/a5678018/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 b420c15..058a039 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
@@ -101,7 +101,6 @@ import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.exception.SQLExceptionInfo;
 import org.apache.phoenix.execute.MutationState;
 import org.apache.phoenix.hbase.index.covered.update.ColumnReference;
-import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
 import org.apache.phoenix.index.IndexMaintainer;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
@@ -514,7 +513,10 @@ public class MetaDataClient {
                 Scan scan = plan.getContext().getScan();
                 ImmutableBytesWritable ptr = new ImmutableBytesWritable();
                 PTable dataTable = dataTableRef.getTable();
-                dataTable.getIndexMaintainers(ptr);
+                List<PTable> indexes = Lists.newArrayListWithExpectedSize(1);
+                // Only build newly created index.
+                indexes.add(index);
+                IndexMaintainer.serialize(dataTable, ptr, indexes);
                 scan.setAttribute(BaseScannerRegionObserver.LOCAL_INDEX_BUILD, ByteUtil.copyKeyBytesIfNecessary(ptr));
                 // By default, we'd use a FirstKeyOnly filter as nothing else needs to be projected for count(*).
                 // However, in this case, we need to project all of the data columns that contribute to the index.
@@ -1379,11 +1381,11 @@ public class MetaDataClient {
                         connection.removeTable(tenantId, tableName);
                     } catch (TableNotFoundException ignore) { } // Ignore - just means wasn't cached
                     
-                    // TODO: we need to drop the index data when a view is dropped
-                    boolean dropMetaData = connection.getQueryServices().getProps().getBoolean(DROP_METADATA_ATTRIB, DEFAULT_DROP_METADATA);
                     if (result.getTable() != null && tableType != PTableType.VIEW) {
                         connection.setAutoCommit(true);
                         PTable table = result.getTable();
+                        boolean dropMetaData = result.getTable().getViewIndexId() == null && 
+                                connection.getQueryServices().getProps().getBoolean(DROP_METADATA_ATTRIB, DEFAULT_DROP_METADATA);
                         long ts = (scn == null ? result.getMutationTime() : scn);
                         // Create empty table and schema - they're only used to get the name from
                         // PName name, PTableType type, long timeStamp, long sequenceNumber, List<PColumn> columns