@Override public boolean decode(ImmutableBytesWritable ptr, int index) { return PArrayDataTypeDecoder.positionAtArrayElement(ptr, index, PVarbinary.INSTANCE, null); }
@Override public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) { Expression arrayExpr = children.get(0); return PArrayDataTypeDecoder.positionAtArrayElement(tuple, ptr, index, arrayExpr, getDataType(), getMaxLength()); }
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); }
@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; }
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); }
@Test public void testForVarCharArrayForWithTwoelementsElementArrayWithIndex() { String[] strArr = new String[2]; strArr[0] = "abx"; strArr[1] = "ereref"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 1, 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("ereref", Bytes.toString(res)); }
@Test public void testPositionSearchWithVarLengthArrayWithNullValue1() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = null; strArr[4] = "ran"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 2, 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("random", Bytes.toString(res)); }
@Test public void testForVarCharArrayForOddNumberWithIndex4() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = null; strArr[4] = null; 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("", Bytes.toString(res)); }
@Test public void testForVarCharArrayForEvenNumberWithIndex() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = "random12"; strArr[4] = "ranzzz"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 4, 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("ranzzz", Bytes.toString(res)); }
@Test public void testForVarCharArrayForOddNumberWithIndex3() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = "random12"; strArr[4] = null; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 4, 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("", Bytes.toString(res)); }
@Test public void testPositionSearchWithVarLengthArrayWithNullValueAtTheStart1() { String[] strArr = new String[5]; strArr[0] = null; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = null; strArr[4] = "ran"; 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("", Bytes.toString(res)); }
@Test public void testPositionSearchWithVarLengthArrayWithNullValue5() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = null; strArr[4] = "ran"; 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("", Bytes.toString(res)); }
@Test public void testPositionSearchWithVarLengthArrayWithNullValue2() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = "random12"; strArr[4] = null; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 2, 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("random", Bytes.toString(res)); } @Test
@Test public void testForVarCharArrayForOddNumberWithIndex() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = "random12"; strArr[4] = "ran"; 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("random12", Bytes.toString(res)); }
@Test public void testForVarCharArrayForOddNumberWithIndex5() { String[] strArr = new String[5]; strArr[0] = "abx"; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = null; strArr[4] = "random12"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 4, 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("random12", Bytes.toString(res)); }
@Test public void testPositionSearchWithVarLengthArrayWithNullValueAtTheStart3() { String[] strArr = new String[5]; strArr[0] = null; strArr[1] = "ereref"; strArr[2] = "random"; strArr[3] = null; strArr[4] = "ran"; PhoenixArray arr = PArrayDataType.instantiatePhoenixArray( PVarchar.INSTANCE, strArr); byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr); ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes); PArrayDataTypeDecoder.positionAtArrayElement(ptr, 4, 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("ran", Bytes.toString(res)); }