Search in sources :

Example 1 with SingleCellColumnExpression

use of org.apache.phoenix.expression.SingleCellColumnExpression 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 2 with SingleCellColumnExpression

use of org.apache.phoenix.expression.SingleCellColumnExpression in project phoenix by apache.

the class DropColumnIT method testDroppingIndexedColDropsIndex.

@Test
public void testDroppingIndexedColDropsIndex() throws Exception {
    String indexTableName = generateUniqueName();
    String dataTableFullName = SchemaUtil.getTableName(SCHEMA_NAME, generateUniqueName());
    String localIndexTableName1 = "LOCAL_" + indexTableName + "_1";
    String localIndexTableName2 = "LOCAL_" + indexTableName + "_2";
    try (Connection conn = getConnection()) {
        conn.setAutoCommit(false);
        conn.createStatement().execute("CREATE TABLE " + dataTableFullName + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) " + tableDDLOptions);
        // create one regular and two local indexes
        conn.createStatement().execute("CREATE INDEX " + indexTableName + " ON " + dataTableFullName + " (v2) INCLUDE (v1)");
        conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName1 + " ON " + dataTableFullName + " (v2) INCLUDE (v1)");
        conn.createStatement().execute("CREATE LOCAL INDEX " + localIndexTableName2 + " ON " + dataTableFullName + " (k) INCLUDE (v1)");
        // upsert a single row
        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + dataTableFullName + " VALUES(?,?,?)");
        stmt.setString(1, "a");
        stmt.setString(2, "x");
        stmt.setString(3, "1");
        stmt.execute();
        conn.commit();
        // verify the indexes were created
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable dataTable = pconn.getTable(new PTableKey(null, dataTableFullName));
        assertEquals("Unexpected number of indexes ", 3, dataTable.getIndexes().size());
        PTable indexTable = dataTable.getIndexes().get(0);
        byte[] indexTablePhysicalName = indexTable.getPhysicalName().getBytes();
        PName localIndexTablePhysicalName = dataTable.getIndexes().get(1).getPhysicalName();
        // drop v2 which causes the regular index and first local index to be dropped
        conn.createStatement().execute("ALTER TABLE " + dataTableFullName + " DROP COLUMN v2 ");
        // verify the both of the indexes' metadata were dropped
        conn.createStatement().execute("SELECT * FROM " + dataTableFullName);
        try {
            conn.createStatement().execute("SELECT * FROM " + indexTableName);
            fail("Index should have been dropped");
        } catch (TableNotFoundException e) {
        }
        pconn = conn.unwrap(PhoenixConnection.class);
        dataTable = pconn.getTable(new PTableKey(null, dataTableFullName));
        try {
            pconn.getTable(new PTableKey(null, indexTableName));
            fail("index should have been dropped");
        } catch (TableNotFoundException e) {
        }
        try {
            pconn.getTable(new PTableKey(null, localIndexTableName1));
            fail("index should have been dropped");
        } catch (TableNotFoundException e) {
        }
        assertEquals("Unexpected number of indexes ", 1, dataTable.getIndexes().size());
        // verify that the regular index physical table was dropped
        try {
            conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(indexTablePhysicalName);
            fail("Index table should have been dropped");
        } catch (TableNotFoundException e) {
        }
        // verify that the local index physical table was *not* dropped
        conn.unwrap(PhoenixConnection.class).getQueryServices().getTableDescriptor(localIndexTablePhysicalName.getBytes());
        PTable localIndex2 = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, localIndexTableName2));
        // there should be a single row belonging to localIndexTableName2
        Scan scan = new Scan();
        scan.addFamily(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES);
        HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(localIndexTablePhysicalName.getBytes());
        ResultScanner results = table.getScanner(scan);
        Result result = results.next();
        assertNotNull(result);
        String indexColumnName = IndexUtil.getIndexColumnName(QueryConstants.DEFAULT_COLUMN_FAMILY, "V1");
        PColumn localIndexCol = localIndex2.getColumnForColumnName(indexColumnName);
        byte[] colValue;
        if (!mutable && columnEncoded) {
            KeyValueColumnExpression colExpression = new SingleCellColumnExpression(localIndexCol, indexColumnName, localIndex2.getEncodingScheme(), localIndex2.getImmutableStorageScheme());
            ImmutableBytesPtr ptr = new ImmutableBytesPtr();
            colExpression.evaluate(new ResultTuple(result), ptr);
            colValue = ptr.copyBytesIfNecessary();
        } else {
            colValue = result.getValue(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES, localIndexCol.getColumnQualifierBytes());
        }
        assertNotNull("localIndexTableName2 row is missing", colValue);
        assertNull(results.next());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PreparedStatement(java.sql.PreparedStatement) 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) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) PName(org.apache.phoenix.schema.PName) Scan(org.apache.hadoop.hbase.client.Scan) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test)

