Skip to content

Commit 9476c68

Browse files
committed
PHOENIX-7512 Report an undefined column error instead of the StringIndexOutOfBoundsException exception
1 parent e876cf7 commit 9476c68

File tree

8 files changed

+66
-13
lines changed

8 files changed

+66
-13
lines changed

phoenix-core-client/src/main/java/org/apache/phoenix/iterate/BaseResultIterators.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,21 @@
3939
import java.io.EOFException;
4040
import java.io.IOException;
4141
import java.sql.SQLException;
42-
import java.util.*;
42+
import java.util.ArrayList;
4343
import java.util.Arrays;
44+
import java.util.Arrays;
45+
import java.util.BitSet;
46+
import java.util.Collections;
47+
import java.util.Iterator;
48+
import java.util.List;
49+
import java.util.Map;
4450
import java.util.Map.Entry;
51+
import java.util.NavigableSet;
52+
import java.util.Queue;
53+
import java.util.Set;
54+
import java.util.TreeMap;
55+
import java.util.TreeSet;
56+
import java.util.UUID;
4557
import java.util.concurrent.CancellationException;
4658
import java.util.concurrent.ConcurrentLinkedQueue;
4759
import java.util.concurrent.ExecutionException;
@@ -96,6 +108,7 @@
96108
import org.apache.phoenix.query.QueryServices;
97109
import org.apache.phoenix.query.QueryServicesOptions;
98110
import org.apache.phoenix.schema.ColumnFamilyNotFoundException;
111+
import org.apache.phoenix.schema.ColumnNotFoundException;
99112
import org.apache.phoenix.schema.PColumn;
100113
import org.apache.phoenix.schema.PColumnFamily;
101114
import org.apache.phoenix.schema.PTable;
@@ -746,7 +759,7 @@ private static class GuidePostEstimate {
746759
private long lastUpdated = Long.MAX_VALUE;
747760
}
748761

