Search in sources :

Example 16 with LiteralExpression

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

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

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

the class HashCacheClient method evaluateKeyExpression.

/**
     * Evaluate the RHS key expression and wrap the result as a new Expression.
     * Unlike other types of Expression which will be evaluated and wrapped as a 
     * single LiteralExpression, RowValueConstructorExpression should be handled 
     * differently. We should evaluate each child of RVC and wrap them into a new
     * RVC Expression, in order to make sure that the later coercion between the 
     * LHS key expression and this RHS key expression will be successful.
     * 
     * @param keyExpression the RHS key expression
     * @param tuple the input tuple
     * @param ptr the temporary pointer
     * @return the Expression containing the evaluated result
     * @throws SQLException 
     */
public static Expression evaluateKeyExpression(Expression keyExpression, Tuple tuple, ImmutableBytesWritable ptr) throws SQLException {
    if (!(keyExpression instanceof RowValueConstructorExpression)) {
        PDataType type = keyExpression.getDataType();
        keyExpression.reset();
        if (keyExpression.evaluate(tuple, ptr)) {
            return LiteralExpression.newConstant(type.toObject(ptr, keyExpression.getSortOrder()), type);
        }
        return LiteralExpression.newConstant(null, type);
    }
    List<Expression> children = keyExpression.getChildren();
    List<Expression> values = Lists.newArrayListWithExpectedSize(children.size());
    for (Expression child : children) {
        PDataType type = child.getDataType();
        child.reset();
        if (child.evaluate(tuple, ptr)) {
            values.add(LiteralExpression.newConstant(type.toObject(ptr, child.getSortOrder()), type));
        } else {
            values.add(LiteralExpression.newConstant(null, type));
        }
    }
    // might be coerced later.
    return new RowValueConstructorExpression(values, false);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) RowValueConstructorExpression(org.apache.phoenix.expression.RowValueConstructorExpression)

Example 19 with LiteralExpression

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

the class QueryCompilerTest method assertLiteralEquals.

private static void assertLiteralEquals(Object o, RowProjector p, int i) {
    assertTrue(i < p.getColumnCount());
    Expression e = p.getColumnProjector(i).getExpression();
    assertTrue(e instanceof LiteralExpression);
    LiteralExpression l = (LiteralExpression) e;
    Object lo = l.getValue();
    assertEquals(o, lo);
}
Also used : Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 20 with LiteralExpression

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

the class ExpressionCompiler method visitLeave.

@Override
public Expression visitLeave(ExistsParseNode node, List<Expression> l) throws SQLException {
    LiteralExpression child = (LiteralExpression) l.get(0);
    PhoenixArray array = (PhoenixArray) child.getValue();
    return LiteralExpression.newConstant(array.getDimensions() > 0 ^ node.isNegate(), PBoolean.INSTANCE);
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray)

Aggregations

LiteralExpression (org.apache.phoenix.expression.LiteralExpression)23 Expression (org.apache.phoenix.expression.Expression)11 PDataType (org.apache.phoenix.schema.types.PDataType)9 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)8 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)6 ArrayList (java.util.ArrayList)5 LiteralParseNode (org.apache.phoenix.parse.LiteralParseNode)5 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)4 ParseNode (org.apache.phoenix.parse.ParseNode)4 List (java.util.List)3 Cell (org.apache.hadoop.hbase.Cell)3 Pair (org.apache.hadoop.hbase.util.Pair)3 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)3 ResultIterator (org.apache.phoenix.iterate.ResultIterator)3 SQLException (java.sql.SQLException)2 Scan (org.apache.hadoop.hbase.client.Scan)2 AndExpression (org.apache.phoenix.expression.AndExpression)2 ArrayConstructorExpression (org.apache.phoenix.expression.ArrayConstructorExpression)2 ByteBasedLikeExpression (org.apache.phoenix.expression.ByteBasedLikeExpression)2 CaseExpression (org.apache.phoenix.expression.CaseExpression)2