PHOENIX-4721 Adding PK column to a table with multiple secondary indexes fails
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/8c9f2f68 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/8c9f2f68 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/8c9f2f68 Branch: refs/heads/4.x-HBase-0.98 Commit: 8c9f2f68d91bd11e057fcee6abae9ea60acae124 Parents: dcbaeac Author: James Taylor <jtay...@salesforce.com> Authored: Mon Apr 30 22:57:49 2018 -0700 Committer: James Taylor <jtay...@salesforce.com> Committed: Tue May 1 11:42:08 2018 -0700 ---------------------------------------------------------------------- .../phoenix/end2end/AlterTableWithViewsIT.java | 45 +++++++++++++++++++- .../apache/phoenix/schema/MetaDataClient.java | 3 +- 2 files changed, 46 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/8c9f2f68/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableWithViewsIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableWithViewsIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableWithViewsIT.java index e1b1372..ab3a4ab 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableWithViewsIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableWithViewsIT.java @@ -33,6 +33,7 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Arrays; import java.util.Collection; +import java.util.Properties; import org.apache.commons.lang.ArrayUtils; import org.apache.hadoop.hbase.client.HTableInterface; @@ -47,7 +48,9 @@ import org.apache.phoenix.schema.PNameFactory; import org.apache.phoenix.schema.PTable; import org.apache.phoenix.schema.PTableKey; import org.apache.phoenix.schema.PTableType; +import org.apache.phoenix.util.PropertiesUtil; import org.apache.phoenix.util.StringUtil; +import org.apache.phoenix.util.TestUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -863,5 +866,45 @@ public class AlterTableWithViewsIT extends ParallelStatsDisabledIT { assertTrue(viewTable.isAppendOnlySchema()); } } - + + @Test + public void testAlterTableWithIndexesExtendPk() throws Exception { + Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES); + Connection conn = DriverManager.getConnection(getUrl(), props); + conn.setAutoCommit(false); + String tableName = generateUniqueName(); + String indexName1 = "I_" + generateUniqueName(); + String indexName2 = "I_" + generateUniqueName(); + + try { + String ddl = "CREATE TABLE " + tableName + + " (ORG_ID CHAR(15) NOT NULL," + + " PARTITION_KEY CHAR(3) NOT NULL, " + + " ACTIVITY_DATE DATE NOT NULL, " + + " FK1_ID CHAR(15) NOT NULL, " + + " FK2_ID CHAR(15) NOT NULL, " + + " TYPE VARCHAR NOT NULL, " + + " IS_OPEN BOOLEAN " + + " CONSTRAINT PKVIEW PRIMARY KEY " + + "(" + + "ORG_ID, PARTITION_KEY, ACTIVITY_DATE, FK1_ID, FK2_ID, TYPE" + + "))"; + createTestTable(getUrl(), ddl); + + String idx1ddl = "CREATE INDEX " + indexName1 + " ON " + tableName + " (FK1_ID, ACTIVITY_DATE DESC) INCLUDE (IS_OPEN)"; + PreparedStatement stmt1 = conn.prepareStatement(idx1ddl); + stmt1.execute(); + + String idx2ddl = "CREATE INDEX " + indexName2 + " ON " + tableName + " (FK2_ID, ACTIVITY_DATE DESC) INCLUDE (IS_OPEN)"; + PreparedStatement stmt2 = conn.prepareStatement(idx2ddl); + stmt2.execute(); + + ddl = "ALTER TABLE " + tableName + " ADD SOURCE VARCHAR(25) NULL PRIMARY KEY"; + PreparedStatement stmt3 = conn.prepareStatement(ddl); + stmt3.execute(); + } finally { + conn.close(); + } + } + } http://git-wip-us.apache.org/repos/asf/phoenix/blob/8c9f2f68/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 c8f7d5e..372371b 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 @@ -3376,13 +3376,14 @@ public class MetaDataClient { if (colDef.isPK()) { PDataType indexColDataType = IndexUtil.getIndexColumnDataType(colDef.isNull(), colDef.getDataType()); ColumnName indexColName = ColumnName.caseSensitiveColumnName(IndexUtil.getIndexColumnName(null, colDef.getColumnDefName().getColumnName())); - Expression expression = new RowKeyColumnExpression(columns.get(i), new RowKeyValueAccessor(pkColumns, ++pkSlotPosition)); + Expression expression = new RowKeyColumnExpression(columns.get(i), new RowKeyValueAccessor(pkColumns, pkSlotPosition)); ColumnDef indexColDef = FACTORY.columnDef(indexColName, indexColDataType.getSqlTypeName(), colDef.isNull(), colDef.getMaxLength(), colDef.getScale(), true, colDef.getSortOrder(), expression.toString(), colDef.isRowTimestamp()); PColumn indexColumn = newColumn(indexPosition++, indexColDef, PrimaryKeyConstraint.EMPTY, null, true, null, willBeImmutableRows); addColumnMutation(schemaName, index.getTableName().getString(), indexColumn, colUpsert, index.getParentTableName().getString(), index.getPKName() == null ? null : index.getPKName().getString(), ++nextIndexKeySeq, index.getBucketNum() != null); } } } + ++pkSlotPosition; } columnMetaData.addAll(connection.getMutationState().toMutations(timeStamp).next().getSecond()); connection.rollback();