Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHOENIX-4345 Error message for incorrect index is not accurate #450

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will pass if the exception isn't thrown.


@Test
public void testIndexNotFoundForDropWongIndexName() throws Exception{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here this test will pass if the exception isn't thrown.

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,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down