Search in sources :

Example 11 with SortOrder

use of org.apache.phoenix.schema.SortOrder 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 12 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class ComparisonExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!children.get(0).evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        // null comparison evals to null
        return true;
    }
    byte[] lhsBytes = ptr.get();
    int lhsOffset = ptr.getOffset();
    int lhsLength = ptr.getLength();
    PDataType lhsDataType = children.get(0).getDataType();
    SortOrder lhsSortOrder = children.get(0).getSortOrder();
    if (!children.get(1).evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        // null comparison evals to null
        return true;
    }
    byte[] rhsBytes = ptr.get();
    int rhsOffset = ptr.getOffset();
    int rhsLength = ptr.getLength();
    PDataType rhsDataType = children.get(1).getDataType();
    SortOrder rhsSortOrder = children.get(1).getSortOrder();
    if (rhsDataType == PChar.INSTANCE) {
        rhsLength = StringUtil.getUnpaddedCharLength(rhsBytes, rhsOffset, rhsLength, rhsSortOrder);
    }
    if (lhsDataType == PChar.INSTANCE) {
        lhsLength = StringUtil.getUnpaddedCharLength(lhsBytes, lhsOffset, lhsLength, lhsSortOrder);
    }
    int comparisonResult = lhsDataType.compareTo(lhsBytes, lhsOffset, lhsLength, lhsSortOrder, rhsBytes, rhsOffset, rhsLength, rhsSortOrder, rhsDataType);
    ptr.set(ByteUtil.compare(op, comparisonResult) ? PDataType.TRUE_BYTES : PDataType.FALSE_BYTES);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 13 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class DecimalAddExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal result = null;
    for (int i = 0; i < children.size(); i++) {
        Expression childExpr = children.get(i);
        if (!childExpr.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        PDataType childType = childExpr.getDataType();
        SortOrder childSortOrder = childExpr.getSortOrder();
        BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, childType, childSortOrder);
        if (result == null) {
            result = bd;
        } else {
            result = result.add(bd);
        }
    }
    if (maxLength != null || scale != null) {
        result = NumberUtil.setDecimalWidthAndScale(result, maxLength, scale);
    }
    if (result == null) {
        throw new DataExceedsCapacityException(PDecimal.INSTANCE, maxLength, scale);
    }
    ptr.set(PDecimal.INSTANCE.toBytes(result));
    return true;
}
Also used : DataExceedsCapacityException(org.apache.phoenix.exception.DataExceedsCapacityException) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) BigDecimal(java.math.BigDecimal)

Example 14 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class DecimalMultiplyExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal result = null;
    for (int i = 0; i < children.size(); i++) {
        Expression childExpr = children.get(i);
        if (!childExpr.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        PDataType childType = children.get(i).getDataType();
        SortOrder childSortOrder = children.get(i).getSortOrder();
        BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, childType, childSortOrder);
        if (result == null) {
            result = bd;
        } else {
            result = result.multiply(bd);
        }
    }
    if (getMaxLength() != null || getScale() != null) {
        result = NumberUtil.setDecimalWidthAndScale(result, getMaxLength(), getScale());
    }
    if (result == null) {
        throw new DataExceedsCapacityException(PDecimal.INSTANCE, getMaxLength(), getScale());
    }
    ptr.set(PDecimal.INSTANCE.toBytes(result));
    return true;
}
Also used : DataExceedsCapacityException(org.apache.phoenix.exception.DataExceedsCapacityException) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) BigDecimal(java.math.BigDecimal)

Example 15 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class DecimalSubtractExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal result = null;
    for (int i = 0; i < children.size(); i++) {
        Expression childExpr = children.get(i);
        if (!childExpr.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        PDataType childType = childExpr.getDataType();
        boolean isDate = childType.isCoercibleTo(PDate.INSTANCE);
        SortOrder childSortOrder = childExpr.getSortOrder();
        BigDecimal bd = isDate ? BigDecimal.valueOf(childType.getCodec().decodeLong(ptr, childSortOrder)) : (BigDecimal) PDecimal.INSTANCE.toObject(ptr, childType, childSortOrder);
        if (result == null) {
            result = bd;
        } else {
            result = result.subtract(bd);
            /*
                 * Special case for date subtraction - note that only first two expression may be dates.
                 * We need to convert the date to a unit of "days" because that's what sql expects.
                 */
            if (isDate) {
                result = result.divide(BD_MILLIS_IN_DAY, PDataType.DEFAULT_MATH_CONTEXT);
            }
        }
    }
    if (maxLength != null || scale != null) {
        result = NumberUtil.setDecimalWidthAndScale(result, maxLength, scale);
    }
    if (result == null) {
        throw new DataExceedsCapacityException(PDecimal.INSTANCE, maxLength, scale);
    }
    ptr.set(PDecimal.INSTANCE.toBytes(result));
    return true;
}
Also used : DataExceedsCapacityException(org.apache.phoenix.exception.DataExceedsCapacityException) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) BigDecimal(java.math.BigDecimal)

Aggregations

SortOrder (org.apache.phoenix.schema.SortOrder)43 PDataType (org.apache.phoenix.schema.types.PDataType)29 BigDecimal (java.math.BigDecimal)8 Expression (org.apache.phoenix.expression.Expression)8 PDatum (org.apache.phoenix.schema.PDatum)7 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)6 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)5 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DataExceedsCapacityException (org.apache.phoenix.exception.DataExceedsCapacityException)4 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)4 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)4 Field (org.apache.phoenix.schema.ValueSchema.Field)4 PTimestamp (org.apache.phoenix.schema.types.PTimestamp)4 PUnsignedTimestamp (org.apache.phoenix.schema.types.PUnsignedTimestamp)4 IOException (java.io.IOException)3 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)3 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)3 RowKeySchemaBuilder (org.apache.phoenix.schema.RowKeySchema.RowKeySchemaBuilder)3