Skip to content

Commit

Permalink
PHOENIX-4345 Error message for incorrect index is not accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxinyi committed Mar 1, 2019
1 parent 08c4496 commit 3cb4252
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, UDFParseNode> udfParseNodes)
throws SQLException {
SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, statement.getTable(), true, 0, udfParseNodes);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.exception.SQLExceptionInfo;
import org.apache.phoenix.util.SchemaUtil;

public class IndexNotFoundException extends MetaDataEntityNotFoundException {
private static final long serialVersionUID = 1L;
private static SQLExceptionCode code = SQLExceptionCode.INDEX_UNDEFINED;
private final long timestamp;

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(new SQLExceptionInfo.Builder(code).setSchemaName(schemaName).setTableName(tableName)
.build().toString(), code.getSQLState(), code.getErrorCode(), schemaName,
tableName, null);
this.timestamp = timestamp;
}

public long getTimeStamp() {
return timestamp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,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;
}
}
Expand Down Expand Up @@ -4281,7 +4284,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();

Expand Down

0 comments on commit 3cb4252

Please sign in to comment.