Search in sources :

Example 1 with PreparedBatchPart

use of org.skife.jdbi.v2.PreparedBatchPart in project presto by prestodb.

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()) {
                batch.execute();
                return;
            }
            PreparedBatchPart part = batch.add();
            for (int column = 0; column < columns.size(); column++) {
                Type type = columns.get(column).getType();
                if (BOOLEAN.equals(type)) {
                    part.bind(column, cursor.getBoolean(column));
                } else if (BIGINT.equals(type)) {
                    part.bind(column, cursor.getLong(column));
                } else if (INTEGER.equals(type)) {
                    part.bind(column, (int) cursor.getLong(column));
                } else if (DOUBLE.equals(type)) {
                    part.bind(column, cursor.getDouble(column));
                } else if (type instanceof VarcharType) {
                    part.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);
                    part.bind(column, new Date(localMillis));
                } else {
                    throw new IllegalArgumentException("Unsupported type " + type);
                }
            }
        }
        batch.execute();
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) ResultSetMapper(org.skife.jdbi.v2.tweak.ResultSetMapper) Time(java.sql.Time) TpchRecordSet.createTpchRecordSet(com.facebook.presto.tpch.TpchRecordSet.createTpchRecordSet) LINE_ITEM(io.airlift.tpch.TpchTable.LINE_ITEM) StatementContext(org.skife.jdbi.v2.StatementContext) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) DecimalType(com.facebook.presto.spi.type.DecimalType) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) ResultSet(java.sql.ResultSet) PreparedBatch(org.skife.jdbi.v2.PreparedBatch) UNKNOWN(com.facebook.presto.type.UnknownType.UNKNOWN) DateTimeZoneIndex.getDateTimeZone(com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) REGION(io.airlift.tpch.TpchTable.REGION) TINYINT(com.facebook.presto.spi.type.TinyintType.TINYINT) Collections.nCopies(java.util.Collections.nCopies) Timestamp(java.sql.Timestamp) TpchMetadata(com.facebook.presto.tpch.TpchMetadata) TINY_SCHEMA_NAME(com.facebook.presto.tpch.TpchMetadata.TINY_SCHEMA_NAME) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TpchTable(io.airlift.tpch.TpchTable) RecordCursor(com.facebook.presto.spi.RecordCursor) TpchTableHandle(com.facebook.presto.tpch.TpchTableHandle) List(java.util.List) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) PreparedBatchPart(org.skife.jdbi.v2.PreparedBatchPart) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) Joiner(com.google.common.base.Joiner) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) Strings.padEnd(com.google.common.base.Strings.padEnd) NATION(io.airlift.tpch.TpchTable.NATION) TIMESTAMP_WITH_TIME_ZONE(com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) ORDERS(io.airlift.tpch.TpchTable.ORDERS) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Type(com.facebook.presto.spi.type.Type) DBI(org.skife.jdbi.v2.DBI) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Language(org.intellij.lang.annotations.Language) Session(com.facebook.presto.Session) TIME(com.facebook.presto.spi.type.TimeType.TIME) SMALLINT(com.facebook.presto.spi.type.SmallintType.SMALLINT) TIME_WITH_TIME_ZONE(com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) RecordSet(com.facebook.presto.spi.RecordSet) Date(java.sql.Date) CharType(com.facebook.presto.spi.type.CharType) TimeUnit(java.util.concurrent.TimeUnit) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Handle(org.skife.jdbi.v2.Handle) DATE(com.facebook.presto.spi.type.DateType.DATE) REAL(com.facebook.presto.spi.type.RealType.REAL) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Closeable(java.io.Closeable) VarcharType(com.facebook.presto.spi.type.VarcharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) DecimalType(com.facebook.presto.spi.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) Type(com.facebook.presto.spi.type.Type) CharType(com.facebook.presto.spi.type.CharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) RecordCursor(com.facebook.presto.spi.RecordCursor) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) PreparedBatchPart(org.skife.jdbi.v2.PreparedBatchPart) PreparedBatch(org.skife.jdbi.v2.PreparedBatch) Date(java.sql.Date)

Aggregations

Session (com.facebook.presto.Session)1 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)1 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)1 RecordCursor (com.facebook.presto.spi.RecordCursor)1 RecordSet (com.facebook.presto.spi.RecordSet)1 SchemaTableName (com.facebook.presto.spi.SchemaTableName)1 BIGINT (com.facebook.presto.spi.type.BigintType.BIGINT)1 BOOLEAN (com.facebook.presto.spi.type.BooleanType.BOOLEAN)1 CharType (com.facebook.presto.spi.type.CharType)1 Chars.isCharType (com.facebook.presto.spi.type.Chars.isCharType)1 DATE (com.facebook.presto.spi.type.DateType.DATE)1 DecimalType (com.facebook.presto.spi.type.DecimalType)1 DOUBLE (com.facebook.presto.spi.type.DoubleType.DOUBLE)1 INTEGER (com.facebook.presto.spi.type.IntegerType.INTEGER)1 REAL (com.facebook.presto.spi.type.RealType.REAL)1 SMALLINT (com.facebook.presto.spi.type.SmallintType.SMALLINT)1 TIME (com.facebook.presto.spi.type.TimeType.TIME)1 TIME_WITH_TIME_ZONE (com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE)1 TIMESTAMP (com.facebook.presto.spi.type.TimestampType.TIMESTAMP)1 TIMESTAMP_WITH_TIME_ZONE (com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)1