use of io.prestosql.spi.type.TimeType 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.TimeType 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);
}
use of io.prestosql.spi.type.TimeType in project hetu-core by openlookeng.
the class TestTypeUtil method testParseType.
@Test
public void testParseType() {
Type type = parseType(typeManager, "bigint");
assertTrue(type instanceof BigintType);
type = parseType(typeManager, "integer");
assertTrue(type instanceof IntegerType);
type = parseType(typeManager, "smallint");
assertTrue(type instanceof SmallintType);
type = parseType(typeManager, "boolean");
assertTrue(type instanceof BooleanType);
type = parseType(typeManager, "date");
assertTrue(type instanceof DateType);
type = parseType(typeManager, "real");
assertTrue(type instanceof RealType);
type = parseType(typeManager, "double");
assertTrue(type instanceof DoubleType);
type = parseType(typeManager, "HyperLogLog");
assertTrue(type instanceof HyperLogLogType);
type = parseType(typeManager, "P4HyperLogLog");
assertTrue(type instanceof P4HyperLogLogType);
type = parseType(typeManager, "timestamp");
assertTrue(type instanceof TimestampType);
type = parseType(typeManager, "timestamp with time zone");
assertTrue(type instanceof TimestampWithTimeZoneType);
type = parseType(typeManager, "time");
assertTrue(type instanceof TimeType);
type = parseType(typeManager, "time with time zone");
assertTrue(type instanceof TimeWithTimeZoneType);
type = parseType(typeManager, "varbinary");
assertTrue(type instanceof VarbinaryType);
type = parseType(typeManager, "unknown");
assertTrue(type instanceof UnknownType);
}
use of io.prestosql.spi.type.TimeType in project TiBigData by tidb-incubator.
the class TypeHelpers method toSqlString.
public static String toSqlString(Type type) {
if (type instanceof TimeWithTimeZoneType || type instanceof TimestampWithTimeZoneType) {
throw new PrestoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
if (type instanceof TimestampType) {
return format("timestamp(%s)", ((TimestampType) type).getPrecision());
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
return "longtext";
}
Integer length = varcharType.getLength().orElseThrow(IllegalStateException::new);
if (length <= 255) {
return "tinytext";
}
if (length <= 65535) {
return "text";
}
if (length <= 16777215) {
return "mediumtext";
}
return "longtext";
}
if (type instanceof CharType) {
int length = ((CharType) type).getLength();
if (length <= 255) {
return "char(" + length + ")";
}
return "text";
}
if (type instanceof DecimalType) {
return format("decimal(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale());
}
if (type instanceof TimeType) {
return format("time(%s)", ((TimeType) type).getPrecision());
}
String sqlType = SQL_TYPES.get(type);
if (sqlType != null) {
return sqlType;
}
return type.getDisplayName();
}
use of io.prestosql.spi.type.TimeType in project TiBigData by tidb-incubator.
the class TiDBPageSink method appendColumn.
private void appendColumn(Page page, int position, int channel) throws SQLException {
Block block = page.getBlock(channel);
int parameter = channel + 1;
if (block.isNull(position)) {
statement.setObject(parameter, null);
return;
}
Type type = columnTypes.get(channel);
switch(type.getDisplayName()) {
case "boolean":
statement.setBoolean(parameter, type.getBoolean(block, position));
break;
case "tinyint":
statement.setByte(parameter, SignedBytes.checkedCast(type.getLong(block, position)));
break;
case "smallint":
statement.setShort(parameter, Shorts.checkedCast(type.getLong(block, position)));
break;
case "integer":
statement.setInt(parameter, toIntExact(type.getLong(block, position)));
break;
case "bigint":
statement.setLong(parameter, type.getLong(block, position));
break;
case "real":
statement.setFloat(parameter, intBitsToFloat(toIntExact(type.getLong(block, position))));
break;
case "double":
statement.setDouble(parameter, type.getDouble(block, position));
break;
case "date":
// convert to midnight in default time zone
long utcMillis = DAYS.toMillis(type.getLong(block, position));
long localMillis = getInstanceUTC().getZone().getMillisKeepLocal(DateTimeZone.getDefault(), utcMillis);
statement.setDate(parameter, new Date(localMillis));
break;
case "varbinary":
statement.setBytes(parameter, type.getSlice(block, position).getBytes());
break;
default:
if (type instanceof DecimalType) {
statement.setBigDecimal(parameter, readBigDecimal((DecimalType) type, block, position));
} else if (type instanceof VarcharType || type instanceof CharType) {
statement.setString(parameter, type.getSlice(block, position).toStringUtf8());
} else if (type instanceof TimestampType) {
statement.setTimestamp(parameter, new Timestamp(type.getLong(block, position) / 1000 - TimeZone.getDefault().getRawOffset()));
} else if (type instanceof TimeType) {
statement.setTime(parameter, new Time(type.getLong(block, position)));
} else {
throw new PrestoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
}
}
Aggregations