Search in sources :

Example 1 with CharType

use of io.prestosql.spi.type.CharType 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 2 with CharType

use of io.prestosql.spi.type.CharType 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 3 with CharType

use of io.prestosql.spi.type.CharType 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)

Example 4 with CharType

use of io.prestosql.spi.type.CharType 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);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) RealType(io.prestosql.spi.type.RealType) Timestamp(java.sql.Timestamp) BigintType(io.prestosql.spi.type.BigintType) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) IntegerType(io.prestosql.spi.type.IntegerType) 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) DateType(io.prestosql.spi.type.DateType)

Example 5 with CharType

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

the class OrcTester method testRow.

// later we can extend for multiple columns
private static boolean testRow(List<Type> types, List<?> values, int row, Map<Integer, TupleDomainFilter> columnFilters) {
    for (int column = 0; column < types.size(); column++) {
        TupleDomainFilter filter = columnFilters.get(column);
        if (filter == null) {
            continue;
        }
        Object value = values.get(row);
        if (value == null) {
            if (!filter.testNull()) {
                return false;
            }
        } else {
            Type type = types.get(column);
            if (type == BOOLEAN) {
                if (!filter.testBoolean((Boolean) value)) {
                    return false;
                }
            } else if (type == BIGINT || type == INTEGER || type == SMALLINT) {
                if (!filter.testLong(((Number) value).longValue())) {
                    return false;
                }
            } else if (type == DATE) {
                if (!filter.testLong(((SqlDate) value).getDays())) {
                    return false;
                }
            } else if (type == TIMESTAMP) {
                return filter.testLong(((SqlTimestamp) value).getMillis());
            } else if (type == VARCHAR) {
                return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
            } else if (type instanceof CharType) {
                String charString = String.valueOf(value);
                return filter.testBytes(charString.getBytes(StandardCharsets.UTF_8), 0, charString.length());
            } else if (type == VARBINARY) {
                byte[] binary = ((SqlVarbinary) value).getBytes();
                return filter.testBytes(binary, 0, binary.length);
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
                if (decimalType.isShort()) {
                    return filter.testLong(bigDecimal.unscaledValue().longValue());
                }
            } else if (type == DOUBLE) {
                if (!filter.testDouble((double) value)) {
                    return false;
                }
            } else {
                fail("Unsupported type: " + type);
            }
        }
    }
    return true;
}
Also used : SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) BigDecimal(java.math.BigDecimal) VarbinaryType(io.prestosql.spi.type.VarbinaryType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) SqlDate(io.prestosql.spi.type.SqlDate) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Aggregations

CharType (io.prestosql.spi.type.CharType)39 VarcharType (io.prestosql.spi.type.VarcharType)33 DecimalType (io.prestosql.spi.type.DecimalType)32 PrestoException (io.prestosql.spi.PrestoException)19 Type (io.prestosql.spi.type.Type)19 Slice (io.airlift.slice.Slice)12 ArrayType (io.prestosql.spi.type.ArrayType)10 TimestampType (io.prestosql.spi.type.TimestampType)10 VarbinaryType (io.prestosql.spi.type.VarbinaryType)10 DoubleType (io.prestosql.spi.type.DoubleType)9 RealType (io.prestosql.spi.type.RealType)9 BigDecimal (java.math.BigDecimal)9 DateType (io.prestosql.spi.type.DateType)8 ArrayList (java.util.ArrayList)8 BigintType (io.prestosql.spi.type.BigintType)7 BooleanType (io.prestosql.spi.type.BooleanType)7 IntegerType (io.prestosql.spi.type.IntegerType)7 SmallintType (io.prestosql.spi.type.SmallintType)7 CharType.createCharType (io.prestosql.spi.type.CharType.createCharType)6 Chars.isCharType (io.prestosql.spi.type.Chars.isCharType)6