Search in sources :

Example 36 with PTable

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

the class QueryCompilerTest method testNullAlterTableDefaultRemoved.

@Test
public void testNullAlterTableDefaultRemoved() throws Exception {
    String ddl = "CREATE TABLE table_with_default (" + "pk INTEGER PRIMARY KEY)";
    String ddl2 = "ALTER TABLE table_with_default " + "ADD v CHAR(3) DEFAULT null";
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(ddl);
    conn.createStatement().execute(ddl2);
    PTable table = conn.unwrap(PhoenixConnection.class).getMetaDataCache().getTableRef(new PTableKey(null, "TABLE_WITH_DEFAULT")).getTable();
    assertNull(table.getColumnForColumnName("V").getExpressionStr());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) 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) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 37 with PTable

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

the class QueryCompilerTest method testNullDefaultRemoved.

@Test
public void testNullDefaultRemoved() throws Exception {
    String ddl = "CREATE TABLE table_with_default (" + "pk INTEGER PRIMARY KEY, " + "v VARCHAR DEFAULT null)";
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute(ddl);
    PTable table = conn.unwrap(PhoenixConnection.class).getMetaDataCache().getTableRef(new PTableKey(null, "TABLE_WITH_DEFAULT")).getTable();
    assertNull(table.getColumnForColumnName("V").getExpressionStr());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) 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) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 38 with PTable

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

the class ParallelIteratorsSplitTest method testGetSplitsWithSkipScanFilter.

@Test
public void testGetSplitsWithSkipScanFilter() throws Exception {
    byte[][] splits = new byte[][] { Ka1A, Ka1B, Ka1E, Ka1G, Ka1I, Ka2A };
    createTestTable(getUrl(), DDL, splits, null);
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), TABLE_NAME));
    TableRef tableRef = new TableRef(table);
    List<HRegionLocation> regions = pconn.getQueryServices().getAllTableRegions(tableRef.getTable().getPhysicalName().getBytes());
    List<KeyRange> ranges = getSplits(tableRef, scan, regions, scanRanges);
    assertEquals("Unexpected number of splits: " + ranges.size(), expectedSplits.size(), ranges.size());
    for (int i = 0; i < expectedSplits.size(); i++) {
        assertEquals(expectedSplits.get(i), ranges.get(i));
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) 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) TableRef(org.apache.phoenix.schema.TableRef) Test(org.junit.Test)

Example 39 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 40 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)

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