use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class UDFSecond method evaluate.
public IntWritable evaluate(HiveIntervalDayTimeWritable i) {
if (i == null) {
return null;
}
HiveIntervalDayTime idt = i.getHiveIntervalDayTime();
result.set(idt.getSeconds());
return result;
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class VerifyFastRow method verifyDeserializeRead.
public static void verifyDeserializeRead(DeserializeRead deserializeRead, PrimitiveTypeInfo primitiveTypeInfo, Writable writable) throws IOException {
boolean isNull;
isNull = !deserializeRead.readNextField();
if (isNull) {
if (writable != null) {
TestCase.fail(deserializeRead.getClass().getName() + " field reports null but object is not null " + "(class " + writable.getClass().getName() + ", " + writable.toString() + ")");
}
return;
} else if (writable == null) {
TestCase.fail("Field report not null but object is null");
}
switch(primitiveTypeInfo.getPrimitiveCategory()) {
case BOOLEAN:
{
boolean value = deserializeRead.currentBoolean;
if (!(writable instanceof BooleanWritable)) {
TestCase.fail("Boolean expected writable not Boolean");
}
boolean expected = ((BooleanWritable) writable).get();
if (value != expected) {
TestCase.fail("Boolean field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case BYTE:
{
byte value = deserializeRead.currentByte;
if (!(writable instanceof ByteWritable)) {
TestCase.fail("Byte expected writable not Byte");
}
byte expected = ((ByteWritable) writable).get();
if (value != expected) {
TestCase.fail("Byte field mismatch (expected " + (int) expected + " found " + (int) value + ")");
}
}
break;
case SHORT:
{
short value = deserializeRead.currentShort;
if (!(writable instanceof ShortWritable)) {
TestCase.fail("Short expected writable not Short");
}
short expected = ((ShortWritable) writable).get();
if (value != expected) {
TestCase.fail("Short field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case INT:
{
int value = deserializeRead.currentInt;
if (!(writable instanceof IntWritable)) {
TestCase.fail("Integer expected writable not Integer");
}
int expected = ((IntWritable) writable).get();
if (value != expected) {
TestCase.fail("Int field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case LONG:
{
long value = deserializeRead.currentLong;
if (!(writable instanceof LongWritable)) {
TestCase.fail("Long expected writable not Long");
}
Long expected = ((LongWritable) writable).get();
if (value != expected) {
TestCase.fail("Long field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case FLOAT:
{
float value = deserializeRead.currentFloat;
if (!(writable instanceof FloatWritable)) {
TestCase.fail("Float expected writable not Float");
}
float expected = ((FloatWritable) writable).get();
if (value != expected) {
TestCase.fail("Float field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case DOUBLE:
{
double value = deserializeRead.currentDouble;
if (!(writable instanceof DoubleWritable)) {
TestCase.fail("Double expected writable not Double");
}
double expected = ((DoubleWritable) writable).get();
if (value != expected) {
TestCase.fail("Double field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case STRING:
{
byte[] stringBytes = Arrays.copyOfRange(deserializeRead.currentBytes, deserializeRead.currentBytesStart, deserializeRead.currentBytesStart + deserializeRead.currentBytesLength);
Text text = new Text(stringBytes);
String string = text.toString();
String expected = ((Text) writable).toString();
if (!string.equals(expected)) {
TestCase.fail("String field mismatch (expected '" + expected + "' found '" + string + "')");
}
}
break;
case CHAR:
{
byte[] stringBytes = Arrays.copyOfRange(deserializeRead.currentBytes, deserializeRead.currentBytesStart, deserializeRead.currentBytesStart + deserializeRead.currentBytesLength);
Text text = new Text(stringBytes);
String string = text.toString();
HiveChar hiveChar = new HiveChar(string, ((CharTypeInfo) primitiveTypeInfo).getLength());
HiveChar expected = ((HiveCharWritable) writable).getHiveChar();
if (!hiveChar.equals(expected)) {
TestCase.fail("Char field mismatch (expected '" + expected + "' found '" + hiveChar + "')");
}
}
break;
case VARCHAR:
{
byte[] stringBytes = Arrays.copyOfRange(deserializeRead.currentBytes, deserializeRead.currentBytesStart, deserializeRead.currentBytesStart + deserializeRead.currentBytesLength);
Text text = new Text(stringBytes);
String string = text.toString();
HiveVarchar hiveVarchar = new HiveVarchar(string, ((VarcharTypeInfo) primitiveTypeInfo).getLength());
HiveVarchar expected = ((HiveVarcharWritable) writable).getHiveVarchar();
if (!hiveVarchar.equals(expected)) {
TestCase.fail("Varchar field mismatch (expected '" + expected + "' found '" + hiveVarchar + "')");
}
}
break;
case DECIMAL:
{
HiveDecimal value = deserializeRead.currentHiveDecimalWritable.getHiveDecimal();
if (value == null) {
TestCase.fail("Decimal field evaluated to NULL");
}
HiveDecimal expected = ((HiveDecimalWritable) writable).getHiveDecimal();
if (!value.equals(expected)) {
DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) primitiveTypeInfo;
int precision = decimalTypeInfo.getPrecision();
int scale = decimalTypeInfo.getScale();
TestCase.fail("Decimal field mismatch (expected " + expected.toString() + " found " + value.toString() + ") precision " + precision + ", scale " + scale);
}
}
break;
case DATE:
{
Date value = deserializeRead.currentDateWritable.get();
Date expected = ((DateWritable) writable).get();
if (!value.equals(expected)) {
TestCase.fail("Date field mismatch (expected " + expected.toString() + " found " + value.toString() + ")");
}
}
break;
case TIMESTAMP:
{
Timestamp value = deserializeRead.currentTimestampWritable.getTimestamp();
Timestamp expected = ((TimestampWritable) writable).getTimestamp();
if (!value.equals(expected)) {
TestCase.fail("Timestamp field mismatch (expected " + expected.toString() + " found " + value.toString() + ")");
}
}
break;
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonth value = deserializeRead.currentHiveIntervalYearMonthWritable.getHiveIntervalYearMonth();
HiveIntervalYearMonth expected = ((HiveIntervalYearMonthWritable) writable).getHiveIntervalYearMonth();
if (!value.equals(expected)) {
TestCase.fail("HiveIntervalYearMonth field mismatch (expected " + expected.toString() + " found " + value.toString() + ")");
}
}
break;
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTime value = deserializeRead.currentHiveIntervalDayTimeWritable.getHiveIntervalDayTime();
HiveIntervalDayTime expected = ((HiveIntervalDayTimeWritable) writable).getHiveIntervalDayTime();
if (!value.equals(expected)) {
TestCase.fail("HiveIntervalDayTime field mismatch (expected " + expected.toString() + " found " + value.toString() + ")");
}
}
break;
case BINARY:
{
byte[] byteArray = Arrays.copyOfRange(deserializeRead.currentBytes, deserializeRead.currentBytesStart, deserializeRead.currentBytesStart + deserializeRead.currentBytesLength);
BytesWritable bytesWritable = (BytesWritable) writable;
byte[] expected = Arrays.copyOfRange(bytesWritable.getBytes(), 0, bytesWritable.getLength());
if (byteArray.length != expected.length) {
TestCase.fail("Byte Array field mismatch (expected " + Arrays.toString(expected) + " found " + Arrays.toString(byteArray) + ")");
}
for (int b = 0; b < byteArray.length; b++) {
if (byteArray[b] != expected[b]) {
TestCase.fail("Byte Array field mismatch (expected " + Arrays.toString(expected) + " found " + Arrays.toString(byteArray) + ")");
}
}
}
break;
default:
throw new Error("Unknown primitive category " + primitiveTypeInfo.getPrimitiveCategory());
}
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class GenericUDFOPNegative method evaluate.
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
if (arguments[0] == null) {
return null;
}
Object input = arguments[0].get();
if (input == null) {
return null;
}
input = converter.convert(input);
if (input == null) {
return null;
}
switch(resultOI.getPrimitiveCategory()) {
case BYTE:
byteWritable.set((byte) -(((ByteWritable) input).get()));
return byteWritable;
case SHORT:
shortWritable.set((short) -(((ShortWritable) input).get()));
return shortWritable;
case INT:
intWritable.set(-(((IntWritable) input).get()));
return intWritable;
case LONG:
longWritable.set(-(((LongWritable) input).get()));
return longWritable;
case FLOAT:
floatWritable.set(-(((FloatWritable) input).get()));
return floatWritable;
case DOUBLE:
doubleWritable.set(-(((DoubleWritable) input).get()));
return doubleWritable;
case DECIMAL:
decimalWritable.set((HiveDecimalWritable) input);
decimalWritable.mutateNegate();
return decimalWritable;
case INTERVAL_YEAR_MONTH:
HiveIntervalYearMonth intervalYearMonth = ((HiveIntervalYearMonthWritable) input).getHiveIntervalYearMonth();
this.intervalYearMonthWritable.set(intervalYearMonth.negate());
return this.intervalYearMonthWritable;
case INTERVAL_DAY_TIME:
HiveIntervalDayTime intervalDayTime = ((HiveIntervalDayTimeWritable) input).getHiveIntervalDayTime();
this.intervalDayTimeWritable.set(intervalDayTime.negate());
return intervalDayTimeWritable;
default:
// Should never happen.
throw new RuntimeException("Unexpected type in evaluating " + opName + ": " + resultOI.getPrimitiveCategory());
}
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class cbo_rp_TestJdbcDriver2 method testIntervalTypes.
@Test
public void testIntervalTypes() throws Exception {
Statement stmt = con.createStatement();
// Since interval types not currently supported as table columns, need to create them
// as expressions.
ResultSet res = stmt.executeQuery("select case when c17 is null then null else interval '1' year end as col1," + " c17 - c17 as col2 from " + dataTypeTableName + " order by col1");
ResultSetMetaData meta = res.getMetaData();
assertEquals("col1", meta.getColumnLabel(1));
assertEquals(java.sql.Types.OTHER, meta.getColumnType(1));
assertEquals("interval_year_month", meta.getColumnTypeName(1));
assertEquals(11, meta.getColumnDisplaySize(1));
assertEquals(11, meta.getPrecision(1));
assertEquals(0, meta.getScale(1));
assertEquals(HiveIntervalYearMonth.class.getName(), meta.getColumnClassName(1));
assertEquals("col2", meta.getColumnLabel(2));
assertEquals(java.sql.Types.OTHER, meta.getColumnType(2));
assertEquals("interval_day_time", meta.getColumnTypeName(2));
assertEquals(29, meta.getColumnDisplaySize(2));
assertEquals(29, meta.getPrecision(2));
assertEquals(0, meta.getScale(2));
assertEquals(HiveIntervalDayTime.class.getName(), meta.getColumnClassName(2));
// row 1 - results should be null
assertTrue(res.next());
// skip the last (partitioning) column since it is always non-null
for (int i = 1; i < meta.getColumnCount(); i++) {
assertNull("Column " + i + " should be null", res.getObject(i));
}
// row 2 - results should be null
assertTrue(res.next());
for (int i = 1; i < meta.getColumnCount(); i++) {
assertNull("Column " + i + " should be null", res.getObject(i));
}
// row 3
assertTrue(res.next());
assertEquals("1-0", res.getString(1));
assertEquals(1, ((HiveIntervalYearMonth) res.getObject(1)).getYears());
assertEquals("0 00:00:00.000000000", res.getString(2));
assertEquals(0, ((HiveIntervalDayTime) res.getObject(2)).getDays());
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class VectorAssignRow method assignConvertRowColumn.
/**
* Convert row's column object and then assign it the ColumnVector at batchIndex
* in the VectorizedRowBatch.
*
* Public so VectorDeserializeRow can use this method to convert a row's column object.
*
* @param batch
* @param batchIndex
* @param logicalColumnIndex
* @param object The row column object whose type is the VectorAssignRow.initConversion
* source data type.
*
*/
public void assignConvertRowColumn(VectorizedRowBatch batch, int batchIndex, int logicalColumnIndex, Object object) {
Preconditions.checkState(isConvert[logicalColumnIndex]);
Category targetCategory = targetCategories[logicalColumnIndex];
if (targetCategory == null) {
/*
* This is a column that we don't want (i.e. not included) -- we are done.
*/
return;
}
final int projectionColumnNum = projectionColumnNums[logicalColumnIndex];
if (object == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
try {
switch(targetCategory) {
case PRIMITIVE:
PrimitiveCategory targetPrimitiveCategory = targetPrimitiveCategories[logicalColumnIndex];
switch(targetPrimitiveCategory) {
case VOID:
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
case BOOLEAN:
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = (PrimitiveObjectInspectorUtils.getBoolean(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]) ? 1 : 0);
break;
case BYTE:
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getByte(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
break;
case SHORT:
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getShort(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
break;
case INT:
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getInt(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
break;
case LONG:
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getLong(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
break;
case TIMESTAMP:
{
Timestamp timestamp = PrimitiveObjectInspectorUtils.getTimestamp(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (timestamp == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
((TimestampColumnVector) batch.cols[projectionColumnNum]).set(batchIndex, timestamp);
}
break;
case DATE:
{
Date date = PrimitiveObjectInspectorUtils.getDate(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (date == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
DateWritable dateWritable = (DateWritable) convertTargetWritables[logicalColumnIndex];
dateWritable.set(date);
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = dateWritable.getDays();
}
break;
case FLOAT:
((DoubleColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getFloat(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
break;
case DOUBLE:
((DoubleColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = PrimitiveObjectInspectorUtils.getDouble(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
break;
case BINARY:
{
BytesWritable bytesWritable = PrimitiveObjectInspectorUtils.getBinary(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (bytesWritable == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
((BytesColumnVector) batch.cols[projectionColumnNum]).setVal(batchIndex, bytesWritable.getBytes(), 0, bytesWritable.getLength());
}
break;
case STRING:
{
String string = PrimitiveObjectInspectorUtils.getString(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (string == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
Text text = (Text) convertTargetWritables[logicalColumnIndex];
text.set(string);
((BytesColumnVector) batch.cols[projectionColumnNum]).setVal(batchIndex, text.getBytes(), 0, text.getLength());
}
break;
case VARCHAR:
{
// UNDONE: Performance problem with conversion to String, then bytes...
HiveVarchar hiveVarchar = PrimitiveObjectInspectorUtils.getHiveVarchar(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (hiveVarchar == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
// TODO: Do we need maxLength checking?
byte[] bytes = hiveVarchar.getValue().getBytes();
((BytesColumnVector) batch.cols[projectionColumnNum]).setVal(batchIndex, bytes, 0, bytes.length);
}
break;
case CHAR:
{
// UNDONE: Performance problem with conversion to String, then bytes...
HiveChar hiveChar = PrimitiveObjectInspectorUtils.getHiveChar(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (hiveChar == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
// We store CHAR in vector row batch with padding stripped.
// TODO: Do we need maxLength checking?
byte[] bytes = hiveChar.getStrippedValue().getBytes();
((BytesColumnVector) batch.cols[projectionColumnNum]).setVal(batchIndex, bytes, 0, bytes.length);
}
break;
case DECIMAL:
{
HiveDecimal hiveDecimal = PrimitiveObjectInspectorUtils.getHiveDecimal(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (hiveDecimal == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
((DecimalColumnVector) batch.cols[projectionColumnNum]).set(batchIndex, hiveDecimal);
}
break;
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonth intervalYearMonth = PrimitiveObjectInspectorUtils.getHiveIntervalYearMonth(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (intervalYearMonth == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
((LongColumnVector) batch.cols[projectionColumnNum]).vector[batchIndex] = intervalYearMonth.getTotalMonths();
}
break;
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTime intervalDayTime = PrimitiveObjectInspectorUtils.getHiveIntervalDayTime(object, convertSourcePrimitiveObjectInspectors[logicalColumnIndex]);
if (intervalDayTime == null) {
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
((IntervalDayTimeColumnVector) batch.cols[projectionColumnNum]).set(batchIndex, intervalDayTime);
}
break;
default:
throw new RuntimeException("Primitive category " + targetPrimitiveCategory.name() + " not supported");
}
break;
default:
throw new RuntimeException("Category " + targetCategory.name() + " not supported");
}
} catch (NumberFormatException e) {
// Some of the conversion methods throw this exception on numeric parsing errors.
VectorizedBatchUtil.setNullColIsNullValue(batch.cols[projectionColumnNum], batchIndex);
return;
}
// We always set the null flag to false when there is a value.
batch.cols[projectionColumnNum].isNull[batchIndex] = false;
}
Aggregations