Search in sources :

Example 16 with RecordCursor

use of com.facebook.presto.spi.RecordCursor in project presto by prestodb.

the class AbstractTestHiveClient method assertPageSourceType.

protected static void assertPageSourceType(ConnectorPageSource pageSource, HiveStorageFormat hiveStorageFormat) {
    if (pageSource instanceof RecordPageSource) {
        RecordCursor hiveRecordCursor = ((RecordPageSource) pageSource).getCursor();
        hiveRecordCursor = ((HiveRecordCursor) hiveRecordCursor).getRegularColumnRecordCursor();
        if (hiveRecordCursor instanceof HiveCoercionRecordCursor) {
            hiveRecordCursor = ((HiveCoercionRecordCursor) hiveRecordCursor).getRegularColumnRecordCursor();
        }
        assertInstanceOf(hiveRecordCursor, recordCursorType(hiveStorageFormat), hiveStorageFormat.name());
    } else {
        assertInstanceOf(((HivePageSource) pageSource).getPageSource(), pageSourceType(hiveStorageFormat), hiveStorageFormat.name());
    }
}
Also used : ParquetHiveRecordCursor(com.facebook.presto.hive.parquet.ParquetHiveRecordCursor) RecordCursor(com.facebook.presto.spi.RecordCursor) RecordPageSource(com.facebook.presto.spi.RecordPageSource)

Example 17 with RecordCursor

use of com.facebook.presto.spi.RecordCursor in project presto by prestodb.

the class TestFilterAndProjectOperator method test.

@Test
public void test() throws Exception {
    List<Page> input = rowPagesBuilder(VARCHAR, BIGINT).addSequencePage(100, 0, 0).build();
    FilterFunction filter = new FilterFunction() {

        @Override
        public boolean filter(int position, Block... blocks) {
            long value = BIGINT.getLong(blocks[1], position);
            return 10 <= value && value < 20;
        }

        @Override
        public boolean filter(RecordCursor cursor) {
            long value = cursor.getLong(0);
            return 10 <= value && value < 20;
        }

        @Override
        public Set<Integer> getInputChannels() {
            return singleton(1);
        }
    };
    OperatorFactory operatorFactory = new FilterAndProjectOperator.FilterAndProjectOperatorFactory(0, new PlanNodeId("test"), () -> new GenericPageProcessor(filter, ImmutableList.of(singleColumn(VARCHAR, 0), new Add5Projection(1))), ImmutableList.of(VARCHAR, BIGINT));
    MaterializedResult expected = MaterializedResult.resultBuilder(driverContext.getSession(), VARCHAR, BIGINT).row("10", 15L).row("11", 16L).row("12", 17L).row("13", 18L).row("14", 19L).row("15", 20L).row("16", 21L).row("17", 22L).row("18", 23L).row("19", 24L).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected);
}
Also used : RecordCursor(com.facebook.presto.spi.RecordCursor) Page(com.facebook.presto.spi.Page) PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) Block(com.facebook.presto.spi.block.Block) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 18 with RecordCursor

use of com.facebook.presto.spi.RecordCursor 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

RecordCursor (com.facebook.presto.spi.RecordCursor)18 Test (org.testng.annotations.Test)13 RecordSet (com.facebook.presto.spi.RecordSet)9 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)5 TupleDomain (com.facebook.presto.spi.predicate.TupleDomain)4 OptionalInt (java.util.OptionalInt)4 ColumnHandle (com.facebook.presto.spi.ColumnHandle)3 ConnectorTransactionHandle (com.facebook.presto.spi.connector.ConnectorTransactionHandle)3 ImmutableList (com.google.common.collect.ImmutableList)3 RecordPageSource (com.facebook.presto.spi.RecordPageSource)2 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 Type (com.facebook.presto.spi.type.Type)2 MaterializedResult (com.facebook.presto.testing.MaterializedResult)2 ArrayType (com.facebook.presto.type.ArrayType)2 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Lists (com.google.common.collect.Lists)2