Search in sources :

Example 76 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class ImmutableStorageSchemeTest method testLeadingNulls.

@Test
public void testLeadingNulls() throws Exception {
    List<Expression> children = Lists.newArrayListWithExpectedSize(4);
    LiteralExpression nullExpression = LiteralExpression.newConstant(null);
    children.add(nullExpression);
    children.add(nullExpression);
    children.add(LiteralExpression.newConstant(BYTE_ARRAY1, PVarbinary.INSTANCE));
    children.add(LiteralExpression.newConstant(BYTE_ARRAY2, PVarbinary.INSTANCE));
    SingleCellConstructorExpression singleCellConstructorExpression = new SingleCellConstructorExpression(immutableStorageScheme, children);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    singleCellConstructorExpression.evaluate(null, ptr);
    ImmutableBytesPtr ptrCopy = new ImmutableBytesPtr(ptr);
    ColumnValueDecoder decoder = immutableStorageScheme.getDecoder();
    assertTrue(decoder.decode(ptrCopy, 0));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 1));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 2));
    assertArrayEquals(BYTE_ARRAY1, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 3));
    assertArrayEquals(BYTE_ARRAY2, ptrCopy.copyBytesIfNecessary());
}
Also used : SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) Expression(org.apache.phoenix.expression.Expression) DelegateExpression(org.apache.phoenix.expression.DelegateExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Test(org.junit.Test)

Example 77 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class ImmutableStorageSchemeTest method testWithExpressionsThatEvaluatetoFalse.

@Test
public void testWithExpressionsThatEvaluatetoFalse() throws Exception {
    List<Expression> children = Lists.newArrayListWithExpectedSize(4);
    children.add(CONSTANT_EXPRESSION);
    children.add(FALSE_EVAL_EXPRESSION);
    children.add(LiteralExpression.newConstant(BYTE_ARRAY1, PVarbinary.INSTANCE));
    children.add(FALSE_EVAL_EXPRESSION);
    children.add(LiteralExpression.newConstant(BYTE_ARRAY2, PVarbinary.INSTANCE));
    SingleCellConstructorExpression singleCellConstructorExpression = new SingleCellConstructorExpression(immutableStorageScheme, children);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    singleCellConstructorExpression.evaluate(null, ptr);
    ImmutableBytesPtr ptrCopy = new ImmutableBytesPtr(ptr);
    ColumnValueDecoder decoder = immutableStorageScheme.getDecoder();
    assertTrue(decoder.decode(ptrCopy, 0));
    assertArrayEquals(QueryConstants.EMPTY_COLUMN_VALUE_BYTES, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertFalse(decoder.decode(ptrCopy, 1));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 2));
    assertArrayEquals(BYTE_ARRAY1, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertFalse(decoder.decode(ptrCopy, 3));
    assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptrCopy.copyBytesIfNecessary());
    ptrCopy = new ImmutableBytesPtr(ptr);
    assertTrue(decoder.decode(ptrCopy, 4));
    assertArrayEquals(BYTE_ARRAY2, ptrCopy.copyBytesIfNecessary());
}
Also used : SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) SingleCellConstructorExpression(org.apache.phoenix.expression.SingleCellConstructorExpression) Expression(org.apache.phoenix.expression.Expression) DelegateExpression(org.apache.phoenix.expression.DelegateExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Test(org.junit.Test)

Example 78 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class PCharPadTest method testRelativeByteArrayOrder.

@Test
public void testRelativeByteArrayOrder() throws SQLException {
    String[] inputs = { "foo", "foo!", "fooA", "foo~" };
    PDataType dataType = PChar.INSTANCE;
    Arrays.sort(inputs);
    List<byte[]> ascOrderedInputs = new ArrayList<>(inputs.length);
    SortOrder sortOrder = SortOrder.ASC;
    for (String input : inputs) {
        LiteralExpression expr = LiteralExpression.newConstant(input, dataType, sortOrder);
        ImmutableBytesPtr ptr = new ImmutableBytesPtr(expr.getBytes());
        dataType.pad(ptr, 8, sortOrder);
        ascOrderedInputs.add(ptr.copyBytes());
    }
    Collections.sort(ascOrderedInputs, Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < inputs.length; i++) {
        byte[] bytes = ascOrderedInputs.get(i);
        String resultValue = (String) dataType.toObject(bytes, 0, bytes.length, dataType, sortOrder);
        assertEquals(inputs[i], resultValue);
    }
    List<byte[]> descOrderedInputs = new ArrayList<>(inputs.length);
    sortOrder = SortOrder.DESC;
    for (String input : inputs) {
        LiteralExpression expr = LiteralExpression.newConstant(input, dataType, sortOrder);
        ImmutableBytesPtr ptr = new ImmutableBytesPtr(expr.getBytes());
        dataType.pad(ptr, 8, sortOrder);
        descOrderedInputs.add(ptr.copyBytes());
    }
    Collections.sort(descOrderedInputs, Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < inputs.length; i++) {
        byte[] bytes = descOrderedInputs.get(i);
        String resultValue = (String) dataType.toObject(bytes, 0, bytes.length, dataType, sortOrder);
        assertEquals(inputs[inputs.length - 1 - i], resultValue);
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 79 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class PCharPadTest method test.

public void test(String value, PDataType dataType, int length, SortOrder sortOrder, byte[] result) throws SQLException {
    LiteralExpression expr = LiteralExpression.newConstant(value, dataType, sortOrder);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr(expr.getBytes());
    dataType.pad(ptr, length, sortOrder);
    String resultValue = (String) dataType.toObject(ptr, dataType, sortOrder);
    assertTrue(Arrays.equals(result, ptr.get()));
    assertEquals(value, resultValue);
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)

Example 80 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class HashJoinInfo method deserializeHashJoinFromScan.

@SuppressWarnings("unchecked")
public static HashJoinInfo deserializeHashJoinFromScan(Scan scan) {
    byte[] join = scan.getAttribute(HASH_JOIN);
    if (join == null) {
        return null;
    }
    ByteArrayInputStream stream = new ByteArrayInputStream(join);
    try {
        DataInputStream input = new DataInputStream(stream);
        KeyValueSchema joinedSchema = new KeyValueSchema();
        joinedSchema.readFields(input);
        int count = WritableUtils.readVInt(input);
        ImmutableBytesPtr[] joinIds = new ImmutableBytesPtr[count];
        List<Expression>[] joinExpressions = new List[count];
        JoinType[] joinTypes = new JoinType[count];
        boolean[] earlyEvaluation = new boolean[count];
        KeyValueSchema[] schemas = new KeyValueSchema[count];
        int[] fieldPositions = new int[count];
        for (int i = 0; i < count; i++) {
            joinIds[i] = new ImmutableBytesPtr();
            joinIds[i].readFields(input);
            int nExprs = WritableUtils.readVInt(input);
            joinExpressions[i] = new ArrayList<Expression>(nExprs);
            for (int j = 0; j < nExprs; j++) {
                int expressionOrdinal = WritableUtils.readVInt(input);
                Expression expression = ExpressionType.values()[expressionOrdinal].newInstance();
                expression.readFields(input);
                joinExpressions[i].add(expression);
            }
            int type = WritableUtils.readVInt(input);
            joinTypes[i] = JoinType.values()[type];
            earlyEvaluation[i] = input.readBoolean();
            schemas[i] = new KeyValueSchema();
            schemas[i].readFields(input);
            fieldPositions[i] = WritableUtils.readVInt(input);
        }
        Expression postJoinFilterExpression = null;
        int expressionOrdinal = WritableUtils.readVInt(input);
        if (expressionOrdinal != -1) {
            postJoinFilterExpression = ExpressionType.values()[expressionOrdinal].newInstance();
            postJoinFilterExpression.readFields(input);
        }
        int limit = -1;
        boolean forceProjection = false;
        // both to be upgraded in lock step.
        try {
            limit = WritableUtils.readVInt(input);
            forceProjection = input.readBoolean();
        } catch (EOFException ignore) {
        }
        return new HashJoinInfo(joinedSchema, joinIds, joinExpressions, joinTypes, earlyEvaluation, schemas, fieldPositions, postJoinFilterExpression, limit >= 0 ? limit : null, forceProjection);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) JoinType(org.apache.phoenix.parse.JoinTableNode.JoinType) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Expression(org.apache.phoenix.expression.Expression) EOFException(java.io.EOFException) ArrayList(java.util.ArrayList) List(java.util.List) KeyValueSchema(org.apache.phoenix.schema.KeyValueSchema)

Aggregations

ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)120 Mutation (org.apache.hadoop.hbase.client.Mutation)31 PTable (org.apache.phoenix.schema.PTable)28 ArrayList (java.util.ArrayList)27 Region (org.apache.hadoop.hbase.regionserver.Region)22 PMetaDataEntity (org.apache.phoenix.schema.PMetaDataEntity)22 Test (org.junit.Test)21 Cell (org.apache.hadoop.hbase.Cell)20 Put (org.apache.hadoop.hbase.client.Put)18 List (java.util.List)15 Scan (org.apache.hadoop.hbase.client.Scan)15 Pair (org.apache.hadoop.hbase.util.Pair)15 IOException (java.io.IOException)14 Expression (org.apache.phoenix.expression.Expression)14 PColumn (org.apache.phoenix.schema.PColumn)14 RowLock (org.apache.hadoop.hbase.regionserver.Region.RowLock)13 PSmallint (org.apache.phoenix.schema.types.PSmallint)12 HashMap (java.util.HashMap)11 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)11 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)11