Search in sources :

Example 36 with SortOrder

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

the class StringConcatExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    byte[] result = ByteUtil.EMPTY_BYTE_ARRAY;
    for (int i = 0; i < children.size(); i++) {
        if (children.get(i).getDataType() == null) {
            continue;
        }
        if (!children.get(i).evaluate(tuple, ptr)) {
            // If not incremental evaluation, skip null children
            if (tuple.isImmutable()) {
                continue;
            }
            // have enough information yet.
            return false;
        }
        PDataType childType = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        // additional space here anyway.
        if (childType.isCoercibleTo(PVarchar.INSTANCE)) {
            result = ByteUtil.concat(result, ByteUtil.concat(sortOrder, ptr));
        } else {
            result = ByteUtil.concat(result, PVarchar.INSTANCE.toBytes(childType.toObject(ptr, sortOrder).toString()));
        }
    }
    ptr.set(result);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 37 with SortOrder

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

the class TimestampAddExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    BigDecimal finalResult = BigDecimal.ZERO;
    for (int i = 0; i < children.size(); i++) {
        if (!children.get(i).evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        BigDecimal value;
        PDataType type = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        if (type == PTimestamp.INSTANCE || type == PUnsignedTimestamp.INSTANCE) {
            value = (BigDecimal) (PDecimal.INSTANCE.toObject(ptr, type, sortOrder));
        } else if (type.isCoercibleTo(PDecimal.INSTANCE)) {
            value = (((BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder)).multiply(QueryConstants.BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
            value = ((BigDecimal.valueOf(type.getCodec().decodeDouble(ptr, sortOrder))).multiply(QueryConstants.BD_MILLIS_IN_DAY)).setScale(6, RoundingMode.HALF_UP);
        } else {
            value = BigDecimal.valueOf(type.getCodec().decodeLong(ptr, sortOrder));
        }
        finalResult = finalResult.add(value);
    }
    Timestamp ts = DateUtil.getTimestamp(finalResult);
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    PTimestamp.INSTANCE.toBytes(ts, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) Timestamp(java.sql.Timestamp) PUnsignedTimestamp(org.apache.phoenix.schema.types.PUnsignedTimestamp) PTimestamp(org.apache.phoenix.schema.types.PTimestamp) BigDecimal(java.math.BigDecimal)

Example 38 with SortOrder

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

the class DateAddExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    long finalResult = 0;
    for (int i = 0; i < children.size(); i++) {
        if (!children.get(i).evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        long value;
        PDataType type = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        if (type == PDecimal.INSTANCE) {
            BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder);
            value = bd.multiply(BD_MILLIS_IN_DAY).longValue();
        } else if (type.isCoercibleTo(PLong.INSTANCE)) {
            value = type.getCodec().decodeLong(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY;
        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
            value = (long) (type.getCodec().decodeDouble(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY);
        } else {
            value = type.getCodec().decodeLong(ptr, sortOrder);
        }
        finalResult += value;
    }
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    getDataType().getCodec().encodeLong(finalResult, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) BigDecimal(java.math.BigDecimal)

Example 39 with SortOrder

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

the class DateSubtractExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    long finalResult = 0;
    for (int i = 0; i < children.size(); i++) {
        if (!children.get(i).evaluate(tuple, ptr) || ptr.getLength() == 0) {
            return false;
        }
        long value;
        PDataType type = children.get(i).getDataType();
        SortOrder sortOrder = children.get(i).getSortOrder();
        if (type == PDecimal.INSTANCE) {
            BigDecimal bd = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, type, sortOrder);
            value = bd.multiply(BD_MILLIS_IN_DAY).longValue();
        } else if (type.isCoercibleTo(PLong.INSTANCE)) {
            value = type.getCodec().decodeLong(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY;
        } else if (type.isCoercibleTo(PDouble.INSTANCE)) {
            value = (long) (type.getCodec().decodeDouble(ptr, sortOrder) * QueryConstants.MILLIS_IN_DAY);
        } else {
            value = type.getCodec().decodeLong(ptr, sortOrder);
        }
        if (i == 0) {
            finalResult = value;
        } else {
            finalResult -= value;
        }
    }
    byte[] resultPtr = new byte[getDataType().getByteSize()];
    getDataType().getCodec().encodeLong(finalResult, resultPtr, 0);
    ptr.set(resultPtr);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) BigDecimal(java.math.BigDecimal)

Example 40 with SortOrder

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

the class DecimalDivideExpression 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.divide(bd, PDataType.DEFAULT_MATH_CONTEXT);
        }
    }
    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)

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