Search in sources :

Example 1 with TIME

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

the class H2QueryRunner method insertRows.

private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data) {
    List<ColumnMetadata> columns = tableMetadata.getColumns().stream().filter(columnMetadata -> !columnMetadata.isHidden()).collect(toImmutableList());
    String vars = Joiner.on(',').join(nCopies(columns.size(), "?"));
    String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);
    RecordCursor cursor = data.cursor();
    while (true) {
        // insert 1000 rows at a time
        PreparedBatch batch = handle.prepareBatch(sql);
        for (int row = 0; row < 1000; row++) {
            if (!cursor.advanceNextPosition()) {
                if (batch.size() > 0) {
                    batch.execute();
                }
                return;
            }
            for (int column = 0; column < columns.size(); column++) {
                Type type = columns.get(column).getType();
                if (BOOLEAN.equals(type)) {
                    batch.bind(column, cursor.getBoolean(column));
                } else if (BIGINT.equals(type)) {
                    batch.bind(column, cursor.getLong(column));
                } else if (INTEGER.equals(type)) {
                    batch.bind(column, (int) cursor.getLong(column));
                } else if (DOUBLE.equals(type)) {
                    batch.bind(column, cursor.getDouble(column));
                } else if (type instanceof VarcharType) {
                    batch.bind(column, cursor.getSlice(column).toStringUtf8());
                } else if (DATE.equals(type)) {
                    long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
                    // H2 expects dates in to be millis at midnight in the JVM timezone
                    long localMillis = DateTimeZone.UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millisUtc);
                    batch.bind(column, new Date(localMillis));
                } else {
                    throw new IllegalArgumentException("Unsupported type " + type);
                }
            }
            batch.add();
        }
        batch.execute();
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LINE_ITEM(io.airlift.tpch.TpchTable.LINE_ITEM) DecimalType(io.prestosql.spi.type.DecimalType) RecordSet(io.prestosql.spi.connector.RecordSet) MaterializedResult(io.prestosql.testing.MaterializedResult) Array(java.sql.Array) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ParsedSql(org.jdbi.v3.core.statement.ParsedSql) ResultSet(java.sql.ResultSet) Handle(org.jdbi.v3.core.Handle) LocalTime(java.time.LocalTime) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) JSON(io.prestosql.type.JsonType.JSON) Type(io.prestosql.spi.type.Type) CUSTOMER(io.airlift.tpch.TpchTable.CUSTOMER) REGION(io.airlift.tpch.TpchTable.REGION) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) JsonFunctions.jsonParse(io.prestosql.operator.scalar.JsonFunctions.jsonParse) MathContext(java.math.MathContext) Collections.nCopies(java.util.Collections.nCopies) ArrayType(io.prestosql.spi.type.ArrayType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TIME(io.prestosql.spi.type.TimeType.TIME) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) String.format(java.lang.String.format) StatementContext(org.jdbi.v3.core.statement.StatementContext) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TpchTable(io.airlift.tpch.TpchTable) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) LocalDate(java.time.LocalDate) Optional(java.util.Optional) TIMESTAMP_WITH_TIME_ZONE(io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) TpchRecordSet.createTpchRecordSet(io.prestosql.plugin.tpch.TpchRecordSet.createTpchRecordSet) UNKNOWN(io.prestosql.spi.type.UnknownType.UNKNOWN) Joiner(com.google.common.base.Joiner) TpchTableHandle(io.prestosql.plugin.tpch.TpchTableHandle) Strings.padEnd(com.google.common.base.Strings.padEnd) NATION(io.airlift.tpch.TpchTable.NATION) TIME_WITH_TIME_ZONE(io.prestosql.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) ORDERS(io.airlift.tpch.TpchTable.ORDERS) LocalDateTime(java.time.LocalDateTime) CharType(io.prestosql.spi.type.CharType) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) PART(io.airlift.tpch.TpchTable.PART) ArrayList(java.util.ArrayList) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) SQLException(java.sql.SQLException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Session(io.prestosql.Session) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) RecordCursor(io.prestosql.spi.connector.RecordCursor) DATE(io.prestosql.spi.type.DateType.DATE) REAL(io.prestosql.spi.type.RealType.REAL) RowMapper(org.jdbi.v3.core.mapper.RowMapper) Jdbi(org.jdbi.v3.core.Jdbi) Language(org.intellij.lang.annotations.Language) TINY_SCHEMA_NAME(io.prestosql.plugin.tpch.TpchMetadata.TINY_SCHEMA_NAME) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) MaterializedRow(io.prestosql.testing.MaterializedRow) ConnectorTableMetadata(io.prestosql.spi.connector.ConnectorTableMetadata) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) TpchMetadata(io.prestosql.plugin.tpch.TpchMetadata) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) Closeable(java.io.Closeable) SqlParser(org.jdbi.v3.core.statement.SqlParser) VarcharType(io.prestosql.spi.type.VarcharType) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) VarcharType(io.prestosql.spi.type.VarcharType) RecordCursor(io.prestosql.spi.connector.RecordCursor) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) VarcharType(io.prestosql.spi.type.VarcharType) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) LocalDate(java.time.LocalDate) Date(java.sql.Date)