749-
private int computeColumnsInCommon() {
762+
private int computeColumnsInCommon() throws ColumnNotFoundException {
750763
PTable dataTable;
751764
if ((dataTable=dataPlan.getTableRef().getTable()).getBucketNum() != null) { // unable to compute prefix range for salted data table
752765
return 0;

phoenix-core-client/src/main/java/org/apache/phoenix/schema/IndexUncoveredDataColumnRef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ColumnRef cloneAtTimestamp(long timestamp) {
6666
}
6767

6868
@Override
69-
public ColumnExpression newColumnExpression(boolean schemaNameCaseSensitive, boolean colNameCaseSensitive) {
69+
public ColumnExpression newColumnExpression(boolean schemaNameCaseSensitive, boolean colNameCaseSensitive) throws ColumnNotFoundException {
7070
String displayName = this.getTableRef().getColumnDisplayName(this, schemaNameCaseSensitive, colNameCaseSensitive);
7171
return new ProjectedColumnExpression(this.getColumn(), columns, position, displayName);
7272
}

phoenix-core-client/src/main/java/org/apache/phoenix/schema/TableRef.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void setHinted(boolean hinted) {
121121
this.hinted = hinted;
122122
}
123123

124-
public String getColumnDisplayName(ColumnRef ref, boolean cfCaseSensitive, boolean cqCaseSensitive) {
124+
public String getColumnDisplayName(ColumnRef ref, boolean cfCaseSensitive, boolean cqCaseSensitive) throws ColumnNotFoundException {
125125
String cf = null;
126126
String cq = null;
127127
PColumn column = ref.getColumn();

phoenix-core-client/src/main/java/org/apache/phoenix/util/IndexUtil.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,12 @@ public static String getDataColumnName(String name) {
191191
return name.substring(name.indexOf(INDEX_COLUMN_NAME_SEP) + 1);
192192
}
193193

194-
public static String getDataColumnFamilyName(String name) {
195-
return name.substring(0,name.indexOf(INDEX_COLUMN_NAME_SEP));
194+
public static String getDataColumnFamilyName(String name) throws ColumnNotFoundException {
195+
int idxOfSeparator = name.indexOf(INDEX_COLUMN_NAME_SEP);
196+
if (idxOfSeparator == -1){
197+
throw new ColumnNotFoundException(name);
198+
}
199+
return name.substring(0, idxOfSeparator);
196200
}
197201

198202
public static String getActualColumnFamilyName(String name) {
@@ -202,7 +206,7 @@ public static String getActualColumnFamilyName(String name) {
202206
return name;
203207
}
204208

205-
public static String getCaseSensitiveDataColumnFullName(String name) {
209+
public static String getCaseSensitiveDataColumnFullName(String name) throws ColumnNotFoundException {
206210
int index = name.indexOf(INDEX_COLUMN_NAME_SEP) ;
207211
return SchemaUtil.getCaseSensitiveColumnDisplayName(getDataColumnFamilyName(name), name.substring(index+1));
208212
}
@@ -242,7 +246,7 @@ public static byte[] getLocalIndexColumnFamily(byte[] dataColumnFamilyBytes) {
242246
return getLocalIndexColumnFamily(dataCF).getBytes(StandardCharsets.UTF_8);
243247
}
244248

245-
public static PColumn getDataColumn(PTable dataTable, String indexColumnName) {
249+
public static PColumn getDataColumn(PTable dataTable, String indexColumnName) throws ColumnNotFoundException {
246250
PColumn column = getDataColumnOrNull(dataTable, indexColumnName);
247251
if (column == null) {
248252
throw new IllegalArgumentException("Could not find column \"" + SchemaUtil.getColumnName(getDataColumnFamilyName(indexColumnName), getDataColumnName(indexColumnName)) + " in " + dataTable);
@@ -267,6 +271,8 @@ public static PColumn getDataColumnOrNull(PTable dataTable, String indexColumnNa
267271
family = dataTable.getColumnFamily(getDataColumnFamilyName(indexColumnName));
268272
} catch (ColumnFamilyNotFoundException e) {
269273
return null;
274+
} catch (ColumnNotFoundException e) {
275+
return null;
270276
}
271277
try {
272278
return family.getPColumnForColumnName(indexColumnName.substring(pos+1));
@@ -587,7 +593,7 @@ public static void addTupleAsOneCell(List<Cell> result,
587593
result.add(keyValue);
588594
}
589595

590-
public static String getIndexColumnExpressionStr(PColumn col) {
596+
public static String getIndexColumnExpressionStr(PColumn col) throws ColumnNotFoundException {
591597
return col.getExpressionStr() == null ? IndexUtil.getCaseSensitiveDataColumnFullName(col.getName().getString())
592598
: col.getExpressionStr();
593599
}

phoenix-core-server/src/main/java/org/apache/phoenix/mapreduce/index/SourceTargetColumnNames.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121

2222
import org.apache.phoenix.mapreduce.util.IndexColumnNames;
23+
import org.apache.phoenix.schema.ColumnNotFoundException;
2324
import org.apache.phoenix.schema.PTable;
2425
import org.apache.phoenix.util.SchemaUtil;
2526

@@ -63,7 +64,7 @@ public static class DataSourceColNames extends IndexColumnNames
6364
* @param pdataTable the data table
6465
* @param pindexTable the index table for the data table
6566
*/
66-
public DataSourceColNames(PTable pdataTable, PTable pindexTable) {
67+
public DataSourceColNames(PTable pdataTable, PTable pindexTable) throws ColumnNotFoundException {
6768
super(pdataTable, pindexTable);
6869
}
6970

@@ -133,7 +134,7 @@ public static class IndexSourceColNames extends IndexColumnNames
133134
* @param pdataTable the data table
134135
* @param pindexTable the index table for the data table
135136
*/
136-
public IndexSourceColNames(PTable pdataTable, PTable pindexTable) {
137+
public IndexSourceColNames(PTable pdataTable, PTable pindexTable) throws ColumnNotFoundException {
137138
super(pdataTable, pindexTable);
138139
}
139140

phoenix-core-server/src/main/java/org/apache/phoenix/mapreduce/util/IndexColumnNames.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Set;
2424

2525
import org.apache.phoenix.query.QueryConstants;
26+
import org.apache.phoenix.schema.ColumnNotFoundException;
2627
import org.apache.phoenix.schema.PColumn;
2728
import org.apache.phoenix.schema.PTable;
2829
import org.apache.phoenix.schema.types.PDataType;
@@ -48,7 +49,7 @@ public class IndexColumnNames {
4849
private PTable pdataTable;
4950
private PTable pindexTable;
5051

51-
public IndexColumnNames(final PTable pdataTable, final PTable pindexTable) {
52+
public IndexColumnNames(final PTable pdataTable, final PTable pindexTable) throws ColumnNotFoundException {
5253
this.pdataTable = pdataTable;
5354
this.pindexTable = pindexTable;
5455
List<PColumn> pindexCols = pindexTable.getColumns();

phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@
6262
import org.apache.phoenix.compile.QueryPlan;
6363
import org.apache.phoenix.coprocessorclient.BaseScannerRegionObserverConstants;
6464
import org.apache.phoenix.end2end.ExplainPlanWithStatsEnabledIT.Estimate;
65+
import org.apache.phoenix.exception.SQLExceptionCode;
6566
import org.apache.phoenix.hbase.index.IndexRegionSplitPolicy;
6667
import org.apache.phoenix.jdbc.PhoenixConnection;
6768
import org.apache.phoenix.jdbc.PhoenixPreparedStatement;
6869
import org.apache.phoenix.jdbc.PhoenixResultSet;
6970
import org.apache.phoenix.jdbc.PhoenixStatement;
7071
import org.apache.phoenix.query.QueryConstants;
72+
import org.apache.phoenix.schema.ColumnNotFoundException;
7173
import org.apache.phoenix.schema.PNameFactory;
7274
import org.apache.phoenix.schema.PTable;
7375
import org.apache.phoenix.schema.PTable.IndexType;
@@ -234,6 +236,35 @@ public void testSelectFromIndexWithAdditionalWhereClause() throws Exception {
234236
testExtraWhere(conn, tableName);
235237
}
236238

239+
@Test
240+
public void testSelectFromIndexWithWhereClauseButTheValueIsSurroundedByDoubleQuotes() throws Exception {
241+
String tableName = schemaName + "." + generateUniqueName();
242+
String indexName = "IDX_" + generateUniqueName();
243+
String indexTableName = schemaName + "." + indexName;
244+
try (Connection conn1 = getConnection()) {
245+
if (isNamespaceMapped) {
246+
conn1.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + schemaName);
247+
}
248+
String ddl = "CREATE TABLE " + tableName + " (t_id VARCHAR NOT NULL,\n" +
249+
"k1 INTEGER NOT NULL,\n" +
250+
"k2 INTEGER NOT NULL,\n" +
251+
"k3 INTEGER,\n" +
252+
"v1 VARCHAR,\n" +
253+
"CONSTRAINT pk PRIMARY KEY (t_id, k1, k2))\n";
254+
conn1.createStatement().execute(ddl);
255+
conn1.commit();
256+
conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " + tableName + "(V1)");
257+
conn1.commit();
258+
ResultSet rs = conn1.createStatement()
259+
.executeQuery("SELECT * FROM " + indexTableName + " WHERE \":T_ID\" = \"f\"");
260+
assertTrue(rs.next());
261+
fail();
262+
} catch (ColumnNotFoundException e) { // Expected
263+
assertEquals(SQLExceptionCode.COLUMN_NOT_FOUND.getErrorCode(), e.getErrorCode());
264+
assertEquals(new ColumnNotFoundException("f").getMessage(), e.getMessage());
265+
}
266+
}
267+
237268
private void testExtraWhere(Connection conn, String tableName) throws SQLException {
238269
ResultSet rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM "+tableName+" WHERE v1 < 3 AND v2 < 4");
239270
rs.next();

phoenix-core/src/test/java/org/apache/phoenix/mapreduce/util/IndexColumnNamesTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.phoenix.jdbc.PhoenixConnection;
2525
import org.apache.phoenix.mapreduce.index.BaseIndexTest;
2626
import org.apache.phoenix.parse.HintNode.Hint;
27+
import org.apache.phoenix.schema.ColumnNotFoundException;
2728
import org.apache.phoenix.schema.PTableKey;
2829
import org.apache.phoenix.util.QueryUtil;
2930
import org.junit.Test;
@@ -42,7 +43,7 @@ public class IndexColumnNamesTest extends BaseIndexTest {
4243
"CREATE INDEX PRECISION_NAME_IDX_TEST ON PRECISION_NAME_TEST(VARCHAR_TEST) INCLUDE (CHAR_TEST,DECIMAL_TEST,BINARY_TEST,VARCHAR_UNSPEC,DEC_UNSPEC)";
4344

4445
@Test
45-
public void testGetColumnNames() {
46+
public void testGetColumnNames() throws ColumnNotFoundException {
4647
IndexColumnNames indexColumnNames = new IndexColumnNames(pDataTable, pIndexTable);
4748
assertEquals("[ID, PK_PART2, 0.NAME, 0.ZIP]", indexColumnNames.getDataColNames().toString());
4849
assertEquals("[:ID, :PK_PART2, 0:NAME, 0:ZIP]", indexColumnNames.getIndexColNames().toString()); //index column names, leading with the data table pk

0 commit comments

Comments
 (0)