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/03/21 11:08:06 UTC

git commit: PHOENIX-885 Metadata ORDINAL_POSITION is off by one if table is salted (JamesTaylor)

Repository: incubator-phoenix
Updated Branches:
  refs/heads/4.0 cab20a0f7 -> b22d3450c


PHOENIX-885 Metadata ORDINAL_POSITION is off by one if table is salted (JamesTaylor)


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

Branch: refs/heads/4.0
Commit: b22d3450c2b8a67e5f51f1e51a6b85a31835eefc
Parents: cab20a0
Author: James Taylor <ja...@apache.org>
Authored: Fri Mar 21 03:06:38 2014 -0700
Committer: James Taylor <ja...@apache.org>
Committed: Fri Mar 21 03:07:53 2014 -0700

----------------------------------------------------------------------
 .../end2end/BaseClientManagedTimeIT.java        |  1 -
 .../phoenix/end2end/BaseConnectedQueryIT.java   |  6 +-
 .../end2end/QueryDatabaseMetaDataIT.java        |  8 +++
 .../coprocessor/MetaDataEndpointImpl.java       | 68 +++++++-------------
 .../phoenix/exception/SQLExceptionCode.java     |  2 +-
 .../query/ConnectionQueryServicesImpl.java      | 10 ++-
 .../apache/phoenix/schema/MetaDataClient.java   | 16 +++--
 .../org/apache/phoenix/util/SchemaUtil.java     |  9 +--
 8 files changed, 57 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseClientManagedTimeIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseClientManagedTimeIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseClientManagedTimeIT.java
index 9168588..61e2762 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseClientManagedTimeIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseClientManagedTimeIT.java
@@ -17,7 +17,6 @@
  */
 package org.apache.phoenix.end2end;
 
-import org.apache.hadoop.hbase.HConstants;
 import org.junit.Before;
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseConnectedQueryIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseConnectedQueryIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseConnectedQueryIT.java
index e22349d..e6bb65f 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseConnectedQueryIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/BaseConnectedQueryIT.java
@@ -76,16 +76,14 @@ import org.apache.hadoop.hbase.IntegrationTestingUtility;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
 import org.apache.phoenix.query.BaseTest;
-import org.apache.phoenix.query.ConnectionQueryServicesImpl;
 import org.apache.phoenix.query.HBaseFactoryProvider;
+import org.apache.phoenix.schema.NewerTableAlreadyExistsException;
 import org.apache.phoenix.schema.PTableType;
+import org.apache.phoenix.schema.TableNotFoundException;
 import org.apache.phoenix.util.PhoenixRuntime;
 import org.apache.phoenix.util.SchemaUtil;
 import org.apache.phoenix.util.TestUtil;
 import org.junit.BeforeClass;
-import org.apache.phoenix.jdbc.PhoenixTestDriver;
-import org.apache.phoenix.schema.NewerTableAlreadyExistsException;
-import org.apache.phoenix.schema.TableNotFoundException;
 import org.slf4j.LoggerFactory;
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryDatabaseMetaDataIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryDatabaseMetaDataIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryDatabaseMetaDataIT.java
index c20b515..5a38076 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryDatabaseMetaDataIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryDatabaseMetaDataIT.java
@@ -31,6 +31,7 @@ import static org.apache.phoenix.util.TestUtil.MDTEST_NAME;
 import static org.apache.phoenix.util.TestUtil.MDTEST_SCHEMA_NAME;
 import static org.apache.phoenix.util.TestUtil.PTSDB_NAME;
 import static org.apache.phoenix.util.TestUtil.STABLE_NAME;
