use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class CoerceExpressionTest method testCoerceExpressionSupportsCoercingAllPDataTypesToBinary.
@Test
public void testCoerceExpressionSupportsCoercingAllPDataTypesToBinary() throws Exception {
for (PDataType p : PDataType.values()) {
if (!p.isArrayType()) {
LiteralExpression v = LiteralExpression.newConstant(map.get(p.getJavaClass()), p);
CoerceExpression e = new CoerceExpression(v, PBinary.INSTANCE);
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
e.evaluate(null, ptr);
Object obj = e.getDataType().toObject(ptr);
assertTrue("Coercing to BINARY failed for PDataType " + p, obj instanceof byte[]);
}
}
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class SortOrderExpressionTest method evaluateAndAssertResult.
private void evaluateAndAssertResult(Expression expression, Object expectedResult, String context) {
context = context == null ? "" : context;
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
assertTrue(expression.evaluate(null, ptr));
PDataType dataType = expression.getDataType();
SortOrder sortOrder = expression.getSortOrder();
Object result = dataType.toObject(ptr.get(), ptr.getOffset(), ptr.getLength(), dataType, sortOrder);
assertEquals(context, expectedResult, result);
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class SortOrderIT method testSkipScanCompare.
@Test
public void testSkipScanCompare() throws Exception {
List<Integer> expectedResults = Lists.newArrayList(2, 4);
List<Integer> rExpectedResults = new ArrayList<>(expectedResults);
Collections.reverse(rExpectedResults);
Integer[] saltBuckets = new Integer[] { null, 3 };
PDataType[] dataTypes = new PDataType[] { PDecimal.INSTANCE, PDouble.INSTANCE, PFloat.INSTANCE };
for (Integer saltBucket : saltBuckets) {
for (PDataType dataType : dataTypes) {
for (SortOrder sortOrder : SortOrder.values()) {
testCompareCompositeKey(saltBucket, dataType, sortOrder, "k1 in (2,4)", expectedResults, "");
testCompareCompositeKey(saltBucket, dataType, sortOrder, "k1 in (2,4)", rExpectedResults, "ORDER BY k1 DESC");
}
}
}
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class IndexTestUtil method coerceDataValueToIndexValue.
private static void coerceDataValueToIndexValue(PColumn dataColumn, PColumn indexColumn, ImmutableBytesWritable ptr) {
PDataType dataType = dataColumn.getDataType();
// TODO: push to RowKeySchema?
SortOrder dataModifier = dataColumn.getSortOrder();
PDataType indexType = indexColumn.getDataType();
SortOrder indexModifier = indexColumn.getSortOrder();
// We know ordinal position will match pk position, because you cannot
// alter an index table.
indexType.coerceBytes(ptr, dataType, dataModifier, indexModifier);
}
use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.
the class CaseExpression method coerceIfNecessary.
private static List<Expression> coerceIfNecessary(List<Expression> children) throws SQLException {
boolean isChildTypeUnknown = false;
PDataType returnType = children.get(0).getDataType();
for (int i = 2; i < children.size(); i += 2) {
Expression child = children.get(i);
PDataType childType = child.getDataType();
if (childType == null) {
isChildTypeUnknown = true;
} else if (returnType == null) {
returnType = childType;
isChildTypeUnknown = true;
} else if (returnType == childType || childType.isCoercibleTo(returnType)) {
continue;
} else if (returnType.isCoercibleTo(childType)) {
returnType = childType;
} else {
throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH).setMessage("Case expressions must have common type: " + returnType + " cannot be coerced to " + childType).build().buildException();
}
}
// make the return type be the most general number type of DECIMAL.
if (isChildTypeUnknown && returnType != null && returnType.isCoercibleTo(PDecimal.INSTANCE)) {
returnType = PDecimal.INSTANCE;
}
List<Expression> newChildren = children;
for (int i = 0; i < children.size(); i += 2) {
Expression child = children.get(i);
PDataType childType = child.getDataType();
if (childType != returnType) {
if (newChildren == children) {
newChildren = new ArrayList<Expression>(children);
}
newChildren.set(i, CoerceExpression.create(child, returnType));
}
}
return newChildren;
}
Aggregations