Search in sources :

Example 46 with PTableKey

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

the class ConnectionQueryServicesImpl method addColumnQualifierColumn.

// Special method for adding the column qualifier column for 4.10. 
private PhoenixConnection addColumnQualifierColumn(PhoenixConnection oldMetaConnection, Long timestamp) throws SQLException {
    Properties props = PropertiesUtil.deepCopy(oldMetaConnection.getClientInfo());
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(timestamp));
    // Cannot go through DriverManager or you end up in an infinite loop because it'll call init again
    PhoenixConnection metaConnection = new PhoenixConnection(oldMetaConnection, this, props);
    PTable sysCatalogPTable = metaConnection.getTable(new PTableKey(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME));
    int numColumns = sysCatalogPTable.getColumns().size();
    try (PreparedStatement mutateTable = metaConnection.prepareStatement(MetaDataClient.MUTATE_TABLE)) {
        mutateTable.setString(1, null);
        mutateTable.setString(2, SYSTEM_CATALOG_SCHEMA);
        mutateTable.setString(3, SYSTEM_CATALOG_TABLE);
        mutateTable.setString(4, PTableType.SYSTEM.getSerializedValue());
        mutateTable.setLong(5, sysCatalogPTable.getSequenceNumber() + 1);
        mutateTable.setInt(6, numColumns + 1);
        mutateTable.execute();
    }
    List<Mutation> tableMetadata = new ArrayList<>();
    tableMetadata.addAll(metaConnection.getMutationState().toMutations(metaConnection.getSCN()).next().getSecond());
    metaConnection.rollback();
    PColumn column = new PColumnImpl(PNameFactory.newName("COLUMN_QUALIFIER"), PNameFactory.newName(DEFAULT_COLUMN_FAMILY_NAME), PVarbinary.INSTANCE, null, null, true, numColumns, SortOrder.ASC, null, null, false, null, false, false, Bytes.toBytes("COLUMN_QUALIFIER"));
    String upsertColumnMetadata = "UPSERT INTO " + SYSTEM_CATALOG_SCHEMA + ".\"" + SYSTEM_CATALOG_TABLE + "\"( " + TENANT_ID + "," + TABLE_SCHEM + "," + TABLE_NAME + "," + COLUMN_NAME + "," + COLUMN_FAMILY + "," + DATA_TYPE + "," + NULLABLE + "," + COLUMN_SIZE + "," + DECIMAL_DIGITS + "," + ORDINAL_POSITION + "," + SORT_ORDER + "," + DATA_TABLE_NAME + "," + ARRAY_SIZE + "," + VIEW_CONSTANT + "," + IS_VIEW_REFERENCED + "," + PK_NAME + "," + KEY_SEQ + "," + COLUMN_DEF + "," + IS_ROW_TIMESTAMP + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    try (PreparedStatement colUpsert = metaConnection.prepareStatement(upsertColumnMetadata)) {
        colUpsert.setString(1, null);
        colUpsert.setString(2, SYSTEM_CATALOG_SCHEMA);
        colUpsert.setString(3, SYSTEM_CATALOG_TABLE);
        colUpsert.setString(4, "COLUMN_QUALIFIER");
        colUpsert.setString(5, DEFAULT_COLUMN_FAMILY);
        colUpsert.setInt(6, column.getDataType().getSqlType());
        colUpsert.setInt(7, ResultSetMetaData.columnNullable);
        colUpsert.setNull(8, Types.INTEGER);
        colUpsert.setNull(9, Types.INTEGER);
        colUpsert.setInt(10, sysCatalogPTable.getBucketNum() != null ? numColumns : (numColumns + 1));
        colUpsert.setInt(11, SortOrder.ASC.getSystemValue());
        colUpsert.setString(12, null);
        colUpsert.setNull(13, Types.INTEGER);
        colUpsert.setBytes(14, null);
        colUpsert.setBoolean(15, false);
        colUpsert.setString(16, sysCatalogPTable.getPKName() == null ? null : sysCatalogPTable.getPKName().getString());
        colUpsert.setNull(17, Types.SMALLINT);
        colUpsert.setNull(18, Types.VARCHAR);
        colUpsert.setBoolean(19, false);
        colUpsert.execute();
    }
    tableMetadata.addAll(metaConnection.getMutationState().toMutations(metaConnection.getSCN()).next().getSecond());
    metaConnection.rollback();
    metaConnection.getQueryServices().addColumn(tableMetadata, sysCatalogPTable, Collections.<String, List<Pair<String, Object>>>emptyMap(), Collections.<String>emptySet(), Lists.newArrayList(column));
    metaConnection.removeTable(null, SYSTEM_CATALOG_NAME, null, timestamp);
    ConnectionQueryServicesImpl.this.removeTable(null, SYSTEM_CATALOG_NAME, null, timestamp);
    clearCache();
    return metaConnection;
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PColumnImpl(org.apache.phoenix.schema.PColumnImpl) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) PTable(org.apache.phoenix.schema.PTable) PTinyint(org.apache.phoenix.schema.types.PTinyint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) MultiRowMutationEndpoint(org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint) PColumn(org.apache.phoenix.schema.PColumn) Mutation(org.apache.hadoop.hbase.client.Mutation) PTableKey(org.apache.phoenix.schema.PTableKey) Pair(org.apache.hadoop.hbase.util.Pair)

Example 47 with PTableKey

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

the class ConnectionlessQueryServicesImpl method getTable.

