use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class TestDateTimeMath method checkTsIntervalDayTimeArithmetic.
private static void checkTsIntervalDayTimeArithmetic(String left, char operationType, String right, String expected) throws Exception {
Timestamp leftTs = null;
if (left != null) {
leftTs = Timestamp.valueOf(left);
}
HiveIntervalDayTime rightInterval = right == null ? null : HiveIntervalDayTime.valueOf(right);
Timestamp expectedResult = null;
if (expected != null) {
expectedResult = Timestamp.valueOf(expected);
}
Timestamp testResult = null;
DateTimeMath dtm = new DateTimeMath();
switch(operationType) {
case '-':
testResult = dtm.subtract(leftTs, rightInterval);
break;
case '+':
testResult = dtm.add(leftTs, rightInterval);
break;
default:
throw new IllegalArgumentException("Invalid operation " + operationType);
}
assertEquals(String.format("%s %s %s", leftTs, operationType, rightInterval), expectedResult, testResult);
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class VerifyFast 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("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 MyTestPrimitiveClass method getRandIntervalDayTime.
public static HiveIntervalDayTime getRandIntervalDayTime(Random r) {
String optionalNanos = "";
if (r.nextInt(2) == 1) {
optionalNanos = String.format(".%09d", Integer.valueOf(0 + r.nextInt(DateUtils.NANOS_PER_SEC)));
}
String yearMonthSignStr = r.nextInt(2) == 0 ? "" : "-";
String dayTimeStr = String.format("%s%d %02d:%02d:%02d%s", yearMonthSignStr, // day
Integer.valueOf(1 + r.nextInt(28)), // hour
Integer.valueOf(0 + r.nextInt(24)), // minute
Integer.valueOf(0 + r.nextInt(60)), // second
Integer.valueOf(0 + r.nextInt(60)), optionalNanos);
HiveIntervalDayTime intervalDayTimeVal = HiveIntervalDayTime.valueOf(dayTimeStr);
TestCase.assertTrue(intervalDayTimeVal != null);
return intervalDayTimeVal;
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class TestHiveIntervalDayTimeWritable method testGettersSetters.
@Test
@Concurrent(count = 4)
@Repeating(repetition = 100)
public void testGettersSetters() throws Exception {
HiveIntervalDayTimeWritable hiw1 = new HiveIntervalDayTimeWritable();
hiw1.set(3, 4, 5, 6, 7);
HiveIntervalDayTime hi1 = hiw1.getHiveIntervalDayTime();
assertEquals(3, hi1.getDays());
assertEquals(4, hi1.getHours());
assertEquals(5, hi1.getMinutes());
assertEquals(6, hi1.getSeconds());
assertEquals(7, hi1.getNanos());
}
use of org.apache.hadoop.hive.common.type.HiveIntervalDayTime in project hive by apache.
the class VectorizedBatchUtil method debugFormatOneRow.
public static StringBuilder debugFormatOneRow(VectorizedRowBatch batch, int index, String prefix, StringBuilder sb) {
sb.append(prefix + " row " + index + " ");
for (int p = 0; p < batch.projectionSize; p++) {
int column = batch.projectedColumns[p];
sb.append("(" + p + "," + column + ") ");
ColumnVector colVector = batch.cols[column];
if (colVector == null) {
sb.append("(null ColumnVector)");
} else {
boolean isRepeating = colVector.isRepeating;
if (isRepeating) {
sb.append("(repeating)");
}
index = (isRepeating ? 0 : index);
if (colVector.noNulls || !colVector.isNull[index]) {
if (colVector instanceof LongColumnVector) {
sb.append(((LongColumnVector) colVector).vector[index]);
} else if (colVector instanceof DoubleColumnVector) {
sb.append(((DoubleColumnVector) colVector).vector[index]);
} else if (colVector instanceof BytesColumnVector) {
BytesColumnVector bytesColumnVector = (BytesColumnVector) colVector;
byte[] bytes = bytesColumnVector.vector[index];
int start = bytesColumnVector.start[index];
int length = bytesColumnVector.length[index];
if (bytes == null) {
sb.append("(Unexpected null bytes with start " + start + " length " + length + ")");
} else {
sb.append("bytes: '" + displayBytes(bytes, start, length) + "'");
}
} else if (colVector instanceof DecimalColumnVector) {
sb.append(((DecimalColumnVector) colVector).vector[index].toString());
} else if (colVector instanceof TimestampColumnVector) {
Timestamp timestamp = new Timestamp(0);
((TimestampColumnVector) colVector).timestampUpdate(timestamp, index);
sb.append(timestamp.toString());
} else if (colVector instanceof IntervalDayTimeColumnVector) {
HiveIntervalDayTime intervalDayTime = ((IntervalDayTimeColumnVector) colVector).asScratchIntervalDayTime(index);
sb.append(intervalDayTime.toString());
} else {
sb.append("Unknown");
}
} else {
sb.append("NULL");
}
}
sb.append(" ");
}
return sb;
}
Aggregations