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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations