Search in sources :

Example 6 with TimeType

use of io.trino.spi.type.TimeType in project trino by trinodb.

the class H2QueryRunner method rowMapper.

private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
    return (resultSet, context) -> {
        int count = resultSet.getMetaData().getColumnCount();
        checkArgument(types.size() == count, "expected types count (%s) does not match actual column count (%s)", types.size(), count);
        List<Object> row = new ArrayList<>(count);
        for (int i = 1; i <= count; i++) {
            Type type = types.get(i - 1);
            if (BOOLEAN.equals(type)) {
                boolean booleanValue = resultSet.getBoolean(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(booleanValue);
                }
            } else if (TINYINT.equals(type)) {
                byte byteValue = resultSet.getByte(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(byteValue);
                }
            } else if (SMALLINT.equals(type)) {
                short shortValue = resultSet.getShort(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(shortValue);
                }
            } else if (INTEGER.equals(type)) {
                int intValue = resultSet.getInt(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(intValue);
                }
            } else if (BIGINT.equals(type)) {
                long longValue = resultSet.getLong(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(longValue);
                }
            } else if (REAL.equals(type)) {
                float floatValue = resultSet.getFloat(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(floatValue);
                }
            } else if (DOUBLE.equals(type)) {
                double doubleValue = resultSet.getDouble(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(doubleValue);
                }
            } else if (JSON.equals(type)) {
                String stringValue = resultSet.getString(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(jsonParse(utf8Slice(stringValue)).toStringUtf8());
                }
            } else if (type instanceof VarcharType) {
                String stringValue = resultSet.getString(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(stringValue);
                }
            } else if (type instanceof CharType) {
                String stringValue = resultSet.getString(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(padSpaces(stringValue, (CharType) type));
                }
            } else if (VARBINARY.equals(type)) {
                byte[] bytes = resultSet.getBytes(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(bytes);
                }
            } else if (DATE.equals(type)) {
                // resultSet.getDate(i) doesn't work if JVM's zone skipped day being retrieved (e.g. 2011-12-30 and Pacific/Apia zone)
                LocalDate dateValue = resultSet.getObject(i, LocalDate.class);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(dateValue);
                }
            } else if (type instanceof TimeType) {
                // resultSet.getTime(i) doesn't work if JVM's zone had forward offset change during 1970-01-01 (e.g. America/Hermosillo zone)
                LocalTime timeValue = resultSet.getObject(i, LocalTime.class);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(timeValue);
                }
            } else if (TIME_WITH_TIME_ZONE.equals(type)) {
                throw new UnsupportedOperationException("H2 does not support TIME WITH TIME ZONE");
            } else if (type instanceof TimestampType) {
                // resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
                LocalDateTime timestampValue;
                try {
                    timestampValue = resultSet.getObject(i, LocalDateTime.class);
                } catch (SQLException first) {
                    // H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
                    try {
                        timestampValue = Optional.ofNullable(resultSet.getObject(i, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
                    } catch (RuntimeException e) {
                        first.addSuppressed(e);
                        throw first;
                    }
                }
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(timestampValue);
                }
            } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
                // This means H2 is unsuitable for testing TIMESTAMP WITH TIME ZONE-bearing queries. Those need to be tested manually.
                throw new UnsupportedOperationException();
            } else if (UUID.equals(type)) {
                java.util.UUID value = (java.util.UUID) resultSet.getObject(i);
                row.add(value);
            } else if (UNKNOWN.equals(type)) {
                Object objectValue = resultSet.getObject(i);
                checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
                row.add(null);
            } else if (type instanceof DecimalType) {
                DecimalType decimalType = (DecimalType) type;
                BigDecimal decimalValue = resultSet.getBigDecimal(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision())));
                }
            } else if (type instanceof ArrayType) {
                Array array = resultSet.getArray(i);
                if (resultSet.wasNull()) {
                    row.add(null);
                } else {
                    row.add(newArrayList((Object[]) array.getArray()));
                }
            } else {
                throw new AssertionError("unhandled type: " + type);
            }
        }
        return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
    };
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) TpchRecordSet.createTpchRecordSet(io.trino.plugin.tpch.TpchRecordSet.createTpchRecordSet) UNKNOWN(io.trino.type.UnknownType.UNKNOWN) Array(java.sql.Array) CUSTOMER(io.trino.tpch.TpchTable.CUSTOMER) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) ParsedSql(org.jdbi.v3.core.statement.ParsedSql) Handle(org.jdbi.v3.core.Handle) LocalTime(java.time.LocalTime) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) TIMESTAMP_WITH_TIME_ZONE(io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) INTEGER(io.trino.spi.type.IntegerType.INTEGER) UUID(io.trino.spi.type.UuidType.UUID) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TpchTable(io.trino.tpch.TpchTable) PART(io.trino.tpch.TpchTable.PART) MathContext(java.math.MathContext) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) REGION(io.trino.tpch.TpchTable.REGION) ArrayType(io.trino.spi.type.ArrayType) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) StatementContext(org.jdbi.v3.core.statement.StatementContext) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BIGINT(io.trino.spi.type.BigintType.BIGINT) LocalDate(java.time.LocalDate) RecordSet(io.trino.spi.connector.RecordSet) Optional(java.util.Optional) DecimalType(io.trino.spi.type.DecimalType) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) LINE_ITEM(io.trino.tpch.TpchTable.LINE_ITEM) Joiner(com.google.common.base.Joiner) Session(io.trino.Session) NATION(io.trino.tpch.TpchTable.NATION) TimeType(io.trino.spi.type.TimeType) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Type(io.trino.spi.type.Type) LocalDateTime(java.time.LocalDateTime) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) JsonFunctions.jsonParse(io.trino.operator.scalar.JsonFunctions.jsonParse) TimestampType(io.trino.spi.type.TimestampType) ORDERS(io.trino.tpch.TpchTable.ORDERS) ArrayList(java.util.ArrayList) VarcharType(io.trino.spi.type.VarcharType) TIME_WITH_TIME_ZONE(io.trino.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) SQLException(java.sql.SQLException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Chars.padSpaces(io.trino.spi.type.Chars.padSpaces) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) Math.toIntExact(java.lang.Math.toIntExact) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Jdbi(org.jdbi.v3.core.Jdbi) TINY_SCHEMA_NAME(io.trino.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) RecordCursor(io.trino.spi.connector.RecordCursor) Language(org.intellij.lang.annotations.Language) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) CharType(io.trino.spi.type.CharType) Closeable(java.io.Closeable) TpchMetadata(io.trino.plugin.tpch.TpchMetadata) SqlParser(org.jdbi.v3.core.statement.SqlParser) TINYINT(io.trino.spi.type.TinyintType.TINYINT) JSON(io.trino.type.JsonType.JSON) LocalDateTime(java.time.LocalDateTime) VarcharType(io.trino.spi.type.VarcharType) SQLException(java.sql.SQLException) LocalDate(java.time.LocalDate) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) UUID(io.trino.spi.type.UuidType.UUID) LocalTime(java.time.LocalTime) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Array(java.sql.Array) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) CharType(io.trino.spi.type.CharType) DecimalType(io.trino.spi.type.DecimalType) CharType(io.trino.spi.type.CharType)

