Search in sources :

Example 1 with PTableKey

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

the class IndexMetadataIT method assertNoActiveIndex.

private static void assertNoActiveIndex(Connection conn, String schemaName, String tableName) throws SQLException {
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    // client side cache will update
    conn.createStatement().executeQuery("SELECT count(*) FROM " + fullTableName).next();
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName)).getIndexMaintainers(ptr, pconn);
    assertTrue(ptr.getLength() == 0);
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 2 with PTableKey

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

the class IndexIT 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) 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 3 with PTableKey

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

the class DropColumnIT method testDropCol.

@Test
public void testDropCol() throws Exception {
    String indexTableName = generateUniqueName();
    String dataTableName = generateUniqueName();
    String localIndexTableName = "LOCAL_" + indexTableName;
    try (Connection conn = getConnection()) {
        conn.setAutoCommit(false);
        conn.createStatement().execute("CREATE TABLE " + dataTableName + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR, v3 VARCHAR) " + tableDDLOptions);
        // create one global and one local index
        conn.createStatement().execute("CREATE INDEX " + indexTableName + " ON " + dataTableName + " (v1) INCLUDE (v2, v3)");
        conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName + " ON " + dataTableName + " (v1) INCLUDE (v2, v3)");
        // upsert a single row
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + dataTableName + " VALUES(?,?,?,?)");
        stmt.setString(1, "a");
        stmt.setString(2, "x");
        stmt.setString(3, "1");
        stmt.setString(4, "2");
        stmt.execute();
        conn.commit();
        // verify v2 exists in the data table
        PTable dataTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableName));
        PColumn dataColumn = dataTable.getColumnForColumnName("V2");
        byte[] dataCq = dataColumn.getColumnQualifierBytes();
        // verify v2 exists in the global index table
        PTable globalIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
        PColumn glovalIndexCol = globalIndexTable.getColumnForColumnName("0:V2");
        byte[] globalIndexCq = glovalIndexCol.getColumnQualifierBytes();
        // verify v2 exists in the global index table
        PTable localIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, localIndexTableName));
        PColumn localIndexCol = localIndexTable.getColumnForColumnName("0:V2");
        byte[] localIndexCq = localIndexCol.getColumnQualifierBytes();
        verifyColValue(indexTableName, dataTableName, conn, dataTable, dataColumn, dataCq, globalIndexTable, glovalIndexCol, globalIndexCq, localIndexTable, localIndexCol, localIndexCq);
        // drop v2 column
        conn.createStatement().execute("ALTER TABLE " + dataTableName + " DROP COLUMN v2 ");
        conn.createStatement().execute("SELECT * FROM " + dataTableName);
        // verify that the column was dropped from the data table
        dataTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableName));
        try {
            dataTable.getColumnForColumnName("V2");
            fail("Column V2 should have been dropped from data table");
        } catch (ColumnNotFoundException e) {
        }
        // verify that the column was dropped from the global index table
        globalIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
        try {
            globalIndexTable.getColumnForColumnName("V2");
            fail("Column V2 should have been dropped from global index table");
        } catch (ColumnNotFoundException e) {
        }
        // verify that the column was dropped from the local index table
        localIndexTable = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
        try {
            localIndexTable.getColumnForColumnName("V2");
            fail("Column V2 should have been dropped from global index table");
        } catch (ColumnNotFoundException e) {
        }
        if (mutable || !columnEncoded) {
            byte[] key = Bytes.toBytes("a");
            Scan scan = new Scan();
            scan.setRaw(true);
            scan.setStartRow(key);
            scan.setStopRow(key);
            HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(dataTableName.getBytes());
            ResultScanner results = table.getScanner(scan);
            Result result = results.next();
            assertNotNull(result);
            assertEquals("data table column value should have been deleted", KeyValue.Type.DeleteColumn.getCode(), result.getColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, dataCq).get(0).getTypeByte());
            assertNull(results.next());
            // key value for v2 should have been deleted from the global index table
            scan = new Scan();
            scan.setRaw(true);
            table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(indexTableName.getBytes());
            results = table.getScanner(scan);
            result = results.next();
            assertNotNull(result);
            assertEquals("data table column value should have been deleted", KeyValue.Type.DeleteColumn.getCode(), result.getColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, globalIndexCq).get(0).getTypeByte());
            assertNull(results.next());
            // key value for v2 should have been deleted from the local index table
            scan = new Scan();
            scan.setRaw(true);
            scan.addFamily(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES);
            table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(dataTableName.getBytes());
            results = table.getScanner(scan);
            result = results.next();
            assertNotNull(result);
            assertEquals("data table col" + "umn value should have been deleted", KeyValue.Type.DeleteColumn.getCode(), result.getColumn(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES, localIndexCq).get(0).getTypeByte());
            assertNull(results.next());
        } else {
            // verify we don't issue deletes when we drop a column from an immutable encoded table
            verifyColValue(indexTableName, dataTableName, conn, dataTable, dataColumn, dataCq, globalIndexTable, glovalIndexCol, globalIndexCq, localIndexTable, localIndexCol, localIndexCq);
        }
    }
}
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) 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) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) Scan(org.apache.hadoop.hbase.client.Scan) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test)

Example 4 with PTableKey

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

the class MutableIndexIT method assertImmutableRows.

private void assertImmutableRows(Connection conn, String fullTableName, boolean expectedValue) throws SQLException {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    assertEquals(expectedValue, pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName)).isImmutableRows());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 5 with PTableKey

use of org.apache.phoenix.schema.PTableKey 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

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