use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class ViewIndexIT method testUpdateOnTenantViewWithGlobalView.
@Test
public void testUpdateOnTenantViewWithGlobalView() throws Exception {
Connection conn = getConnection();
String baseSchemaName = "PLATFORM_ENTITY";
String baseTableName = generateUniqueName();
String baseFullName = SchemaUtil.getTableName(baseSchemaName, baseTableName);
String viewTableName = "V_" + generateUniqueName();
String viewFullName = SchemaUtil.getTableName(baseSchemaName, viewTableName);
String indexName = "I_" + generateUniqueName();
String tsViewTableName = "TSV_" + generateUniqueName();
String tsViewFullName = SchemaUtil.getTableName(baseSchemaName, tsViewTableName);
try {
if (isNamespaceMapped) {
conn.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + baseSchemaName);
}
conn.createStatement().execute("CREATE TABLE " + baseFullName + "(\n" + " ORGANIZATION_ID CHAR(15) NOT NULL,\n" + " KEY_PREFIX CHAR(3) NOT NULL,\n" + " CREATED_DATE DATE,\n" + " CREATED_BY CHAR(15),\n" + " CONSTRAINT PK PRIMARY KEY (\n" + " ORGANIZATION_ID,\n" + " KEY_PREFIX\n" + " )\n" + ") VERSIONS=1, IMMUTABLE_ROWS=true, MULTI_TENANT=true");
conn.createStatement().execute("CREATE VIEW " + viewFullName + " (\n" + "INT1 BIGINT NOT NULL,\n" + "DOUBLE1 DECIMAL(12, 3),\n" + "IS_BOOLEAN BOOLEAN,\n" + "TEXT1 VARCHAR,\n" + "CONSTRAINT PKVIEW PRIMARY KEY\n" + "(\n" + "INT1\n" + ")) AS SELECT * FROM " + baseFullName + " WHERE KEY_PREFIX = '123'");
conn.createStatement().execute("CREATE INDEX " + indexName + " \n" + "ON " + viewFullName + " (TEXT1 DESC, INT1)\n" + "INCLUDE (CREATED_BY, DOUBLE1, IS_BOOLEAN, CREATED_DATE)");
Properties tsProps = PropertiesUtil.deepCopy(TEST_PROPERTIES);
tsProps.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "123451234512345");
Connection tsConn = DriverManager.getConnection(getUrl(), tsProps);
tsConn.createStatement().execute("CREATE VIEW " + tsViewFullName + " AS SELECT * FROM " + viewFullName);
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (1,1.0, true, 'a')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (2,2.0, true, 'b')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (3,3.0, true, 'c')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (4,4.0, true, 'd')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (5,5.0, true, 'e')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (6,6.0, true, 'f')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (7,7.0, true, 'g')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (8,8.0, true, 'h')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (9,9.0, true, 'i')");
tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1) VALUES (10,10.0, true, 'j')");
tsConn.commit();
String basePhysicalName = SchemaUtil.getPhysicalHBaseTableName(baseFullName, isNamespaceMapped, PTableType.TABLE).getString();
assertRowCount(tsConn, tsViewFullName, basePhysicalName, 10);
tsConn.createStatement().execute("DELETE FROM " + tsViewFullName + " WHERE TEXT1='d'");
tsConn.commit();
assertRowCount(tsConn, tsViewFullName, basePhysicalName, 9);
tsConn.createStatement().execute("DELETE FROM " + tsViewFullName + " WHERE INT1=2");
tsConn.commit();
assertRowCount(tsConn, tsViewFullName, basePhysicalName, 8);
// Use different connection for delete
Connection tsConn2 = DriverManager.getConnection(getUrl(), tsProps);
tsConn2.createStatement().execute("DELETE FROM " + tsViewFullName + " WHERE DOUBLE1 > 7.5 AND DOUBLE1 < 9.5");
tsConn2.commit();
assertRowCount(tsConn2, tsViewFullName, basePhysicalName, 6);
tsConn2.createStatement().execute("DROP VIEW " + tsViewFullName);
// Should drop view and index and remove index data
conn.createStatement().execute("DROP VIEW " + viewFullName);
// Deletes table data (but wouldn't update index)
conn.setAutoCommit(true);
conn.createStatement().execute("DELETE FROM " + baseFullName);
Connection tsConn3 = DriverManager.getConnection(getUrl(), tsProps);
try {
tsConn3.createStatement().execute("SELECT * FROM " + tsViewFullName + " LIMIT 1");
fail("Expected table not to be found");
} catch (TableNotFoundException e) {
}
conn.createStatement().execute("CREATE VIEW " + viewFullName + " (\n" + "INT1 BIGINT NOT NULL,\n" + "DOUBLE1 DECIMAL(12, 3),\n" + "IS_BOOLEAN BOOLEAN,\n" + "TEXT1 VARCHAR,\n" + "CONSTRAINT PKVIEW PRIMARY KEY\n" + "(\n" + "INT1\n" + ")) AS SELECT * FROM " + baseFullName + " WHERE KEY_PREFIX = '123'");
tsConn3.createStatement().execute("CREATE VIEW " + tsViewFullName + " AS SELECT * FROM " + viewFullName);
conn.createStatement().execute("CREATE INDEX " + indexName + " \n" + "ON " + viewFullName + " (TEXT1 DESC, INT1)\n" + "INCLUDE (CREATED_BY, DOUBLE1, IS_BOOLEAN, CREATED_DATE)");
assertRowCount(tsConn3, tsViewFullName, basePhysicalName, 0);
tsConn.close();
tsConn2.close();
tsConn3.close();
} finally {
conn.close();
}
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class ViewIT method testViewAndTableInDifferentSchemas.
public void testViewAndTableInDifferentSchemas(boolean isNamespaceMapped) throws Exception {
Properties props = new Properties();
props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(isNamespaceMapped));
Connection conn = DriverManager.getConnection(getUrl(), props);
String schemaName1 = "S_" + generateUniqueName();
String fullTableName1 = SchemaUtil.getTableName(schemaName1, tableName);
if (isNamespaceMapped) {
conn.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + schemaName1);
}
String ddl = "CREATE TABLE " + fullTableName1 + " (k INTEGER NOT NULL PRIMARY KEY, v1 DATE)" + tableDDLOptions;
HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
conn.createStatement().execute(ddl);
assertTrue(admin.tableExists(SchemaUtil.getPhysicalTableName(SchemaUtil.normalizeIdentifier(fullTableName1), conn.unwrap(PhoenixConnection.class).getQueryServices().getProps())));
String viewName = "V_" + generateUniqueName();
String viewSchemaName = "S_" + generateUniqueName();
String fullViewName1 = SchemaUtil.getTableName(viewSchemaName, viewName);
ddl = "CREATE VIEW " + fullViewName1 + " (v2 VARCHAR) AS SELECT * FROM " + fullTableName1 + " WHERE k > 5";
conn.createStatement().execute(ddl);
String fullViewName2 = "V_" + generateUniqueName();
ddl = "CREATE VIEW " + fullViewName2 + " (v2 VARCHAR) AS SELECT * FROM " + fullTableName1 + " WHERE k > 5";
conn.createStatement().execute(ddl);
conn.createStatement().executeQuery("SELECT * FROM " + fullViewName1);
conn.createStatement().executeQuery("SELECT * FROM " + fullViewName2);
ddl = "DROP VIEW " + viewName;
try {
conn.createStatement().execute(ddl);
fail();
} catch (TableNotFoundException ignore) {
}
ddl = "DROP VIEW " + fullViewName1;
conn.createStatement().execute(ddl);
ddl = "DROP VIEW " + SchemaUtil.getTableName(viewSchemaName, generateUniqueName());
try {
conn.createStatement().execute(ddl);
fail();
} catch (TableNotFoundException ignore) {
}
ddl = "DROP TABLE " + fullTableName1;
validateCannotDropTableWithChildViewsWithoutCascade(conn, fullTableName1);
ddl = "DROP VIEW " + fullViewName2;
conn.createStatement().execute(ddl);
ddl = "DROP TABLE " + fullTableName1;
conn.createStatement().execute(ddl);
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class MetaDataRegionObserver method updateIndexState.
private static void updateIndexState(PhoenixConnection conn, String indexTableName, RegionCoprocessorEnvironment env, PIndexState oldState, PIndexState newState) throws ServiceException, Throwable {
byte[] indexTableKey = SchemaUtil.getTableKeyFromFullName(indexTableName);
String schemaName = SchemaUtil.getSchemaNameFromFullName(indexTableName);
String indexName = SchemaUtil.getTableNameFromFullName(indexTableName);
// Mimic the Put that gets generated by the client on an update of the
// index state
Put put = new Put(indexTableKey);
put.addColumn(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.INDEX_STATE_BYTES, newState.getSerializedBytes());
if (newState == PIndexState.ACTIVE) {
put.addColumn(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.INDEX_DISABLE_TIMESTAMP_BYTES, PLong.INSTANCE.toBytes(0));
put.addColumn(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.ASYNC_REBUILD_TIMESTAMP_BYTES, PLong.INSTANCE.toBytes(0));
}
final List<Mutation> tableMetadata = Collections.<Mutation>singletonList(put);
MetaDataMutationResult result = conn.getQueryServices().updateIndexState(tableMetadata, null);
MutationCode code = result.getMutationCode();
if (code == MutationCode.TABLE_NOT_FOUND) {
throw new TableNotFoundException(schemaName, indexName);
}
if (code == MutationCode.UNALLOWED_TABLE_MUTATION) {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.INVALID_INDEX_STATE_TRANSITION).setMessage(" currentState=" + oldState + ". requestedState=" + newState).setSchemaName(schemaName).setTableName(indexName).build().buildException();
}
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class BaseTest method deletePriorTables.
private static void deletePriorTables(long ts, Connection globalConn, String url) throws Exception {
DatabaseMetaData dbmd = globalConn.getMetaData();
// Drop VIEWs first, as we don't allow a TABLE with views to be dropped
// Tables are sorted by TENANT_ID
List<String[]> tableTypesList = Arrays.asList(new String[] { PTableType.VIEW.toString() }, new String[] { PTableType.TABLE.toString() });
for (String[] tableTypes : tableTypesList) {
ResultSet rs = dbmd.getTables(null, null, null, tableTypes);
String lastTenantId = null;
Connection conn = globalConn;
while (rs.next()) {
String fullTableName = SchemaUtil.getEscapedTableName(rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM), rs.getString(PhoenixDatabaseMetaData.TABLE_NAME));
String ddl = "DROP " + rs.getString(PhoenixDatabaseMetaData.TABLE_TYPE) + " " + fullTableName;
String tenantId = rs.getString(1);
if (tenantId != null && !tenantId.equals(lastTenantId)) {
if (lastTenantId != null) {
conn.close();
}
// Open tenant-specific connection when we find a new one
Properties props = PropertiesUtil.deepCopy(globalConn.getClientInfo());
props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
conn = DriverManager.getConnection(url, props);
lastTenantId = tenantId;
}
try {
conn.createStatement().executeUpdate(ddl);
} catch (NewerTableAlreadyExistsException ex) {
logger.info("Newer table " + fullTableName + " or its delete marker exists. Ignore current deletion");
} catch (TableNotFoundException ex) {
logger.info("Table " + fullTableName + " is already deleted.");
}
}
rs.close();
if (lastTenantId != null) {
conn.close();
}
}
}
use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.
the class PhoenixRuntimeTest method testGetTenantIdExpression.
@Test
public void testGetTenantIdExpression() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
Expression e1 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_STATS_NAME);
assertNull(e1);
Expression e2 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME);
assertNotNull(e2);
Expression e3 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_NAME);
assertNotNull(e3);
conn.createStatement().execute("CREATE TABLE FOO (k VARCHAR PRIMARY KEY)");
Expression e4 = PhoenixRuntime.getTenantIdExpression(conn, "FOO");
assertNull(e4);
conn.createStatement().execute("CREATE TABLE A.BAR (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2)) MULTI_TENANT=true");
Expression e5 = PhoenixRuntime.getTenantIdExpression(conn, "A.BAR");
assertNotNull(e5);
conn.createStatement().execute("CREATE INDEX I1 ON A.BAR (K2)");
Expression e5A = PhoenixRuntime.getTenantIdExpression(conn, "A.I1");
assertNotNull(e5A);
conn.createStatement().execute("CREATE TABLE BAS (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2)) MULTI_TENANT=true, SALT_BUCKETS=3");
Expression e6 = PhoenixRuntime.getTenantIdExpression(conn, "BAS");
assertNotNull(e6);
conn.createStatement().execute("CREATE INDEX I2 ON BAS (K2)");
Expression e6A = PhoenixRuntime.getTenantIdExpression(conn, "I2");
assertNotNull(e6A);
try {
PhoenixRuntime.getTenantIdExpression(conn, "NOT.ATABLE");
fail();
} catch (TableNotFoundException e) {
// Expected
}
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "t1");
Connection tsconn = DriverManager.getConnection(getUrl(), props);
tsconn.createStatement().execute("CREATE VIEW V(V1 VARCHAR) AS SELECT * FROM BAS");
Expression e7 = PhoenixRuntime.getTenantIdExpression(tsconn, "V");
assertNotNull(e7);
tsconn.createStatement().execute("CREATE LOCAL INDEX I3 ON V (V1)");
try {
PhoenixRuntime.getTenantIdExpression(tsconn, "I3");
fail();
} catch (SQLFeatureNotSupportedException e) {
// Expected
}
}
Aggregations