Example 7 with TimeType

use of io.trino.spi.type.TimeType in project trino by trinodb.

the class PostgreSqlClient method toWriteMapping.

@Override
public WriteMapping toWriteMapping(ConnectorSession session, Type type) {
    if (type == BOOLEAN) {
        return WriteMapping.booleanMapping("boolean", booleanWriteFunction());
    }
    if (type == TINYINT) {
        // PostgreSQL has no type corresponding to tinyint
        return WriteMapping.longMapping("smallint", tinyintWriteFunction());
    }
    if (type == SMALLINT) {
        return WriteMapping.longMapping("smallint", smallintWriteFunction());
    }
    if (type == INTEGER) {
        return WriteMapping.longMapping("integer", integerWriteFunction());
    }
    if (type == BIGINT) {
        return WriteMapping.longMapping("bigint", bigintWriteFunction());
    }
    if (type == REAL) {
        return WriteMapping.longMapping("real", realWriteFunction());
    }
    if (type == DOUBLE) {
        return WriteMapping.doubleMapping("double precision", doubleWriteFunction());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        String dataType = format("decimal(%s, %s)", decimalType.getPrecision(), decimalType.getScale());
        if (decimalType.isShort()) {
            return WriteMapping.longMapping(dataType, shortDecimalWriteFunction(decimalType));
        }
        return WriteMapping.objectMapping(dataType, longDecimalWriteFunction(decimalType));
    }
    if (type instanceof CharType) {
        return WriteMapping.sliceMapping("char(" + ((CharType) type).getLength() + ")", charWriteFunction());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        String dataType;
        if (varcharType.isUnbounded()) {
            dataType = "varchar";
        } else {
            dataType = "varchar(" + varcharType.getBoundedLength() + ")";
        }
        return WriteMapping.sliceMapping(dataType, varcharWriteFunction());
    }
    if (VARBINARY.equals(type)) {
        return WriteMapping.sliceMapping("bytea", varbinaryWriteFunction());
    }
    if (type == DATE) {
        return WriteMapping.longMapping("date", dateWriteFunctionUsingLocalDate());
    }
    if (type instanceof TimeType) {
        TimeType timeType = (TimeType) type;
        if (timeType.getPrecision() <= POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION) {
            return WriteMapping.longMapping(format("time(%s)", timeType.getPrecision()), timeWriteFunction(timeType.getPrecision()));
        }
        return WriteMapping.longMapping(format("time(%s)", POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION), timeWriteFunction(POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION));
    }
    if (type instanceof TimestampType) {
        TimestampType timestampType = (TimestampType) type;
        if (timestampType.getPrecision() <= POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION) {
            verify(timestampType.getPrecision() <= TimestampType.MAX_SHORT_PRECISION);
            return WriteMapping.longMapping(format("timestamp(%s)", timestampType.getPrecision()), PostgreSqlClient::shortTimestampWriteFunction);
        }
        verify(timestampType.getPrecision() > TimestampType.MAX_SHORT_PRECISION);
        return WriteMapping.objectMapping(format("timestamp(%s)", POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION), longTimestampWriteFunction());
    }
    if (type instanceof TimestampWithTimeZoneType) {
        TimestampWithTimeZoneType timestampWithTimeZoneType = (TimestampWithTimeZoneType) type;
        if (timestampWithTimeZoneType.getPrecision() <= POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION) {
            String dataType = format("timestamptz(%d)", timestampWithTimeZoneType.getPrecision());
            if (timestampWithTimeZoneType.getPrecision() <= TimestampWithTimeZoneType.MAX_SHORT_PRECISION) {
                return WriteMapping.longMapping(dataType, shortTimestampWithTimeZoneWriteFunction());
            }
            return WriteMapping.objectMapping(dataType, longTimestampWithTimeZoneWriteFunction());
        }
        return WriteMapping.objectMapping(format("timestamptz(%d)", POSTGRESQL_MAX_SUPPORTED_TIMESTAMP_PRECISION), longTimestampWithTimeZoneWriteFunction());
    }
    if (type.equals(jsonType)) {
        return WriteMapping.sliceMapping("jsonb", typedVarcharWriteFunction("json"));
    }
    if (type.equals(uuidType)) {
        return WriteMapping.sliceMapping("uuid", uuidWriteFunction());
    }
    if (type instanceof ArrayType && getArrayMapping(session) == AS_ARRAY) {
        Type elementType = ((ArrayType) type).getElementType();
        String elementDataType = toWriteMapping(session, elementType).getDataType();
        return WriteMapping.objectMapping(elementDataType + "[]", arrayWriteFunction(session, elementType, getArrayElementPgTypeName(session, this, elementType)));
    }
    throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}
