From a479aac70c13ac08b68e1918bd5ab087c16e2e08 Mon Sep 17 00:00:00 2001 From: Xinyi Yan Date: Wed, 27 Feb 2019 23:33:23 -0800 Subject: [PATCH] PHOENIX-4345 Error message for incorrect index is not accurate --- .../phoenix/end2end/index/IndexUsageIT.java | 40 +++++++++++++++++ .../apache/phoenix/compile/FromCompiler.java | 12 +++++- .../phoenix/exception/SQLExceptionCode.java | 7 +++ .../schema/IndexNotFoundException.java | 43 +++++++++++++++++++ .../apache/phoenix/schema/MetaDataClient.java | 7 ++- .../schema/TableNotFoundException.java | 4 ++ 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 phoenix-core/src/main/java/org/apache/phoenix/schema/IndexNotFoundException.java diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexUsageIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexUsageIT.java index f114010c660..6433f5a1ae5 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexUsageIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexUsageIT.java @@ -35,6 +35,7 @@ import java.util.Properties; import org.apache.phoenix.end2end.ParallelStatsDisabledIT; +import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.execute.CommitException; import org.apache.phoenix.query.QueryConstants; import org.apache.phoenix.util.DateUtil; @@ -772,4 +773,43 @@ public void helpTestIndexExpressionWithJoin(boolean mutable, } } + @Test + public void testIndexNotFoundForWrongIndexNameRebuild() throws Exception{ + Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); + Connection conn = DriverManager.getConnection(getUrl(), props); + String dataTableName = generateUniqueName(); + String wrongIndexName = generateUniqueName(); + + try { + conn.createStatement().execute("CREATE TABLE " + dataTableName + + " (k VARCHAR NOT NULL PRIMARY KEY, v VARCHAR)"); + + conn.createStatement().execute( + "ALTER INDEX " + wrongIndexName + " ON " + dataTableName + " rebuild"); + + }catch (SQLException e) { + assertEquals(e.getErrorCode(), SQLExceptionCode.INDEX_UNDEFINED.getErrorCode()); + } finally { + conn.close(); + } + } + + @Test + public void testIndexNotFoundForDropWongIndexName() throws Exception{ + Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); + Connection conn = DriverManager.getConnection(getUrl(), props); + String dataTableName = generateUniqueName(); + String wrongIndexName = generateUniqueName(); + + try { + conn.createStatement().execute("CREATE TABLE " + dataTableName + + " (k VARCHAR NOT NULL PRIMARY KEY, v VARCHAR)"); + conn.createStatement().execute("DROP INDEX " + wrongIndexName + " ON " + + dataTableName); + }catch (SQLException e) { + assertEquals(e.getErrorCode(), SQLExceptionCode.INDEX_UNDEFINED.getErrorCode()); + } finally { + conn.close(); + } + } } diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java index d0a49cc1738..dbfc60766c1 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java @@ -64,6 +64,7 @@ import org.apache.phoenix.schema.ColumnNotFoundException; import org.apache.phoenix.schema.ColumnRef; import org.apache.phoenix.schema.FunctionNotFoundException; +import org.apache.phoenix.schema.IndexNotFoundException; import org.apache.phoenix.schema.MetaDataClient; import org.apache.phoenix.schema.MetaDataEntityNotFoundException; import org.apache.phoenix.schema.PColumn; @@ -265,6 +266,15 @@ public static ColumnResolver getResolver(SingleTableStatement statement, Phoenix return visitor; } + public static ColumnResolver getIndexResolver(SingleTableStatement statement, + PhoenixConnection connection) throws SQLException { + try { + return getResolver(statement, connection); + } catch (TableNotFoundException e) { + throw new IndexNotFoundException(e.getSchemaName(), e.getTableName(), e.getTimeStamp()); + } + } + public static ColumnResolver getResolver(SingleTableStatement statement, PhoenixConnection connection, Map udfParseNodes) throws SQLException { SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, statement.getTable(), true, 0, udfParseNodes); @@ -287,7 +297,7 @@ public static ColumnResolver getResolverForCompiledDerivedTable(PhoenixConnectio .build(); return new SingleTableColumnResolver(connection, new TableRef(tableRef.getTableAlias(), t, tableRef.getLowerBoundTimeStamp(), tableRef.hasDynamicCols())); } - + public static ColumnResolver getResolver(TableRef tableRef) throws SQLException { SingleTableColumnResolver visitor = new SingleTableColumnResolver(tableRef); 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 e53152999e5..00479ee4e24 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 @@ -34,6 +34,7 @@ import org.apache.phoenix.schema.ConcurrentTableMutationException; import org.apache.phoenix.schema.FunctionAlreadyExistsException; import org.apache.phoenix.schema.FunctionNotFoundException; +import org.apache.phoenix.schema.IndexNotFoundException; import org.apache.phoenix.schema.ReadOnlyTableException; import org.apache.phoenix.schema.SchemaAlreadyExistsException; import org.apache.phoenix.schema.SchemaNotFoundException; @@ -225,6 +226,12 @@ public SQLException newException(SQLExceptionInfo info) { return new TableNotFoundException(info.getSchemaName(), info.getTableName()); } }), + INDEX_UNDEFINED(1042, "42M06", "Index undefined.", new Factory() { + @Override + public SQLException newException(SQLExceptionInfo info) { + return new IndexNotFoundException(info.getSchemaName(), info.getTableName()); + } + }), TABLE_ALREADY_EXIST(1013, "42M04", "Table already exists.", new Factory() { @Override public SQLException newException(SQLExceptionInfo info) { diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/IndexNotFoundException.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/IndexNotFoundException.java new file mode 100644 index 00000000000..12e098cb4ee --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/IndexNotFoundException.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.phoenix.schema; + +import org.apache.hadoop.hbase.HConstants; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.util.SchemaUtil; + +public class IndexNotFoundException extends TableNotFoundException { + private static SQLExceptionCode code = SQLExceptionCode.INDEX_UNDEFINED; + + public IndexNotFoundException(IndexNotFoundException e, long timestamp) { + this(e.getSchemaName(),e.getTableName(), timestamp); + } + + public IndexNotFoundException(String tableName) { + this(SchemaUtil.getSchemaNameFromFullName(tableName), + SchemaUtil.getTableNameFromFullName(tableName)); + } + + public IndexNotFoundException(String schemaName, String tableName) { + this(schemaName, tableName, HConstants.LATEST_TIMESTAMP); + } + + public IndexNotFoundException(String schemaName, String tableName, long timestamp) { + super(schemaName, tableName, timestamp, code); + } +} \ No newline at end of file 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 734a5d063d5..0527664d287 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 @@ -3159,6 +3159,9 @@ MutationState dropTable(String schemaName, String tableName, String parentTableN } } catch (TableNotFoundException e) { if (!ifExists) { + if (tableType == PTableType.INDEX) + throw new IndexNotFoundException(e.getSchemaName(), + e.getTableName(), e.getTimeStamp()); throw e; } } @@ -4287,7 +4290,9 @@ public MutationState alterIndex(AlterIndexStatement statement) throws SQLExcepti String indexName = statement.getTable().getName().getTableName(); boolean isAsync = statement.isAsync(); String tenantId = connection.getTenantId() == null ? null : connection.getTenantId().getString(); - PTable table = FromCompiler.getResolver(statement, connection).getTables().get(0).getTable(); + PTable table = FromCompiler.getIndexResolver(statement, connection) + .getTables().get(0).getTable(); + String schemaName = statement.getTable().getName().getSchemaName(); String tableName = table.getTableName().getString(); diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/TableNotFoundException.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/TableNotFoundException.java index 298a2885886..ebc6b4dc3c3 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/TableNotFoundException.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/TableNotFoundException.java @@ -49,6 +49,10 @@ public TableNotFoundException(String schemaName, String tableName) { } public TableNotFoundException(String schemaName, String tableName, long timestamp) { + this(schemaName, tableName, timestamp, code); + } + + public TableNotFoundException(String schemaName, String tableName, long timestamp, SQLExceptionCode code) { super(new SQLExceptionInfo.Builder(code).setSchemaName(schemaName).setTableName(tableName).build().toString(), code.getSQLState(), code.getErrorCode(), schemaName, tableName, null); this.timestamp = timestamp;