use of io.prestosql.spi.type.CharType in project hetu-core by openlookeng.
the class LogicalPlanner method noTruncationCast.
private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
// cast larger size varchar to small size varchar
if ((fromType instanceof VarcharType || fromType instanceof CharType) && (toType instanceof VarcharType || toType instanceof CharType)) {
int targetLength;
if (toType instanceof VarcharType) {
if (((VarcharType) toType).isUnbounded()) {
return new Cast(expression, toType.getTypeSignature().toString());
}
targetLength = ((VarcharType) toType).getBoundedLength();
} else {
targetLength = ((CharType) toType).getLength();
}
Signature spaceTrimmedLength = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
Signature fail = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of("fail"), fromTypes(VARCHAR));
return new IfExpression(// check if the trimmed value fits in the target type
new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new FunctionCall(QualifiedName.of("$space_trimmed_length"), ImmutableList.of(new Cast(expression, VARCHAR.getTypeSignature().toString())))), new Cast(expression, toType.getTypeSignature().toString()), new Cast(new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(new Cast(new StringLiteral(format("Out of range for insert query type: Table: %s, Query: %s", toType.toString(), fromType.toString())), VARCHAR.getTypeSignature().toString()))), toType.getTypeSignature().toString()));
}
return new Cast(expression, toType.getTypeSignature().toString());
}
use of io.prestosql.spi.type.CharType 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.CharType in project hetu-core by openlookeng.
the class SerDeUtils method serializePrimitive.
private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
requireNonNull(builder, "parent builder is null");
if (object == null) {
builder.appendNull();
return;
}
switch(inspector.getPrimitiveCategory()) {
case BOOLEAN:
BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
return;
case BYTE:
TinyintType.TINYINT.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
return;
case SHORT:
SmallintType.SMALLINT.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
return;
case INT:
IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object));
return;
case LONG:
BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object));
return;
case FLOAT:
RealType.REAL.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
return;
case DOUBLE:
DoubleType.DOUBLE.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
return;
case STRING:
type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case VARCHAR:
type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
return;
case CHAR:
CharType charType = (CharType) type;
HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), charType.getLength()));
return;
case DATE:
DateType.DATE.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
return;
case TIMESTAMP:
TimestampType.TIMESTAMP.writeLong(builder, formatTimestampAsLong(object, (TimestampObjectInspector) inspector));
return;
case BINARY:
VARBINARY.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case DECIMAL:
DecimalType decimalType = (DecimalType) type;
HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
if (decimalType.isShort()) {
decimalType.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
} else {
decimalType.writeSlice(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
}
return;
}
throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
use of io.prestosql.spi.type.CharType 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.CharType in project hetu-core by openlookeng.
the class BuiltInTypeRegistry method compatibility.
private TypeCompatibility compatibility(Type fromType, Type toType) {
if (fromType.equals(toType)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(toType, true);
}
if (toType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(fromType, false);
}
String fromTypeBaseName = fromType.getTypeSignature().getBase();
String toTypeBaseName = toType.getTypeSignature().getBase();
if (featuresConfig.isLegacyDateTimestampToVarcharCoercion()) {
if ((fromTypeBaseName.equals(StandardTypes.DATE) || fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) && toTypeBaseName.equals(StandardTypes.VARCHAR)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR) && (toTypeBaseName.equals(StandardTypes.DATE) || toTypeBaseName.equals(StandardTypes.TIMESTAMP))) {
return TypeCompatibility.compatible(fromType, true);
}
}
if (fromTypeBaseName.equals(toTypeBaseName)) {
if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.CHAR) && !featuresConfig.isLegacyCharToVarcharCoercion()) {
Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.ROW)) {
return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
}
if (isCovariantParametrizedType(fromType)) {
return typeCompatibilityForCovariantParametrizedType(fromType, toType);
}
return TypeCompatibility.incompatible();
}
Optional<Type> coercedType = coerceTypeBase(fromType, toType.getTypeSignature().getBase());
if (coercedType.isPresent()) {
return compatibility(coercedType.get(), toType);
}
coercedType = coerceTypeBase(toType, fromType.getTypeSignature().getBase());
if (coercedType.isPresent()) {
TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
}
return TypeCompatibility.incompatible();
}
Aggregations