use of io.prestosql.spi.type.IntegerType 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.IntegerType 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.IntegerType in project hetu-core by openlookeng.
the class InCodeGenerator method checkSwitchGenerationCase.
@VisibleForTesting
static SwitchGenerationCase checkSwitchGenerationCase(Type type, List<RowExpression> values) {
if (values.size() > 32) {
// * Benchmark shows performance of SET_CONTAINS is better at 50, but similar at 25.
return SwitchGenerationCase.SET_CONTAINS;
}
if (!(type instanceof IntegerType || type instanceof BigintType || type instanceof DateType)) {
return SwitchGenerationCase.HASH_SWITCH;
}
for (RowExpression expression : values) {
// Same argument applies for nulls.
if (!(expression instanceof ConstantExpression)) {
continue;
}
Object constant = ((ConstantExpression) expression).getValue();
if (constant == null) {
continue;
}
long longConstant = ((Number) constant).longValue();
if (longConstant < Integer.MIN_VALUE || longConstant > Integer.MAX_VALUE) {
return SwitchGenerationCase.HASH_SWITCH;
}
}
return SwitchGenerationCase.DIRECT_SWITCH;
}
use of io.prestosql.spi.type.IntegerType 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.IntegerType 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