Search in sources :

Example 51 with HiveDecimal

use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.

the class GenericUDFOPDivide method evaluate.

@Override
protected HiveDecimalWritable evaluate(HiveDecimal left, HiveDecimal right) {
    if (right.compareTo(HiveDecimal.ZERO) == 0) {
        return null;
    }
    HiveDecimal dec = left.divide(right);
    if (dec == null) {
        return null;
    }
    decimalWritable.set(dec);
    return decimalWritable;
}
Also used : HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal)

Example 52 with HiveDecimal

use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.

the class GenericUDFBaseNumeric method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    if (arguments[0] == null || arguments[1] == null) {
        return null;
    }
    Object left = arguments[0].get();
    Object right = arguments[1].get();
    if (left == null && right == null) {
        return null;
    }
    // Handle decimal separately.
    if (resultOI.getPrimitiveCategory() == PrimitiveCategory.DECIMAL) {
        HiveDecimal hdLeft = PrimitiveObjectInspectorUtils.getHiveDecimal(left, leftOI);
        HiveDecimal hdRight = PrimitiveObjectInspectorUtils.getHiveDecimal(right, rightOI);
        if (hdLeft == null || hdRight == null) {
            return null;
        }
        HiveDecimalWritable result = evaluate(hdLeft, hdRight);
        return resultOI.getPrimitiveWritableObject(result);
    }
    left = converterLeft.convert(left);
    if (left == null) {
        return null;
    }
    right = converterRight.convert(right);
    if (right == null) {
        return null;
    }
    switch(resultOI.getPrimitiveCategory()) {
        case BYTE:
            return evaluate((ByteWritable) left, (ByteWritable) right);
        case SHORT:
            return evaluate((ShortWritable) left, (ShortWritable) right);
        case INT:
            return evaluate((IntWritable) left, (IntWritable) right);
        case LONG:
            return evaluate((LongWritable) left, (LongWritable) right);
        case FLOAT:
            return evaluate((FloatWritable) left, (FloatWritable) right);
        case DOUBLE:
            return evaluate((DoubleWritable) left, (DoubleWritable) right);
        default:
            // Should never happen.
            throw new RuntimeException("Unexpected type in evaluating " + opName + ": " + resultOI.getPrimitiveCategory());
    }
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal)

Example 53 with HiveDecimal

use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.

the class TestVectorArithmeticExpressions method testDecimalScalarDivideDecimalColumn.

/* Test decimal scalar divided column. This tests the primary logic
   * for template ScalarDivideColumnDecimal.txt.
   */
@Test
public void testDecimalScalarDivideDecimalColumn() {
    VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols();
    // 1.20 * 3.30
    HiveDecimal d = HiveDecimal.create("3.96");
    VectorExpression expr = new DecimalScalarDivideDecimalColumn(d, 0, 2);
    // test without nulls
    expr.evaluate(b);
    DecimalColumnVector r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("3.3")));
    assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1.2")));
    // entry 2 is null due to zero-divide
    assertFalse(r.noNulls);
    assertTrue(r.isNull[2]);
    // test null propagation
    b = getVectorizedRowBatch3DecimalCols();
    DecimalColumnVector in = (DecimalColumnVector) b.cols[0];
    r = (DecimalColumnVector) b.cols[2];
    in.noNulls = false;
    in.isNull[0] = true;
    expr.evaluate(b);
    assertTrue(!r.noNulls);
    assertTrue(r.isNull[0]);
    // test repeating case, no nulls
    b = getVectorizedRowBatch3DecimalCols();
    in = (DecimalColumnVector) b.cols[0];
    in.isRepeating = true;
    expr.evaluate(b);
    r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.isRepeating);
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("3.3")));
    // test repeating case for null value
    b = getVectorizedRowBatch3DecimalCols();
    in = (DecimalColumnVector) b.cols[0];
    in.isRepeating = true;
    in.isNull[0] = true;
    in.noNulls = false;
    expr.evaluate(b);
    r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.isRepeating);
    assertTrue(!r.noNulls);
    assertTrue(r.isNull[0]);
}
Also used : VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) TestVectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch) DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) DecimalScalarDivideDecimalColumn(org.apache.hadoop.hive.ql.exec.vector.expressions.gen.DecimalScalarDivideDecimalColumn) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) Test(org.junit.Test)

Example 54 with HiveDecimal

use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.

the class TestVectorArithmeticExpressions method testDecimalColModuloDecimalScalar.