Also used : ArrayType(io.trino.spi.type.ArrayType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) TimestampType(io.trino.spi.type.TimestampType) MapType(io.trino.spi.type.MapType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) DecimalType(io.trino.spi.type.DecimalType) TimeType(io.trino.spi.type.TimeType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) CharType(io.trino.spi.type.CharType) ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) VarcharType(io.trino.spi.type.VarcharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) DecimalType(io.trino.spi.type.DecimalType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) TrinoException(io.trino.spi.TrinoException) CharType.createCharType(io.trino.spi.type.CharType.createCharType) CharType(io.trino.spi.type.CharType) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 8 with TimeType

use of io.trino.spi.type.TimeType in project TiBigData by tidb-incubator.

the class TypeHelpers method toSqlString.

public static String toSqlString(Type type) {
    if (type instanceof TimeWithTimeZoneType || type instanceof TimestampWithTimeZoneType) {
        throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
    }
    if (type instanceof TimestampType) {
        return format("timestamp(%s)", ((TimestampType) type).getPrecision());
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        if (varcharType.isUnbounded()) {
            return "longtext";
        }
        Integer length = varcharType.getLength().orElseThrow(IllegalStateException::new);
        if (length <= 255) {
            return "tinytext";
        }
        if (length <= 65535) {
            return "text";
        }
        if (length <= 16777215) {
            return "mediumtext";
        }
        return "longtext";
    }
    if (type instanceof CharType) {
        int length = ((CharType) type).getLength();
        if (length <= 255) {
            return "char(" + length + ")";
        }
        return "text";
    }
    if (type instanceof DecimalType) {
        return format("decimal(%s, %s)", ((DecimalType) type).getPrecision(), ((DecimalType) type).getScale());
    }
    if (type instanceof TimeType) {
        return format("time(%s)", ((TimeType) type).getPrecision());
    }
    String sqlType = SQL_TYPES.get(type);
    if (sqlType != null) {
        return sqlType;
    }
    return type.getDisplayName();
}
Also used : VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TrinoException(io.trino.spi.TrinoException) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) CharType(io.trino.spi.type.CharType) TimeType(io.trino.spi.type.TimeType)

