Search in sources :

Example 1 with PTable

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

the class AlterTableIT method testMetadataForMutableTable.

@Test
public void testMetadataForMutableTable() throws Exception {
    String schemaName = "XYZ";
    String baseTableName = generateUniqueName();
    String viewName = generateUniqueName();
    String fullTableName = schemaName + "." + baseTableName;
    String fullViewName = schemaName + "." + viewName;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS " + fullTableName + " (" + " ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " KV1 VARCHAR" + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)" + " ) " + tableDDLOptions);
        PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        long initBaseTableSeqNumber = baseTable.getSequenceNumber();
        // assert that the client side cache is updated.
        EncodedCQCounter cqCounter = baseTable.getEncodedCQCounter();
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, cqCounter.getNextQualifier(QueryConstants.DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata is updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "KV1", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber);
        // now create a view and validate client and server side metadata
        String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName;
        conn.createStatement().execute(viewDDL);
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));
        // verify that the client side cache is updated. Base table's cq counters should be updated.
        assertEquals(columnEncoded ? (Integer) (ENCODED_CQ_COUNTER_INITIAL_VALUE + 3) : null, baseTable.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        assertNull("A view should always have the null cq counter", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        // assert that the server side metadata for the base table and the view is also updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + (columnEncoded ? 1 : 0));
        assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM);
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) EncodedCQCounter(org.apache.phoenix.schema.PTable.EncodedCQCounter) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 2 with PTable

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

the class AppendOnlySchemaIT method testAddColumns.

private void testAddColumns(boolean sameClient) throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    try (Connection conn1 = DriverManager.getConnection(getUrl(), props);
        Connection conn2 = sameClient ? conn1 : DriverManager.getConnection(getUrl(), props)) {
        String metricTableName = generateUniqueName();
        String viewName = generateUniqueName();
        String metricIdSeqTableName = generateUniqueName();
        // create sequence for auto partition
        conn1.createStatement().execute("CREATE SEQUENCE " + metricIdSeqTableName + " CACHE 1");
        // create base table
        conn1.createStatement().execute("CREATE TABLE " + metricTableName + " (metricId INTEGER NOT NULL, metricVal1 DOUBLE, CONSTRAINT PK PRIMARY KEY(metricId))" + " APPEND_ONLY_SCHEMA = true, UPDATE_CACHE_FREQUENCY=1, AUTO_PARTITION_SEQ=" + metricIdSeqTableName);
        // create view
        String ddl = "CREATE VIEW IF NOT EXISTS " + viewName + "( hostName varchar NOT NULL," + " CONSTRAINT HOSTNAME_PK PRIMARY KEY (hostName))" + " AS SELECT * FROM " + metricTableName + " UPDATE_CACHE_FREQUENCY=300000";
        conn1.createStatement().execute(ddl);
        conn1.createStatement().execute("UPSERT INTO " + viewName + "(hostName, metricVal1) VALUES('host1', 1.0)");
        conn1.commit();
        // execute ddl that creates that same view with an additional pk column and regular column
        // and also changes the order of the pk columns (which is not respected since we only 
        // allow appending columns)
        ddl = "CREATE VIEW IF NOT EXISTS " + viewName + "( instanceName varchar, hostName varchar, metricVal2 double, metricVal1 double" + " CONSTRAINT HOSTNAME_PK PRIMARY KEY (instancename, hostName))" + " AS SELECT * FROM " + metricTableName + " UPDATE_CACHE_FREQUENCY=300000";
        conn2.createStatement().execute(ddl);
        conn2.createStatement().execute("UPSERT INTO " + viewName + "(hostName, instanceName, metricVal1, metricval2) VALUES('host2', 'instance2', 21.0, 22.0)");
        conn2.commit();
        conn1.createStatement().execute("UPSERT INTO " + viewName + "(hostName, metricVal1) VALUES('host3', 3.0)");
        conn1.commit();
        // verify data exists
        ResultSet rs = conn2.createStatement().executeQuery("SELECT * from " + viewName);
        // verify the two columns were added correctly
        PTable table = conn2.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, viewName));
        List<PColumn> pkColumns = table.getPKColumns();
        assertEquals(3, table.getPKColumns().size());
        // even though the second create view statement changed the order of the pk, the original order is maintained
        PColumn metricId = pkColumns.get(0);
        assertEquals("METRICID", metricId.getName().getString());
        assertFalse(metricId.isNullable());
        PColumn hostName = pkColumns.get(1);
        assertEquals("HOSTNAME", hostName.getName().getString());
        // hostname name is not nullable even though the second create statement changed it to nullable
        // since we only allow appending columns
        assertFalse(hostName.isNullable());
        PColumn instanceName = pkColumns.get(2);
        assertEquals("INSTANCENAME", instanceName.getName().getString());
        assertTrue(instanceName.isNullable());
        List<PColumn> columns = table.getColumns();
        assertEquals("METRICID", columns.get(0).getName().getString());
        assertEquals("METRICVAL1", columns.get(1).getName().getString());
        assertEquals("HOSTNAME", columns.get(2).getName().getString());
        assertEquals("INSTANCENAME", columns.get(3).getName().getString());
        assertEquals("METRICVAL2", columns.get(4).getName().getString());
        // verify the data
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(1.0, rs.getDouble(2), 1e-6);
        assertEquals("host1", rs.getString(3));
        assertEquals(null, rs.getString(4));
        assertEquals(0.0, rs.getDouble(5), 1e-6);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(21.0, rs.getDouble(2), 1e-6);
        assertEquals("host2", rs.getString(3));
        assertEquals("instance2", rs.getString(4));
        assertEquals(22.0, rs.getDouble(5), 1e-6);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(3.0, rs.getDouble(2), 1e-6);
        assertEquals("host3", rs.getString(3));
        assertEquals(null, rs.getString(4));
        assertEquals(0.0, rs.getDouble(5), 1e-6);
        assertFalse(rs.next());
    }
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Example 3 with PTable

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