// Spot check Decimal Col-Scalar Modulo
@Test
public void testDecimalColModuloDecimalScalar() {
    VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols();
    HiveDecimal d = HiveDecimal.create("2.00");
    VectorExpression expr = new DecimalColModuloDecimalScalar(0, d, 2);
    // test without nulls
    expr.evaluate(b);
    DecimalColumnVector r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.20")));
    assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1.30")));
    assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0")));
    // try again with some different data values and divisor
    DecimalColumnVector in = (DecimalColumnVector) b.cols[0];
    in.vector[0].set(HiveDecimal.create("15.40"));
    in.vector[1].set(HiveDecimal.create("-17.20"));
    in.vector[2].set(HiveDecimal.create("70.00"));
    d = HiveDecimal.create("4.75");
    expr = new DecimalColModuloDecimalScalar(0, d, 2);
    expr.evaluate(b);
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.15")));
    assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.95")));
    assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("3.50")));
    // try a zero-divide to show a repeating NULL is produced
    d = HiveDecimal.create("0.00");
    expr = new DecimalColModuloDecimalScalar(0, d, 2);
    expr.evaluate(b);
    assertFalse(r.noNulls);
    assertTrue(r.isNull[0]);
    assertTrue(r.isRepeating);
}
Also used : VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) TestVectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch) DecimalColModuloDecimalScalar(org.apache.hadoop.hive.ql.exec.vector.expressions.gen.DecimalColModuloDecimalScalar) DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) Test(org.junit.Test)

Example 55 with HiveDecimal

use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.

the class TestVectorArithmeticExpressions method testDecimalColAddDecimalScalar.

/* Test decimal column to decimal scalar addition. This is used to cover all the
   * cases used in the source code template ColumnArithmeticScalarDecimal.txt.
   */
@Test
public void testDecimalColAddDecimalScalar() {
    VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols();
    HiveDecimal d = HiveDecimal.create(1);
    VectorExpression expr = new DecimalColAddDecimalScalar(0, d, 2);
    // test without nulls
    expr.evaluate(b);
    DecimalColumnVector r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.20")));
    assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.30")));
    assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("1")));
    // test null propagation
    b = getVectorizedRowBatch3DecimalCols();
    DecimalColumnVector in = (DecimalColumnVector) b.cols[0];
    r = (DecimalColumnVector) b.cols[2];
    in.noNulls = false;
    in.isNull[0] = true;
    expr.evaluate(b);
    assertTrue(!r.noNulls);
    assertTrue(r.isNull[0]);
    // test repeating case, no nulls
    b = getVectorizedRowBatch3DecimalCols();
    in = (DecimalColumnVector) b.cols[0];
    in.isRepeating = true;
    expr.evaluate(b);
    r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.isRepeating);
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.20")));
    // test repeating case for null value
    b = getVectorizedRowBatch3DecimalCols();
    in = (DecimalColumnVector) b.cols[0];
    in.isRepeating = true;
    in.isNull[0] = true;
    in.noNulls = false;
    expr.evaluate(b);
    r = (DecimalColumnVector) b.cols[2];
    assertTrue(r.isRepeating);
    assertTrue(!r.noNulls);
    assertTrue(r.isNull[0]);
    // test that overflow produces null
    b = getVectorizedRowBatch3DecimalCols();
    in = (DecimalColumnVector) b.cols[0];
    // set to max possible value
    in.vector[0].set(HiveDecimal.create("9999999999999999.99"));
    expr.evaluate(b);
    r = (DecimalColumnVector) b.cols[2];
    assertFalse(r.noNulls);
    assertTrue(r.isNull[0]);
}
Also used : VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) TestVectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch) DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) DecimalColAddDecimalScalar(org.apache.hadoop.hive.ql.exec.vector.expressions.gen.DecimalColAddDecimalScalar) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) Test(org.junit.Test)

Aggregations

HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)94 Test (org.junit.Test)28 Timestamp (java.sql.Timestamp)24 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)23 DecimalColumnVector (org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector)22 Text (org.apache.hadoop.io.Text)22 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)21 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)21 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)20 Date (java.sql.Date)19 VectorizedRowBatch (org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch)18 BytesWritable (org.apache.hadoop.io.BytesWritable)17 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)16 HiveIntervalYearMonth (org.apache.hadoop.hive.common.type.HiveIntervalYearMonth)15 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)15 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)15 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)14 IntWritable (org.apache.hadoop.io.IntWritable)14 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)13 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)13