Search in sources :

Example 16 with CharType

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());
}
Also used : Cast(io.prestosql.sql.tree.Cast) IfExpression(io.prestosql.sql.tree.IfExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) VarcharType(io.prestosql.spi.type.VarcharType) Signature(io.prestosql.spi.function.Signature) CharType(io.prestosql.spi.type.CharType) FunctionCall(io.prestosql.sql.tree.FunctionCall) GenericLiteral(io.prestosql.sql.tree.GenericLiteral)

Example 17 with CharType

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();
}
Also used : IntervalYearMonthType(io.prestosql.type.IntervalYearMonthType) VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTime(io.prestosql.spi.type.SqlTime) SqlIntervalYearMonth(io.prestosql.type.SqlIntervalYearMonth) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) IntervalDayTimeType(io.prestosql.type.IntervalDayTimeType) TimeType(io.prestosql.spi.type.TimeType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) BigintType(io.prestosql.spi.type.BigintType) IntegerType(io.prestosql.spi.type.IntegerType) DecimalType(io.prestosql.spi.type.DecimalType) IntervalDayTimeType(io.prestosql.type.IntervalDayTimeType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) IntervalYearMonthType(io.prestosql.type.IntervalYearMonthType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) TinyintType(io.prestosql.spi.type.TinyintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) DoubleType(io.prestosql.spi.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(io.prestosql.spi.type.SqlDate) SqlIntervalDayTime(io.prestosql.type.SqlIntervalDayTime) IntervalDayTimeType(io.prestosql.type.IntervalDayTimeType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 18 with CharType

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());
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) DecimalType(io.prestosql.spi.type.DecimalType) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) CharType(io.prestosql.spi.type.CharType) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)

Example 19 with CharType

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));
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) BigintType(io.prestosql.spi.type.BigintType) IntegerType(io.prestosql.spi.type.IntegerType) DecimalType(io.prestosql.spi.type.DecimalType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) TimestampType(io.prestosql.spi.type.TimestampType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) DoubleType(io.prestosql.spi.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) CharType(io.prestosql.spi.type.CharType) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 20 with CharType

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();
}
Also used : Re2JRegexpType(io.prestosql.type.Re2JRegexpType) DecimalType(io.prestosql.spi.type.DecimalType) ColorType(io.prestosql.type.ColorType) ParametricType(io.prestosql.spi.type.ParametricType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) LikePatternType(io.prestosql.spi.type.LikePatternType) ArrayType(io.prestosql.spi.type.ArrayType) CharType.createCharType(io.prestosql.spi.type.CharType.createCharType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.prestosql.spi.type.VarcharType.createUnboundedVarcharType) CharParametricType(io.prestosql.spi.type.CharParametricType) VarcharParametricType(io.prestosql.spi.type.VarcharParametricType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) JoniRegexpType(io.prestosql.type.JoniRegexpType) CodePointsType(io.prestosql.type.CodePointsType) MapType(io.prestosql.spi.type.MapType) JsonPathType(io.prestosql.type.JsonPathType) SetDigestType(io.prestosql.type.setdigest.SetDigestType) DecimalParametricType(io.prestosql.spi.type.DecimalParametricType) VarcharType(io.prestosql.spi.type.VarcharType)

Aggregations

CharType (io.prestosql.spi.type.CharType)48 DecimalType (io.prestosql.spi.type.DecimalType)40 VarcharType (io.prestosql.spi.type.VarcharType)39 PrestoException (io.prestosql.spi.PrestoException)23 Type (io.prestosql.spi.type.Type)23 Slice (io.airlift.slice.Slice)16 TimestampType (io.prestosql.spi.type.TimestampType)13 ArrayType (io.prestosql.spi.type.ArrayType)11 Chars.isCharType (io.prestosql.spi.type.Chars.isCharType)11 DateType (io.prestosql.spi.type.DateType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 RealType (io.prestosql.spi.type.RealType)11 VarbinaryType (io.prestosql.spi.type.VarbinaryType)11 BigDecimal (java.math.BigDecimal)10 ArrayList (java.util.ArrayList)10 BigintType (io.prestosql.spi.type.BigintType)9 BooleanType (io.prestosql.spi.type.BooleanType)9 IntegerType (io.prestosql.spi.type.IntegerType)9 SmallintType (io.prestosql.spi.type.SmallintType)9 TinyintType (io.prestosql.spi.type.TinyintType)8