diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java b/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java index 3eddf2278fe..ce3985dabc5 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java @@ -789,6 +789,9 @@ public SQLException newException(SQLExceptionInfo info) { AUTO_COMMIT_NOT_ENABLED(916, "43M27", "Connection does not have auto-commit enabled"), + ALTER_INDEX_CONSISTENCY_NOT_SUPPORTED(917, "43M28", + "Changing index consistency with ALTER INDEX is not supported yet."), + // SQLCode for testing exceptions FAILED_KNOWINGLY_FOR_TEST(7777, "TEST", "Exception was thrown to test something"); diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java index cdad48aa0b7..8a1eb71de6d 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/MetaDataClient.java @@ -5967,6 +5967,14 @@ Collections. singletonList(new Put(SchemaUtil.getTableKey(tenantIdByte } public MutationState alterIndex(AlterIndexStatement statement) throws SQLException { + // ALTER INDEX ... CONSISTENCY is disabled until the change is coordinated with the other + // clients, the region servers and the index consumers (PHOENIX-8046). The grammar still + // accepts the clause. Removing this check enables the consistency handling below again. + if (statement.getIndexConsistency() != null) { + throw new SQLExceptionInfo.Builder(SQLExceptionCode.ALTER_INDEX_CONSISTENCY_NOT_SUPPORTED) + .setSchemaName(statement.getTable().getName().getSchemaName()) + .setTableName(statement.getTable().getName().getTableName()).build().buildException(); + } connection.rollback(); boolean wasAutoCommit = connection.getAutoCommit(); String dataTableName; diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexMaintainerConsistencyIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexMaintainerConsistencyIT.java index 26d231f0ea3..929b4271ac5 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexMaintainerConsistencyIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexMaintainerConsistencyIT.java @@ -23,15 +23,18 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.SQLException; import java.util.List; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.TableDescriptor; import org.apache.phoenix.coprocessor.generated.ServerCachingProtos; +import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.index.IndexMaintainer; import org.apache.phoenix.jdbc.PhoenixConnection; import org.apache.phoenix.schema.PTable; @@ -41,8 +44,10 @@ import org.apache.phoenix.schema.types.IndexConsistency; import org.apache.phoenix.util.CDCUtil; import org.apache.phoenix.util.PropertiesUtil; +import org.apache.phoenix.util.SchemaUtil; import org.junit.After; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -157,6 +162,7 @@ public void testIndexMaintainerConsistencyFromPTable() throws Exception { } @Test + @Ignore("ALTER INDEX ... CONSISTENCY is not supported until PHOENIX-8046 is fixed") public void testAlterIndexConsistency() throws Exception { String dataTableName = "\"ScheMa1234-_\".\"alterTable1.1_0-001\""; String dataTableNameWithoutQuotes = "ScheMa1234-_.alterTable1.1_0-001"; @@ -207,6 +213,40 @@ public void testAlterIndexConsistency() throws Exception { } } + @Test + public void testAlterIndexConsistencyNotSupported() throws Exception { + String schemaName = generateUniqueName(); + String dataTableName = SchemaUtil.getTableName(schemaName, generateUniqueName()); + String indexName = generateUniqueName(); + + try (Connection conn = + DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES))) { + PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class); + conn.createStatement().execute( + "CREATE TABLE " + dataTableName + " (id INTEGER PRIMARY KEY, name VARCHAR, city VARCHAR)"); + conn.createStatement() + .execute("CREATE INDEX " + indexName + " ON " + dataTableName + " (name) INCLUDE (city)"); + + // The parser still accepts the CONSISTENCY clause, but the client rejects the statement. + for (IndexConsistency consistency : IndexConsistency.values()) { + try { + conn.createStatement().execute( + "ALTER INDEX " + indexName + " ON " + dataTableName + " CONSISTENCY=" + consistency); + fail("ALTER INDEX ... CONSISTENCY=" + consistency + " should not be supported"); + } catch (SQLException e) { + assertEquals(SQLExceptionCode.ALTER_INDEX_CONSISTENCY_NOT_SUPPORTED.getErrorCode(), + e.getErrorCode()); + } + } + + // The rejected statements must not change the index or create a CDC object. + PTable index = pconn.getTableNoCache(SchemaUtil.getTableName(schemaName, indexName)); + PTable dataTable = pconn.getTableNoCache(dataTableName); + assertNull(index.getIndexConsistency()); + assertFalse("Data table should not have a CDC index", CDCUtil.hasCDCIndex(dataTable)); + } + } + private void verifyTableDescriptor(PhoenixConnection pconn, PTable... tables) throws Exception { try (Admin admin = pconn.getQueryServices().getAdmin()) { for (PTable table : tables) {