use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class QueryDatabaseMetaDataIT method testCreateDropTable.
@Test
public void testCreateDropTable() throws Exception {
long ts = nextTimestamp();
String tenantId = getOrganizationId();
initATableValues(ATABLE_NAME, tenantId, getDefaultSplits(tenantId), null, ts, getUrl(), null);
ensureTableCreated(getUrl(), BTABLE_NAME, BTABLE_NAME, ts - 2);
ensureTableCreated(getUrl(), PTSDB_NAME, PTSDB_NAME, ts - 2);
Properties props = new Properties();
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 5));
Connection conn5 = DriverManager.getConnection(getUrl(), props);
String query = "SELECT a_string FROM aTable";
// Data should still be there b/c we only dropped the schema
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 8));
assertTrue(conn5.prepareStatement(query).executeQuery().next());
conn5.createStatement().executeUpdate("DROP TABLE " + ATABLE_NAME);
// Confirm that data is no longer there because we dropped the table
// This needs to be done natively b/c the metadata is gone
HTableInterface htable = conn5.unwrap(PhoenixConnection.class).getQueryServices().getTable(SchemaUtil.getTableNameAsBytes(ATABLE_SCHEMA_NAME, ATABLE_NAME));
Scan scan = new Scan();
scan.setFilter(new FirstKeyOnlyFilter());
scan.setTimeRange(0, ts + 9);
assertNull(htable.getScanner(scan).next());
conn5.close();
// Still should work b/c we're at an earlier timestamp than when table was deleted
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2));
Connection conn2 = DriverManager.getConnection(getUrl(), props);
assertTrue(conn2.prepareStatement(query).executeQuery().next());
conn2.close();
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
Connection conn10 = DriverManager.getConnection(getUrl(), props);
try {
conn10.prepareStatement(query).executeQuery().next();
fail();
} catch (TableNotFoundException e) {
}
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class MetaDataEndpointImpl method addIndexToTable.
private void addIndexToTable(PName tenantId, PName schemaName, PName indexName, PName tableName, long clientTimeStamp, List<PTable> indexes) throws IOException, SQLException {
byte[] key = SchemaUtil.getTableKey(tenantId == null ? ByteUtil.EMPTY_BYTE_ARRAY : tenantId.getBytes(), schemaName.getBytes(), indexName.getBytes());
PTable indexTable = doGetTable(key, clientTimeStamp);
if (indexTable == null) {
ServerUtil.throwIOException("Index not found", new TableNotFoundException(schemaName.getString(), indexName.getString()));
return;
}
indexes.add(indexTable);
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class ProjectionCompiler method projectAllIndexColumns.
private static void projectAllIndexColumns(StatementContext context, TableRef tableRef, boolean resolveColumn, List<Expression> projectedExpressions, List<ExpressionProjector> projectedColumns, List<? extends PDatum> targetColumns) throws SQLException {
ColumnResolver resolver = context.getResolver();
PTable index = tableRef.getTable();
int projectedOffset = projectedExpressions.size();
PhoenixConnection conn = context.getConnection();
PName tenantId = conn.getTenantId();
String tableName = index.getParentName().getString();
PTable dataTable = null;
try {
dataTable = conn.getTable(new PTableKey(tenantId, tableName));
} catch (TableNotFoundException e) {
if (tenantId != null) {
// Check with null tenantId
dataTable = conn.getTable(new PTableKey(null, tableName));
} else {
throw e;
}
}
int tableOffset = dataTable.getBucketNum() == null ? 0 : 1;
int minTablePKOffset = getMinPKOffset(dataTable, tenantId);
int minIndexPKOffset = getMinPKOffset(index, tenantId);
if (index.getIndexType() != IndexType.LOCAL) {
if (index.getColumns().size() - minIndexPKOffset != dataTable.getColumns().size() - minTablePKOffset) {
// We'll end up not using this by the optimizer, so just throw
String schemaNameStr = dataTable.getSchemaName() == null ? null : dataTable.getSchemaName().getString();
String tableNameStr = dataTable.getTableName() == null ? null : dataTable.getTableName().getString();
throw new ColumnNotFoundException(schemaNameStr, tableNameStr, null, WildcardParseNode.INSTANCE.toString());
}
}
for (int i = tableOffset, j = tableOffset; i < dataTable.getColumns().size(); i++) {
PColumn column = dataTable.getColumns().get(i);
// Skip tenant ID column (which may not be the first column, but is the first PK column)
if (SchemaUtil.isPKColumn(column) && j++ < minTablePKOffset) {
tableOffset++;
continue;
}
PColumn tableColumn = dataTable.getColumns().get(i);
String indexColName = IndexUtil.getIndexColumnName(tableColumn);
PColumn indexColumn = null;
ColumnRef ref = null;
try {
indexColumn = index.getColumnForColumnName(indexColName);
ref = new ColumnRef(tableRef, indexColumn.getPosition());
} catch (ColumnNotFoundException e) {
if (index.getIndexType() == IndexType.LOCAL) {
try {
ref = new LocalIndexDataColumnRef(context, indexColName);
indexColumn = ref.getColumn();
} catch (ColumnFamilyNotFoundException c) {
throw e;
}
} else {
throw e;
}
}
String colName = tableColumn.getName().getString();
String tableAlias = tableRef.getTableAlias();
if (resolveColumn) {
try {
if (tableAlias != null) {
ref = resolver.resolveColumn(null, tableAlias, indexColName);
} else {
String schemaName = index.getSchemaName().getString();
ref = resolver.resolveColumn(schemaName.length() == 0 ? null : schemaName, index.getTableName().getString(), indexColName);
}
} catch (AmbiguousColumnException e) {
if (indexColumn.getFamilyName() != null) {
ref = resolver.resolveColumn(tableAlias != null ? tableAlias : index.getTableName().getString(), indexColumn.getFamilyName().getString(), indexColName);
} else {
throw e;
}
}
}
Expression expression = ref.newColumnExpression();
expression = coerceIfNecessary(i - tableOffset + projectedOffset, targetColumns, expression);
// We do not need to check if the column is a viewConstant, because view constants never
// appear as a column in an index
projectedExpressions.add(expression);
boolean isCaseSensitive = !SchemaUtil.normalizeIdentifier(colName).equals(colName);
ExpressionProjector projector = new ExpressionProjector(colName, tableRef.getTableAlias() == null ? dataTable.getName().getString() : tableRef.getTableAlias(), expression, isCaseSensitive);
projectedColumns.add(projector);
}
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class UpgradeUtil method upgradeTable.
public static void upgradeTable(PhoenixConnection conn, String srcTable) throws SQLException, SnapshotCreationException, IllegalArgumentException, IOException, InterruptedException {
ReadOnlyProps readOnlyProps = conn.getQueryServices().getProps();
if (conn.getSchema() != null) {
throw new IllegalArgumentException("Schema should not be set for connection!!");
}
if (!SchemaUtil.isNamespaceMappingEnabled(PTableType.TABLE, readOnlyProps)) {
throw new IllegalArgumentException(QueryServices.IS_NAMESPACE_MAPPING_ENABLED + " is not enabled!!");
}
try (HBaseAdmin admin = conn.getQueryServices().getAdmin();
HTableInterface metatable = conn.getQueryServices().getTable(SchemaUtil.getPhysicalName(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES, readOnlyProps).getName())) {
String tableName = SchemaUtil.normalizeIdentifier(srcTable);
String schemaName = SchemaUtil.getSchemaNameFromFullName(tableName);
// Confirm table is not already upgraded
PTable table = PhoenixRuntime.getTable(conn, tableName);
// Upgrade is not required if schemaName is not present.
if (schemaName.equals("") && !PTableType.VIEW.equals(table.getType())) {
throw new IllegalArgumentException("Table doesn't have schema name");
}
if (table.isNamespaceMapped()) {
throw new IllegalArgumentException("Table is already upgraded");
}
if (!schemaName.equals("")) {
logger.info(String.format("Creating schema %s..", schemaName));
conn.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + schemaName);
}
String oldPhysicalName = table.getPhysicalName().getString();
String newPhysicalTablename = SchemaUtil.normalizeIdentifier(SchemaUtil.getPhysicalTableName(oldPhysicalName, readOnlyProps).getNameAsString());
logger.info(String.format("Upgrading %s %s..", table.getType(), tableName));
// Upgrade the data or main table
mapTableToNamespace(admin, metatable, tableName, newPhysicalTablename, readOnlyProps, PhoenixRuntime.getCurrentScn(readOnlyProps), tableName, table.getType(), conn.getTenantId());
// clear the cache and get new table
conn.getQueryServices().clearTableFromCache(conn.getTenantId() == null ? ByteUtil.EMPTY_BYTE_ARRAY : conn.getTenantId().getBytes(), table.getSchemaName().getBytes(), table.getTableName().getBytes(), PhoenixRuntime.getCurrentScn(readOnlyProps));
MetaDataMutationResult result = new MetaDataClient(conn).updateCache(conn.getTenantId(), schemaName, SchemaUtil.getTableNameFromFullName(tableName), true);
if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
throw new TableNotFoundException(schemaName, tableName);
}
table = result.getTable();
// check whether table is properly upgraded before upgrading indexes
if (table.isNamespaceMapped()) {
for (PTable index : table.getIndexes()) {
String srcTableName = index.getPhysicalName().getString();
String destTableName = null;
String phoenixTableName = index.getName().getString();
boolean updateLink = true;
if (srcTableName.contains(QueryConstants.NAMESPACE_SEPARATOR)) {
// Skip already migrated
logger.info(String.format("skipping as it seems index '%s' is already upgraded..", index.getName()));
continue;
}
if (MetaDataUtil.isLocalIndex(srcTableName)) {
logger.info(String.format("local index '%s' found with physical hbase table name ''..", index.getName(), srcTableName));
destTableName = Bytes.toString(MetaDataUtil.getLocalIndexPhysicalName(newPhysicalTablename.getBytes()));
// update parent_table property in local index table descriptor
conn.createStatement().execute(String.format("ALTER TABLE %s set " + MetaDataUtil.PARENT_TABLE_KEY + "='%s'", phoenixTableName, table.getPhysicalName()));
} else if (MetaDataUtil.isViewIndex(srcTableName)) {
logger.info(String.format("View index '%s' found with physical hbase table name ''..", index.getName(), srcTableName));
destTableName = Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(newPhysicalTablename.getBytes()));
} else {
logger.info(String.format("Global index '%s' found with physical hbase table name ''..", index.getName(), srcTableName));
destTableName = SchemaUtil.getPhysicalTableName(index.getPhysicalName().getString(), readOnlyProps).getNameAsString();
}
logger.info(String.format("Upgrading index %s..", index.getName()));
if (!(table.getType() == PTableType.VIEW && !MetaDataUtil.isViewIndex(srcTableName) && IndexType.LOCAL != index.getIndexType())) {
mapTableToNamespace(admin, metatable, srcTableName, destTableName, readOnlyProps, PhoenixRuntime.getCurrentScn(readOnlyProps), phoenixTableName, index.getType(), conn.getTenantId());
}
if (updateLink) {
logger.info(String.format("Updating link information for index '%s' ..", index.getName()));
updateLink(conn, srcTableName, destTableName, index.getSchemaName(), index.getTableName());
conn.commit();
}
conn.getQueryServices().clearTableFromCache(conn.getTenantId() == null ? ByteUtil.EMPTY_BYTE_ARRAY : conn.getTenantId().getBytes(), index.getSchemaName().getBytes(), index.getTableName().getBytes(), PhoenixRuntime.getCurrentScn(readOnlyProps));
}
updateIndexesSequenceIfPresent(conn, table);
conn.commit();
} else {
throw new RuntimeException("Error: problem occured during upgrade. Table is not upgraded successfully");
}
if (table.getType() == PTableType.VIEW) {
updateLink(conn, oldPhysicalName, newPhysicalTablename, table.getSchemaName(), table.getTableName());
conn.commit();
}
}
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class FromCompiler method getResolverForCreation.
public static ColumnResolver getResolverForCreation(final CreateTableStatement statement, final PhoenixConnection connection) throws SQLException {
TableName baseTable = statement.getBaseTableName();
String schemaName;
if (baseTable == null) {
if (SchemaUtil.isSchemaCheckRequired(statement.getTableType(), connection.getQueryServices().getProps())) {
schemaName = statement.getTableName().getSchemaName();
if (schemaName != null) {
new SchemaResolver(connection, statement.getTableName().getSchemaName(), true);
} else if (connection.getSchema() != null) {
// To ensure schema set through properties or connection string exists before creating table
new SchemaResolver(connection, connection.getSchema(), true);
}
}
return EMPTY_TABLE_RESOLVER;
}
NamedTableNode tableNode = NamedTableNode.create(null, baseTable, Collections.<ColumnDef>emptyList());
// Always use non-tenant-specific connection here
try {
// We need to always get the latest meta data for the parent table of a create view call to ensure that
// that we're copying the current table meta data as of when the view is created. Once we no longer
// copy the parent meta data, but store only the local diffs (PHOENIX-3534), we will no longer need
// to do this.
SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, tableNode, true, true);
return visitor;
} catch (TableNotFoundException e) {
// A tenant-specific connection may not create a mapped VIEW.
if (connection.getTenantId() == null && statement.getTableType() == PTableType.VIEW) {
ConnectionQueryServices services = connection.getQueryServices();
byte[] fullTableName = SchemaUtil.getPhysicalName(SchemaUtil.getTableNameAsBytes(baseTable.getSchemaName(), baseTable.getTableName()), connection.getQueryServices().getProps()).getName();
HTableInterface htable = null;
try {
htable = services.getTable(fullTableName);
} catch (UnsupportedOperationException ignore) {
// For Connectionless
throw e;
} finally {
if (htable != null)
Closeables.closeQuietly(htable);
}
tableNode = NamedTableNode.create(null, baseTable, statement.getColumnDefs());
return new SingleTableColumnResolver(connection, tableNode, e.getTimeStamp(), new HashMap<String, UDFParseNode>(1), false);
}
throw e;
}
}
Aggregations