Search in sources :

Example 6 with TableNotFoundException

use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.

the class IndexUtil method getPDataTable.

public static PTable getPDataTable(Connection conn, HTableDescriptor tableDesc) throws SQLException {
    String dataTableName = Bytes.toString(tableDesc.getValue(MetaDataUtil.DATA_TABLE_NAME_PROP_BYTES));
    String physicalTableName = tableDesc.getTableName().getNameAsString();
    PTable pDataTable = null;
    if (dataTableName == null) {
        if (physicalTableName.contains(QueryConstants.NAMESPACE_SEPARATOR)) {
            try {
                pDataTable = PhoenixRuntime.getTable(conn, physicalTableName.replace(QueryConstants.NAMESPACE_SEPARATOR, QueryConstants.NAME_SEPARATOR));
            } catch (TableNotFoundException e) {
                // could be a table mapped to external table
                pDataTable = PhoenixRuntime.getTable(conn, physicalTableName);
            }
        } else {
            pDataTable = PhoenixRuntime.getTable(conn, physicalTableName);
        }
    } else {
        pDataTable = PhoenixRuntime.getTable(conn, dataTableName);
    }
    return pDataTable;
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PTable(org.apache.phoenix.schema.PTable)

Example 7 with TableNotFoundException

use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.

the class PhoenixRuntime method getTable.

/**
     * 
     * @param conn
     * @param name requires a pre-normalized table name or a pre-normalized schema and table name
     * @return
     * @throws SQLException
     */
public static PTable getTable(Connection conn, String name) throws SQLException {
    PTable table = null;
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    try {
        table = pconn.getTable(new PTableKey(pconn.getTenantId(), name));
    } catch (TableNotFoundException e) {
        String schemaName = SchemaUtil.getSchemaNameFromFullName(name);
        String tableName = SchemaUtil.getTableNameFromFullName(name);
        MetaDataMutationResult result = new MetaDataClient(pconn).updateCache(schemaName, tableName);
        if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
            throw e;
        }
        table = result.getTable();
    }
    return table;
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) PTable(org.apache.phoenix.schema.PTable)

Example 8 with TableNotFoundException

use of org.apache.phoenix.schema.TableNotFoundException 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());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) HTable(org.apache.hadoop.hbase.client.HTable) PTable(org.apache.phoenix.schema.PTable) Result(org.apache.hadoop.hbase.client.Result) PColumn(org.apache.phoenix.schema.PColumn) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) PName(org.apache.phoenix.schema.PName) Scan(org.apache.hadoop.hbase.client.Scan) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 9 with TableNotFoundException

use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.

the class DropColumnIT method testDroppingIndexedColDropsIndex.

