use of org.apache.hadoop.hive.serde2.io.ShortWritable in project hive by apache.
the class TestGenericUDFOPMultiply method testByteTimesShort.
@Test
public void testByteTimesShort() throws HiveException {
GenericUDFOPMultiply udf = new GenericUDFOPMultiply();
ByteWritable left = new ByteWritable((byte) 4);
ShortWritable right = new ShortWritable((short) 6);
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableByteObjectInspector, PrimitiveObjectInspectorFactory.writableShortObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(oi.getTypeInfo(), TypeInfoFactory.shortTypeInfo);
ShortWritable res = (ShortWritable) udf.evaluate(args);
Assert.assertEquals(24, res.get());
}
use of org.apache.hadoop.hive.serde2.io.ShortWritable in project hive by apache.
the class TestOrcFile method testSeek.
@Test
public void testSeek() throws Exception {
ObjectInspector inspector;
synchronized (TestOrcFile.class) {
inspector = ObjectInspectorFactory.getReflectionObjectInspector(BigRow.class, ObjectInspectorFactory.ObjectInspectorOptions.JAVA);
}
Writer writer = OrcFile.createWriter(testFilePath, OrcFile.writerOptions(conf).inspector(inspector).stripeSize(200000).bufferSize(65536).rowIndexStride(1000));
Random rand = new Random(42);
final int COUNT = 32768;
long[] intValues = new long[COUNT];
double[] doubleValues = new double[COUNT];
String[] stringValues = new String[COUNT];
BytesWritable[] byteValues = new BytesWritable[COUNT];
String[] words = new String[128];
for (int i = 0; i < words.length; ++i) {
words[i] = Integer.toHexString(rand.nextInt());
}
for (int i = 0; i < COUNT / 2; ++i) {
intValues[2 * i] = rand.nextLong();
intValues[2 * i + 1] = intValues[2 * i];
stringValues[2 * i] = words[rand.nextInt(words.length)];
stringValues[2 * i + 1] = stringValues[2 * i];
}
for (int i = 0; i < COUNT; ++i) {
doubleValues[i] = rand.nextDouble();
byte[] buf = new byte[20];
rand.nextBytes(buf);
byteValues[i] = new BytesWritable(buf);
}
for (int i = 0; i < COUNT; ++i) {
writer.addRow(createRandomRow(intValues, doubleValues, stringValues, byteValues, words, i));
}
writer.close();
Reader reader = OrcFile.createReader(testFilePath, OrcFile.readerOptions(conf).filesystem(fs));
assertEquals(COUNT, reader.getNumberOfRows());
RecordReader rows = reader.rows();
OrcStruct row = null;
for (int i = COUNT - 1; i >= 0; --i) {
// we load the previous buffer of rows
if (i % COUNT == COUNT - 1) {
rows.seekToRow(i - (COUNT - 1));
}
rows.seekToRow(i);
row = (OrcStruct) rows.next(row);
BigRow expected = createRandomRow(intValues, doubleValues, stringValues, byteValues, words, i);
// assertEquals(expected, row);
assertEquals(expected.boolean1.booleanValue(), ((BooleanWritable) row.getFieldValue(0)).get());
assertEquals(expected.byte1.byteValue(), ((ByteWritable) row.getFieldValue(1)).get());
assertEquals(expected.short1.shortValue(), ((ShortWritable) row.getFieldValue(2)).get());
assertEquals(expected.int1.intValue(), ((IntWritable) row.getFieldValue(3)).get());
assertEquals(expected.long1.longValue(), ((LongWritable) row.getFieldValue(4)).get());
assertEquals(expected.float1, ((FloatWritable) row.getFieldValue(5)).get(), 0.0001);
assertEquals(expected.double1, ((DoubleWritable) row.getFieldValue(6)).get(), 0.0001);
assertEquals(expected.bytes1, row.getFieldValue(7));
assertEquals(expected.string1, row.getFieldValue(8));
List<InnerStruct> expectedList = expected.middle.list;
List<OrcStruct> actualList = (List<OrcStruct>) ((OrcStruct) row.getFieldValue(9)).getFieldValue(0);
compareList(expectedList, actualList);
compareList(expected.list, (List<OrcStruct>) row.getFieldValue(10));
}
rows.close();
Iterator<StripeInformation> stripeIterator = reader.getStripes().iterator();
long offsetOfStripe2 = 0;
long offsetOfStripe4 = 0;
long lastRowOfStripe2 = 0;
for (int i = 0; i < 5; ++i) {
StripeInformation stripe = stripeIterator.next();
if (i < 2) {
lastRowOfStripe2 += stripe.getNumberOfRows();
} else if (i == 2) {
offsetOfStripe2 = stripe.getOffset();
lastRowOfStripe2 += stripe.getNumberOfRows() - 1;
} else if (i == 4) {
offsetOfStripe4 = stripe.getOffset();
}
}
boolean[] columns = new boolean[reader.getStatistics().length];
// long colulmn
columns[5] = true;
// text column
columns[9] = true;
rows = reader.rowsOptions(new Reader.Options().range(offsetOfStripe2, offsetOfStripe4 - offsetOfStripe2).include(columns));
rows.seekToRow(lastRowOfStripe2);
for (int i = 0; i < 2; ++i) {
row = (OrcStruct) rows.next(row);
BigRow expected = createRandomRow(intValues, doubleValues, stringValues, byteValues, words, (int) (lastRowOfStripe2 + i));
assertEquals(expected.long1.longValue(), ((LongWritable) row.getFieldValue(4)).get());
assertEquals(expected.string1, row.getFieldValue(8));
}
rows.close();
}
use of org.apache.hadoop.hive.serde2.io.ShortWritable in project hive by apache.
the class TestOrcFile method testZeroCopySeek.
@Test
public void testZeroCopySeek() throws Exception {
ObjectInspector inspector;
synchronized (TestOrcFile.class) {
inspector = ObjectInspectorFactory.getReflectionObjectInspector(BigRow.class, ObjectInspectorFactory.ObjectInspectorOptions.JAVA);
}
Writer writer = OrcFile.createWriter(testFilePath, OrcFile.writerOptions(conf).inspector(inspector).stripeSize(200000).bufferSize(65536).rowIndexStride(1000));
Random rand = new Random(42);
final int COUNT = 32768;
long[] intValues = new long[COUNT];
double[] doubleValues = new double[COUNT];
String[] stringValues = new String[COUNT];
BytesWritable[] byteValues = new BytesWritable[COUNT];
String[] words = new String[128];
for (int i = 0; i < words.length; ++i) {
words[i] = Integer.toHexString(rand.nextInt());
}
for (int i = 0; i < COUNT / 2; ++i) {
intValues[2 * i] = rand.nextLong();
intValues[2 * i + 1] = intValues[2 * i];
stringValues[2 * i] = words[rand.nextInt(words.length)];
stringValues[2 * i + 1] = stringValues[2 * i];
}
for (int i = 0; i < COUNT; ++i) {
doubleValues[i] = rand.nextDouble();
byte[] buf = new byte[20];
rand.nextBytes(buf);
byteValues[i] = new BytesWritable(buf);
}
for (int i = 0; i < COUNT; ++i) {
writer.addRow(createRandomRow(intValues, doubleValues, stringValues, byteValues, words, i));
}
writer.close();
writer = null;
Reader reader = OrcFile.createReader(testFilePath, OrcFile.readerOptions(conf).filesystem(fs));
assertEquals(COUNT, reader.getNumberOfRows());
/* enable zero copy record reader */
Configuration conf = new Configuration();
conf.setBoolean(OrcConf.USE_ZEROCOPY.getHiveConfName(), true);
RecordReader rows = reader.rows();
/* all tests are identical to the other seek() tests */
OrcStruct row = null;
for (int i = COUNT - 1; i >= 0; --i) {
// we load the previous buffer of rows
if (i % COUNT == COUNT - 1) {
rows.seekToRow(i - (COUNT - 1));
}
rows.seekToRow(i);
row = (OrcStruct) rows.next(row);
BigRow expected = createRandomRow(intValues, doubleValues, stringValues, byteValues, words, i);
assertEquals(expected.boolean1.booleanValue(), ((BooleanWritable) row.getFieldValue(0)).get());
assertEquals(expected.byte1.byteValue(), ((ByteWritable) row.getFieldValue(1)).get());
assertEquals(expected.short1.shortValue(), ((ShortWritable) row.getFieldValue(2)).get());
assertEquals(expected.int1.intValue(), ((IntWritable) row.getFieldValue(3)).get());
assertEquals(expected.long1.longValue(), ((LongWritable) row.getFieldValue(4)).get());
assertEquals(expected.float1.floatValue(), ((FloatWritable) row.getFieldValue(5)).get(), 0.0001);
assertEquals(expected.double1.doubleValue(), ((DoubleWritable) row.getFieldValue(6)).get(), 0.0001);
assertEquals(expected.bytes1, row.getFieldValue(7));
assertEquals(expected.string1, row.getFieldValue(8));
List<InnerStruct> expectedList = expected.middle.list;
List<OrcStruct> actualList = (List) ((OrcStruct) row.getFieldValue(9)).getFieldValue(0);
compareList(expectedList, actualList);
compareList(expected.list, (List) row.getFieldValue(10));
}
rows.close();
Iterator<StripeInformation> stripeIterator = reader.getStripes().iterator();
long offsetOfStripe2 = 0;
long offsetOfStripe4 = 0;
long lastRowOfStripe2 = 0;
for (int i = 0; i < 5; ++i) {
StripeInformation stripe = stripeIterator.next();
if (i < 2) {
lastRowOfStripe2 += stripe.getNumberOfRows();
} else if (i == 2) {
offsetOfStripe2 = stripe.getOffset();
lastRowOfStripe2 += stripe.getNumberOfRows() - 1;
} else if (i == 4) {
offsetOfStripe4 = stripe.getOffset();
}
}
boolean[] columns = new boolean[reader.getStatistics().length];
// long colulmn
columns[5] = true;
// text column
columns[9] = true;
/* use zero copy record reader */
rows = reader.rowsOptions(new Reader.Options().range(offsetOfStripe2, offsetOfStripe4 - offsetOfStripe2).include(columns));
rows.seekToRow(lastRowOfStripe2);
for (int i = 0; i < 2; ++i) {
row = (OrcStruct) rows.next(row);
BigRow expected = createRandomRow(intValues, doubleValues, stringValues, byteValues, words, (int) (lastRowOfStripe2 + i));
assertEquals(expected.long1.longValue(), ((LongWritable) row.getFieldValue(4)).get());
assertEquals(expected.string1, row.getFieldValue(8));
}
rows.close();
}
use of org.apache.hadoop.hive.serde2.io.ShortWritable in project hive by apache.
the class TestGenericUDFFloor method testShort.
@Test
public void testShort() throws HiveException {
GenericUDFFloor udf = new GenericUDFFloor();
ShortWritable input = new ShortWritable((short) 74);
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableShortObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(input) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.longTypeInfo, oi.getTypeInfo());
LongWritable res = (LongWritable) udf.evaluate(args);
Assert.assertEquals(74L, res.get());
}
use of org.apache.hadoop.hive.serde2.io.ShortWritable in project hive by apache.
the class KafkaJsonSerDe method parseAsPrimitive.
private Object parseAsPrimitive(JsonNode value, TypeInfo typeInfo) throws SerDeException {
switch(TypeInfoFactory.getPrimitiveTypeInfo(typeInfo.getTypeName()).getPrimitiveCategory()) {
case TIMESTAMP:
TimestampWritable timestampWritable = new TimestampWritable();
timestampWritable.setTime(TS_PARSER.get().parseMillis(value.textValue()));
return timestampWritable;
case TIMESTAMPLOCALTZ:
final long numberOfMillis = TS_PARSER.get().parseMillis(value.textValue());
return new TimestampLocalTZWritable(new TimestampTZ(ZonedDateTime.ofInstant(Instant.ofEpochMilli(numberOfMillis), ((TimestampLocalTZTypeInfo) typeInfo).timeZone())));
case BYTE:
return new ByteWritable((byte) value.intValue());
case SHORT:
return (new ShortWritable(value.shortValue()));
case INT:
return new IntWritable(value.intValue());
case LONG:
return (new LongWritable((value.longValue())));
case FLOAT:
return (new FloatWritable(value.floatValue()));
case DOUBLE:
return (new DoubleWritable(value.doubleValue()));
case DECIMAL:
return (new HiveDecimalWritable(HiveDecimal.create(value.decimalValue())));
case CHAR:
return (new HiveCharWritable(new HiveChar(value.textValue(), ((CharTypeInfo) typeInfo).getLength())));
case VARCHAR:
return (new HiveVarcharWritable(new HiveVarchar(value.textValue(), ((CharTypeInfo) typeInfo).getLength())));
case STRING:
return (new Text(value.textValue()));
case BOOLEAN:
return (new BooleanWritable(value.isBoolean() ? value.booleanValue() : Boolean.valueOf(value.textValue())));
default:
throw new SerDeException("Unknown type: " + typeInfo.getTypeName());
}
}
Aggregations