+import static org.apache.phoenix.util.TestUtil.TABLE_WITH_SALTING;
 import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -354,6 +355,13 @@ public class QueryDatabaseMetaDataIT extends BaseClientManagedTimeIT {
         assertEquals(SchemaUtil.normalizeIdentifier("b"), rs.getString("COLUMN_FAMILY"));
         assertEquals(SchemaUtil.normalizeIdentifier("col2"), rs.getString("COLUMN_NAME"));
         assertFalse(rs.next());
+
+        ensureTableCreated(getUrl(), TABLE_WITH_SALTING, null, ts);
+        rs = dbmd.getColumns("", "", TABLE_WITH_SALTING, StringUtil.escapeLike("A_INTEGER"));
+        assertTrue(rs.next());
+        assertEquals(1, rs.getInt("ORDINAL_POSITION"));
+        assertFalse(rs.next());
+    
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/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 5adf649..dcda21e 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
@@ -444,7 +444,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
 
     @SuppressWarnings("deprecation")
     private void addColumnToTable(List<Cell> results, PName colName, PName famName,
-        Cell[] colKeyValues, List<PColumn> columns) {
+        Cell[] colKeyValues, List<PColumn> columns, boolean isSalted) {
         int i = 0;
         int j = 0;
         while (i < results.size() && j < COLUMN_KV_COLUMNS.size()) {
@@ -481,7 +481,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
         Cell ordinalPositionKv = colKeyValues[ORDINAL_POSITION_INDEX];
         int position =
                 PDataType.INTEGER.getCodec().decodeInt(ordinalPositionKv.getValueArray(),
-                    ordinalPositionKv.getValueOffset(), SortOrder.getDefault());
+                    ordinalPositionKv.getValueOffset(), SortOrder.getDefault()) + (isSalted ? 1 : 0);
         Cell nullableKv = colKeyValues[NULLABLE_INDEX];
         boolean isNullable =
                 PDataType.INTEGER.getCodec().decodeInt(nullableKv.getValueArray(),
@@ -650,7 +650,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
                   logger.warn("Unknown link type: " + colKv.getValueArray()[colKv.getValueOffset()] + " for " + SchemaUtil.getTableName(schemaName.getString(), tableName.getString()));
               }
           } else {
-              addColumnToTable(results, colName, famName, colKeyValues, columns);
+              addColumnToTable(results, colName, famName, colKeyValues, columns, saltBucketNum != null);
           }
         }
 