Example 9 with TimeType

use of io.trino.spi.type.TimeType in project trino by trinodb.

the class TypeCoercion 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.getBaseName();
    String toTypeBaseName = toType.getBaseName();
    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)) {
            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 (fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) {
            Type commonSuperType = createTimestampType(Math.max(((TimestampType) fromType).getPrecision(), ((TimestampType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
            Type commonSuperType = createTimestampWithTimeZoneType(Math.max(((TimestampWithTimeZoneType) fromType).getPrecision(), ((TimestampWithTimeZoneType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.TIME)) {
            Type commonSuperType = createTimeType(Math.max(((TimeType) fromType).getPrecision(), ((TimeType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (fromTypeBaseName.equals(StandardTypes.TIME_WITH_TIME_ZONE)) {
            Type commonSuperType = createTimeWithTimeZoneType(Math.max(((TimeWithTimeZoneType) fromType).getPrecision(), ((TimeWithTimeZoneType) toType).getPrecision()));
            return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
        }
        if (isCovariantParametrizedType(fromType)) {
            return typeCompatibilityForCovariantParametrizedType(fromType, toType);
        }
        return TypeCompatibility.incompatible();
    }
    Optional<Type> coercedType = coerceTypeBase(fromType, toType.getBaseName());
    if (coercedType.isPresent()) {
        return compatibility(coercedType.get(), toType);
    }
    coercedType = coerceTypeBase(toType, fromType.getBaseName());
    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 : TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) TimeWithTimeZoneType.createTimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType) TimestampType(io.trino.spi.type.TimestampType) CharType.createCharType(io.trino.spi.type.CharType.createCharType) VarcharType(io.trino.spi.type.VarcharType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) SetDigestType(io.trino.type.setdigest.SetDigestType) RowType(io.trino.spi.type.RowType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) CharType(io.trino.spi.type.CharType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) DecimalType(io.trino.spi.type.DecimalType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampWithTimeZoneType.createTimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) TimeWithTimeZoneType.createTimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType.createTimeWithTimeZoneType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) TimeType.createTimeType(io.trino.spi.type.TimeType.createTimeType)

Example 10 with TimeType

use of io.trino.spi.type.TimeType in project trino by trinodb.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, (Long) value);
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, (Integer) value);
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, (Short) value);
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, (Byte) value);
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, (Double) value);
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (type instanceof TimeType) {
        SqlTime time = (SqlTime) value;
        type.writeLong(blockBuilder, time.getPicos());
    } else if (type instanceof TimeWithTimeZoneType) {
        long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
        int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
        type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
    } else if (type instanceof TimestampType) {
        long micros = ((SqlTimestamp) value).getEpochMicros();
        if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
            type.writeLong(blockBuilder, micros);
        } else {
            type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
        }
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (type instanceof ArrayType) {
        List<?> list = (List<?>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType) {
        Map<?, ?> map = (Map<?, ?>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<?> row = (List<?>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TimeZoneKey(io.trino.spi.type.TimeZoneKey) BlockBuilder(io.trino.spi.block.BlockBuilder) LongTimestamp(io.trino.spi.type.LongTimestamp) SqlTimeWithTimeZone(io.trino.spi.type.SqlTimeWithTimeZone) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) SqlDate(io.trino.spi.type.SqlDate) OptionalLong(java.util.OptionalLong) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

TimeType (io.trino.spi.type.TimeType)16 VarcharType (io.trino.spi.type.VarcharType)14 CharType (io.trino.spi.type.CharType)13 TimestampType (io.trino.spi.type.TimestampType)13 DecimalType (io.trino.spi.type.DecimalType)12 Type (io.trino.spi.type.Type)11 TrinoException (io.trino.spi.TrinoException)10 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)7 TimeType.createTimeType (io.trino.spi.type.TimeType.createTimeType)7 List (java.util.List)7 TimestampType.createTimestampType (io.trino.spi.type.TimestampType.createTimestampType)6 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)4 ImmutableList (com.google.common.collect.ImmutableList)3 ColumnMapping (io.trino.plugin.jdbc.ColumnMapping)3 StandardColumnMappings.bigintColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.bigintColumnMapping)3 StandardColumnMappings.booleanColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.booleanColumnMapping)3 StandardColumnMappings.decimalColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.decimalColumnMapping)3 StandardColumnMappings.defaultCharColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.defaultCharColumnMapping)3 StandardColumnMappings.defaultVarcharColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.defaultVarcharColumnMapping)3 StandardColumnMappings.doubleColumnMapping (io.trino.plugin.jdbc.StandardColumnMappings.doubleColumnMapping)3