phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamestay...@apache.org
Subject git commit: PHOENIX-885 Metadata ORDINAL_POSITION is off by one if table is salted
Date Fri, 21 Mar 2014 09:29:48 GMT
Repository: incubator-phoenix
Updated Branches:
  refs/heads/3.0 cf7e3015c -> 07b54113f


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


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

Branch: refs/heads/3.0
Commit: 07b54113f9667c99c24e8f2b871b430fde4f36fc
Parents: cf7e301
Author: James Taylor <jamestaylor@apache.org>
Authored: Fri Mar 21 02:29:39 2014 -0700
Committer: James Taylor <jamestaylor@apache.org>
Committed: Fri Mar 21 02:29:39 2014 -0700

----------------------------------------------------------------------
 .../end2end/BaseClientManagedTimeIT.java        |  1 -
 .../phoenix/end2end/BaseConnectedQueryIT.java   |  6 +--
 .../end2end/QueryDatabaseMetaDataIT.java        |  8 ++++
 .../coprocessor/MetaDataEndpointImpl.java       | 49 +++++++++-----------
 .../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, 54 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-phoenix/blob/07b54113/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/07b54113/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/07b54113/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 1c015e1..cb02085 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/07b54113/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 543d5b0..f83f02d 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
@@ -277,7 +277,7 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
         indexes.add(indexTable);
     }
 
-    private void addColumnToTable(List<KeyValue> results, PName colName, PName famName,
KeyValue[] colKeyValues, List<PColumn> columns) {
+    private void addColumnToTable(List<KeyValue> results, PName colName, PName famName,
KeyValue[] colKeyValues, List<PColumn> columns, boolean isSalted) {
         int i = 0;
         int j = 0;
         while (i < results.size() && j < COLUMN_KV_COLUMNS.size()) {
@@ -304,7 +304,7 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
         KeyValue decimalDigitKv = colKeyValues[DECIMAL_DIGITS_INDEX];
         Integer scale = decimalDigitKv == null ? null : PDataType.INTEGER.getCodec().decodeInt(decimalDigitKv.getBuffer(),
decimalDigitKv.getValueOffset(), SortOrder.getDefault());
         KeyValue ordinalPositionKv = colKeyValues[ORDINAL_POSITION_INDEX];
-        int position = PDataType.INTEGER.getCodec().decodeInt(ordinalPositionKv.getBuffer(),
ordinalPositionKv.getValueOffset(), SortOrder.getDefault());
+        int position = PDataType.INTEGER.getCodec().decodeInt(ordinalPositionKv.getBuffer(),
ordinalPositionKv.getValueOffset(), SortOrder.getDefault()) + (isSalted ? 1 : 0);
         KeyValue nullableKv = colKeyValues[NULLABLE_INDEX];
         boolean isNullable = PDataType.INTEGER.getCodec().decodeInt(nullableKv.getBuffer(),
nullableKv.getValueOffset(), SortOrder.getDefault()) != ResultSetMetaData.columnNoNulls;
         KeyValue dataTypeKv = colKeyValues[DATA_TYPE_INDEX];
@@ -436,7 +436,7 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
                     logger.warn("Unknown link type: " + colKv.getBuffer()[colKv.getValueOffset()]
+ " for " + SchemaUtil.getTableName(schemaName.getString(), tableName.getString()));
                 }
             } else {
-                addColumnToTable(results, colName, famName, colKeyValues, columns);
+                addColumnToTable(results, colName, famName, colKeyValues, columns, saltBucketNum
!= null);
             }
         }
         
@@ -600,11 +600,12 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
     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);
@@ -660,7 +661,7 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
                 }
                 List<ImmutableBytesPtr> invalidateList = new ArrayList<ImmutableBytesPtr>();
                 result = doDropTable(key, tenantIdBytes, schemaName, tableName, PTableType.fromSerializedValue(tableType),
tableMetadata, invalidateList, lids, tableNamesToDelete);
-                if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS || result.getTable()
== null) {
+                if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
                     return result;
                 }
                 Cache<ImmutableBytesPtr,PTable> metaDataCache = GlobalCache.getInstance(this.getEnvironment()).getMetaDataCache();
@@ -699,42 +700,36 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
         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
+                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<KeyValue> results = Lists.newArrayList();
         scanner.next(results);
-        if (results.isEmpty()) {
-            return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(),
null);
-        }
-        KeyValue 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.getBuffer()[typeKeyValue.getValueOffset()]))
 {
-            // We said to drop a table, but found a view or visa versa
+        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)) {
+        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);
@@ -770,7 +765,7 @@ public class MetaDataEndpointImpl extends BaseEndpointCoprocessor implements
Met
             rowsToDelete.add(delete);
             acquireLock(region, indexKey, lids);
             MetaDataMutationResult result = doDropTable(indexKey, tenantId, schemaName, indexName,
PTableType.INDEX, rowsToDelete, invalidateList, lids, 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/07b54113/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/07b54113/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 281a054..0d5b575 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
@@ -1891,6 +1891,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;
@@ -1907,6 +1908,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));
@@ -1931,6 +1933,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);
@@ -1992,7 +2000,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/07b54113/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 1a65fa9..b46001a 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());
@@ -1453,7 +1453,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();
                 }
@@ -1536,7 +1536,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) {
@@ -1549,7 +1549,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);
                                 }
                             }
                         }
@@ -1708,6 +1708,7 @@ public class MetaDataClient {
             }
         });
     
+        boolean isSalted = table.getBucketNum() != null;
         int columnsToDropIndex = 0;
         PreparedStatement colUpdate = connection.prepareStatement(UPDATE_COLUMN_POSITION);
         colUpdate.setString(1, tenantId);
@@ -1721,7 +1722,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/07b54113/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 08a684f..25576be 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
@@ -545,12 +545,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);
     }
 


Mime
View raw message