use of io.prestosql.spi.type.TimestampType in project hetu-core by openlookeng.
the class TypeUtils method getActualValue.
public static Object getActualValue(Type type, Object value) {
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
return value;
} else if (type instanceof BooleanType) {
return value;
} else if (type instanceof DoubleType) {
return value;
} else if (type instanceof DateType) {
// keep the `long` representation of date
return value;
} else if (type instanceof RealType) {
Long number = (Long) value;
return intBitsToFloat(number.intValue());
} else if (type instanceof VarcharType || type instanceof CharType) {
if (value instanceof Slice) {
return ((Slice) value).toStringUtf8();
}
return value;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(value instanceof Long);
return new BigDecimal(BigInteger.valueOf((Long) value), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
Slice slice;
if (value instanceof String) {
slice = Slices.utf8Slice((String) value);
} else {
checkState(value instanceof Slice);
slice = (Slice) value;
}
return new BigDecimal(decodeUnscaledValue(slice), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
} else if (type instanceof TimestampType) {
Long time = (Long) value;
return new Timestamp(time);
}
throw new UnsupportedOperationException("Not Implemented Exception: " + value + "->" + type);
}
use of io.prestosql.spi.type.TimestampType in project hetu-core by openlookeng.
the class TestHiveBucketing method testHashingCompare.
@Test
public void testHashingCompare() {
assertBucketEquals("boolean", null, 0, 0);
assertBucketEquals("boolean", true, 1, 1);
assertBucketEquals("boolean", false, 0, 0);
assertBucketEquals("tinyint", null, 0, 0);
assertBucketEquals("tinyint", (byte) 5, 5, 5);
assertBucketEquals("tinyint", Byte.MIN_VALUE, -128, -128);
assertBucketEquals("tinyint", Byte.MAX_VALUE, 127, 127);
assertBucketEquals("smallint", null, 0, 0);
assertBucketEquals("smallint", (short) 300, 300, 2107031704);
assertBucketEquals("smallint", Short.MIN_VALUE, -32768, 1342976838);
assertBucketEquals("smallint", Short.MAX_VALUE, 32767, -684075052);
assertBucketEquals("int", null, 0, 0);
assertBucketEquals("int", 300_000, 300000, -678663480);
assertBucketEquals("int", Integer.MIN_VALUE, -2147483648, 1194881028);
assertBucketEquals("int", Integer.MAX_VALUE, 2147483647, 1133859967);
assertBucketEquals("bigint", null, 0, 0);
assertBucketEquals("bigint", 300_000_000_000L, -647710651, -888935297);
assertBucketEquals("bigint", Long.MIN_VALUE, -2147483648, 1728983947);
assertBucketEquals("bigint", Long.MAX_VALUE, -2147483648, -536577852);
assertBucketEquals("float", null, 0, 0);
assertBucketEquals("float", 12.34F, 1095069860, -381747602);
assertBucketEquals("float", -Float.MAX_VALUE, -8388609, 470252243);
assertBucketEquals("float", Float.MIN_VALUE, 1, 1206721797);
assertBucketEquals("float", Float.POSITIVE_INFINITY, 2139095040, -292175804);
assertBucketEquals("float", Float.NEGATIVE_INFINITY, -8388608, -1433270801);
assertBucketEquals("float", Float.NaN, 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0xffc00000), 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0x7fc00000), 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0x7fc01234), 2143289344, -480354314);
// also a NaN
assertBucketEquals("float", intBitsToFloat(0xffc01234), 2143289344, -480354314);
assertBucketEquals("double", null, 0, 0);
assertBucketEquals("double", 12.34, 986311098, -2070733568);
assertBucketEquals("double", -Double.MAX_VALUE, 1048576, 14392725);
assertBucketEquals("double", Double.MIN_VALUE, 1, -8838199);
assertBucketEquals("double", Double.POSITIVE_INFINITY, 2146435072, 1614292060);
assertBucketEquals("double", Double.NEGATIVE_INFINITY, -1048576, 141388605);
assertBucketEquals("double", Double.NaN, 2146959360, 1138026565);
// also a NaN
assertBucketEquals("double", longBitsToDouble(0xfff8000000000000L), 2146959360, 1138026565);
// also a NaN
assertBucketEquals("double", longBitsToDouble(0x7ff8123412341234L), 2146959360, 1138026565);
// also a NaN
assertBucketEquals("double", longBitsToDouble(0xfff8123412341234L), 2146959360, 1138026565);
assertBucketEquals("varchar(15)", null, 0, 0);
assertBucketEquals("varchar(15)", "", 1, -965378730);
assertBucketEquals("varchar(15)", "test string", -189841218, -138301454);
// 3-byte UTF-8 sequences (in Basic Plane, i.e. Plane 0)
assertBucketEquals("varchar(15)", "\u5f3a\u5927\u7684Hetu\u5f15\u64ce", 481023052, 1436831192);
// 4 code points: 20FFC - 20FFF. 4-byte UTF-8 sequences in Supplementary Plane 2
assertBucketEquals("varchar(15)", "\uD843\uDFFC\uD843\uDFFD\uD843\uDFFE\uD843\uDFFF", -457487557, -697348811);
assertBucketEquals("string", null, 0, 0);
assertBucketEquals("string", "", 0, -965378730);
assertBucketEquals("string", "test string", -318923937, -138301454);
// 3-byte UTF-8 sequences (in Basic Plane, i.e. Plane 0)
assertBucketEquals("string", "\u5f3a\u5927\u7684Hetu\u5f15\u64ce", 889847277, 1436831192);
// 4 code points: 20FFC - 20FFF. 4-byte UTF-8 sequences in Supplementary Plane 2
assertBucketEquals("string", "\uD843\uDFFC\uD843\uDFFD\uD843\uDFFE\uD843\uDFFF", -1810797254, -697348811);
assertBucketEquals("char(6)", null, 0, 0);
assertBucketEquals("char(6)", "", 1, -965378730);
assertBucketEquals("char(6)", "test_1", 10333957, 1284522943);
assertBucketEquals("date", null, 0, 0);
assertBucketEquals("date", Date.valueOf("1970-01-01"), 0, 1362653161);
assertBucketEquals("date", Date.valueOf("2015-11-19"), 16758, 8542395);
assertBucketEquals("date", Date.valueOf("1950-11-19"), -6983, -431619185);
for (BucketingVersion version : BucketingVersion.values()) {
List<TypeInfo> typeInfos = ImmutableList.of(timestampTypeInfo);
assertThatThrownBy(() -> getBucketHashCode(version, typeInfos, new Object[] { 0 })).hasMessage("Computation of Hive bucket hashCode is not supported for Hive primitive category: TIMESTAMP");
TimestampType timestampType = TimestampType.TIMESTAMP;
BlockBuilder builder = timestampType.createBlockBuilder(null, 1);
timestampType.writeLong(builder, 0);
Page page = new Page(builder.build());
assertThatThrownBy(() -> getBucketHashCode(version, typeInfos, page, 0)).hasMessage("Computation of Hive bucket hashCode is not supported for Hive primitive category: TIMESTAMP");
}
assertBucketEquals("array<double>", null, 0, 0);
assertBucketEquals("array<boolean>", ImmutableList.of(), 0, 0);
assertBucketEquals("array<smallint>", ImmutableList.of((short) 5, (short) 8, (short) 13), 5066, -905011156);
assertBucketEquals("array<string>", ImmutableList.of("test1", "test2", "test3", "test4"), 957612994, 1305539282);
assertBucketEquals("array<array<bigint>>", ImmutableList.of(ImmutableList.of(10L, 20L), ImmutableList.of(-10L, -20L), asList((Object) null)), 326368, 611324477);
assertBucketEquals("map<float,date>", null, 0, 0);
assertBucketEquals("map<double,timestamp>", ImmutableMap.of(), 0, 0);
assertBucketEquals("map<string,bigint>", ImmutableMap.of("key", 123L, "key2", 123456789L, "key3", -123456L), 127880789, -1910999650);
assertBucketEquals("map<array<double>,map<int,string>>", ImmutableMap.of(ImmutableList.of(12.3, 45.7), ImmutableMap.of(123, "test99")), -34001111, -1565874874);
// multiple bucketing columns
assertBucketEquals(ImmutableList.of("float", "array<smallint>", "map<string,bigint>"), ImmutableList.of(12.34F, ImmutableList.of((short) 5, (short) 8, (short) 13), ImmutableMap.of("key", 123L)), 95411006, 932898434);
assertBucketEquals(ImmutableList.of("double", "array<smallint>", "boolean", "map<string,bigint>", "tinyint"), asList(null, ImmutableList.of((short) 5, (short) 8, (short) 13), null, ImmutableMap.of("key", 123L), null), 154207826, -1120812524);
}
use of io.prestosql.spi.type.TimestampType in project hetu-core by openlookeng.
the class LiteralInterpreter method evaluate.
public static Object evaluate(ConstantExpression node) {
Type type = node.getType();
if (node.getValue() == null) {
return null;
}
if (type instanceof BooleanType) {
return node.getValue();
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
return node.getValue();
}
if (type instanceof DoubleType) {
return node.getValue();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return intBitsToFloat(number.intValue());
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(node.getValue() instanceof Long);
return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType);
}
if (type instanceof VarcharType || type instanceof CharType) {
return (node.getValue() instanceof String) ? node.getValue() : ((Slice) node.getValue()).toStringUtf8();
}
if (type instanceof VarbinaryType) {
return new SqlVarbinary(((Slice) node.getValue()).getBytes());
}
if (type instanceof DateType) {
return new SqlDate(((Long) node.getValue()).intValue());
}
if (type instanceof TimeType) {
return new SqlTime((long) node.getValue());
}
if (type instanceof TimestampType) {
try {
return new SqlTimestamp((long) node.getValue());
} catch (RuntimeException e) {
throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
}
}
if (type instanceof IntervalDayTimeType) {
return new SqlIntervalDayTime((long) node.getValue());
}
if (type instanceof IntervalYearMonthType) {
return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
}
if (type.getJavaType().equals(Slice.class)) {
// DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
}
// We should not fail at the moment; just return the raw value (block, regex, etc) to the user
return node.getValue();
}
use of io.prestosql.spi.type.TimestampType in project hetu-core by openlookeng.
the class BaseJdbcRowExpressionConverter method visitConstant.
@Override
public String visitConstant(ConstantExpression literal, JdbcConverterContext context) {
Type type = literal.getType();
if (literal.getValue() == null) {
return "null";
}
if (type instanceof BooleanType) {
return String.valueOf(((Boolean) literal.getValue()).booleanValue());
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
Number number = (Number) literal.getValue();
return format("%d", number.longValue());
}
if (type instanceof DoubleType) {
return literal.getValue().toString();
}
if (type instanceof RealType) {
Long number = (Long) literal.getValue();
return format("%f", intBitsToFloat(number.intValue()));
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
checkState(literal.getValue() instanceof Long);
return decodeDecimal(BigInteger.valueOf((long) literal.getValue()), decimalType).toString();
}
checkState(literal.getValue() instanceof Slice);
Slice value = (Slice) literal.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType).toString();
}
if (type instanceof VarcharType || type instanceof CharType) {
return "'" + ((Slice) literal.getValue()).toStringUtf8() + "'";
}
if (type.equals(DATE)) {
String date = printDate(Integer.parseInt(String.valueOf(literal.getValue())));
return StandardTypes.DATE + " " + ExpressionFormatter.formatStringLiteral(date);
}
if (type instanceof TimestampType) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
Long time = (Long) literal.getValue();
return format("TIMESTAMP '%s'", formatter.format(Instant.ofEpochMilli(time).atZone(UTC).toLocalDateTime()));
}
throw new PrestoException(NOT_SUPPORTED, String.format("Cannot handle the constant expression %s with value of type %s", literal.getValue(), type));
}
use of io.prestosql.spi.type.TimestampType in project pulsar by apache.
the class PulsarPrimitiveRowDecoder method decodeRow.
@Override
public Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodeRow(ByteBuf byteBuf) {
if (columnHandle == null) {
return Optional.empty();
}
Object value = schema.decode(byteBuf);
Map<DecoderColumnHandle, FieldValueProvider> primitiveColumn = new HashMap<>();
if (value == null) {
primitiveColumn.put(columnHandle, FieldValueProviders.nullValueProvider());
} else {
Type type = columnHandle.getType();
if (type instanceof BooleanType) {
primitiveColumn.put(columnHandle, booleanValueProvider(Boolean.valueOf((Boolean) value)));
} else if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) {
primitiveColumn.put(columnHandle, longValueProvider(Long.parseLong(value.toString())));
} else if (type instanceof DoubleType) {
primitiveColumn.put(columnHandle, doubleValueProvider(Double.parseDouble(value.toString())));
} else if (type instanceof RealType) {
primitiveColumn.put(columnHandle, longValueProvider(Float.floatToIntBits((Float.parseFloat(value.toString())))));
} else if (type instanceof VarbinaryType) {
primitiveColumn.put(columnHandle, bytesValueProvider((byte[]) value));
} else if (type instanceof VarcharType) {
primitiveColumn.put(columnHandle, bytesValueProvider(value.toString().getBytes()));
} else if (type instanceof DateType) {
primitiveColumn.put(columnHandle, longValueProvider(((Date) value).getTime()));
} else if (type instanceof TimeType) {
primitiveColumn.put(columnHandle, longValueProvider(((Time) value).getTime()));
} else if (type instanceof TimestampType) {
primitiveColumn.put(columnHandle, longValueProvider(((Timestamp) value).getTime()));
} else {
primitiveColumn.put(columnHandle, bytesValueProvider(value.toString().getBytes()));
}
}
return Optional.of(primitiveColumn);
}
Aggregations