Search in sources :

Example 16 with PTableKey

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

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

Example 18 with PTableKey

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

the class AlterTableWithViewsIT method testAlterTablePropertyOnView.

@Test
public void testAlterTablePropertyOnView() 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);
        try {
            viewConn.createStatement().execute("ALTER VIEW " + viewOfTable + " SET IMMUTABLE_ROWS = true");
            fail();
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.CANNOT_ALTER_TABLE_PROPERTY_ON_VIEW.getErrorCode(), e.getErrorCode());
        }
        viewConn.createStatement().execute("ALTER VIEW " + viewOfTable + " SET UPDATE_CACHE_FREQUENCY = 100");
        viewConn.createStatement().execute("SELECT * FROM " + viewOfTable);
        PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
        assertEquals(100, viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable)).getUpdateCacheFrequency());
        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());
        }
    }
}
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) Test(org.junit.Test)

Example 19 with PTableKey

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

the class ColumnEncodedBytesPropIT method testValidateProperty.

@Test
public void testValidateProperty() throws SQLException {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String dataTableFullName1 = SchemaUtil.getTableName("", generateUniqueName());
    String dataTableFullName2 = SchemaUtil.getTableName("", generateUniqueName());
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        Statement stmt = conn.createStatement();
        String ddl = "CREATE TABLE  " + dataTableFullName1 + "  (id varchar not null, val varchar " + "  CONSTRAINT pk PRIMARY KEY (id)) COLUMN_ENCODED_BYTES=4";
        stmt.execute(ddl);
        ddl = "CREATE TABLE  " + dataTableFullName2 + "  (id varchar not null, val varchar " + "  CONSTRAINT pk PRIMARY KEY (id)) COLUMN_ENCODED_BYTES=NONE";
        stmt.execute(ddl);
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable dataTable1 = phxConn.getTable(new PTableKey(null, dataTableFullName1));
        assertEquals("Encoding scheme set incorrectly", QualifierEncodingScheme.FOUR_BYTE_QUALIFIERS, dataTable1.getEncodingScheme());
        PTable dataTable2 = phxConn.getTable(new PTableKey(null, dataTableFullName2));
        assertEquals("Encoding scheme set incorrectly", QualifierEncodingScheme.NON_ENCODED_QUALIFIERS, dataTable2.getEncodingScheme());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Statement(java.sql.Statement) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Example 20 with PTableKey

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

the class AlterMultiTenantTableWithViewsIT method checkColumnPartOfPk.

private boolean checkColumnPartOfPk(PhoenixConnection conn, String columnName, String tableName) throws SQLException {
    String normalizedTableName = SchemaUtil.normalizeIdentifier(tableName);
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), normalizedTableName));
    List<PColumn> pkCols = table.getPKColumns();
    String normalizedColumnName = SchemaUtil.normalizeIdentifier(columnName);
    for (PColumn pkCol : pkCols) {
        if (pkCol.getName().getString().equals(normalizedColumnName)) {
            return true;
        }
    }
    return false;
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) 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