Search in sources :

Example 21 with HiveDecimal

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

the class MyTestPrimitiveClass method getRandHiveDecimal.

public static HiveDecimal getRandHiveDecimal(Random r, ExtraTypeInfo extraTypeInfo) {
    while (true) {
        StringBuilder sb = new StringBuilder();
        int precision = 1 + r.nextInt(18);
        int scale = 0 + r.nextInt(precision + 1);
        int integerDigits = precision - scale;
        if (r.nextBoolean()) {
            sb.append("-");
        }
        if (integerDigits == 0) {
            sb.append("0");
        } else {
            sb.append(getRandString(r, DECIMAL_CHARS, integerDigits));
        }
        if (scale != 0) {
            sb.append(".");
            sb.append(getRandString(r, DECIMAL_CHARS, scale));
        }
        HiveDecimal dec = HiveDecimal.create(sb.toString());
        extraTypeInfo.precision = dec.precision();
        extraTypeInfo.scale = dec.scale();
        return dec;
    }
}
Also used : HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal)

Example 22 with HiveDecimal

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

the class TestTimestampWritable method testDecimalToTimestampCornerCases.

@Test
@Concurrent(count = 4)
@Repeating(repetition = 100)
public void testDecimalToTimestampCornerCases() {
    Timestamp ts = new Timestamp(parseToMillis("1969-03-04 05:44:33"));
    assertEquals(0, ts.getTime() % 1000);
    for (int nanos : new int[] { 100000, 900000, 999100000, 999900000 }) {
        ts.setNanos(nanos);
        HiveDecimal d = timestampToDecimal(ts);
        assertEquals(ts, TimestampUtils.decimalToTimestamp(d));
        assertEquals(ts, TimestampUtils.doubleToTimestamp(d.bigDecimalValue().doubleValue()));
    }
}
Also used : HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) Timestamp(java.sql.Timestamp)

Example 23 with HiveDecimal

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

the class TestHiveDecimalWritable method testHiveDecimalWritable.

@Test
public void testHiveDecimalWritable() {
    HiveDecimalWritable decWritable;
    HiveDecimal nullDec = null;
    decWritable = new HiveDecimalWritable(nullDec);
    assertTrue(!decWritable.isSet());
    decWritable = new HiveDecimalWritable("1");
    assertTrue(decWritable.isSet());
// UNDONE: more!
}
Also used : HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal)

Example 24 with HiveDecimal

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

the class StatsUtils method getColStatistics.

/**
   * Convert ColumnStatisticsObj to ColStatistics
   * @param cso
   *          - ColumnStatisticsObj
   * @param tabName
   *          - table name
   * @param colName
   *          - column name
   * @return ColStatistics
   */