Example 3 with SingleCellColumnExpression

use of org.apache.phoenix.expression.SingleCellColumnExpression in project phoenix by apache.

the class DropColumnIT method verifyColValue.

private void verifyColValue(String indexTableName, String dataTableName, Connection conn, PTable dataTable, PColumn dataColumn, byte[] dataCq, PTable globalIndexTable, PColumn glovalIndexCol, byte[] globalIndexCq, PTable localIndexTable, PColumn localIndexCol, byte[] localIndexCq) throws SQLException, IOException, ArrayComparisonFailure {
    // key value for v2 should exist in the data table
    Scan scan = new Scan();
    scan.setRaw(true);
    byte[] key = Bytes.toBytes("a");
    scan.setStartRow(key);
    scan.setStopRow(key);
    HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(dataTableName.getBytes());
    ResultScanner results = table.getScanner(scan);
    Result result = results.next();
    assertNotNull(result);
    byte[] colValue;
    if (!mutable && columnEncoded) {
        KeyValueColumnExpression colExpression = new SingleCellColumnExpression(dataColumn, "V2", dataTable.getEncodingScheme(), dataTable.getImmutableStorageScheme());
        ImmutableBytesPtr ptr = new ImmutableBytesPtr();
        colExpression.evaluate(new ResultTuple(result), ptr);
        colValue = ptr.copyBytesIfNecessary();
    } else {
        colValue = result.getValue(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, dataCq);
    }
    assertArrayEquals("wrong column value for v2", Bytes.toBytes("1"), colValue);
    assertNull(results.next());
    // key value for v2 should exist in the global index table
    scan = new Scan();
    scan.setRaw(true);
    table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(indexTableName.getBytes());
    results = table.getScanner(scan);
    result = results.next();
    assertNotNull(result);
    if (!mutable && columnEncoded) {
        KeyValueColumnExpression colExpression = new SingleCellColumnExpression(glovalIndexCol, "0:V2", globalIndexTable.getEncodingScheme(), globalIndexTable.getImmutableStorageScheme());
        ImmutableBytesPtr ptr = new ImmutableBytesPtr();
        colExpression.evaluate(new ResultTuple(result), ptr);
        colValue = ptr.copyBytesIfNecessary();
    } else {
        colValue = result.getValue(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, globalIndexCq);
    }
    assertArrayEquals("wrong column value for v2", Bytes.toBytes("1"), colValue);
    assertNull(results.next());
    // key value for v2 should exist in the local index table
    scan = new Scan();
    scan.setRaw(true);
    scan.addFamily(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES);
    table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(dataTableName.getBytes());
    results = table.getScanner(scan);
    result = results.next();
    assertNotNull(result);
    if (!mutable && columnEncoded) {
        KeyValueColumnExpression colExpression = new SingleCellColumnExpression(localIndexCol, "0:V2", localIndexTable.getEncodingScheme(), localIndexTable.getImmutableStorageScheme());
        ImmutableBytesPtr ptr = new ImmutableBytesPtr();
        assertTrue(colExpression.evaluate(new ResultTuple(result), ptr));
        colValue = ptr.copyBytesIfNecessary();
    } else {
        colValue = result.getValue(QueryConstants.DEFAULT_LOCAL_INDEX_COLUMN_FAMILY_BYTES, localIndexCq);
    }
    assertArrayEquals("wrong column value for v2", Bytes.toBytes("1"), colValue);
    assertNull(results.next());
}
Also used : SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple) Scan(org.apache.hadoop.hbase.client.Scan) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) HTable(org.apache.hadoop.hbase.client.HTable) Result(org.apache.hadoop.hbase.client.Result)

Example 4 with SingleCellColumnExpression

use of org.apache.phoenix.expression.SingleCellColumnExpression in project phoenix by apache.

the class NonAggregateRegionScannerFactory method deserializeArrayPostionalExpressionInfoFromScan.

