Search in sources :

Example 11 with DataExceedsCapacityException

use of org.apache.phoenix.exception.DataExceedsCapacityException 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)

Example 12 with DataExceedsCapacityException

use of org.apache.phoenix.exception.DataExceedsCapacityException in project phoenix by apache.

the class SchemaUtil method padData.

/**
     * Pads the data in ptr by the required amount for fixed width data types
     */
public static void padData(String tableName, PColumn column, ImmutableBytesWritable ptr) {
    PDataType type = column.getDataType();
    byte[] byteValue = ptr.get();
    boolean isNull = type.isNull(byteValue);
    Integer maxLength = column.getMaxLength();
    if (!isNull && type.isFixedWidth() && maxLength != null) {
        if (ptr.getLength() < maxLength) {
            type.pad(ptr, maxLength, column.getSortOrder());
        } else if (ptr.getLength() > maxLength) {
            throw new DataExceedsCapacityException(tableName + "." + column.getName().getString() + " may not exceed " + maxLength + " bytes (" + type.toObject(byteValue) + ")");
        }
    }
}
Also used : DataExceedsCapacityException(org.apache.phoenix.exception.DataExceedsCapacityException) PDataType(org.apache.phoenix.schema.types.PDataType)

Aggregations

DataExceedsCapacityException (org.apache.phoenix.exception.DataExceedsCapacityException)12 BigDecimal (java.math.BigDecimal)8 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)6 PDataType (org.apache.phoenix.schema.types.PDataType)6 Test (org.junit.Test)4 Expression (org.apache.phoenix.expression.Expression)3 SortOrder (org.apache.phoenix.schema.SortOrder)3 SQLException (java.sql.SQLException)2 List (java.util.List)2 Cell (org.apache.hadoop.hbase.Cell)2 Delete (org.apache.hadoop.hbase.client.Delete)2 Mutation (org.apache.hadoop.hbase.client.Mutation)2 Put (org.apache.hadoop.hbase.client.Put)2 PColumn (org.apache.phoenix.schema.PColumn)2 PRow (org.apache.phoenix.schema.PRow)2 PTable (org.apache.phoenix.schema.PTable)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1