@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)); ArrayConstructorExpression arrayConstructorExpression = new ArrayConstructorExpression(children, PVarbinary.INSTANCE, false); ImmutableBytesPtr ptr = new ImmutableBytesPtr(); ArrayElemRefExpression arrayElemRefExpression = new ArrayElemRefExpression(Lists.<Expression>newArrayList(arrayConstructorExpression)); arrayElemRefExpression.setIndex(1); arrayElemRefExpression.evaluate(null, ptr); assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptr.copyBytesIfNecessary()); arrayElemRefExpression.setIndex(2); arrayElemRefExpression.evaluate(null, ptr); assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptr.copyBytesIfNecessary()); arrayElemRefExpression.setIndex(3); arrayElemRefExpression.evaluate(null, ptr); assertArrayEquals(BYTE_ARRAY1, ptr.copyBytesIfNecessary()); arrayElemRefExpression.setIndex(4); arrayElemRefExpression.evaluate(null, ptr); assertArrayEquals(BYTE_ARRAY2, ptr.copyBytesIfNecessary()); }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (position == elements.length) { ptr.set(valuePtr.get(), valuePtr.getOffset(), valuePtr.getLength()); return true; } TrustedByteArrayOutputStream byteStream = new TrustedByteArrayOutputStream(estimatedSize); DataOutputStream oStream = new DataOutputStream(byteStream); PArrayDataTypeEncoder builder = new PArrayDataTypeEncoder(byteStream, oStream, children.size(), baseType, getSortOrder(), rowKeyOrderOptimizable, PArrayDataType.SORTABLE_SERIALIZATION_VERSION); for (int i = position >= 0 ? position : 0; i < elements.length; i++) { Expression child = children.get(i); if (!child.evaluate(tuple, ptr)) { if (tuple != null && !tuple.isImmutable()) { if (position >= 0) position = i; return false; } } else { builder.appendValue(ptr.get(), ptr.getOffset(), ptr.getLength()); } } if (position >= 0) position = elements.length; byte[] bytes = builder.encode(); ptr.set(bytes, 0, bytes.length); valuePtr.set(ptr.get(), ptr.getOffset(), ptr.getLength()); return true; }
ArrayConstructorExpression extends BaseCompoundExpression { @Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (position == elements.length) { ptr.set(valuePtr.get(), valuePtr.getOffset(), valuePtr.getLength()); return true; } TrustedByteArrayOutputStream byteStream = new TrustedByteArrayOutputStream(estimatedSize); DataOutputStream oStream = new DataOutputStream(byteStream); PArrayDataTypeEncoder builder = new PArrayDataTypeEncoder(byteStream, oStream, children.size(), baseType, getSortOrder(), rowKeyOrderOptimizable, PArrayDataType.SORTABLE_SERIALIZATION_VERSION); for (int i = position >= 0 ? position : 0; i < elements.length; i++) { Expression child = children.get(i); if (!child.evaluate(tuple, ptr)) { if (tuple != null && !tuple.isImmutable()) { if (position >= 0) position = i; return false; } } else { builder.appendValue(ptr.get(), ptr.getOffset(), ptr.getLength()); } } if (position >= 0) position = elements.length; byte[] bytes = builder.encode(); ptr.set(bytes, 0, bytes.length); valuePtr.set(ptr.get(), ptr.getOffset(), ptr.getLength()); return true; } }
ArrayConstructorExpression extends BaseCompoundExpression { @Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (position == elements.length) { ptr.set(valuePtr.get(), valuePtr.getOffset(), valuePtr.getLength()); return true; } TrustedByteArrayOutputStream byteStream = new TrustedByteArrayOutputStream(estimatedSize); DataOutputStream oStream = new DataOutputStream(byteStream); PArrayDataTypeEncoder builder = new PArrayDataTypeEncoder(byteStream, oStream, children.size(), baseType, getSortOrder(), rowKeyOrderOptimizable, PArrayDataType.SORTABLE_SERIALIZATION_VERSION); for (int i = position >= 0 ? position : 0; i < elements.length; i++) { Expression child = children.get(i); if (!child.evaluate(tuple, ptr)) { if (tuple != null && !tuple.isImmutable()) { if (position >= 0) position = i; return false; } } else { builder.appendValue(ptr.get(), ptr.getOffset(), ptr.getLength()); } } if (position >= 0) position = elements.length; byte[] bytes = builder.encode(); ptr.set(bytes, 0, bytes.length); valuePtr.set(ptr.get(), ptr.getOffset(), ptr.getLength()); return true; } ArrayConstructorExpression();  ArrayConstructorExpression(List<Expression> children, PDataType baseType, boolean rowKeyOrderOptimizable); }
ArrayConstructorExpression extends BaseCompoundExpression { @Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (position == elements.length) { ptr.set(valuePtr.get(), valuePtr.getOffset(), valuePtr.getLength()); return true; } TrustedByteArrayOutputStream byteStream = new TrustedByteArrayOutputStream(estimatedSize); DataOutputStream oStream = new DataOutputStream(byteStream); PArrayDataTypeEncoder builder = new PArrayDataTypeEncoder(byteStream, oStream, children.size(), baseType, getSortOrder(), rowKeyOrderOptimizable, PArrayDataType.SORTABLE_SERIALIZATION_VERSION); for (int i = position >= 0 ? position : 0; i < elements.length; i++) { Expression child = children.get(i); if (!child.evaluate(tuple, ptr)) { if (tuple != null && !tuple.isImmutable()) { if (position >= 0) position = i; return false; } } else { builder.appendValue(ptr.get(), ptr.getOffset(), ptr.getLength()); } } if (position >= 0) position = elements.length; byte[] bytes = builder.encode(); ptr.set(bytes, 0, bytes.length); valuePtr.set(ptr.get(), ptr.getOffset(), ptr.getLength()); return true; } ArrayConstructorExpression();  ArrayConstructorExpression(List<Expression> children, PDataType baseType, boolean rowKeyOrderOptimizable); ArrayConstructorExpression clone(List<Expression> children); @Override PDataType getDataType(); @Override void reset(); @Override boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr); @Override void readFields(DataInput input); @Override void write(DataOutput output); @Override boolean requiresFinalEvaluation(); @Override final T accept(ExpressionVisitor<T> visitor); @Override String toString(); }
ArrayConstructorExpression extends BaseCompoundExpression { @Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { if (position == elements.length) { ptr.set(valuePtr.get(), valuePtr.getOffset(), valuePtr.getLength()); return true; } TrustedByteArrayOutputStream byteStream = new TrustedByteArrayOutputStream(estimatedSize); DataOutputStream oStream = new DataOutputStream(byteStream); PArrayDataTypeEncoder builder = new PArrayDataTypeEncoder(byteStream, oStream, children.size(), baseType, getSortOrder(), rowKeyOrderOptimizable, PArrayDataType.SORTABLE_SERIALIZATION_VERSION); for (int i = position >= 0 ? position : 0; i < elements.length; i++) { Expression child = children.get(i); if (!child.evaluate(tuple, ptr)) { if (tuple != null && !tuple.isImmutable()) { if (position >= 0) position = i; return false; } } else { builder.appendValue(ptr.get(), ptr.getOffset(), ptr.getLength()); } } if (position >= 0) position = elements.length; byte[] bytes = builder.encode(); ptr.set(bytes, 0, bytes.length); valuePtr.set(ptr.get(), ptr.getOffset(), ptr.getLength()); return true; } ArrayConstructorExpression();  ArrayConstructorExpression(List<Expression> children, PDataType baseType, boolean rowKeyOrderOptimizable); ArrayConstructorExpression clone(List<Expression> children); @Override PDataType getDataType(); @Override void reset(); @Override boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr); @Override void readFields(DataInput input); @Override void write(DataOutput output); @Override boolean requiresFinalEvaluation(); @Override final T accept(ExpressionVisitor<T> visitor); @Override String toString();  }