private Expression[] deserializeArrayPostionalExpressionInfoFromScan(Scan scan, RegionScanner s, Set<KeyValueColumnExpression> arrayKVRefs) {
    byte[] specificArrayIdx = scan.getAttribute(BaseScannerRegionObserver.SPECIFIC_ARRAY_INDEX);
    if (specificArrayIdx == null) {
        return null;
    }
    KeyValueSchema.KeyValueSchemaBuilder builder = new KeyValueSchema.KeyValueSchemaBuilder(0);
    ByteArrayInputStream stream = new ByteArrayInputStream(specificArrayIdx);
    try {
        DataInputStream input = new DataInputStream(stream);
        int arrayKVRefSize = WritableUtils.readVInt(input);
        for (int i = 0; i < arrayKVRefSize; i++) {
            PTable.ImmutableStorageScheme scheme = EncodedColumnsUtil.getImmutableStorageScheme(scan);
            KeyValueColumnExpression kvExp = scheme != PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN ? new SingleCellColumnExpression(scheme) : new KeyValueColumnExpression();
            kvExp.readFields(input);
            arrayKVRefs.add(kvExp);
        }
        int arrayKVFuncSize = WritableUtils.readVInt(input);
        Expression[] arrayFuncRefs = new Expression[arrayKVFuncSize];
        for (int i = 0; i < arrayKVFuncSize; i++) {
            ArrayIndexFunction arrayIdxFunc = new ArrayIndexFunction();
            arrayIdxFunc.readFields(input);
            arrayFuncRefs[i] = arrayIdxFunc;
            builder.addField(arrayIdxFunc);
        }
        kvSchema = builder.build();
        kvSchemaBitSet = ValueBitSet.newInstance(kvSchema);
        return arrayFuncRefs;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ArrayIndexFunction(org.apache.phoenix.expression.function.ArrayIndexFunction) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) PTable(org.apache.phoenix.schema.PTable) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) Expression(org.apache.phoenix.expression.Expression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) OrderByExpression(org.apache.phoenix.expression.OrderByExpression) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema)

Example 5 with SingleCellColumnExpression

use of org.apache.phoenix.expression.SingleCellColumnExpression in project phoenix by apache.

the class IndexUtil method getTupleProjector.

public static TupleProjector getTupleProjector(Scan scan, ColumnReference[] dataColumns) {
    if (dataColumns != null && dataColumns.length != 0) {
        KeyValueSchema keyValueSchema = deserializeLocalIndexJoinSchemaFromScan(scan);
        boolean storeColsInSingleCell = scan.getAttribute(BaseScannerRegionObserver.COLUMNS_STORED_IN_SINGLE_CELL) != null;
        QualifierEncodingScheme encodingScheme = EncodedColumnsUtil.getQualifierEncodingScheme(scan);
        ImmutableStorageScheme immutableStorageScheme = EncodedColumnsUtil.getImmutableStorageScheme(scan);
        Expression[] colExpressions = storeColsInSingleCell ? new SingleCellColumnExpression[dataColumns.length] : new KeyValueColumnExpression[dataColumns.length];
        for (int i = 0; i < dataColumns.length; i++) {
            byte[] family = dataColumns[i].getFamily();
            byte[] qualifier = dataColumns[i].getQualifier();
            Field field = keyValueSchema.getField(i);
            Expression dataColumnExpr = storeColsInSingleCell ? new SingleCellColumnExpression(field, family, qualifier, encodingScheme, immutableStorageScheme) : new KeyValueColumnExpression(field, family, qualifier);
            colExpressions[i] = dataColumnExpr;
        }
        return new TupleProjector(keyValueSchema, colExpressions);
    }
    return null;
}
Also used : Field(org.apache.phoenix.schema.ValueSchema.Field) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) QualifierEncodingScheme(org.apache.phoenix.schema.PTable.QualifierEncodingScheme) Expression(org.apache.phoenix.expression.Expression) RowKeyColumnExpression(org.apache.phoenix.expression.RowKeyColumnExpression) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) SingleCellColumnExpression(org.apache.phoenix.expression.SingleCellColumnExpression) TupleProjector(org.apache.phoenix.execute.TupleProjector) ImmutableStorageScheme(org.apache.phoenix.schema.PTable.ImmutableStorageScheme) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema)

Aggregations

KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)7 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)7 Expression (org.apache.phoenix.expression.Expression)4 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)4 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)4 HTable (org.apache.hadoop.hbase.client.HTable)3 Result (org.apache.hadoop.hbase.client.Result)3 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)3 Scan (org.apache.hadoop.hbase.client.Scan)3 PTable (org.apache.phoenix.schema.PTable)3 ResultTuple (org.apache.phoenix.schema.tuple.ResultTuple)3 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)2 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)2 KeyValueSchema (org.apache.phoenix.schema.KeyValueSchema)2 PColumn (org.apache.phoenix.schema.PColumn)2 PTableKey (org.apache.phoenix.schema.PTableKey)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1