the class AlterTableWithViewsIT method testAlterPropertiesOfParentTable.

@Test
public void testAlterPropertiesOfParentTable() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl());
        Connection viewConn = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : conn) {
        String tableName = generateUniqueName();
        String viewOfTable1 = tableName + "_VIEW1";
        String viewOfTable2 = tableName + "_VIEW2";
        String ddlFormat = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + " %s ID char(1) NOT NULL," + " COL1 integer NOT NULL," + " COL2 bigint NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (%s ID, COL1, COL2)" + " ) %s ";
        conn.createStatement().execute(generateDDL("UPDATE_CACHE_FREQUENCY=2", ddlFormat));
        viewConn.createStatement().execute("CREATE VIEW " + viewOfTable1 + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableName);
        viewConn.createStatement().execute("CREATE VIEW " + viewOfTable2 + " ( VIEW_COL1 DECIMAL(10,2), VIEW_COL2 VARCHAR ) AS SELECT * FROM " + tableName);
        viewConn.createStatement().execute("ALTER VIEW " + viewOfTable2 + " SET UPDATE_CACHE_FREQUENCY = 1");
        PhoenixConnection phoenixConn = conn.unwrap(PhoenixConnection.class);
        PTable table = phoenixConn.getTable(new PTableKey(null, tableName));
        PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
        assertFalse(table.isImmutableRows());
        assertEquals(2, table.getUpdateCacheFrequency());
        PTable viewTable1 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable1));
        assertFalse(viewTable1.isImmutableRows());
        assertEquals(2, viewTable1.getUpdateCacheFrequency());
        // query the view to force the table cache to be updated
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable2);
        PTable viewTable2 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable2));
        assertFalse(viewTable2.isImmutableRows());
        assertEquals(1, viewTable2.getUpdateCacheFrequency());
        conn.createStatement().execute("ALTER TABLE " + tableName + " SET IMMUTABLE_ROWS=true, UPDATE_CACHE_FREQUENCY=3");
        // query the views to force the table cache to be updated
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable1);
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable2);
        phoenixConn = conn.unwrap(PhoenixConnection.class);
        table = phoenixConn.getTable(new PTableKey(null, tableName));
        assertTrue(table.isImmutableRows());
        assertEquals(3, table.getUpdateCacheFrequency());
        viewTable1 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable1));
        assertTrue(viewTable1.isImmutableRows());
        assertEquals(3, viewTable1.getUpdateCacheFrequency());
        viewTable2 = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable2));
        assertTrue(viewTable2.isImmutableRows());
        // update cache frequency is not propagated to the view since it was altered on the view
        assertEquals(1, viewTable2.getUpdateCacheFrequency());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PName(org.apache.phoenix.schema.PName) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Example 4 with PTable

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

the class AlterTableIT method testClientCacheUpdatedOnChangingPhoenixTableProperties.

