@Override public boolean decode(ImmutableBytesWritable ptr, int index) { return PArrayDataTypeDecoder.positionAtArrayElement(ptr, index, PVarbinary.INSTANCE, null); }
@Override public ColumnValueDecoder getDecoder() { return new PArrayDataTypeDecoder(); } };
private static boolean isNullValue(int arrayIndex, byte[] bytes, int initPos, byte serializationVersion, boolean useShort, int indexOffset, int currOffset, int elementLength) { if (isSeparatorByte(bytes, initPos, currOffset)) { if (isPriorValueZeroLength(arrayIndex, bytes, serializationVersion, useShort, indexOffset, currOffset)) { return true; } else { // if there's no prior null, there can be at most 1 null if (elementLength == 2) { // nullByte calculation comes from the encoding of one null // see PArrayDataType#serializeNulls byte nullByte = SortOrder.invert((byte)(0)); if (bytes[initPos+currOffset+1] == nullByte) { return true; } } } } return false; }
if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) { elementLength = 0; elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0 : indexOffset - (currOffset + initPos) - separatorBytes; useShort, indexOffset, serializationVersion) - currOffset; if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) { elementLength = 0; : 0; elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0
if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) { elementLength = 0; elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0 : indexOffset - (currOffset + initPos) - separatorBytes; useShort, indexOffset, serializationVersion) - currOffset; if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) { elementLength = 0; : 0; elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { Expression arrayExpr = children.get(0); return PArrayDataTypeDecoder.positionAtArrayElement(tuple, ptr, index, arrayExpr, getDataType(), getMaxLength()); }
private static boolean isNullValue(int arrayIndex, byte[] bytes, int initPos, byte serializationVersion, boolean useShort, int indexOffset, int currOffset, int elementLength) { if (isSeparatorByte(bytes, initPos, currOffset)) { if (isPriorValueZeroLength(arrayIndex, bytes, serializationVersion, useShort, indexOffset, currOffset)) { return true; } else { // if there's no prior null, there can be at most 1 null if (elementLength == 2) { // nullByte calculation comes from the encoding of one null // see PArrayDataType#serializeNulls byte nullByte = SortOrder.invert((byte)(0)); if (bytes[initPos+currOffset+1] == nullByte) { return true; } } } } return false; }
if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) { elementLength = 0; elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0 : indexOffset - (currOffset + initPos) - separatorBytes; useShort, indexOffset, serializationVersion) - currOffset; if (isNullValue(arrayIndex, bytes, initPos, serializationVersion, useShort, indexOffset, currOffset, elementLength)) { elementLength = 0; : 0; elementLength = isSeparatorByte(bytes, initPos, currOffset) && serializationVersion == PArrayDataType.SORTABLE_SERIALIZATION_VERSION ? 0
@Override public ColumnValueDecoder getDecoder() { return new PArrayDataTypeDecoder(); } };
public static boolean positionAtArrayElement(Tuple tuple, ImmutableBytesWritable ptr, int index, Expression arrayExpr, PDataType pDataType, Integer maxLen) { if (!arrayExpr.evaluate(tuple, ptr)) { return false; } else if (ptr.getLength() == 0) { return true; } // Given a ptr to the entire array, set ptr to point to a particular element within that array // given the type of an array element (see comments in PDataTypeForArray) return positionAtArrayElement(ptr, index - 1, pDataType, maxLen); }
private static boolean isNullValue(int arrayIndex, byte[] bytes, int initPos, byte serializationVersion, boolean useShort, int indexOffset, int currOffset, int elementLength) { if (isSeparatorByte(bytes, initPos, currOffset)) { if (isPriorValueZeroLength(arrayIndex, bytes, serializationVersion, useShort, indexOffset, currOffset)) { return true; } else { // if there's no prior null, there can be at most 1 null if (elementLength == 2) { // nullByte calculation comes from the encoding of one null // see PArrayDataType#serializeNulls byte nullByte = SortOrder.invert((byte)(0)); if (bytes[initPos+currOffset+1] == nullByte) { return true; } } } } return false; }
@Override public ColumnValueDecoder getDecoder() { return new PArrayDataTypeDecoder(); } };
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { Expression indexExpr = children.get(1); if (!indexExpr.evaluate(tuple, ptr)) { return false; } else if (ptr.getLength() == 0) { return true; } // Use Codec to prevent Integer object allocation int index = PInteger.INSTANCE.getCodec().decodeInt(ptr, indexExpr.getSortOrder()); if(index < 0) { throw new ParseException("Index cannot be negative :" + index); } Expression arrayExpr = children.get(0); return PArrayDataTypeDecoder.positionAtArrayElement(tuple, ptr, index, arrayExpr, getDataType(), getMaxLength()); }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { ptr.set(arrayPtr.get(), arrayPtr.getOffset(), arrayPtr.getLength()); PArrayDataTypeDecoder.positionAtArrayElement(ptr, index++, getDataType(), getMaxLength()); return true; }
@Override public boolean decode(ImmutableBytesWritable ptr, int index) { return PArrayDataTypeDecoder.positionAtArrayElement(ptr, index, PVarbinary.INSTANCE, null); }
@Override public boolean decode(ImmutableBytesWritable ptr, int index) { return PArrayDataTypeDecoder.positionAtArrayElement(ptr, index, PVarbinary.INSTANCE, null); }
public void testVariableLengthArrayWithElementsMoreThanShortMax() { String[] strArr = new String[(2 * Short.MAX_VALUE) + 100]; for(int i = 0 ; i < (2 * Short.MAX_VALUE) + 100; i++ ) { String str = "abc"; for(int j = 0 ; j <= i ;j++) { str += "-"; } strArr[i] = str; } PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 3, PVarchar.INSTANCE, PVarchar.INSTANCE.getByteSize()); int offset = ptr.getOffset(); int length = ptr.getLength(); byte[] bs = ptr.get(); byte[] res = new byte[length]; System.arraycopy(bs, offset, res, 0, length); assertEquals("abc---", Bytes.toString(res)); }
@Test public void testForVarCharArrayForOneElementArrayWithIndex() { String[] strArr = new String[1]; strArr[0] = "abx"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 0, PVarchar.INSTANCE, PVarchar.INSTANCE.getByteSize()); int offset = ptr.getOffset(); int length = ptr.getLength(); byte[] bs = ptr.get(); byte[] res = new byte[length]; System.arraycopy(bs, offset, res, 0, length); assertEquals("abx", Bytes.toString(res)); }
@Test public void testForVarCharArrayWithOneElementIndex() { String[] strArr = new String[1]; strArr[0] = "abx"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 0, PVarchar.INSTANCE, PVarchar.INSTANCE.getByteSize()); int offset = ptr.getOffset(); int length = ptr.getLength(); byte[] bs = ptr.get(); byte[] res = new byte[length]; System.arraycopy(bs, offset, res, 0, length); assertEquals("abx", Bytes.toString(res)); }
@Test public void testPositionAtArrayElementWithDescArray(){ Object[] objects = new Object[]{"a", "b", null}; PhoenixArray arr = new PhoenixArray(PVarchar.INSTANCE, objects); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr, PVarchar.INSTANCE, SortOrder.DESC); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 2, PVarchar.INSTANCE, null); String value = (String)PVarchar.INSTANCE.toObject(ptr, SortOrder.DESC); assertEquals(null, value); }