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