Search in sources :

Example 26 with PTableKey

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

the class ConnectionlessQueryServicesImpl method getFunctions.

@Override
public MetaDataMutationResult getFunctions(PName tenantId, List<Pair<byte[], Long>> functionNameAndTimeStampPairs, long clientTimestamp) throws SQLException {
    List<PFunction> functions = new ArrayList<PFunction>(functionNameAndTimeStampPairs.size());
    for (Pair<byte[], Long> functionInfo : functionNameAndTimeStampPairs) {
        try {
            PFunction function2 = metaData.getFunction(new PTableKey(tenantId, Bytes.toString(functionInfo.getFirst())));
            functions.add(function2);
        } catch (FunctionNotFoundException e) {
            return new MetaDataMutationResult(MutationCode.FUNCTION_NOT_FOUND, 0, null);
        }
    }
    if (functions.isEmpty()) {
        return null;
    }
    return new MetaDataMutationResult(MutationCode.FUNCTION_ALREADY_EXISTS, 0, functions, true);
}
Also used : FunctionNotFoundException(org.apache.phoenix.schema.FunctionNotFoundException) PFunction(org.apache.phoenix.parse.PFunction) ArrayList(java.util.ArrayList) PTableKey(org.apache.phoenix.schema.PTableKey) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)

Example 27 with PTableKey

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

the class ViewCompilerTest method assertViewType.

public void assertViewType(String[] viewNames, String[] viewDDLs, ViewType viewType) throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    String ct = "CREATE TABLE t (k1 INTEGER NOT NULL, k2 VARCHAR, v VARCHAR, CONSTRAINT pk PRIMARY KEY (k1,k2))";
    conn.createStatement().execute(ct);
    for (String viewDDL : viewDDLs) {
        conn.createStatement().execute(viewDDL);
    }
    StringBuilder buf = new StringBuilder();
    int count = 0;
    for (String view : viewNames) {
        PTable table = conn.getTable(new PTableKey(null, view));
        assertEquals(viewType, table.getViewType());
        conn.createStatement().execute("DROP VIEW " + table.getName().getString());
        buf.append(' ');
        buf.append(table.getName().getString());
        count++;
    }
    assertEquals("Expected " + viewDDLs.length + ", but got " + count + ":" + buf.toString(), viewDDLs.length, count);
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Example 28 with PTableKey

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

the class SetPropertyIT method assertImmutableRows.

private static 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 29 with PTableKey

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

the class StoreNullsIT method ensureNullsStoredCorrectly.

private void ensureNullsStoredCorrectly(Connection conn) throws Exception {
    ResultSet rs1 = conn.createStatement().executeQuery("SELECT NAME FROM " + dataTableName);
    rs1.next();
    assertEquals("v1", rs1.getString(1));
    rs1.next();
    assertNull(rs1.getString(1));
    rs1.next();
    HTable htable = new HTable(getUtility().getConfiguration(), dataTableName);
    Scan s = new Scan();
    s.setRaw(true);
    ResultScanner scanner = htable.getScanner(s);
    // first row has a value for name
    Result rs = scanner.next();
    PTable table = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableName));
    PColumn nameColumn = table.getColumnForColumnName("NAME");
    byte[] qualifier = table.getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS ? QueryConstants.SINGLE_KEYVALUE_COLUMN_QUALIFIER_BYTES : nameColumn.getColumnQualifierBytes();
    assertTrue(rs.containsColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, qualifier));
    // 2 because it also includes the empty key value column
    assertTrue(rs.size() == 2);
    KeyValueColumnExpression colExpression = table.getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS ? new SingleCellColumnExpression(nameColumn, "NAME", table.getEncodingScheme(), table.getImmutableStorageScheme()) : new KeyValueColumnExpression(nameColumn);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    colExpression.evaluate(new ResultTuple(rs), ptr);
    assertEquals(new ImmutableBytesPtr(PVarchar.INSTANCE.toBytes("v1")), ptr);
    rs = scanner.next();
    if (// we don't issue a put with empty value for immutable tables with cols stored per key value
    !mutable && !columnEncoded || (mutable && !storeNulls)) {
        // for this case we use a delete to represent the null
        assertFalse(rs.containsColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, qualifier));
        assertEquals(1, rs.size());
    } else {
        assertTrue(rs.containsColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, qualifier));
        assertEquals(2, rs.size());
    }
    // assert null stored correctly
    ptr = new ImmutableBytesPtr();
    if (colExpression.evaluate(new ResultTuple(rs), ptr)) {
        assertEquals(new ImmutableBytesPtr(ByteUtil.EMPTY_BYTE_ARRAY), ptr);
    }
    assertNull(scanner.next());
    scanner.close();
    htable.close();
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) 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) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) ResultSet(java.sql.ResultSet) Scan(org.apache.hadoop.hbase.client.Scan) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 30 with PTableKey

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

the class ConnectionQueryServicesImpl method metaDataMutated.

/**
 * Ensures that metaData mutations are handled in the correct order
 */
private PMetaData metaDataMutated(PName tenantId, String tableName, long tableSeqNum, Mutator mutator) throws SQLException {
    synchronized (latestMetaDataLock) {
        throwConnectionClosedIfNullMetaData();
        PMetaData metaData = latestMetaData;
        PTable table;
        long endTime = System.currentTimeMillis() + DEFAULT_OUT_OF_ORDER_MUTATIONS_WAIT_TIME_MS;
        while (true) {
            try {
                try {
                    table = metaData.getTableRef(new PTableKey(tenantId, tableName)).getTable();
                    /* If the table is at the prior sequence number, then we're good to go.
                         * We know if we've got this far, that the server validated the mutations,
                         * so we'd just need to wait until the other connection that mutated the same
                         * table is processed.
                         */
                    if (table.getSequenceNumber() + 1 == tableSeqNum) {
                        // TODO: assert that timeStamp is bigger that table timeStamp?
                        mutator.mutate(metaData);
                        break;
                    } else if (table.getSequenceNumber() >= tableSeqNum) {
                        logger.warn("Attempt to cache older version of " + tableName + ": current= " + table.getSequenceNumber() + ", new=" + tableSeqNum);
                        break;
                    }
                } catch (TableNotFoundException e) {
                }
                long waitTime = endTime - System.currentTimeMillis();
                // and the next time it's used it'll be pulled over from the server.
                if (waitTime <= 0) {
                    logger.warn("Unable to update meta data repo within " + (DEFAULT_OUT_OF_ORDER_MUTATIONS_WAIT_TIME_MS / 1000) + " seconds for " + tableName);
                    // There will never be a parentTableName here, as that would only
                    // be non null for an index an we never add/remove columns from an index.
                    metaData.removeTable(tenantId, tableName, null, HConstants.LATEST_TIMESTAMP);
                    break;
                }
                latestMetaDataLock.wait(waitTime);
            } catch (InterruptedException e) {
                // restore the interrupt status
                Thread.currentThread().interrupt();
                throw new SQLExceptionInfo.Builder(SQLExceptionCode.INTERRUPTED_EXCEPTION).setRootCause(e).build().buildException();
            }
        }
        latestMetaData = metaData;
        latestMetaDataLock.notifyAll();
        return metaData;
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PMetaData(org.apache.phoenix.schema.PMetaData) KeyValueBuilder(org.apache.phoenix.hbase.index.util.KeyValueBuilder) NonTxIndexBuilder(org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) 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