Search in sources :

Example 36 with PTableKey

use of org.apache.phoenix.schema.PTableKey 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(), localIndex2.getImmutableStorageScheme());
            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 37 with PTableKey

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

the class UpdateCacheAcrossDifferentClientsIT method testTableSentWhenIndexStateChanges.

@Test
public void testTableSentWhenIndexStateChanges() throws Throwable {
    // Create connections 1 and 2
    // Must update config before starting server
    Properties longRunningProps = new Properties();
    longRunningProps.put(QueryServices.EXTRA_JDBC_ARGUMENTS_ATTRIB, QueryServicesOptions.DEFAULT_EXTRA_JDBC_ARGUMENTS);
    longRunningProps.put(QueryServices.DROP_METADATA_ATTRIB, Boolean.TRUE.toString());
    Connection conn1 = DriverManager.getConnection(getUrl(), longRunningProps);
    String url2 = getUrl() + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + "LongRunningQueries";
    Connection conn2 = DriverManager.getConnection(url2, longRunningProps);
    conn1.setAutoCommit(true);
    conn2.setAutoCommit(true);
    try {
        String schemaName = generateUniqueName();
        String tableName = generateUniqueName();
        String indexName = generateUniqueName();
        final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
        String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
        conn1.createStatement().execute("CREATE TABLE " + fullTableName + "(k INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true");
        conn1.createStatement().execute("CREATE INDEX " + indexName + " ON " + fullTableName + " (v1) INCLUDE (v2)");
        HTableInterface metaTable = conn2.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        IndexUtil.updateIndexState(fullIndexName, 0, metaTable, PIndexState.DISABLE);
        conn2.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES(1,2,3)");
        conn2.commit();
        conn1.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES(4,5,6)");
        conn1.commit();
        PTableKey key = new PTableKey(null, fullTableName);
        PMetaData metaCache = conn1.unwrap(PhoenixConnection.class).getMetaDataCache();
        PTable table = metaCache.getTableRef(key).getTable();
        for (PTable index : table.getIndexes()) {
            assertEquals(PIndexState.DISABLE, index.getIndexState());
        }
    } finally {
        conn1.close();
        conn2.close();
    }
}
Also used : PMetaData(org.apache.phoenix.schema.PMetaData) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Example 38 with PTableKey

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

the class BaseIndexIT method testIndexWithCaseSensitiveCols.