public static ColStatistics getColStatistics(ColumnStatisticsObj cso, String tabName, String colName) {
    String colTypeLowerCase = cso.getColType().toLowerCase();
    ColStatistics cs = new ColStatistics(colName, colTypeLowerCase);
    ColumnStatisticsData csd = cso.getStatsData();
    if (colTypeLowerCase.equals(serdeConstants.TINYINT_TYPE_NAME) || colTypeLowerCase.equals(serdeConstants.SMALLINT_TYPE_NAME) || colTypeLowerCase.equals(serdeConstants.INT_TYPE_NAME)) {
        cs.setCountDistint(csd.getLongStats().getNumDVs());
        cs.setNumNulls(csd.getLongStats().getNumNulls());
        cs.setAvgColLen(JavaDataModel.get().primitive1());
        cs.setRange(csd.getLongStats().getLowValue(), csd.getLongStats().getHighValue());
    } else if (colTypeLowerCase.equals(serdeConstants.BIGINT_TYPE_NAME)) {
        cs.setCountDistint(csd.getLongStats().getNumDVs());
        cs.setNumNulls(csd.getLongStats().getNumNulls());
        cs.setAvgColLen(JavaDataModel.get().primitive2());
        cs.setRange(csd.getLongStats().getLowValue(), csd.getLongStats().getHighValue());
    } else if (colTypeLowerCase.equals(serdeConstants.FLOAT_TYPE_NAME)) {
        cs.setCountDistint(csd.getDoubleStats().getNumDVs());
        cs.setNumNulls(csd.getDoubleStats().getNumNulls());
        cs.setAvgColLen(JavaDataModel.get().primitive1());
        cs.setRange(csd.getDoubleStats().getLowValue(), csd.getDoubleStats().getHighValue());
    } else if (colTypeLowerCase.equals(serdeConstants.DOUBLE_TYPE_NAME)) {
        cs.setCountDistint(csd.getDoubleStats().getNumDVs());
        cs.setNumNulls(csd.getDoubleStats().getNumNulls());
        cs.setAvgColLen(JavaDataModel.get().primitive2());
        cs.setRange(csd.getDoubleStats().getLowValue(), csd.getDoubleStats().getHighValue());
    } else if (colTypeLowerCase.equals(serdeConstants.STRING_TYPE_NAME) || colTypeLowerCase.startsWith(serdeConstants.CHAR_TYPE_NAME) || colTypeLowerCase.startsWith(serdeConstants.VARCHAR_TYPE_NAME)) {
        cs.setCountDistint(csd.getStringStats().getNumDVs());
        cs.setNumNulls(csd.getStringStats().getNumNulls());
        cs.setAvgColLen(csd.getStringStats().getAvgColLen());
    } else if (colTypeLowerCase.equals(serdeConstants.BOOLEAN_TYPE_NAME)) {
        if (csd.getBooleanStats().getNumFalses() > 0 && csd.getBooleanStats().getNumTrues() > 0) {
            cs.setCountDistint(2);
        } else {
            cs.setCountDistint(1);
        }
        cs.setNumTrues(csd.getBooleanStats().getNumTrues());
        cs.setNumFalses(csd.getBooleanStats().getNumFalses());
        cs.setNumNulls(csd.getBooleanStats().getNumNulls());
        cs.setAvgColLen(JavaDataModel.get().primitive1());
    } else if (colTypeLowerCase.equals(serdeConstants.BINARY_TYPE_NAME)) {
        cs.setAvgColLen(csd.getBinaryStats().getAvgColLen());
        cs.setNumNulls(csd.getBinaryStats().getNumNulls());
    } else if (colTypeLowerCase.equals(serdeConstants.TIMESTAMP_TYPE_NAME)) {
        cs.setAvgColLen(JavaDataModel.get().lengthOfTimestamp());
    } else if (colTypeLowerCase.startsWith(serdeConstants.DECIMAL_TYPE_NAME)) {
        cs.setAvgColLen(JavaDataModel.get().lengthOfDecimal());
        cs.setCountDistint(csd.getDecimalStats().getNumDVs());
        cs.setNumNulls(csd.getDecimalStats().getNumNulls());
        Decimal highValue = csd.getDecimalStats().getHighValue();
        Decimal lowValue = csd.getDecimalStats().getLowValue();
        if (highValue != null && highValue.getUnscaled() != null && lowValue != null && lowValue.getUnscaled() != null) {
            HiveDecimal maxHiveDec = HiveDecimal.create(new BigInteger(highValue.getUnscaled()), highValue.getScale());
            BigDecimal maxVal = maxHiveDec == null ? null : maxHiveDec.bigDecimalValue();
            HiveDecimal minHiveDec = HiveDecimal.create(new BigInteger(lowValue.getUnscaled()), lowValue.getScale());
            BigDecimal minVal = minHiveDec == null ? null : minHiveDec.bigDecimalValue();
            if (minVal != null && maxVal != null) {
                cs.setRange(minVal, maxVal);
            }
        }
    } else if (colTypeLowerCase.equals(serdeConstants.DATE_TYPE_NAME)) {
        cs.setAvgColLen(JavaDataModel.get().lengthOfDate());
        cs.setNumNulls(csd.getDateStats().getNumNulls());
        Long lowVal = (csd.getDateStats().getLowValue() != null) ? csd.getDateStats().getLowValue().getDaysSinceEpoch() : null;
        Long highVal = (csd.getDateStats().getHighValue() != null) ? csd.getDateStats().getHighValue().getDaysSinceEpoch() : null;
        cs.setRange(lowVal, highVal);
    } else {
        // Columns statistics for complex datatypes are not supported yet
        return null;
    }
    return cs;
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.apache.hadoop.hive.metastore.api.Decimal) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) ColStatistics(org.apache.hadoop.hive.ql.plan.ColStatistics) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) BigInteger(java.math.BigInteger) ColumnStatisticsData(org.apache.hadoop.hive.metastore.api.ColumnStatisticsData) BigDecimal(java.math.BigDecimal)

Example 25 with HiveDecimal

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

the class VectorizationContext method castConstantToLong.

private Long castConstantToLong(Object scalar, TypeInfo type, PrimitiveCategory integerPrimitiveCategory) throws HiveException {
    if (null == scalar) {
        return null;
    }
    PrimitiveTypeInfo ptinfo = (PrimitiveTypeInfo) type;
    String typename = type.getTypeName();
    switch(ptinfo.getPrimitiveCategory()) {
        case FLOAT:
        case DOUBLE:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
            return ((Number) scalar).longValue();
        case DECIMAL:
            HiveDecimal decimalVal = (HiveDecimal) scalar;
            switch(integerPrimitiveCategory) {
                case BYTE:
                    if (!decimalVal.isByte()) {
                        // Accurate byte value cannot be obtained.
                        return null;
                    }
                    break;
                case SHORT:
                    if (!decimalVal.isShort()) {
                        // Accurate short value cannot be obtained.
                        return null;
                    }
                    break;
                case INT:
                    if (!decimalVal.isInt()) {
                        // Accurate int value cannot be obtained.
                        return null;
                    }
                    break;
                case LONG:
                    if (!decimalVal.isLong()) {
                        // Accurate long value cannot be obtained.
                        return null;
                    }
                    break;
                default:
                    throw new RuntimeException("Unexpected integer primitive type " + integerPrimitiveCategory);
            }
            // We only store longs in our LongColumnVector.
            return decimalVal.longValue();
        default:
            throw new HiveException("Unsupported type " + typename + " for cast to Long");
    }
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) VectorUDAFMaxString(org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.gen.VectorUDAFMaxString) VectorUDAFMinString(org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.gen.VectorUDAFMinString) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Aggregations

HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)83 Test (org.junit.Test)28 DecimalColumnVector (org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector)24 VectorizedRowBatch (org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch)18 Text (org.apache.hadoop.io.Text)16 Timestamp (java.sql.Timestamp)15 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)15 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)15 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)14 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)13 Date (java.sql.Date)11 BytesWritable (org.apache.hadoop.io.BytesWritable)11 TestVectorizedRowBatch (org.apache.hadoop.hive.ql.exec.vector.TestVectorizedRowBatch)10 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)10 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)9 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)9 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)9 IntWritable (org.apache.hadoop.io.IntWritable)9 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)8 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)8