@Override
public MetaDataMutationResult getTable(PName tenantId, byte[] schemaBytes, byte[] tableBytes, long tableTimestamp, long clientTimestamp) throws SQLException {
    // to get anything from the server (since we don't have a connection)
    try {
        String fullTableName = SchemaUtil.getTableName(schemaBytes, tableBytes);
        PTable table = metaData.getTableRef(new PTableKey(tenantId, fullTableName)).getTable();
        return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, 0, table, true);
    } catch (TableNotFoundException e) {
        return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, 0, null);
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PTableKey(org.apache.phoenix.schema.PTableKey) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) PTable(org.apache.phoenix.schema.PTable)

Example 48 with PTableKey

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

the class BaseConnectionlessQueryTest method doSetup.

@BeforeClass
public static void doSetup() throws Exception {
    startServer(getUrl());
    ensureTableCreated(getUrl(), ATABLE_NAME);
    ensureTableCreated(getUrl(), ENTITY_HISTORY_TABLE_NAME);
    ensureTableCreated(getUrl(), FUNKY_NAME);
    ensureTableCreated(getUrl(), PTSDB_NAME);
    ensureTableCreated(getUrl(), PTSDB2_NAME);
    ensureTableCreated(getUrl(), PTSDB3_NAME);
    ensureTableCreated(getUrl(), MULTI_CF_NAME);
    ensureTableCreated(getUrl(), TABLE_WITH_ARRAY);
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(HConstants.LATEST_TIMESTAMP));
    PhoenixConnection conn = DriverManager.getConnection(PHOENIX_CONNECTIONLESS_JDBC_URL, props).unwrap(PhoenixConnection.class);
    try {
        PTable table = conn.getTable(new PTableKey(null, ATABLE_NAME));
        ATABLE = table;
        ORGANIZATION_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ORGANIZATION_ID").getPosition()).newColumnExpression();
        ENTITY_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ENTITY_ID").getPosition()).newColumnExpression();
        A_INTEGER = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression();
        A_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression();
        B_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("B_STRING").getPosition()).newColumnExpression();
        A_DATE = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_DATE").getPosition()).newColumnExpression();
        A_TIME = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIME").getPosition()).newColumnExpression();
        A_TIMESTAMP = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIMESTAMP").getPosition()).newColumnExpression();
        X_DECIMAL = new ColumnRef(new TableRef(table), table.getColumnForColumnName("X_DECIMAL").getPosition()).newColumnExpression();
    } finally {
        conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ColumnRef(org.apache.phoenix.schema.ColumnRef) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) TableRef(org.apache.phoenix.schema.TableRef) BeforeClass(org.junit.BeforeClass)

Example 49 with PTableKey

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

the class LocalIndexIT method testLocalIndexCreationWithSplitsShouldFail.

@Test
public void testLocalIndexCreationWithSplitsShouldFail() 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)" + " split on (1,2,3)");
        fail("Local index cannot be pre-split");
    } 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)

Example 50 with PTableKey

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

the class LocalIndexIT method testLocalIndexRoundTrip.

@Ignore
public //FIXME: PHOENIX-3496 
void testLocalIndexRoundTrip() throws Exception {
    String tableName = schemaName + "." + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    String indexTableName = schemaName + "." + indexName;
    createBaseTable(tableName, null, null);
    Connection conn1 = DriverManager.getConnection(getUrl());
    conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " + tableName + "(v1)");
    conn1.createStatement().executeQuery("SELECT * FROM " + tableName).next();
    PTable localIndex = conn1.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, indexTableName));
    assertEquals(IndexType.LOCAL, localIndex.getIndexType());
    assertNotNull(localIndex.getViewIndexId());
    String tableName2 = "test_table" + generateUniqueName();
    String indexName2 = "idx_test_table" + generateUniqueName();
    String createTable = "CREATE TABLE IF NOT EXISTS " + tableName2 + " (user_time UNSIGNED_TIMESTAMP NOT NULL,user_id varchar NOT NULL,col1 varchar,col2 double," + "CONSTRAINT pk PRIMARY KEY(user_time,user_id)) SALT_BUCKETS = 20";
    conn1.createStatement().execute(createTable);
    conn1.createStatement().execute("CREATE local INDEX IF NOT EXISTS " + indexName2 + " on " + tableName2 + "(\"HOUR\"(user_time))");
    conn1.createStatement().execute("upsert into " + tableName2 + " values(TO_TIME('2005-10-01 14:03:22.559'), 'foo')");
    conn1.commit();
    ResultSet rs = conn1.createStatement().executeQuery("select substr(to_char(user_time), 0, 10) as ddate, \"HOUR\"(user_time) as hhour, user_id, col1,col2 from " + tableName2 + " where \"HOUR\"(user_time)=14 group by user_id, col1, col2, ddate, hhour limit 1");
    assertTrue(rs.next());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Ignore(org.junit.Ignore)

Aggregations

PTableKey (org.apache.phoenix.schema.PTableKey)69 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)58 PTable (org.apache.phoenix.schema.PTable)48 Connection (java.sql.Connection)37 Test (org.junit.Test)29 Properties (java.util.Properties)18 PColumn (org.apache.phoenix.schema.PColumn)15 SQLException (java.sql.SQLException)11 PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)10 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)9 PName (org.apache.phoenix.schema.PName)9 Scan (org.apache.hadoop.hbase.client.Scan)8 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)7 BaseTest (org.apache.phoenix.query.BaseTest)7 Statement (java.sql.Statement)5 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)5 Result (org.apache.hadoop.hbase.client.Result)5 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)5