use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.
the class GenericUDFTrunc method evaluateNumber.
private Object evaluateNumber(DeferredObject[] arguments) throws HiveException, UDFArgumentTypeException {
if (arguments[0] == null) {
return null;
}
Object input = arguments[0].get();
if (input == null) {
return null;
}
if (arguments.length == 2 && arguments[1] != null && arguments[1].get() != null && !inputSacleConst) {
Object scaleObj = null;
switch(inputScaleOI.getPrimitiveCategory()) {
case BYTE:
scaleObj = byteConverter.convert(arguments[1].get());
scale = ((ByteWritable) scaleObj).get();
break;
case SHORT:
scaleObj = shortConverter.convert(arguments[1].get());
scale = ((ShortWritable) scaleObj).get();
break;
case INT:
scaleObj = intConverter.convert(arguments[1].get());
scale = ((IntWritable) scaleObj).get();
break;
case LONG:
scaleObj = longConverter.convert(arguments[1].get());
long l = ((LongWritable) scaleObj).get();
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new UDFArgumentException(getFuncName().toUpperCase() + " scale argument out of allowed range");
}
scale = (int) l;
default:
break;
}
}
switch(inputType1) {
case VOID:
return null;
case DECIMAL:
HiveDecimalWritable decimalWritable = (HiveDecimalWritable) inputOI.getPrimitiveWritableObject(input);
HiveDecimal dec = trunc(decimalWritable.getHiveDecimal(), scale);
if (dec == null) {
return null;
}
return new HiveDecimalWritable(dec);
case BYTE:
ByteWritable byteWritable = (ByteWritable) inputOI.getPrimitiveWritableObject(input);
if (scale >= 0) {
return byteWritable;
} else {
return new ByteWritable((byte) trunc(byteWritable.get(), scale));
}
case SHORT:
ShortWritable shortWritable = (ShortWritable) inputOI.getPrimitiveWritableObject(input);
if (scale >= 0) {
return shortWritable;
} else {
return new ShortWritable((short) trunc(shortWritable.get(), scale));
}
case INT:
IntWritable intWritable = (IntWritable) inputOI.getPrimitiveWritableObject(input);
if (scale >= 0) {
return intWritable;
} else {
return new IntWritable((int) trunc(intWritable.get(), scale));
}
case LONG:
LongWritable longWritable = (LongWritable) inputOI.getPrimitiveWritableObject(input);
if (scale >= 0) {
return longWritable;
} else {
return new LongWritable(trunc(longWritable.get(), scale));
}
case FLOAT:
float f = ((FloatWritable) inputOI.getPrimitiveWritableObject(input)).get();
return new FloatWritable((float) trunc(f, scale));
case DOUBLE:
return trunc(((DoubleWritable) inputOI.getPrimitiveWritableObject(input)), scale);
default:
throw new UDFArgumentTypeException(0, "Only numeric or string group data types are allowed for TRUNC function. Got " + inputType1.name());
}
}
use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.
the class TestVectorSerDeRow method serializeRow.
private Output serializeRow(Object[] row, VectorRandomRowSource source, SerializeWrite serializeWrite) throws HiveException, IOException {
Output output = new Output();
serializeWrite.set(output);
PrimitiveTypeInfo[] primitiveTypeInfos = source.primitiveTypeInfos();
for (int i = 0; i < primitiveTypeInfos.length; i++) {
Object object = row[i];
PrimitiveCategory primitiveCategory = primitiveTypeInfos[i].getPrimitiveCategory();
switch(primitiveCategory) {
case BOOLEAN:
{
BooleanWritable expectedWritable = (BooleanWritable) object;
boolean value = expectedWritable.get();
serializeWrite.writeBoolean(value);
}
break;
case BYTE:
{
ByteWritable expectedWritable = (ByteWritable) object;
byte value = expectedWritable.get();
serializeWrite.writeByte(value);
}
break;
case SHORT:
{
ShortWritable expectedWritable = (ShortWritable) object;
short value = expectedWritable.get();
serializeWrite.writeShort(value);
}
break;
case INT:
{
IntWritable expectedWritable = (IntWritable) object;
int value = expectedWritable.get();
serializeWrite.writeInt(value);
}
break;
case LONG:
{
LongWritable expectedWritable = (LongWritable) object;
long value = expectedWritable.get();
serializeWrite.writeLong(value);
}
break;
case DATE:
{
DateWritable expectedWritable = (DateWritable) object;
Date value = expectedWritable.get();
serializeWrite.writeDate(value);
}
break;
case FLOAT:
{
FloatWritable expectedWritable = (FloatWritable) object;
float value = expectedWritable.get();
serializeWrite.writeFloat(value);
}
break;
case DOUBLE:
{
DoubleWritable expectedWritable = (DoubleWritable) object;
double value = expectedWritable.get();
serializeWrite.writeDouble(value);
}
break;
case STRING:
{
Text text = (Text) object;
serializeWrite.writeString(text.getBytes(), 0, text.getLength());
}
break;
case CHAR:
{
HiveCharWritable expectedWritable = (HiveCharWritable) object;
HiveChar value = expectedWritable.getHiveChar();
serializeWrite.writeHiveChar(value);
}
break;
case VARCHAR:
{
HiveVarcharWritable expectedWritable = (HiveVarcharWritable) object;
HiveVarchar value = expectedWritable.getHiveVarchar();
serializeWrite.writeHiveVarchar(value);
}
break;
case BINARY:
{
BytesWritable expectedWritable = (BytesWritable) object;
byte[] bytes = expectedWritable.getBytes();
int length = expectedWritable.getLength();
serializeWrite.writeBinary(bytes, 0, length);
}
break;
case TIMESTAMP:
{
TimestampWritable expectedWritable = (TimestampWritable) object;
Timestamp value = expectedWritable.getTimestamp();
serializeWrite.writeTimestamp(value);
}
break;
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonthWritable expectedWritable = (HiveIntervalYearMonthWritable) object;
HiveIntervalYearMonth value = expectedWritable.getHiveIntervalYearMonth();
serializeWrite.writeHiveIntervalYearMonth(value);
}
break;
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTimeWritable expectedWritable = (HiveIntervalDayTimeWritable) object;
HiveIntervalDayTime value = expectedWritable.getHiveIntervalDayTime();
serializeWrite.writeHiveIntervalDayTime(value);
}
break;
case DECIMAL:
{
HiveDecimalWritable expectedWritable = (HiveDecimalWritable) object;
HiveDecimal value = expectedWritable.getHiveDecimal();
serializeWrite.writeHiveDecimal(value, ((DecimalTypeInfo) primitiveTypeInfos[i]).scale());
}
break;
default:
throw new HiveException("Unexpected primitive category " + primitiveCategory);
}
}
return output;
}
use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.
the class TestVectorSerDeRow method deserializeAndVerify.
void deserializeAndVerify(Output output, DeserializeRead deserializeRead, VectorRandomRowSource source, Object[] expectedRow) throws HiveException, IOException {
deserializeRead.set(output.getData(), 0, output.getLength());
PrimitiveCategory[] primitiveCategories = source.primitiveCategories();
for (int i = 0; i < primitiveCategories.length; i++) {
Object expected = expectedRow[i];
PrimitiveCategory primitiveCategory = primitiveCategories[i];
PrimitiveTypeInfo primitiveTypeInfo = source.primitiveTypeInfos()[i];
if (!deserializeRead.readNextField()) {
throw new HiveException("Unexpected NULL when reading primitiveCategory " + primitiveCategory + " expected (" + expected.getClass().getName() + ", " + expected.toString() + ") " + " deserializeRead " + deserializeRead.getClass().getName());
}
switch(primitiveCategory) {
case BOOLEAN:
{
Boolean value = deserializeRead.currentBoolean;
BooleanWritable expectedWritable = (BooleanWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Boolean field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case BYTE:
{
Byte value = deserializeRead.currentByte;
ByteWritable expectedWritable = (ByteWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Byte field mismatch (expected " + (int) expected + " found " + (int) value + ")");
}
}
break;
case SHORT:
{
Short value = deserializeRead.currentShort;
ShortWritable expectedWritable = (ShortWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Short field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case INT:
{
Integer value = deserializeRead.currentInt;
IntWritable expectedWritable = (IntWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Int field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case LONG:
{
Long value = deserializeRead.currentLong;
LongWritable expectedWritable = (LongWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Long field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case DATE:
{
DateWritable value = deserializeRead.currentDateWritable;
DateWritable expectedWritable = (DateWritable) expected;
if (!value.equals(expectedWritable)) {
TestCase.fail("Date field mismatch (expected " + expected.toString() + " found " + value.toString() + ")");
}
}
break;
case FLOAT:
{
Float value = deserializeRead.currentFloat;
FloatWritable expectedWritable = (FloatWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Float field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case DOUBLE:
{
Double value = deserializeRead.currentDouble;
DoubleWritable expectedWritable = (DoubleWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Double field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case STRING:
case CHAR:
case VARCHAR:
case BINARY:
{
byte[] stringBytes = Arrays.copyOfRange(deserializeRead.currentBytes, deserializeRead.currentBytesStart, deserializeRead.currentBytesStart + deserializeRead.currentBytesLength);
Text text = new Text(stringBytes);
String string = text.toString();
switch(primitiveCategory) {
case STRING:
{
Text expectedWritable = (Text) expected;
if (!string.equals(expectedWritable.toString())) {
TestCase.fail("String field mismatch (expected '" + expectedWritable.toString() + "' found '" + string + "')");
}
}
break;
case CHAR:
{
HiveChar hiveChar = new HiveChar(string, ((CharTypeInfo) primitiveTypeInfo).getLength());
HiveCharWritable expectedWritable = (HiveCharWritable) expected;
if (!hiveChar.equals(expectedWritable.getHiveChar())) {
TestCase.fail("Char field mismatch (expected '" + expectedWritable.getHiveChar() + "' found '" + hiveChar + "')");
}
}
break;
case VARCHAR:
{
HiveVarchar hiveVarchar = new HiveVarchar(string, ((VarcharTypeInfo) primitiveTypeInfo).getLength());
HiveVarcharWritable expectedWritable = (HiveVarcharWritable) expected;
if (!hiveVarchar.equals(expectedWritable.getHiveVarchar())) {
TestCase.fail("Varchar field mismatch (expected '" + expectedWritable.getHiveVarchar() + "' found '" + hiveVarchar + "')");
}
}
break;
case BINARY:
{
BytesWritable expectedWritable = (BytesWritable) expected;
if (stringBytes.length != expectedWritable.getLength()) {
TestCase.fail("Byte Array field mismatch (expected " + expected + " found " + stringBytes + ")");
}
byte[] expectedBytes = expectedWritable.getBytes();
for (int b = 0; b < stringBytes.length; b++) {
if (stringBytes[b] != expectedBytes[b]) {
TestCase.fail("Byte Array field mismatch (expected " + expected + " found " + stringBytes + ")");
}
}
}
break;
default:
throw new HiveException("Unexpected primitive category " + primitiveCategory);
}
}
break;
case DECIMAL:
{
HiveDecimal value = deserializeRead.currentHiveDecimalWritable.getHiveDecimal();
if (value == null) {
TestCase.fail("Decimal field evaluated to NULL");
}
HiveDecimalWritable expectedWritable = (HiveDecimalWritable) expected;
if (!value.equals(expectedWritable.getHiveDecimal())) {
DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) primitiveTypeInfo;
int precision = decimalTypeInfo.getPrecision();
int scale = decimalTypeInfo.getScale();
TestCase.fail("Decimal field mismatch (expected " + expectedWritable.getHiveDecimal() + " found " + value.toString() + ") precision " + precision + ", scale " + scale);
}
}
break;
case TIMESTAMP:
{
Timestamp value = deserializeRead.currentTimestampWritable.getTimestamp();
TimestampWritable expectedWritable = (TimestampWritable) expected;
if (!value.equals(expectedWritable.getTimestamp())) {
TestCase.fail("Timestamp field mismatch (expected " + expectedWritable.getTimestamp() + " found " + value.toString() + ")");
}
}
break;
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonth value = deserializeRead.currentHiveIntervalYearMonthWritable.getHiveIntervalYearMonth();
HiveIntervalYearMonthWritable expectedWritable = (HiveIntervalYearMonthWritable) expected;
HiveIntervalYearMonth expectedValue = expectedWritable.getHiveIntervalYearMonth();
if (!value.equals(expectedValue)) {
TestCase.fail("HiveIntervalYearMonth field mismatch (expected " + expectedValue + " found " + value.toString() + ")");
}
}
break;
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTime value = deserializeRead.currentHiveIntervalDayTimeWritable.getHiveIntervalDayTime();
HiveIntervalDayTimeWritable expectedWritable = (HiveIntervalDayTimeWritable) expected;
HiveIntervalDayTime expectedValue = expectedWritable.getHiveIntervalDayTime();
if (!value.equals(expectedValue)) {
TestCase.fail("HiveIntervalDayTime field mismatch (expected " + expectedValue + " found " + value.toString() + ")");
}
}
break;
default:
throw new HiveException("Unexpected primitive category " + primitiveCategory);
}
}
TestCase.assertTrue(deserializeRead.isEndOfInputReached());
}
use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.
the class VectorRandomRowSource method getRandHiveDecimal.
public static HiveDecimal getRandHiveDecimal(Random r, DecimalTypeInfo decimalTypeInfo) {
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(RandomTypeUtil.getRandString(r, DECIMAL_CHARS, integerDigits));
}
if (scale != 0) {
sb.append(".");
sb.append(RandomTypeUtil.getRandString(r, DECIMAL_CHARS, scale));
}
HiveDecimal dec = HiveDecimal.create(sb.toString());
return dec;
}
}
use of org.apache.hadoop.hive.common.type.HiveDecimal in project hive by apache.
the class TestConstantVectorExpression method testConstantExpression.
@Test
public void testConstantExpression() {
ConstantVectorExpression longCve = new ConstantVectorExpression(0, 17);
ConstantVectorExpression doubleCve = new ConstantVectorExpression(1, 17.34);
String str = "alpha";
ConstantVectorExpression bytesCve = new ConstantVectorExpression(2, str.getBytes());
HiveDecimal decVal = HiveDecimal.create("25.8");
ConstantVectorExpression decimalCve = new ConstantVectorExpression(3, decVal, "decimal");
ConstantVectorExpression nullCve = new ConstantVectorExpression(4, "string", 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());
}
Aggregations