Search in sources :

Example 26 with PDataType

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[]);
        }
    }
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) Test(org.junit.Test)

Example 27 with PDataType

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);
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 28 with PDataType

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");
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) PDataType(org.apache.phoenix.schema.types.PDataType) ArrayList(java.util.ArrayList) SortOrder(org.apache.phoenix.schema.SortOrder) Test(org.junit.Test)

Example 29 with PDataType

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);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 30 with PDataType

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;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Aggregations

PDataType (org.apache.phoenix.schema.types.PDataType)152 Expression (org.apache.phoenix.expression.Expression)54 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)31 SortOrder (org.apache.phoenix.schema.SortOrder)29 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)21 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)18 Test (org.junit.Test)15 PDatum (org.apache.phoenix.schema.PDatum)12 BigDecimal (java.math.BigDecimal)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)10 IOException (java.io.IOException)9 PreparedStatement (java.sql.PreparedStatement)7 Pair (org.apache.hadoop.hbase.util.Pair)7 Date (java.sql.Date)6 AndExpression (org.apache.phoenix.expression.AndExpression)6