use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.
the class QueryDatabaseMetaDataIT method testTableMetadataScan.
@Test
public void testTableMetadataScan() throws SQLException {
String tableAName = generateUniqueName() + "TABLE";
String tableASchema = "";
ensureTableCreated(getUrl(), tableAName, ATABLE_NAME, null);
String tableS = generateUniqueName() + "TABLE";
ensureTableCreated(getUrl(), tableS, STABLE_NAME, null);
String tableC = generateUniqueName();
String tableCSchema = generateUniqueName();
ensureTableCreated(getUrl(), tableCSchema + "." + tableC, CUSTOM_ENTITY_DATA_FULL_NAME, null);
try (Connection conn = DriverManager.getConnection(getUrl())) {
DatabaseMetaData dbmd = conn.getMetaData();
ResultSet rs = dbmd.getTables(null, tableASchema, tableAName, null);
assertTrue(rs.next());
assertEquals(rs.getString("TABLE_NAME"), tableAName);
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertEquals(rs.getString(3), tableAName);
assertEquals(PTableType.TABLE.toString(), rs.getString(4));
assertFalse(rs.next());
rs = dbmd.getTables(null, null, null, null);
assertTrue(rs.next());
assertEquals(SYSTEM_CATALOG_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(SYSTEM_CATALOG_TABLE, rs.getString("TABLE_NAME"));
assertEquals(PTableType.SYSTEM.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(SYSTEM_CATALOG_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(SYSTEM_FUNCTION_TABLE, rs.getString("TABLE_NAME"));
assertEquals(PTableType.SYSTEM.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(SYSTEM_CATALOG_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(TYPE_SEQUENCE, rs.getString("TABLE_NAME"));
assertEquals(PTableType.SYSTEM.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(SYSTEM_CATALOG_SCHEMA, rs.getString("TABLE_SCHEM"));
assertEquals(PhoenixDatabaseMetaData.SYSTEM_STATS_TABLE, rs.getString("TABLE_NAME"));
assertEquals(PTableType.SYSTEM.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(null, rs.getString("TABLE_SCHEM"));
assertEquals(tableAName, rs.getString("TABLE_NAME"));
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(null, rs.getString("TABLE_SCHEM"));
assertEquals(tableS, rs.getString("TABLE_NAME"));
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(tableCSchema, rs.getString("TABLE_SCHEM"));
assertEquals(tableC, rs.getString("TABLE_NAME"));
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertEquals("false", rs.getString(PhoenixDatabaseMetaData.TRANSACTIONAL));
assertEquals(Boolean.FALSE, rs.getBoolean(PhoenixDatabaseMetaData.IS_NAMESPACE_MAPPED));
rs = dbmd.getTables(null, tableCSchema, tableC, null);
assertTrue(rs.next());
try {
rs.getString("RANDOM_COLUMN_NAME");
fail();
} catch (ColumnNotFoundException e) {
// expected
}
assertEquals(tableCSchema, rs.getString("TABLE_SCHEM"));
assertEquals(tableC, rs.getString("TABLE_NAME"));
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertFalse(rs.next());
rs = dbmd.getTables(null, "", "%TABLE", new String[] { PTableType.TABLE.toString() });
assertTrue(rs.next());
assertEquals(null, rs.getString("TABLE_SCHEM"));
assertEquals(tableAName, rs.getString("TABLE_NAME"));
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertTrue(rs.next());
assertEquals(null, rs.getString("TABLE_SCHEM"));
assertEquals(tableS, rs.getString("TABLE_NAME"));
assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
assertFalse(rs.next());
}
}
use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.
the class DropColumnIT method helpTestDroppingIndexedColDropsViewIndex.
public void helpTestDroppingIndexedColDropsViewIndex(boolean isMultiTenant) throws Exception {
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
props.setProperty(TENANT_ID_ATTRIB, TENANT_ID);
try (Connection conn = getConnection();
Connection viewConn = isMultiTenant ? getConnection(props) : conn) {
String tableWithView = generateUniqueName();
String viewOfTable = generateUniqueName();
String viewIndex1 = generateUniqueName();
String viewIndex2 = generateUniqueName();
conn.setAutoCommit(false);
viewConn.setAutoCommit(false);
String ddlFormat = "CREATE TABLE " + tableWithView + " (%s k VARCHAR NOT NULL, v1 VARCHAR, v2 VARCHAR, v3 VARCHAR, v4 VARCHAR CONSTRAINT PK PRIMARY KEY(%s k))%s";
String ddl = String.format(ddlFormat, isMultiTenant ? "TENANT_ID VARCHAR NOT NULL, " : "", isMultiTenant ? "TENANT_ID, " : "", isMultiTenant ? "MULTI_TENANT=true" : "");
conn.createStatement().execute(ddl);
viewConn.createStatement().execute("CREATE VIEW " + viewOfTable + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableWithView);
// create an index with the column that will be dropped
viewConn.createStatement().execute("CREATE INDEX " + viewIndex1 + " ON " + viewOfTable + "(v2) INCLUDE (v4)");
// create an index without the column that will be dropped
viewConn.createStatement().execute("CREATE INDEX " + viewIndex2 + " ON " + viewOfTable + "(v1) INCLUDE (v4)");
// verify index was created
try {
viewConn.createStatement().execute("SELECT * FROM " + viewIndex1);
} catch (TableNotFoundException e) {
fail("Index on view was not created");
}
// upsert a single row
PreparedStatement stmt = viewConn.prepareStatement("UPSERT INTO " + viewOfTable + " VALUES(?,?,?,?,?,?,?)");
stmt.setString(1, "a");
stmt.setString(2, "b");
stmt.setString(3, "c");
stmt.setString(4, "d");
stmt.setString(5, "e");
stmt.setInt(6, 1);
stmt.setString(7, "g");
stmt.execute();
viewConn.commit();
// verify the index was created
PhoenixConnection pconn = viewConn.unwrap(PhoenixConnection.class);
PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
PTable view = pconn.getTable(new PTableKey(tenantId, viewOfTable));
PTable viewIndex = pconn.getTable(new PTableKey(tenantId, viewIndex1));
byte[] viewIndexPhysicalTable = viewIndex.getPhysicalName().getBytes();
assertNotNull("Can't find view index", viewIndex);
assertEquals("Unexpected number of indexes ", 2, view.getIndexes().size());
assertEquals("Unexpected index ", viewIndex1, view.getIndexes().get(0).getName().getString());
assertEquals("Unexpected index ", viewIndex2, view.getIndexes().get(1).getName().getString());
// drop two columns
conn.createStatement().execute("ALTER TABLE " + tableWithView + " DROP COLUMN v2, v3 ");
// verify columns were dropped
try {
conn.createStatement().execute("SELECT v2 FROM " + tableWithView);
fail("Column should have been dropped");
} catch (ColumnNotFoundException e) {
}
try {
conn.createStatement().execute("SELECT v3 FROM " + tableWithView);
fail("Column should have been dropped");
} catch (ColumnNotFoundException e) {
}
// verify index metadata was dropped
try {
viewConn.createStatement().execute("SELECT * FROM " + viewIndex1);
fail("Index metadata should have been dropped");
} catch (TableNotFoundException e) {
}
pconn = viewConn.unwrap(PhoenixConnection.class);
view = pconn.getTable(new PTableKey(tenantId, viewOfTable));
try {
viewIndex = pconn.getTable(new PTableKey(tenantId, viewIndex1));
fail("View index should have been dropped");
} catch (TableNotFoundException e) {
}
assertEquals("Unexpected number of indexes ", 1, view.getIndexes().size());
assertEquals("Unexpected index ", viewIndex2, view.getIndexes().get(0).getName().getString());
// verify that the physical index view table is *not* dropped
conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(viewIndexPhysicalTable);
// scan the physical table and verify there is a single row for the second local index
Scan scan = new Scan();
HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(viewIndexPhysicalTable);
ResultScanner results = table.getScanner(scan);
Result result = results.next();
assertNotNull(result);
PTable viewIndexPTable = pconn.getTable(new PTableKey(pconn.getTenantId(), viewIndex2));
PColumn column = viewIndexPTable.getColumnForColumnName(IndexUtil.getIndexColumnName(QueryConstants.DEFAULT_COLUMN_FAMILY, "V4"));
byte[] cq = column.getColumnQualifierBytes();
// there should be a single row belonging to VIEWINDEX2
assertNotNull(viewIndex2 + " row is missing", result.getValue(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, cq));
assertNull(results.next());
}
}
use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.
the class TenantSpecificViewIndexIT method createViewAndIndexesWithTenantId.
private void createViewAndIndexesWithTenantId(String tableName, String baseViewName, boolean localIndex, String tenantId, boolean isNamespaceMapped) throws Exception {
Properties props = new Properties();
String viewName = baseViewName + "_" + tenantId;
String indexName = "idx_" + viewName;
if (tenantId != null) {
props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
}
Connection conn = DriverManager.getConnection(getUrl(), props);
conn.createStatement().execute("CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName);
ResultSet rs = conn.createStatement().executeQuery("select * from " + viewName);
int i = 1;
if ("a".equals(tenantId)) {
assertTrue(rs.next());
assertEquals("b", rs.getString(i++));
assertEquals("c", rs.getString(i++));
assertEquals("d", rs.getString(i++));
}
assertFalse(rs.next());
conn.createStatement().execute("UPSERT INTO " + viewName + " VALUES ('e','f','g')");
conn.commit();
if (localIndex) {
conn.createStatement().execute("create local index " + indexName + " on " + viewName + " (COL1)");
} else {
conn.createStatement().execute("create index " + indexName + " on " + viewName + " (COL1)");
}
rs = conn.createStatement().executeQuery("select * from " + viewName);
i = 1;
if ("a".equals(tenantId)) {
assertTrue(rs.next());
assertEquals("b", rs.getString(i++));
assertEquals("c", rs.getString(i++));
assertEquals("d", rs.getString(i++));
}
assertTrue(rs.next());
assertEquals("e", rs.getString(1));
assertEquals("f", rs.getString(2));
assertEquals("g", rs.getString(3));
assertFalse(rs.next());
rs = conn.createStatement().executeQuery("explain select * from " + viewName);
assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + SchemaUtil.getPhysicalTableName(Bytes.toBytes(tableName), isNamespaceMapped) + " ['" + tenantId + "']", QueryUtil.getExplainPlan(rs));
rs = conn.createStatement().executeQuery("select pk2,col1 from " + viewName + " where col1='f'");
assertTrue(rs.next());
assertEquals("e", rs.getString(1));
assertEquals("f", rs.getString(2));
assertFalse(rs.next());
rs = conn.createStatement().executeQuery("explain select pk2,col1 from " + viewName + " where col1='f'");
if (localIndex) {
assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + SchemaUtil.getPhysicalTableName(Bytes.toBytes(tableName), isNamespaceMapped) + " [1,'" + tenantId + "','f']\n" + " SERVER FILTER BY FIRST KEY ONLY\n" + "CLIENT MERGE SORT", QueryUtil.getExplainPlan(rs));
} else {
assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(SchemaUtil.getPhysicalTableName(Bytes.toBytes(tableName), isNamespaceMapped).toBytes())) + " [-32768,'" + tenantId + "','f']\n" + " SERVER FILTER BY FIRST KEY ONLY", QueryUtil.getExplainPlan(rs));
}
try {
// Cannot reference tenant_id column in tenant specific connection
conn.createStatement().executeQuery("select * from " + tableName + " where (pk1,pk2) IN (('a','b'),('b','b'))");
if (tenantId != null) {
fail();
}
} catch (ColumnNotFoundException e) {
if (tenantId == null) {
fail();
}
}
// This is ok, though
rs = conn.createStatement().executeQuery("select * from " + tableName + " where pk2 IN ('b','e')");
if ("a".equals(tenantId)) {
assertTrue(rs.next());
assertEquals("b", rs.getString(1));
}
assertTrue(rs.next());
assertEquals("e", rs.getString(1));
assertFalse(rs.next());
rs = conn.createStatement().executeQuery("select * from " + viewName + " where pk2 IN ('b','e')");
if ("a".equals(tenantId)) {
assertTrue(rs.next());
assertEquals("b", rs.getString(1));
}
assertTrue(rs.next());
assertEquals("e", rs.getString(1));
assertFalse(rs.next());
conn.close();
}
use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.
the class FunkyNamesIT method testCaseSensitive.
@Test
public void testCaseSensitive() throws Exception {
String query = "SELECT \"Value\",\"VALUE\",\"value\" FROM %s";
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
Connection conn = DriverManager.getConnection(getUrl(), props);
try {
String tableName = initTableValues(null);
PreparedStatement statement = conn.prepareStatement(String.format(query, tableName));
ResultSet rs = statement.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(2, rs.getInt(2));
assertEquals(3, rs.getInt(3));
assertEquals(1, rs.getInt("Value"));
assertEquals(2, rs.getInt("VALUE"));
assertEquals(3, rs.getInt("value"));
try {
rs.getInt("vAlue");
fail();
} catch (ColumnNotFoundException e) {
}
assertFalse(rs.next());
} finally {
conn.close();
}
}
use of org.apache.phoenix.schema.ColumnNotFoundException in project phoenix by apache.
the class MetaDataEndpointImpl method dropColumnsFromChildViews.
private MetaDataMutationResult dropColumnsFromChildViews(Region region, PTable basePhysicalTable, List<RowLock> locks, List<Mutation> tableMetadata, List<Mutation> mutationsForAddingColumnsToViews, byte[] schemaName, byte[] tableName, List<ImmutableBytesPtr> invalidateList, long clientTimeStamp, TableViewFinder childViewsResult, List<byte[]> tableNamesToDelete, List<SharedTableState> sharedTablesToDelete, int clientVersion) throws IOException, SQLException {
List<Delete> columnDeletesForBaseTable = new ArrayList<>(tableMetadata.size());
// are being added.
for (Mutation m : tableMetadata) {
if (m instanceof Delete) {
byte[][] rkmd = new byte[5][];
int pkCount = getVarChars(m.getRow(), rkmd);
if (pkCount > COLUMN_NAME_INDEX && Bytes.compareTo(schemaName, rkmd[SCHEMA_NAME_INDEX]) == 0 && Bytes.compareTo(tableName, rkmd[TABLE_NAME_INDEX]) == 0) {
columnDeletesForBaseTable.add((Delete) m);
}
}
}
for (ViewInfo viewInfo : childViewsResult.getViewInfoList()) {
short numColsDeleted = 0;
byte[] viewTenantId = viewInfo.getTenantId();
byte[] viewSchemaName = viewInfo.getSchemaName();
byte[] viewName = viewInfo.getViewName();
byte[] viewKey = SchemaUtil.getTableKey(viewTenantId, viewSchemaName, viewName);
// lock the rows corresponding to views so that no other thread can modify the view
// meta-data
RowLock viewRowLock = acquireLock(region, viewKey, locks);
PTable view = doGetTable(viewKey, clientTimeStamp, viewRowLock, clientVersion);
ColumnOrdinalPositionUpdateList ordinalPositionList = new ColumnOrdinalPositionUpdateList();
int numCols = view.getColumns().size();
int minDroppedColOrdinalPos = Integer.MAX_VALUE;
for (Delete columnDeleteForBaseTable : columnDeletesForBaseTable) {
PColumn existingViewColumn = null;
byte[][] rkmd = new byte[5][];
getVarChars(columnDeleteForBaseTable.getRow(), rkmd);
String columnName = Bytes.toString(rkmd[COLUMN_NAME_INDEX]);
String columnFamily = rkmd[FAMILY_NAME_INDEX] == null ? null : Bytes.toString(rkmd[FAMILY_NAME_INDEX]);
byte[] columnKey = getColumnKey(viewKey, columnName, columnFamily);
try {
existingViewColumn = columnFamily == null ? view.getColumnForColumnName(columnName) : view.getColumnFamily(columnFamily).getPColumnForColumnName(columnName);
} catch (ColumnFamilyNotFoundException e) {
// ignore since it means that the column family is not present for the column to
// be added.
} catch (ColumnNotFoundException e) {
// ignore since it means the column is not present in the view
}
// it
if (existingViewColumn != null && view.getViewStatement() != null) {
ParseNode viewWhere = new SQLParser(view.getViewStatement()).parseQuery().getWhere();
PhoenixConnection conn = null;
try {
conn = QueryUtil.getConnectionOnServer(env.getConfiguration()).unwrap(PhoenixConnection.class);
} catch (ClassNotFoundException e) {
}
PhoenixStatement statement = new PhoenixStatement(conn);
TableRef baseTableRef = new TableRef(basePhysicalTable);
ColumnResolver columnResolver = FromCompiler.getResolver(baseTableRef);
StatementContext context = new StatementContext(statement, columnResolver);
Expression whereExpression = WhereCompiler.compile(context, viewWhere);
Expression colExpression = new ColumnRef(baseTableRef, existingViewColumn.getPosition()).newColumnExpression();
ColumnFinder columnFinder = new ColumnFinder(colExpression);
whereExpression.accept(columnFinder);
if (columnFinder.getColumnFound()) {
return new MetaDataMutationResult(MutationCode.UNALLOWED_TABLE_MUTATION, EnvironmentEdgeManager.currentTimeMillis(), basePhysicalTable);
}
}
minDroppedColOrdinalPos = Math.min(getOrdinalPosition(view, existingViewColumn), minDroppedColOrdinalPos);
if (existingViewColumn != null) {
--numColsDeleted;
if (ordinalPositionList.size() == 0) {
ordinalPositionList.setOffset(view.getBucketNum() == null ? 1 : 0);
for (PColumn col : view.getColumns()) {
ordinalPositionList.addColumn(getColumnKey(viewKey, col));
}
}
ordinalPositionList.dropColumn(columnKey);
Delete viewColumnDelete = new Delete(columnKey, clientTimeStamp);
mutationsForAddingColumnsToViews.add(viewColumnDelete);
// drop any view indexes that need this column
dropIndexes(view, region, invalidateList, locks, clientTimeStamp, schemaName, view.getName().getBytes(), mutationsForAddingColumnsToViews, existingViewColumn, tableNamesToDelete, sharedTablesToDelete, clientVersion);
}
}
updateViewHeaderRow(basePhysicalTable, tableMetadata, mutationsForAddingColumnsToViews, invalidateList, clientTimeStamp, numColsDeleted, numColsDeleted, viewKey, view, ordinalPositionList, numCols, true);
}
return null;
}
Aggregations