Example 2 with TIME

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

the class FormatFunction method valueConverter.

private static BiFunction<ConnectorSession, Block, Object> valueConverter(FunctionAndTypeManager functionAndTypeManager, Type type, int position) {
    if (type.equals(UNKNOWN)) {
        return (session, block) -> null;
    }
    if (type.equals(BOOLEAN)) {
        return (session, block) -> type.getBoolean(block, position);
    }
    if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
        return (session, block) -> type.getLong(block, position);
    }
    if (type.equals(REAL)) {
        return (session, block) -> intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type.equals(DOUBLE)) {
        return (session, block) -> type.getDouble(block, position);
    }
    if (type.equals(DATE)) {
        return (session, block) -> LocalDate.ofEpochDay(type.getLong(block, position));
    }
    if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
        return (session, block) -> toZonedDateTime(type.getLong(block, position));
    }
    if (type.equals(TIMESTAMP)) {
        return (session, block) -> toLocalDateTime(type.getLong(block, position));
    }
    if (type.equals(TIME)) {
        return (session, block) -> toLocalTime(session, type.getLong(block, position));
    }
    // TODO: support TIME WITH TIME ZONE by making SqlTimeWithTimeZone implement TemporalAccessor
    if (type.equals(JSON)) {
        FunctionHandle functionHandle = functionAndTypeManager.resolveFunction(Optional.empty(), QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "json_format"), fromTypes(JSON));
        MethodHandle handle = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionHandle).getMethodHandle();
        return (session, block) -> convertToString(handle, type.getSlice(block, position));
    }
    if (isShortDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale);
    }
    if (isLongDecimal(type)) {
        int scale = ((DecimalType) type).getScale();
        return (session, block) -> new BigDecimal(decodeUnscaledValue(type.getSlice(block, position)), scale);
    }
    if (isVarcharType(type) || isCharType(type)) {
        return (session, block) -> type.getSlice(block, position).toStringUtf8();
    }
    BiFunction<ConnectorSession, Block, Object> function;
    if (type.getJavaType() == long.class) {
        function = (session, block) -> type.getLong(block, position);
    } else if (type.getJavaType() == double.class) {
        function = (session, block) -> type.getDouble(block, position);
    } else if (type.getJavaType() == boolean.class) {
        function = (session, block) -> type.getBoolean(block, position);
    } else if (type.getJavaType() == Slice.class) {
        function = (session, block) -> type.getSlice(block, position);
    } else {
        function = (session, block) -> type.getObject(block, position);
    }
    MethodHandle handle = castToVarchar(functionAndTypeManager, type);
    if ((handle == null) || (handle.type().parameterCount() != 1)) {
        throw new PrestoException(NOT_SUPPORTED, "Type not supported for formatting: " + type.getDisplayName());
    }
    return (session, block) -> convertToString(handle, function.apply(session, block));
}
Also used : DateTimeEncoding.unpackZoneKey(io.prestosql.spi.type.DateTimeEncoding.unpackZoneKey) Varchars.isVarcharType(io.prestosql.spi.type.Varchars.isVarcharType) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) OperatorNotFoundException(io.prestosql.metadata.OperatorNotFoundException) INVALID_FUNCTION_ARGUMENT(io.prestosql.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) DecimalType(io.prestosql.spi.type.DecimalType) BoundVariables(io.prestosql.metadata.BoundVariables) DEFAULT_NAMESPACE(io.prestosql.spi.connector.CatalogSchemaName.DEFAULT_NAMESPACE) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) BigDecimal(java.math.BigDecimal) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) LocalTime(java.time.LocalTime) IllegalFormatException(java.util.IllegalFormatException) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) JSON(io.prestosql.type.JsonType.JSON) Type(io.prestosql.spi.type.Type) ZoneOffset(java.time.ZoneOffset) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) PrestoException(io.prestosql.spi.PrestoException) DateTimeEncoding.unpackMillisUtc(io.prestosql.spi.type.DateTimeEncoding.unpackMillisUtc) CastType(io.prestosql.metadata.CastType) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TIME(io.prestosql.spi.type.TimeType.TIME) Reflection.methodHandle(io.prestosql.spi.util.Reflection.methodHandle) Decimals.isLongDecimal(io.prestosql.spi.type.Decimals.isLongDecimal) TIMESTAMP(io.prestosql.spi.type.TimestampType.TIMESTAMP) TINYINT(io.prestosql.spi.type.TinyintType.TINYINT) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) FunctionHandle(io.prestosql.spi.function.FunctionHandle) Decimals.isShortDecimal(io.prestosql.spi.type.Decimals.isShortDecimal) List(java.util.List) LocalDate(java.time.LocalDate) ArgumentProperty.valueTypeArgumentProperty(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.ArgumentProperty.valueTypeArgumentProperty) Optional(java.util.Optional) NOT_SUPPORTED(io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED) TIMESTAMP_WITH_TIME_ZONE(io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) Signature.withVariadicBound(io.prestosql.spi.function.Signature.withVariadicBound) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) UNKNOWN(io.prestosql.spi.type.UnknownType.UNKNOWN) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) LocalDateTime(java.time.LocalDateTime) INTEGER(io.prestosql.spi.type.IntegerType.INTEGER) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) Failures.internalError(io.prestosql.util.Failures.internalError) ImmutableList(com.google.common.collect.ImmutableList) RETURN_NULL_ON_NULL(io.prestosql.spi.function.BuiltInScalarFunctionImplementation.NullConvention.RETURN_NULL_ON_NULL) DOUBLE(io.prestosql.spi.type.DoubleType.DOUBLE) DATE(io.prestosql.spi.type.DateType.DATE) REAL(io.prestosql.spi.type.RealType.REAL) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Block(io.prestosql.spi.block.Block) Streams.mapWithIndex(com.google.common.collect.Streams.mapWithIndex) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) Decimals.decodeUnscaledValue(io.prestosql.spi.type.Decimals.decodeUnscaledValue) SMALLINT(io.prestosql.spi.type.SmallintType.SMALLINT) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) Block(io.prestosql.spi.block.Block) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) PrestoException(io.prestosql.spi.PrestoException) FunctionHandle(io.prestosql.spi.function.FunctionHandle) BigDecimal(java.math.BigDecimal) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)2 BIGINT (io.prestosql.spi.type.BigintType.BIGINT)2 BOOLEAN (io.prestosql.spi.type.BooleanType.BOOLEAN)2 Chars.isCharType (io.prestosql.spi.type.Chars.isCharType)2 DATE (io.prestosql.spi.type.DateType.DATE)2 DecimalType (io.prestosql.spi.type.DecimalType)2 DOUBLE (io.prestosql.spi.type.DoubleType.DOUBLE)2 INTEGER (io.prestosql.spi.type.IntegerType.INTEGER)2 REAL (io.prestosql.spi.type.RealType.REAL)2 SMALLINT (io.prestosql.spi.type.SmallintType.SMALLINT)2 TIME (io.prestosql.spi.type.TimeType.TIME)2 TIMESTAMP (io.prestosql.spi.type.TimestampType.TIMESTAMP)2 TIMESTAMP_WITH_TIME_ZONE (io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)2 TINYINT (io.prestosql.spi.type.TinyintType.TINYINT)2 Type (io.prestosql.spi.type.Type)2 UNKNOWN (io.prestosql.spi.type.UnknownType.UNKNOWN)2 Varchars.isVarcharType (io.prestosql.spi.type.Varchars.isVarcharType)2 JSON (io.prestosql.type.JsonType.JSON)2 String.format (java.lang.String.format)2