use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.
the class VectorUDAFSumDecimal64ToDecimal method assignRowColumn.
@Override
public void assignRowColumn(VectorizedRowBatch batch, int batchIndex, int columnNum, AggregationBuffer agg) throws HiveException {
DecimalColumnVector outputColVector = (DecimalColumnVector) batch.cols[columnNum];
Aggregation myagg = (Aggregation) agg;
final boolean isNull;
if (!myagg.isNull) {
if (!myagg.usingRegularDecimal) {
myagg.regularDecimalSum.deserialize64(myagg.sum, inputScale);
} else {
myagg.temp.deserialize64(myagg.sum, inputScale);
myagg.regularDecimalSum.mutateAdd(myagg.temp);
}
// Now, check for overflow.
myagg.regularDecimalSum.mutateEnforcePrecisionScale(outputDecimalTypeInfo.getPrecision(), outputDecimalTypeInfo.getScale());
isNull = !myagg.regularDecimalSum.isSet();
} else {
isNull = true;
}
if (isNull) {
outputColVector.noNulls = false;
outputColVector.isNull[batchIndex] = true;
return;
}
outputColVector.isNull[batchIndex] = false;
outputColVector.set(batchIndex, myagg.regularDecimalSum);
}
use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.
the class VectorizedListColumnReader method fillColumnVector.
private void fillColumnVector(PrimitiveObjectInspector.PrimitiveCategory category, ListColumnVector lcv, List valueList, int elementNum) {
int total = valueList.size();
setChildrenInfo(lcv, total, elementNum);
switch(category) {
case BOOLEAN:
lcv.child = new LongColumnVector(total);
for (int i = 0; i < valueList.size(); i++) {
if (valueList.get(i) == null) {
lcv.child.isNull[i] = true;
} else {
((LongColumnVector) lcv.child).vector[i] = ((List<Integer>) valueList).get(i);
}
}
break;
case INT:
case BYTE:
case SHORT:
case DATE:
case INTERVAL_YEAR_MONTH:
case LONG:
lcv.child = new LongColumnVector(total);
for (int i = 0; i < valueList.size(); i++) {
if (valueList.get(i) == null) {
lcv.child.isNull[i] = true;
} else {
((LongColumnVector) lcv.child).vector[i] = ((List<Long>) valueList).get(i);
}
}
break;
case DOUBLE:
lcv.child = new DoubleColumnVector(total);
for (int i = 0; i < valueList.size(); i++) {
if (valueList.get(i) == null) {
lcv.child.isNull[i] = true;
} else {
((DoubleColumnVector) lcv.child).vector[i] = ((List<Double>) valueList).get(i);
}
}
break;
case BINARY:
case STRING:
case CHAR:
case VARCHAR:
lcv.child = new BytesColumnVector(total);
lcv.child.init();
for (int i = 0; i < valueList.size(); i++) {
byte[] src = ((List<byte[]>) valueList).get(i);
if (src == null) {
((BytesColumnVector) lcv.child).setRef(i, src, 0, 0);
lcv.child.isNull[i] = true;
} else {
((BytesColumnVector) lcv.child).setRef(i, src, 0, src.length);
}
}
break;
case FLOAT:
lcv.child = new DoubleColumnVector(total);
for (int i = 0; i < valueList.size(); i++) {
if (valueList.get(i) == null) {
lcv.child.isNull[i] = true;
} else {
((DoubleColumnVector) lcv.child).vector[i] = ((List<Float>) valueList).get(i);
}
}
break;
case DECIMAL:
decimalTypeCheck(type);
DecimalLogicalTypeAnnotation logicalType = (DecimalLogicalTypeAnnotation) type.getLogicalTypeAnnotation();
int precision = logicalType.getPrecision();
int scale = logicalType.getScale();
lcv.child = new DecimalColumnVector(total, precision, scale);
for (int i = 0; i < valueList.size(); i++) {
if (valueList.get(i) == null) {
lcv.child.isNull[i] = true;
} else {
((DecimalColumnVector) lcv.child).vector[i].set(((List<byte[]>) valueList).get(i), scale);
}
}
break;
case INTERVAL_DAY_TIME:
case TIMESTAMP:
default:
throw new RuntimeException("Unsupported type in the list: " + type);
}
}
use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.
the class VectorizedPrimitiveColumnReader method decodeDictionaryIds.
/**
* Reads `num` values into column, decoding the values from `dictionaryIds` and `dictionary`.
*/
private void decodeDictionaryIds(int rowId, int num, ColumnVector column, TypeInfo columnType, LongColumnVector dictionaryIds) {
System.arraycopy(dictionaryIds.isNull, rowId, column.isNull, rowId, num);
if (column.noNulls) {
column.noNulls = dictionaryIds.noNulls;
}
column.isRepeating = column.isRepeating && dictionaryIds.isRepeating;
PrimitiveTypeInfo primitiveColumnType = (PrimitiveTypeInfo) columnType;
switch(primitiveColumnType.getPrimitiveCategory()) {
case INT:
for (int i = rowId; i < rowId + num; ++i) {
((LongColumnVector) column).vector[i] = dictionary.readInteger((int) dictionaryIds.vector[i]);
if (!dictionary.isValid()) {
setNullValue(column, i);
((LongColumnVector) column).vector[i] = 0;
}
}
break;
case BYTE:
for (int i = rowId; i < rowId + num; ++i) {
((LongColumnVector) column).vector[i] = dictionary.readTinyInt((int) dictionaryIds.vector[i]);
if (!dictionary.isValid()) {
setNullValue(column, i);
((LongColumnVector) column).vector[i] = 0;
}
}
break;
case SHORT:
for (int i = rowId; i < rowId + num; ++i) {
((LongColumnVector) column).vector[i] = dictionary.readSmallInt((int) dictionaryIds.vector[i]);
if (!dictionary.isValid()) {
setNullValue(column, i);
((LongColumnVector) column).vector[i] = 0;
}
}
break;
case DATE:
DateColumnVector dc = (DateColumnVector) column;
dc.setUsingProlepticCalendar(true);
for (int i = rowId; i < rowId + num; ++i) {
dc.vector[i] = skipProlepticConversion ? dictionary.readLong((int) dictionaryIds.vector[i]) : CalendarUtils.convertDateToProleptic((int) dictionary.readLong((int) dictionaryIds.vector[i]));
if (!dictionary.isValid()) {
setNullValue(column, i);
dc.vector[i] = 0;
}
}
break;
case INTERVAL_YEAR_MONTH:
case LONG:
for (int i = rowId; i < rowId + num; ++i) {
((LongColumnVector) column).vector[i] = dictionary.readLong((int) dictionaryIds.vector[i]);
if (!dictionary.isValid()) {
setNullValue(column, i);
((LongColumnVector) column).vector[i] = 0;
}
}
break;
case BOOLEAN:
for (int i = rowId; i < rowId + num; ++i) {
((LongColumnVector) column).vector[i] = dictionary.readBoolean((int) dictionaryIds.vector[i]) ? 1 : 0;
}
break;
case DOUBLE:
for (int i = rowId; i < rowId + num; ++i) {
((DoubleColumnVector) column).vector[i] = dictionary.readDouble((int) dictionaryIds.vector[i]);
if (!dictionary.isValid()) {
setNullValue(column, i);
((DoubleColumnVector) column).vector[i] = 0;
}
}
break;
case BINARY:
for (int i = rowId; i < rowId + num; ++i) {
((BytesColumnVector) column).setVal(i, dictionary.readBytes((int) dictionaryIds.vector[i]));
}
break;
case STRING:
for (int i = rowId; i < rowId + num; ++i) {
((BytesColumnVector) column).setVal(i, dictionary.readString((int) dictionaryIds.vector[i]));
}
break;
case VARCHAR:
for (int i = rowId; i < rowId + num; ++i) {
((BytesColumnVector) column).setVal(i, dictionary.readVarchar((int) dictionaryIds.vector[i]));
}
break;
case CHAR:
for (int i = rowId; i < rowId + num; ++i) {
((BytesColumnVector) column).setVal(i, dictionary.readChar((int) dictionaryIds.vector[i]));
}
break;
case FLOAT:
for (int i = rowId; i < rowId + num; ++i) {
((DoubleColumnVector) column).vector[i] = dictionary.readFloat((int) dictionaryIds.vector[i]);
if (!dictionary.isValid()) {
setNullValue(column, i);
((DoubleColumnVector) column).vector[i] = 0;
}
}
break;
case DECIMAL:
DecimalLogicalTypeAnnotation decimalLogicalType = null;
if (type.getLogicalTypeAnnotation() instanceof DecimalLogicalTypeAnnotation) {
decimalLogicalType = (DecimalLogicalTypeAnnotation) type.getLogicalTypeAnnotation();
}
DecimalColumnVector decimalColumnVector = ((DecimalColumnVector) column);
byte[] decimalData = null;
fillDecimalPrecisionScale(decimalLogicalType, decimalColumnVector);
for (int i = rowId; i < rowId + num; ++i) {
decimalData = dictionary.readDecimal((int) dictionaryIds.vector[i]);
if (dictionary.isValid()) {
decimalColumnVector.vector[i].set(decimalData, decimalColumnVector.scale);
} else {
setNullValue(column, i);
}
}
break;
case TIMESTAMP:
TimestampColumnVector tsc = (TimestampColumnVector) column;
tsc.setUsingProlepticCalendar(true);
for (int i = rowId; i < rowId + num; ++i) {
tsc.set(i, dictionary.readTimestamp((int) dictionaryIds.vector[i]).toSqlTimestamp());
}
break;
case INTERVAL_DAY_TIME:
default:
throw new UnsupportedOperationException("Unsupported type: " + type);
}
}
use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.
the class TestConstantVectorExpression method testConstantExpression.
@Test
public void testConstantExpression() throws Exception {
ConstantVectorExpression longCve = new ConstantVectorExpression(0, 17, TypeInfoFactory.longTypeInfo);
ConstantVectorExpression doubleCve = new ConstantVectorExpression(1, 17.34, TypeInfoFactory.doubleTypeInfo);
String str = "alpha";
ConstantVectorExpression bytesCve = new ConstantVectorExpression(2, str.getBytes(), TypeInfoFactory.stringTypeInfo);
HiveDecimal decVal = HiveDecimal.create("25.8");
ConstantVectorExpression decimalCve = new ConstantVectorExpression(3, decVal, TypeInfoFactory.decimalTypeInfo);
ConstantVectorExpression nullCve = new ConstantVectorExpression(4, TypeInfoFactory.stringTypeInfo, true);
int size = 20;
VectorizedRowBatch vrg = VectorizedRowGroupGenUtil.getVectorizedRowBatch(size, 5, 0);
LongColumnVector lcv = (LongColumnVector) vrg.cols[0];
DoubleColumnVector dcv = new DoubleColumnVector(size);
BytesColumnVector bcv = new BytesColumnVector(size);
DecimalColumnVector dv = new DecimalColumnVector(5, 1);
BytesColumnVector bcvn = new BytesColumnVector(size);
vrg.cols[1] = dcv;
vrg.cols[2] = bcv;
vrg.cols[3] = dv;
vrg.cols[4] = bcvn;
longCve.evaluate(vrg);
doubleCve.evaluate(vrg);
bytesCve.evaluate(vrg);
decimalCve.evaluate(vrg);
nullCve.evaluate(vrg);
assertTrue(lcv.isRepeating);
assertTrue(dcv.isRepeating);
assertTrue(bcv.isRepeating);
assertEquals(17, lcv.vector[0]);
assertTrue(17.34 == dcv.vector[0]);
assertTrue(bcvn.isRepeating);
assertTrue(bcvn.isNull[0]);
assertTrue(!bcvn.noNulls);
byte[] alphaBytes = "alpha".getBytes();
assertTrue(bcv.length[0] == alphaBytes.length);
assertTrue(sameFirstKBytes(alphaBytes, bcv.vector[0], alphaBytes.length));
// Evaluation of the bytes Constant Vector Expression after the vector is
// modified.
((BytesColumnVector) (vrg.cols[2])).vector[0] = "beta".getBytes();
bytesCve.evaluate(vrg);
assertTrue(bcv.length[0] == alphaBytes.length);
assertTrue(sameFirstKBytes(alphaBytes, bcv.vector[0], alphaBytes.length));
assertTrue(25.8 == dv.vector[0].getHiveDecimal().doubleValue());
// Evaluation of the decimal Constant Vector Expression after the vector is
// modified.
((DecimalColumnVector) (vrg.cols[3])).vector[0].set(HiveDecimal.create("39.7"));
decimalCve.evaluate(vrg);
assertTrue(25.8 == dv.vector[0].getHiveDecimal().doubleValue());
}
use of org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector in project hive by apache.
the class TestVectorArithmeticExpressions method testDecimalColMultiplyDecimalScalar.
/* Spot check correctness of decimal column multiply decimal scalar. The case for
* addition checks all the cases for the template, so don't do that redundantly here.
*/
@Test
public void testDecimalColMultiplyDecimalScalar() throws HiveException {
VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols();
HiveDecimal d = HiveDecimal.create(2);
VectorExpression expr = new DecimalColMultiplyDecimalScalar(0, d, 2);
// test without nulls
expr.evaluate(b);
DecimalColumnVector r = (DecimalColumnVector) b.cols[2];
assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("2.40")));
assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-6.60")));
assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0")));
// test that overflow produces null
b = getVectorizedRowBatch3DecimalCols();
DecimalColumnVector 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]);
}
Aggregations