use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class PDataTypeForArraysTest method testForVarCharArrayOneElement.
@Test
public void testForVarCharArrayOneElement() {
String[] strArr = new String[1];
strArr[0] = "ereref";
PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PVarchar.INSTANCE, strArr);
byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
PhoenixArray resultArr = (PhoenixArray) PVarcharArray.INSTANCE.toObject(bytes, 0, bytes.length);
assertEquals(arr, resultArr);
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class PDataTypeForArraysTest method testForVarCharArrayForOddNumberWithIndex5.
@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));
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class PDataTypeForArraysTest method testVarCharArrayWithGreatherThan255NullsInMiddle.
@Test
public void testVarCharArrayWithGreatherThan255NullsInMiddle() {
String[] strArr = new String[300];
strArr[0] = "abc";
strArr[1] = "bcd";
strArr[2] = null;
strArr[3] = null;
strArr[4] = "bcd";
for (int i = 5; i < strArr.length - 2; i++) {
strArr[i] = null;
}
strArr[strArr.length - 1] = "abc";
PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PVarchar.INSTANCE, strArr);
byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
PhoenixArray resultArr = (PhoenixArray) PVarcharArray.INSTANCE.toObject(bytes, 0, bytes.length);
assertEquals(arr, resultArr);
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class PatternPerformanceTest method testSplit.
private void testSplit(AbstractBaseSplitter pattern, String name) throws SQLException {
timer.reset();
for (int i = 0; i < maxTimes; ++i) {
ImmutableBytesWritable ptr = dataPtr[i % 3];
resultPtr.set(ptr.get(), ptr.getOffset(), ptr.getLength());
boolean ret = pattern.split(resultPtr);
if (ENABLE_ASSERT) {
PhoenixArray array = (PhoenixArray) PVarcharArray.INSTANCE.toObject(resultPtr);
assertTrue(ret && (i % 3 != 1 || ((String[]) array.getArray()).length == 2));
}
}
timer.printTime(name);
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class LiteralExpression method newConstant.
// TODO: cache?
public static LiteralExpression newConstant(Object value, PDataType type, Integer maxLength, Integer scale, SortOrder sortOrder, Determinism determinism, boolean rowKeyOrderOptimizable) throws SQLException {
if (value == null) {
return (type == null) ? getNullLiteralExpression(determinism) : getTypedNullLiteralExpression(type, determinism);
} else if (value instanceof Boolean) {
return getBooleanLiteralExpression((Boolean) value, determinism);
}
PDataType actualType = PDataType.fromLiteral(value);
try {
value = type.toObject(value, actualType);
} catch (IllegalDataException e) {
throw TypeMismatchException.newException(type, actualType, value.toString());
}
byte[] b = type.isArrayType() ? ((PArrayDataType) type).toBytes(value, PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable) : type.toBytes(value, sortOrder);
if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {
if (type == PChar.INSTANCE && maxLength != null && b.length < maxLength) {
if (rowKeyOrderOptimizable) {
b = type.pad(b, maxLength, sortOrder);
} else {
b = StringUtil.padChar(b, maxLength);
}
} else if (value != null) {
maxLength = ((String) value).length();
}
} else if (type.isArrayType()) {
maxLength = ((PhoenixArray) value).getMaxLength();
}
if (b.length == 0) {
return getTypedNullLiteralExpression(type, determinism);
}
if (maxLength == null) {
maxLength = type == null || !type.isFixedWidth() ? null : type.getMaxLength(value);
}
return new LiteralExpression(value, type, b, maxLength, scale, sortOrder, determinism);
}
Aggregations