Search in sources :

Example 11 with TableNotFoundException

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();
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) Connection(java.sql.Connection) Properties(java.util.Properties) Test(org.junit.Test)

Example 12 with TableNotFoundException

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);
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties)

Example 13 with TableNotFoundException

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();
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) Mutation(org.apache.hadoop.hbase.client.Mutation) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) Put(org.apache.hadoop.hbase.client.Put) MutationCode(org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode)

Example 14 with TableNotFoundException

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();
        }
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) ResultSet(java.sql.ResultSet) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) PhoenixDatabaseMetaData(org.apache.phoenix.jdbc.PhoenixDatabaseMetaData) Properties(java.util.Properties) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException)

Example 15 with TableNotFoundException

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
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) Expression(org.apache.phoenix.expression.Expression) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) Connection(java.sql.Connection) Properties(java.util.Properties) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)32 Connection (java.sql.Connection)14 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)13 PTable (org.apache.phoenix.schema.PTable)13 Properties (java.util.Properties)11 Test (org.junit.Test)11 PTableKey (org.apache.phoenix.schema.PTableKey)10 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)8 SQLException (java.sql.SQLException)7 ResultSet (java.sql.ResultSet)5 PColumn (org.apache.phoenix.schema.PColumn)5 PreparedStatement (java.sql.PreparedStatement)4 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)4 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)4 Scan (org.apache.hadoop.hbase.client.Scan)4 PhoenixIndexBuilder (org.apache.phoenix.index.PhoenixIndexBuilder)4 MetaDataClient (org.apache.phoenix.schema.MetaDataClient)4 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)3 IOException (java.io.IOException)3 PhoenixIOException (org.apache.phoenix.exception.PhoenixIOException)3