@Test
public void testDroppingIndexedColDropsIndex() throws Exception {
    String indexTableName = generateUniqueName();
    String dataTableFullName = SchemaUtil.getTableName(SCHEMA_NAME, generateUniqueName());
    String localIndexTableName1 = "LOCAL_" + indexTableName + "_1";
    String localIndexTableName2 = "LOCAL_" + indexTableName + "_2";
    try (Connection conn = getConnection()) {
        conn.setAutoCommit(false);
        conn.createStatement().execute("CREATE TABLE " + dataTableFullName + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) " + tableDDLOptions);
        // create one regular and two local indexes
        conn.createStatement().execute("CREATE INDEX " + indexTableName + " ON " + dataTableFullName + " (v2) INCLUDE (v1)");
        conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName1 + " ON " + dataTableFullName + " (v2) INCLUDE (v1)");
        conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName2 + " ON " + dataTableFullName + " (k) INCLUDE (v1)");
        // upsert a single row
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + dataTableFullName + " VALUES(?,?,?)");
        stmt.setString(1, "a");
        stmt.setString(2, "x");
        stmt.setString(3, "1");
        stmt.execute();
        conn.commit();
        // verify the indexes were created
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable dataTable = pconn.getTable(new PTableKey(null, dataTableFullName));
        assertEquals("Unexpected number of indexes ", 3, dataTable.getIndexes().size());
        PTable indexTable = dataTable.getIndexes().get(0);
        byte[] indexTablePhysicalName = indexTable.getPhysicalName().getBytes();
        PName localIndexTablePhysicalName = dataTable.getIndexes().get(1).getPhysicalName();
        // drop v2 which causes the regular index and first local index to be dropped
        conn.createStatement().execute("ALTER TABLE " + dataTableFullName + " DROP COLUMN v2 ");
        // verify the both of the indexes' metadata were dropped
        conn.createStatement().execute("SELECT * FROM " + dataTableFullName);
        try {
            conn.createStatement().execute("SELECT * FROM " + indexTableName);
            fail("Index should have been dropped");
        } catch (TableNotFoundException e) {
        }
        pconn = conn.unwrap(PhoenixConnection.class);
        dataTable = pconn.getTable(new PTableKey(null, dataTableFullName));
        try {
            pconn.getTable(new PTableKey(null, indexTableName));
            fail("index should have been dropped");
        } catch (TableNotFoundException e) {
        }
        try {
            pconn.getTable(new PTableKey(null, localIndexTableName1));
            fail("index should have been dropped");
        } catch (TableNotFoundException e) {
        }
        assertEquals("Unexpected number of indexes ", 1, dataTable.getIndexes().size());
        // verify that the regular index physical table was dropped
        try {
            conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(indexTablePhysicalName);
            fail("Index table should have been dropped");
        } catch (TableNotFoundException e) {
        }
        // verify that the local index physical table was *not* dropped
        conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(localIndexTablePhysicalName.getBytes());
        PTable localIndex2 = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, localIndexTableName2));
        // there should be a single row belonging to localIndexTableName2 
        Scan scan = new Scan();
        scan.addFamily(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES);
        HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(localIndexTablePhysicalName.getBytes());
        ResultScanner results = table.getScanner(scan);
        Result result = results.next();
        assertNotNull(result);
        String indexColumnName = IndexUtil.getIndexColumnName(QueryConstants.DEFAULT_COLUMN_FAMILY, "V1");
        PColumn localIndexCol = localIndex2.getColumnForColumnName(indexColumnName);
        byte[] colValue;
        if (!mutable && columnEncoded) {
            KeyValueColumnExpression colExpression = new SingleCellColumnExpression(localIndexCol, indexColumnName, localIndex2.getEncodingScheme());
            ImmutableBytesPtr ptr = new ImmutableBytesPtr();
            colExpression.evaluate(new ResultTuple(result), ptr);
            colValue = ptr.copyBytesIfNecessary();
        } else {
            colValue = result.getValue(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES, localIndexCol.getColumnQualifierBytes());
        }
        assertNotNull("localIndexTableName2 row is missing", colValue);
        assertNull(results.next());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PreparedStatement(java.sql.PreparedStatement) HTable(org.apache.hadoop.hbase.client.HTable) PTable(org.apache.phoenix.schema.PTable) Result(org.apache.hadoop.hbase.client.Result) PColumn(org.apache.phoenix.schema.PColumn) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) PName(org.apache.phoenix.schema.PName) Scan(org.apache.hadoop.hbase.client.Scan) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test)

Example 10 with TableNotFoundException

use of org.apache.phoenix.schema.TableNotFoundException in project phoenix by apache.

the class LocalIndexIT method testLocalIndexCreationWithSaltingShouldFail.

@Test
public void testLocalIndexCreationWithSaltingShouldFail() throws Exception {
    String tableName = schemaName + "." + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    createBaseTable(tableName, null, null);
    Connection conn1 = getConnection();
    Connection conn2 = getConnection();
    try {
        conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " + tableName + "(v1)" + " salt_buckets=16");
        fail("Local index cannot be salted.");
    } catch (SQLException e) {
    }
    try {
        conn2.createStatement().executeQuery("SELECT * FROM " + tableName).next();
        conn2.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexName));
        fail("Local index should not be created.");
    } catch (TableNotFoundException e) {
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test)

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