@Test
public void testIndexWithCaseSensitiveCols() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String tableName = "TBL_" + generateUniqueName();
    String indexName = "IND_" + generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
    String fullIndexName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName);
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.setAutoCommit(false);
        String query;
        ResultSet rs;
        conn.createStatement().execute("CREATE TABLE " + fullTableName + " (k VARCHAR NOT NULL PRIMARY KEY, \"V1\" VARCHAR, \"v2\" VARCHAR)" + tableDDLOptions);
        query = "SELECT * FROM " + fullTableName;
        rs = conn.createStatement().executeQuery(query);
        long ts = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, fullTableName)).getTimeStamp();
        assertFalse(rs.next());
        conn.createStatement().execute("CREATE " + (localIndex ? "LOCAL " : "") + "INDEX " + indexName + " ON " + fullTableName + "(\"v2\") INCLUDE (\"V1\")");
        query = "SELECT * FROM " + fullIndexName;
        rs = conn.createStatement().executeQuery(query);
        assertFalse(rs.next());
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + fullTableName + " VALUES(?,?,?)");
        stmt.setString(1, "a");
        stmt.setString(2, "x");
        stmt.setString(3, "1");
        stmt.execute();
        stmt.setString(1, "b");
        stmt.setString(2, "y");
        stmt.setString(3, "2");
        stmt.execute();
        conn.commit();
        query = "SELECT * FROM " + fullTableName + " WHERE \"v2\" = '1'";
        rs = conn.createStatement().executeQuery("EXPLAIN " + query);
        if (localIndex) {
            assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + fullTableName + " [1,'1']\n" + "CLIENT MERGE SORT", QueryUtil.getExplainPlan(rs));
        } else {
            assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + fullIndexName + " ['1']", QueryUtil.getExplainPlan(rs));
        }
        rs = conn.createStatement().executeQuery(query);
        assertTrue(rs.next());
        assertEquals("a", rs.getString(1));
        assertEquals("x", rs.getString(2));
        assertEquals("1", rs.getString(3));
        assertEquals("a", rs.getString("k"));
        assertEquals("x", rs.getString("V1"));
        assertEquals("1", rs.getString("v2"));
        assertFalse(rs.next());
        query = "SELECT \"V1\", \"V1\" as foo1, \"v2\" as foo, \"v2\" as \"Foo1\", \"v2\" FROM " + fullTableName + " ORDER BY foo";
        rs = conn.createStatement().executeQuery("EXPLAIN " + query);
        if (localIndex) {
            assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + fullTableName + " [1]\nCLIENT MERGE SORT", QueryUtil.getExplainPlan(rs));
        } else {
            assertEquals("CLIENT PARALLEL 1-WAY FULL SCAN OVER " + fullIndexName, QueryUtil.getExplainPlan(rs));
        }
        rs = conn.createStatement().executeQuery(query);
        assertTrue(rs.next());
        assertEquals("x", rs.getString(1));
        assertEquals("x", rs.getString("V1"));
        assertEquals("x", rs.getString(2));
        assertEquals("x", rs.getString("foo1"));
        assertEquals("1", rs.getString(3));
        assertEquals("1", rs.getString("Foo"));
        assertEquals("1", rs.getString(4));
        assertEquals("1", rs.getString("Foo1"));
        assertEquals("1", rs.getString(5));
        assertEquals("1", rs.getString("v2"));
        assertTrue(rs.next());
        assertEquals("y", rs.getString(1));
        assertEquals("y", rs.getString("V1"));
        assertEquals("y", rs.getString(2));
        assertEquals("y", rs.getString("foo1"));
        assertEquals("2", rs.getString(3));
        assertEquals("2", rs.getString("Foo"));
        assertEquals("2", rs.getString(4));
        assertEquals("2", rs.getString("Foo1"));
        assertEquals("2", rs.getString(5));
        assertEquals("2", rs.getString("v2"));
        assertFalse(rs.next());
        assertNoIndexDeletes(conn, ts, fullIndexName);
    }
}
Also used : Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) PhoenixResultSet(org.apache.phoenix.jdbc.PhoenixResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 39 with PTableKey

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

the class StatsCollectorIT method invalidateStats.

private static void invalidateStats(Connection conn, String tableName) throws SQLException {
    PTable ptable = conn.unwrap(PhoenixConnection.class).getMetaDataCache().getTableRef(new PTableKey(null, tableName)).getTable();
    byte[] name = ptable.getPhysicalName().getBytes();
    conn.unwrap(PhoenixConnection.class).getQueryServices().invalidateStats(new GuidePostsKey(name, SchemaUtil.getEmptyColumnFamily(ptable)));
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Example 40 with PTableKey

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

the class PhoenixConfigurationUtil method getStatsForParallelizationProp.

public static boolean getStatsForParallelizationProp(PhoenixConnection conn, PTable table) {
    Boolean useStats = table.useStatsForParallelization();
    if (useStats != null) {
        return useStats;
    }
    /*
	     * For a view index, we use the property set on view. For indexes on base table, whether
	     * global or local, we use the property set on the base table. Null check needed when
	     * dropping local indexes.
	     */
    PName tenantId = conn.getTenantId();
    int retryCount = 0;
    while (retryCount++ < 2) {
        if (table.getType() == PTableType.INDEX && table.getParentName() != null) {
            String parentTableName = table.getParentName().getString();
            try {
                PTable parentTable = conn.getTable(new PTableKey(tenantId, parentTableName));
                useStats = parentTable.useStatsForParallelization();
                if (useStats != null) {
                    return useStats;
                }
            } catch (TableNotFoundException e) {
                // global tables)
                if (tenantId != null) {
                    tenantId = null;
                } else {
                    BaseResultIterators.logger.warn("Unable to find parent table \"" + parentTableName + "\" of table \"" + table.getName().getString() + "\" to determine USE_STATS_FOR_PARALLELIZATION", e);
                }
            }
        }
    }
    return conn.getQueryServices().getConfiguration().getBoolean(USE_STATS_FOR_PARALLELIZATION, DEFAULT_USE_STATS_FOR_PARALLELIZATION);
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PName(org.apache.phoenix.schema.PName) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Aggregations

PTableKey (org.apache.phoenix.schema.PTableKey)89 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)77 PTable (org.apache.phoenix.schema.PTable)55 Connection (java.sql.Connection)48 Test (org.junit.Test)40 Properties (java.util.Properties)23 ResultSet (java.sql.ResultSet)14 PColumn (org.apache.phoenix.schema.PColumn)14 PreparedStatement (java.sql.PreparedStatement)13 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)12 SQLException (java.sql.SQLException)11 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)11 PMetaData (org.apache.phoenix.schema.PMetaData)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)9 PName (org.apache.phoenix.schema.PName)9 BaseTest (org.apache.phoenix.query.BaseTest)8 Result (org.apache.hadoop.hbase.client.Result)7 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)7 Scan (org.apache.hadoop.hbase.client.Scan)7 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)6