Search in sources :

Example 66 with DecimalColumnVector

use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.

the class TestVectorTypeCasts method getBatchDecimalTimestamp.

private VectorizedRowBatch getBatchDecimalTimestamp(double[] doubleValues) {
    VectorizedRowBatch b = new VectorizedRowBatch(2);
    DecimalColumnVector dv;
    b.cols[0] = dv = new DecimalColumnVector(doubleValues.length, HiveDecimal.SYSTEM_DEFAULT_PRECISION, HiveDecimal.SYSTEM_DEFAULT_SCALE);
    b.cols[1] = new TimestampColumnVector(doubleValues.length);
    dv.noNulls = true;
    Random r = new Random(94830);
    for (int i = 0; i < doubleValues.length; i++) {
        long millis = RandomTypeUtil.randomMillis(r);
        Timestamp ts = new Timestamp(millis);
        int nanos = RandomTypeUtil.randomNanos(r);
        ts.setNanos(nanos);
        TimestampWritable tsw = new TimestampWritable(ts);
        double asDouble = tsw.getDouble();
        doubleValues[i] = asDouble;
        HiveDecimal hiveDecimal = HiveDecimal.create(new BigDecimal(asDouble));
        dv.set(i, hiveDecimal);
    }
    b.size = doubleValues.length;
    return b;
}
Also used : VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) TimestampColumnVector(org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector) DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) Random(java.util.Random) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 67 with DecimalColumnVector

use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.

the class TestVectorTypeCasts method testCastLongToDecimal.

@Test
public void testCastLongToDecimal() {
    VectorizedRowBatch b = getBatchLongDecimal();
    VectorExpression expr = new CastLongToDecimal(0, 1);
    expr.evaluate(b);
    DecimalColumnVector r = (DecimalColumnVector) b.cols[1];
    assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0")));
    assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1")));
    assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("99999999999999")));
}
Also used : VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) Test(org.junit.Test)

Example 68 with DecimalColumnVector

use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.

the class TestVectorTypeCasts method testCastDecimalToBoolean.

@Test
public /* Just spot check the basic case because code path is the same as
   * for cast of decimal to long due to inheritance.
   */
void testCastDecimalToBoolean() {
    VectorizedRowBatch b = getBatchDecimalLong();
    VectorExpression expr = new CastDecimalToBoolean(0, 1);
    DecimalColumnVector in = (DecimalColumnVector) b.cols[0];
    in.vector[1].set(HiveDecimal.create(0));
    expr.evaluate(b);
    LongColumnVector r = (LongColumnVector) b.cols[1];
    assertEquals(1, r.vector[0]);
    assertEquals(0, r.vector[1]);
    assertEquals(1, r.vector[2]);
}
Also used : VectorizedRowBatch(org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch) DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) LongColumnVector(org.apache.hadoop.hive.ql.exec.vector.LongColumnVector) Test(org.junit.Test)

Example 69 with DecimalColumnVector

use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.

the class VectorizedRowGroupGenUtil method generateDecimalColumnVector.

public static DecimalColumnVector generateDecimalColumnVector(DecimalTypeInfo typeInfo, boolean nulls, boolean repeating, int size, Random rand) {
    DecimalColumnVector dcv = new DecimalColumnVector(size, typeInfo.precision(), typeInfo.scale());
    dcv.noNulls = !nulls;
    dcv.isRepeating = repeating;
    HiveDecimalWritable repeatingValue = new HiveDecimalWritable();
    do {
        repeatingValue.set(HiveDecimal.create(((Double) rand.nextDouble()).toString()).setScale((short) typeInfo.scale(), HiveDecimal.ROUND_HALF_UP));
    } while (repeatingValue.getHiveDecimal().doubleValue() == 0);
    int nullFrequency = generateNullFrequency(rand);
    for (int i = 0; i < size; i++) {
        if (nulls && (repeating || i % nullFrequency == 0)) {
            dcv.isNull[i] = true;
            dcv.vector[i] = null;
        } else {
            dcv.isNull[i] = false;
            if (repeating) {
                dcv.vector[i].set(repeatingValue);
            } else {
                dcv.vector[i].set(HiveDecimal.create(((Double) rand.nextDouble()).toString()).setScale((short) typeInfo.scale(), HiveDecimal.ROUND_HALF_UP));
            }
            if (dcv.vector[i].getHiveDecimal().doubleValue() == 0) {
                i--;
            }
        }
    }
    return dcv;
}
Also used : DecimalColumnVector(org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)

Aggregations

DecimalColumnVector (org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector)69 VectorizedRowBatch (org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch)38 Test (org.junit.Test)28 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)24 TestVectorizedRowBatch (org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch)16 LongColumnVector (org.apache.hadoop.hive.ql.exec.vector.LongColumnVector)14 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)13 BytesColumnVector (org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector)10 DoubleColumnVector (org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector)10 TimestampColumnVector (org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector)8 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)5 Random (java.util.Random)4 Timestamp (java.sql.Timestamp)3 ColumnVector (org.apache.hadoop.hive.ql.exec.vector.ColumnVector)3 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)3 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)3 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)3 BooleanWritable (org.apache.hadoop.io.BooleanWritable)3 IntWritable (org.apache.hadoop.io.IntWritable)3 LongWritable (org.apache.hadoop.io.LongWritable)3