Search in sources :

Example 1 with DecimalType

use of io.prestosql.spi.type.DecimalType in project carbondata by apache.

the class DecimalSliceStreamReader method decimalBlockWriter.

private void decimalBlockWriter(BigDecimal value) {
    if (isShortDecimal(type)) {
        long rescaledDecimal = Decimals.rescale(value.unscaledValue().longValue(), value.scale(), ((DecimalType) type).getScale());
        type.writeLong(builder, rescaledDecimal);
    } else {
        Slice slice = getSlice(value, type);
        type.writeSlice(builder, parseSlice((DecimalType) type, slice, slice.length()));
    }
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(io.prestosql.spi.type.DecimalType)

Example 2 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class TypeUtils method prestoNativeToJdbcObject.

private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative) {
    if (prestoNative == null) {
        return null;
    }
    if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
        return prestoNative;
    }
    if (prestoType instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) prestoType;
        if (decimalType.isShort()) {
            BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
            return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        BigInteger unscaledValue = decodeUnscaledValue((Slice) prestoNative);
        return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    }
    if (REAL.equals(prestoType)) {
        return intBitsToFloat(toIntExact((long) prestoNative));
    }
    if (TINYINT.equals(prestoType)) {
        return SignedBytes.checkedCast((long) prestoNative);
    }
    if (SMALLINT.equals(prestoType)) {
        return Shorts.checkedCast((long) prestoNative);
    }
    if (INTEGER.equals(prestoType)) {
        return toIntExact((long) prestoNative);
    }
    if (DATE.equals(prestoType)) {
        // convert to midnight in default time zone
        long millis = DAYS.toMillis((long) prestoNative);
        return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
    }
    if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
        return ((Slice) prestoNative).toStringUtf8();
    }
    if (prestoType instanceof ArrayType) {
        // process subarray of multi-dimensional array
        return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
    }
    throw new PrestoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) VarcharType(io.prestosql.spi.type.VarcharType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(io.prestosql.spi.type.DecimalType) BigInteger(java.math.BigInteger) PrestoException(io.prestosql.spi.PrestoException) CharType(io.prestosql.spi.type.CharType) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date)

Example 3 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class TypeUtils method jdbcObjectToPrestoNative.

private static Object jdbcObjectToPrestoNative(ConnectorSession session, Object jdbcObject, Type prestoType) {
    if (jdbcObject == null) {
        return null;
    }
    if (BOOLEAN.equals(prestoType) || TINYINT.equals(prestoType) || SMALLINT.equals(prestoType) || INTEGER.equals(prestoType) || BIGINT.equals(prestoType) || DOUBLE.equals(prestoType)) {
        return jdbcObject;
    }
    if (prestoType instanceof ArrayType) {
        return jdbcObjectArrayToBlock(session, ((ArrayType) prestoType).getElementType(), (Object[]) jdbcObject);
    }
    if (prestoType instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) prestoType;
        BigDecimal value = (BigDecimal) jdbcObject;
        if (decimalType.isShort()) {
            return encodeShortScaledValue(value, decimalType.getScale());
        }
        return encodeScaledValue(value, decimalType.getScale());
    }
    if (REAL.equals(prestoType)) {
        return floatToRawIntBits((float) jdbcObject);
    }
    if (DATE.equals(prestoType)) {
        long localMillis = ((Date) jdbcObject).getTime();
        // Convert it to a ~midnight in UTC.
        long utcMillis = ISOChronology.getInstance().getZone().getMillisKeepLocal(UTC, localMillis);
        // convert to days
        return MILLISECONDS.toDays(utcMillis);
    }
    if (TIMESTAMP.equals(prestoType)) {
        Timestamp timestamp = (Timestamp) jdbcObject;
        return timestamp.toLocalDateTime().atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
    }
    if (prestoType instanceof VarcharType) {
        return utf8Slice((String) jdbcObject);
    }
    if (prestoType instanceof CharType) {
        return utf8Slice(CharMatcher.is(' ').trimTrailingFrom((String) jdbcObject));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported type %s and object type %s", prestoType, jdbcObject.getClass()));
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) PrestoException(io.prestosql.spi.PrestoException) CharType(io.prestosql.spi.type.CharType) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Date(java.sql.Date)

Example 4 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class MySqlClient method getColumns.

@SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public Map<String, ColumnHandle> getColumns(ConnectorSession session, String sql, Map<String, Type> types) {
    try (Connection connection = connectionFactory.openConnection(JdbcIdentity.from(session));
        PreparedStatement statement = connection.prepareStatement(sql)) {
        ResultSetMetaData metaData = statement.getMetaData();
        ImmutableMap.Builder<String, ColumnHandle> columnBuilder = new ImmutableMap.Builder<>();
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            String columnName = metaData.getColumnLabel(i);
            String typeName = metaData.getColumnTypeName(i);
            int precision = metaData.getPrecision(i);
            int dataType = metaData.getColumnType(i);
            int scale = metaData.getScale(i);
            // extracted from logical plan during pre-processing
            if (dataType == Types.DECIMAL && (precision > MAX_PRECISION || scale < 0)) {
                // Covert MySql Decimal type to Presto type
                Type type = types.get(columnName.toLowerCase(ENGLISH));
                if (type instanceof AbstractType) {
                    TypeSignature signature = type.getTypeSignature();
                    typeName = signature.getBase().toUpperCase(ENGLISH);
                    dataType = JDBCType.valueOf(typeName).getVendorTypeNumber();
                    if (type instanceof DecimalType) {
                        precision = ((DecimalType) type).getPrecision();
                        scale = ((DecimalType) type).getScale();
                    }
                }
            }
            boolean isNullable = metaData.isNullable(i) != ResultSetMetaData.columnNoNulls;
            JdbcTypeHandle typeHandle = new JdbcTypeHandle(dataType, Optional.ofNullable(typeName), precision, scale, Optional.empty());
            Optional<ColumnMapping> columnMapping;
            try {
                columnMapping = toPrestoType(session, connection, typeHandle);
            } catch (UnsupportedOperationException ex) {
                throw new PrestoException(JDBC_UNSUPPORTED_EXPRESSION, format("Data type [%s] is not support", typeHandle.getJdbcTypeName()));
            }
            // skip unsupported column types
            if (columnMapping.isPresent()) {
                Type type = columnMapping.get().getType();
                JdbcColumnHandle handle = new JdbcColumnHandle(columnName, typeHandle, type, isNullable);
                columnBuilder.put(columnName.toLowerCase(ENGLISH), handle);
            } else {
                return Collections.emptyMap();
            }
        }
        return columnBuilder.build();
    } catch (SQLException | PrestoException e) {
        throw new PrestoException(JDBC_QUERY_GENERATOR_FAILURE, String.format("Query generator failed for [%s]", e.getMessage()));
    }
}
Also used : JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcColumnHandle(io.prestosql.plugin.jdbc.JdbcColumnHandle) PreparedStatement(java.sql.PreparedStatement) PrestoException(io.prestosql.spi.PrestoException) ImmutableMap(com.google.common.collect.ImmutableMap) ResultSetMetaData(java.sql.ResultSetMetaData) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) AbstractType(io.prestosql.spi.type.AbstractType) JDBCType(java.sql.JDBCType) VarcharType(io.prestosql.spi.type.VarcharType) TypeSignature(io.prestosql.spi.type.TypeSignature) JdbcTypeHandle(io.prestosql.plugin.jdbc.JdbcTypeHandle) AbstractType(io.prestosql.spi.type.AbstractType) DecimalType(io.prestosql.spi.type.DecimalType) ColumnMapping(io.prestosql.plugin.jdbc.ColumnMapping)

Example 5 with DecimalType

use of io.prestosql.spi.type.DecimalType in project hetu-core by openlookeng.

the class MySqlRowExpressionConverter method visitCall.

@Override
public String visitCall(CallExpression call, JdbcConverterContext context) {
    // remote udf verify
    FunctionHandle functionHandle = call.getFunctionHandle();
    if (!isDefaultFunction(call)) {
        Optional<String> result = mySqlApplyRemoteFunctionPushDown.rewriteRemoteFunction(call, this, context);
        if (result.isPresent()) {
            return result.get();
        }
        throw new PrestoException(NOT_SUPPORTED, String.format("MySql connector does not support remote function: %s.%s", call.getDisplayName(), call.getFunctionHandle().getFunctionNamespace()));
    }
    if (standardFunctionResolution.isArrayConstructor(functionHandle)) {
        throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support array constructor");
    }
    if (standardFunctionResolution.isSubscriptFunction(functionHandle)) {
        throw new PrestoException(NOT_SUPPORTED, "MySql connector does not support subscript expression");
    }
    if (standardFunctionResolution.isCastFunction(functionHandle)) {
        // deal with literal, when generic literal expression translate to rowExpression, it will be
        // translated to a 'CAST' rowExpression with a varchar type 'CONSTANT' rowExpression, in some
        // case, 'CAST' is superfluous
        RowExpression argument = call.getArguments().get(0);
        Type type = call.getType();
        if (argument instanceof ConstantExpression && argument.getType().equals(VARCHAR)) {
            String value = argument.accept(this, context);
            if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType || type instanceof DecimalType || type instanceof RealType || type instanceof DoubleType) {
                return value;
            }
        }
        if (call.getType().getDisplayName().equals(LIKE_PATTERN_NAME)) {
            return call.getArguments().get(0).accept(this, context);
        }
        return getCastExpression(call.getArguments().get(0).accept(this, context), call.getType());
    }
    return super.visitCall(call, context);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) DoubleType(io.prestosql.spi.type.DoubleType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) VarcharType(io.prestosql.spi.type.VarcharType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) DoubleType(io.prestosql.spi.type.DoubleType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Aggregations

DecimalType (io.prestosql.spi.type.DecimalType)67 VarcharType (io.prestosql.spi.type.VarcharType)42 CharType (io.prestosql.spi.type.CharType)37 Type (io.prestosql.spi.type.Type)31 PrestoException (io.prestosql.spi.PrestoException)24 BigDecimal (java.math.BigDecimal)18 Slice (io.airlift.slice.Slice)17 ArrayType (io.prestosql.spi.type.ArrayType)12 BigInteger (java.math.BigInteger)12 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)11 Block (io.prestosql.spi.block.Block)11 ArrayList (java.util.ArrayList)11 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)10 TimestampType (io.prestosql.spi.type.TimestampType)10 VarbinaryType (io.prestosql.spi.type.VarbinaryType)10 DoubleType (io.prestosql.spi.type.DoubleType)9 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)9 ImmutableList (com.google.common.collect.ImmutableList)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 DateType (io.prestosql.spi.type.DateType)8