@@ -854,11 +854,12 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
     private boolean hasViews(HRegion region, byte[] tenantId, PTable table) throws IOException {
         byte[] schemaName = table.getSchemaName().getBytes();
         byte[] tableName = table.getTableName().getBytes();
+        boolean isMultiTenant = table.isMultiTenant();
         Scan scan = new Scan();
         // If the table is multi-tenant, we need to check across all tenant_ids,
         // so we can't constrain the row key. Otherwise, any views would have
         // the same tenantId.
-        if (!table.isMultiTenant()) {
+        if (!isMultiTenant) {
             byte[] startRow = ByteUtil.concat(tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY);
             byte[] stopRow = ByteUtil.nextKey(startRow);
             scan.setStartRow(startRow);
@@ -929,8 +930,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
                         doDropTable(key, tenantIdBytes, schemaName, tableName,
                             PTableType.fromSerializedValue(tableType), tableMetadata,
                             invalidateList, locks, tableNamesToDelete);
-                if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS
-                        || result.getTable() == null) {
+                if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
                     done.run(MetaDataMutationResult.toProto(result));
                     return;
                 }
@@ -973,54 +973,37 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
         if (table != null
                 || (table = buildTable(key, cacheKey, region, HConstants.LATEST_TIMESTAMP)) != null) {
             if (table.getTimeStamp() < clientTimeStamp) {
-                // If the table is older than the client time stamp and its deleted, continue
-                if (isTableDeleted(table)) {
-                    return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS,
-                            EnvironmentEdgeManager.currentTimeMillis(), null);
-                }
-                if (tableType != table.getType()) {
-                    // We said to drop a table, but found a view or visa versa
-                    return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND,
-                            EnvironmentEdgeManager.currentTimeMillis(), null);
+                if (isTableDeleted(table) || tableType != table.getType()) {
+                    return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
                 }
             } else {
                 return new MetaDataMutationResult(MutationCode.NEWER_TABLE_FOUND,
                         EnvironmentEdgeManager.currentTimeMillis(), null);
             }
         }
-        if (table == null && buildDeletedTable(key, cacheKey, region, clientTimeStamp) != null) {
-            return new MetaDataMutationResult(MutationCode.NEWER_TABLE_FOUND,
-                    EnvironmentEdgeManager.currentTimeMillis(), null);
+        // We didn't find a table at the latest timestamp, so either there is no table or
+        // there was a table, but it's been deleted. In either case we want to return.
+        if (table == null) {
+            if (buildDeletedTable(key, cacheKey, region, clientTimeStamp) != null) {
+                return new MetaDataMutationResult(MutationCode.NEWER_TABLE_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
+            }
+            return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
         }
-        // Get mutations for main table.
+        // Since we don't allow back in time DDL, we know if we have a table it's the one
+        // we want to delete. FIXME: we shouldn't need a scan here, but should be able to
+        // use the table to generate the Delete markers.
         Scan scan = newTableRowsScan(key, MIN_TABLE_TIMESTAMP, clientTimeStamp);
         RegionScanner scanner = region.getScanner(scan);
         List<Cell> results = Lists.newArrayList();
         scanner.next(results);
-        if (results.isEmpty()) {
-            return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND,
-                    EnvironmentEdgeManager.currentTimeMillis(), null);
-        }
-
-        Cell typeKeyValue =
-                KeyValueUtil.getColumnLatest(GenericKeyValueBuilder.INSTANCE, results, PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES,
-                    PhoenixDatabaseMetaData.TABLE_TYPE_BYTES);
-        assert (typeKeyValue != null && typeKeyValue.getValueLength() == 1);
-        if (tableType != PTableType.fromSerializedValue(typeKeyValue.getValueArray()[typeKeyValue
-                .getValueOffset()])) {
-            // We said to drop a table, but found a view or visa versa
-            return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND,
-                    EnvironmentEdgeManager.currentTimeMillis(), null);
+        if (results.isEmpty()) { // Should not be possible
+            return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
         }
-        // Don't allow a table with views to be deleted
-        // TODO: support CASCADE with DROP
-        if (tableType == PTableType.TABLE && hasViews(region, tenantId, table)) {
-            return new MetaDataMutationResult(MutationCode.UNALLOWED_TABLE_MUTATION, 
-              EnvironmentEdgeManager.currentTimeMillis(), null);
+        if (hasViews(region, tenantId, table)) {
+            return new MetaDataMutationResult(MutationCode.UNALLOWED_TABLE_MUTATION, EnvironmentEdgeManager.currentTimeMillis(), null);
         }
-        if (table.getType() != PTableType.VIEW) { // Add to list of HTables to delete, unless it's a view
-            byte[] fullName = table.getName().getBytes();
-            tableNamesToDelete.add(fullName);
+        if (tableType != PTableType.VIEW) { // Add to list of HTables to delete, unless it's a view
+            tableNamesToDelete.add(table.getName().getBytes());
         }
         List<byte[]> indexNames = Lists.newArrayList();
         invalidateList.add(cacheKey);
@@ -1058,8 +1041,7 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements Coprocesso
             MetaDataMutationResult result =
                     doDropTable(indexKey, tenantId, schemaName, indexName, PTableType.INDEX,
                         rowsToDelete, invalidateList, locks, tableNamesToDelete);
-            if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS
-                    || result.getTable() == null) {
+            if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
                 return result;
             }
         }

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/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 3bf0ec0..d9e23f5 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
@@ -158,7 +158,7 @@ public enum SQLExceptionCode {
     PRIMARY_KEY_OUT_OF_ORDER(1004, "42J02", "Order of columns in primary key constraint must match the order in which they're declared."),
     VARBINARY_IN_ROW_KEY(1005, "42J03", "The VARBINARY/ARRAY type can only be used as the last part of a multi-part row key."),
     NOT_NULLABLE_COLUMN_IN_ROW_KEY(1006, "42J04", "Only nullable columns may be added to a multi-part row key."),
-    VARBINARY_LAST_PK(1015, "42J04", "Cannot add column to table when the last PK column is of type VARBINARY."),
+    VARBINARY_LAST_PK(1015, "42J04", "Cannot add column to table when the last PK column is of type VARBINARY or ARRAY."),
     NULLABLE_FIXED_WIDTH_LAST_PK(1023, "42J04", "Cannot add column to table when the last PK column is nullable and fixed width."),
     CANNOT_MODIFY_VIEW_PK(1036, "42J04", "Cannot modify the primary key of a VIEW."),
     BASE_TABLE_COLUMN(1037, "42J04", "Cannot modify columns of base table used by tenant-specific tables."),

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
index 75295c1..e10f96f 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
@@ -2018,6 +2018,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                 String lastTableName = null;
                 short nextKeySeq = 1;
                 String tablePkName = null;
+                boolean isSalted = false;
                 while (rs.next()) {
                     String pkName = null;
                     String defaultColumnFamily = null;
@@ -2034,6 +2035,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                         lastTableName = tableName;
                         nextKeySeq = 1; // reset
                         defaultColumnFamily = "_0"; // old default column family
+                        isSalted = rs.getInt(SALT_BUCKETS) > 0;
                         pkName = tablePkName = rs.getString(PK_NAME);
                         if (logger.isInfoEnabled()) {
                             logger.info("Upgrading table meta data for: " + SchemaUtil.getTableName(schemaName, tableName));
@@ -2058,6 +2060,12 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                      * - don't persist maxLength and scale for non decimal numbers
                      * - don't persist default precision for decimal
                      */
+                    Integer ordinalPosition = (Integer)rs.getObject(ORDINAL_POSITION);
+                    // ORDINAL_POSITION is not taking into account salting so we need
+                    // to subtract one
+                    if (ordinalPosition != null && isSalted) {
+                        ordinalPosition--;
+                    }
                     Integer maxLength = (Integer)rs.getObject(COLUMN_SIZE);
                     Integer scale = (Integer)rs.getObject(DECIMAL_DIGITS);
                     Integer dataTypeNum = (Integer)rs.getObject(DATA_TYPE);
@@ -2119,7 +2127,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement
                     stmt.setObject(16, maxLength);
                     stmt.setObject(17, scale);
                     stmt.setObject(18, rs.getObject(NULLABLE));
-                    stmt.setObject(19, rs.getObject(ORDINAL_POSITION));
+                    stmt.setObject(19, ordinalPosition);
                     stmt.setObject(20, rs.getObject("COLUMN_MODIFIER")); // old column name
                     stmt.setObject(21, keySeq); // new column for PK position
                     stmt.setObject(22, linkType); // link to index

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/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 9a5dfb6..9092c46 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
@@ -331,7 +331,7 @@ public class MetaDataClient {
     }
 
 
-    private void addColumnMutation(String schemaName, String tableName, PColumn column, PreparedStatement colUpsert, String parentTableName, String pkName, Short keySeq) throws SQLException {
+    private void addColumnMutation(String schemaName, String tableName, PColumn column, PreparedStatement colUpsert, String parentTableName, String pkName, Short keySeq, boolean isSalted) throws SQLException {
         colUpsert.setString(1, connection.getTenantId() == null ? null : connection.getTenantId().getString());
         colUpsert.setString(2, schemaName);
         colUpsert.setString(3, tableName);
@@ -349,7 +349,7 @@ public class MetaDataClient {
         } else {
             colUpsert.setInt(9, column.getScale());
         }
-        colUpsert.setInt(10, column.getPosition() + 1);
+        colUpsert.setInt(10, column.getPosition() + (isSalted ? 0 : 1));
         colUpsert.setInt(11, column.getSortOrder().getSystemValue());
         colUpsert.setString(12, parentTableName);
         if (column.getArraySize() == null) {
@@ -1100,7 +1100,7 @@ public class MetaDataClient {
                     }
                 }
                 Short keySeq = SchemaUtil.isPKColumn(column) ? ++nextKeySeq : null;
-                addColumnMutation(schemaName, tableName, column, colUpsert, parentTableName, pkName, keySeq);
+                addColumnMutation(schemaName, tableName, column, colUpsert, parentTableName, pkName, keySeq, saltBucketNum != null);
             }
             
             tableMetaData.addAll(connection.getMutationState().toMutations().next().getSecond());
@@ -1451,7 +1451,7 @@ public class MetaDataClient {
                 List<PColumn> currentPKs = table.getPKColumns();
                 PColumn lastPK = currentPKs.get(currentPKs.size()-1);
                 // Disallow adding columns if the last column is VARBIANRY.
-                if (lastPK.getDataType() == PDataType.VARBINARY) {
+                if (lastPK.getDataType() == PDataType.VARBINARY || lastPK.getDataType().isArrayType()) {
                     throw new SQLExceptionInfo.Builder(SQLExceptionCode.VARBINARY_LAST_PK)
                         .setColumnName(lastPK.getName().getString()).build().buildException();
                 }
@@ -1534,7 +1534,7 @@ public class MetaDataClient {
                         } else {
                             families.add(new Pair<byte[],Map<String,Object>>(column.getFamilyName().getBytes(),statement.getProps()));
                         }
-                        addColumnMutation(schemaName, tableName, column, colUpsert, null, pkName, keySeq);
+                        addColumnMutation(schemaName, tableName, column, colUpsert, null, pkName, keySeq, table.getBucketNum() != null);
                     }
                     // Add any new PK columns to end of index PK
                     if (isAddingPKColumn) {
@@ -1547,7 +1547,7 @@ public class MetaDataClient {
                                     ColumnName indexColName = ColumnName.caseSensitiveColumnName(IndexUtil.getIndexColumnName(null, colDef.getColumnDefName().getColumnName()));
                                     ColumnDef indexColDef = FACTORY.columnDef(indexColName, indexColDataType.getSqlTypeName(), colDef.isNull(), colDef.getMaxLength(), colDef.getScale(), true, colDef.getSortOrder());
                                     PColumn indexColumn = newColumn(indexPosition++, indexColDef, PrimaryKeyConstraint.EMPTY, null, true);
-                                    addColumnMutation(schemaName, index.getTableName().getString(), indexColumn, colUpsert, index.getParentTableName().getString(), index.getPKName() == null ? null : index.getPKName().getString(), ++nextIndexKeySeq);
+                                    addColumnMutation(schemaName, index.getTableName().getString(), indexColumn, colUpsert, index.getParentTableName().getString(), index.getPKName() == null ? null : index.getPKName().getString(), ++nextIndexKeySeq, index.getBucketNum() != null);
                                 }
                             }
                         }
@@ -1706,6 +1706,7 @@ public class MetaDataClient {
             }
         });
     
+        boolean isSalted = table.getBucketNum() != null;
         int columnsToDropIndex = 0;
         PreparedStatement colUpdate = connection.prepareStatement(UPDATE_COLUMN_POSITION);
         colUpdate.setString(1, tenantId);
@@ -1719,7 +1720,8 @@ public class MetaDataClient {
             }
             colUpdate.setString(4, column.getName().getString());
             colUpdate.setString(5, column.getFamilyName() == null ? null : column.getFamilyName().getString());
-            colUpdate.setInt(6, column.getPosition() - columnsToDropIndex);
+            // Adjust position to not include the salt column
+            colUpdate.setInt(6, column.getPosition() - columnsToDropIndex - (isSalted ? 1 : 0));
             colUpdate.execute();
         }
        return familyName;

http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/b22d3450/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
index 9213fea..2fd1fed 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
@@ -543,12 +543,9 @@ public class SchemaUtil {
         if (table.getBucketNum() != null) {
             offset++;
         }
-        if (table.isMultiTenant()) {
-            offset++;
-        }
-        if (table.getViewIndexId() != null) {
-            offset++;
-        }
+        // TODO: for tenant-specific table on tenant-specific connection,
+        // we should subtract one for tenant column and another one for
+        // index ID
         return (short)(table.getPKColumns().size() - offset);
     }