@Test
public void testClientCacheUpdatedOnChangingPhoenixTableProperties() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    try {
        String ddl = "create table " + dataTableFullName + " (" + " id char(1) NOT NULL," + " col1 integer NOT NULL," + " col2 bigint NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (id, col1, col2)" + " ) " + tableDDLOptions;
        conn.createStatement().execute(ddl);
        asssertIsWALDisabled(conn, dataTableFullName, false);
        ddl = "ALTER TABLE " + dataTableFullName + " SET DISABLE_WAL = true";
        conn.createStatement().execute(ddl);
        // check metadata cache is updated with DISABLE_WAL = true
        asssertIsWALDisabled(conn, dataTableFullName, true);
        ddl = "ALTER TABLE " + dataTableFullName + " SET DISABLE_WAL = false";
        conn.createStatement().execute(ddl);
        // check metadata cache is updated with DISABLE_WAL = false
        asssertIsWALDisabled(conn, dataTableFullName, false);
        ddl = "ALTER TABLE " + dataTableFullName + " SET MULTI_TENANT = true";
        conn.createStatement().execute(ddl);
        // check metadata cache is updated with MULTI_TENANT = true
        PTable t = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableFullName));
        assertTrue(t.isMultiTenant());
        // check table metadata updated server side
        ResultSet rs = conn.createStatement().executeQuery("SELECT DISABLE_WAL, MULTI_TENANT FROM \"SYSTEM\".\"CATALOG\"" + "WHERE table_name = '" + dataTableFullName + "' AND DISABLE_WAL IS NOT NULL AND MULTI_TENANT IS NOT NULL");
        assertTrue(rs.next());
        assertFalse(rs.getBoolean(1));
        assertTrue(rs.getBoolean(2));
        assertFalse(rs.next());
        rs.close();
    } finally {
        conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) ResultSet(java.sql.ResultSet) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 5 with PTable

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

the class AlterTableWithViewsIT method testAlterAppendOnlySchema.

@Test
public void testAlterAppendOnlySchema() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl());
        Connection viewConn = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : conn) {
        String baseTableName = "NONTXNTBL_" + generateUniqueName() + (isMultiTenant ? "0" : "1");
        String viewOfTable = baseTableName + "_VIEW";
        String ddl = "CREATE TABLE " + baseTableName + " (\n" + "%s ID VARCHAR(15) NOT NULL,\n" + " COL1 integer NOT NULL," + "CREATED_DATE DATE,\n" + "CONSTRAINT PK PRIMARY KEY (%s ID, COL1)) %s";
        conn.createStatement().execute(generateDDL(ddl));
        ddl = "CREATE VIEW " + viewOfTable + " AS SELECT * FROM " + baseTableName;
        viewConn.createStatement().execute(ddl);
        PhoenixConnection phoenixConn = conn.unwrap(PhoenixConnection.class);
        PTable table = phoenixConn.getTable(new PTableKey(null, baseTableName));
        PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
        assertFalse(table.isAppendOnlySchema());
        PTable viewTable = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable));
        assertFalse(viewTable.isAppendOnlySchema());
        try {
            viewConn.createStatement().execute("ALTER VIEW " + viewOfTable + " SET APPEND_ONLY_SCHEMA = true");
            fail();
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.CANNOT_ALTER_TABLE_PROPERTY_ON_VIEW.getErrorCode(), e.getErrorCode());
        }
        conn.createStatement().execute("ALTER TABLE " + baseTableName + " SET APPEND_ONLY_SCHEMA = true");
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable);
        phoenixConn = conn.unwrap(PhoenixConnection.class);
        table = phoenixConn.getTable(new PTableKey(null, baseTableName));
        assertTrue(table.isAppendOnlySchema());
        viewTable = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable));
        assertTrue(viewTable.isAppendOnlySchema());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) PName(org.apache.phoenix.schema.PName) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Aggregations

PTable (org.apache.phoenix.schema.PTable)153 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)63 PTableKey (org.apache.phoenix.schema.PTableKey)48 PColumn (org.apache.phoenix.schema.PColumn)47 Connection (java.sql.Connection)35 TableRef (org.apache.phoenix.schema.TableRef)29 SQLException (java.sql.SQLException)28 ArrayList (java.util.ArrayList)28 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)28 Test (org.junit.Test)27 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)24 Expression (org.apache.phoenix.expression.Expression)24 Scan (org.apache.hadoop.hbase.client.Scan)21 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)21 Properties (java.util.Properties)20 Mutation (org.apache.hadoop.hbase.client.Mutation)17 ColumnRef (org.apache.phoenix.schema.ColumnRef)16 IOException (java.io.IOException)15 Hint (org.apache.phoenix.parse.HintNode.Hint)14 PName (org.apache.phoenix.schema.PName)14