use of com.facebook.presto.spi.RecordCursor in project presto by prestodb.
the class TestHiveFileFormats method testCursorProvider.
private void testCursorProvider(HiveRecordCursorProvider cursorProvider, FileSplit split, HiveStorageFormat storageFormat, List<TestColumn> testColumns, ConnectorSession session, int rowCount) {
List<HivePartitionKey> partitionKeys = testColumns.stream().filter(TestColumn::isPartitionKey).map(TestColumn::toHivePartitionKey).collect(toList());
List<HiveColumnHandle> partitionKeyColumnHandles = getColumnHandles(testColumns.stream().filter(TestColumn::isPartitionKey).collect(toImmutableList()));
List<Column> tableDataColumns = testColumns.stream().filter(column -> !column.isPartitionKey()).map(column -> new Column(column.getName(), HiveType.valueOf(column.getType()), Optional.empty(), Optional.empty())).collect(toImmutableList());
Configuration configuration = new Configuration();
configuration.set("io.compression.codecs", LzoCodec.class.getName() + "," + LzopCodec.class.getName());
Optional<ConnectorPageSource> pageSource = HivePageSourceProvider.createHivePageSource(ImmutableSet.of(cursorProvider), ImmutableSet.of(), configuration, session, split.getPath(), OptionalInt.empty(), split.getStart(), split.getLength(), split.getLength(), Instant.now().toEpochMilli(), new Storage(StorageFormat.create(storageFormat.getSerDe(), storageFormat.getInputFormat(), storageFormat.getOutputFormat()), "location", Optional.empty(), false, ImmutableMap.of(), ImmutableMap.of()), TupleDomain.all(), getColumnHandles(testColumns), ImmutableMap.of(), partitionKeys, DateTimeZone.getDefault(), FUNCTION_AND_TYPE_MANAGER, new SchemaTableName("schema", "table"), partitionKeyColumnHandles, tableDataColumns, ImmutableMap.of(), tableDataColumns.size(), TableToPartitionMapping.empty(), Optional.empty(), false, DEFAULT_HIVE_FILE_CONTEXT, TRUE_CONSTANT, false, ROW_EXPRESSION_SERVICE, Optional.empty(), ImmutableMap.of());
RecordCursor cursor = ((RecordPageSource) pageSource.get()).getCursor();
checkCursor(cursor, testColumns, rowCount);
}
use of com.facebook.presto.spi.RecordCursor in project presto by prestodb.
the class TestLocalFileRecordSet method assertData.
private static void assertData(LocalFileTables localFileTables, LocalFileMetadata metadata) {
SchemaTableName tableName = getSchemaTableName();
List<LocalFileColumnHandle> columnHandles = metadata.getColumnHandles(SESSION, new LocalFileTableHandle(tableName, OptionalInt.of(0), OptionalInt.of(-1))).values().stream().map(column -> (LocalFileColumnHandle) column).collect(Collectors.toList());
LocalFileRecordSet recordSet = new LocalFileRecordSet(localFileTables, new LocalFileSplit(address, tableName, TupleDomain.all()), columnHandles);
RecordCursor cursor = recordSet.cursor();
for (int i = 0; i < columnHandles.size(); i++) {
assertEquals(cursor.getType(i), columnHandles.get(i).getColumnType());
}
// test one row
assertTrue(cursor.advanceNextPosition());
assertEquals(cursor.getSlice(0).toStringUtf8(), address.toString());
assertEquals(cursor.getSlice(2).toStringUtf8(), "127.0.0.1");
assertEquals(cursor.getSlice(3).toStringUtf8(), "POST");
assertEquals(cursor.getSlice(4).toStringUtf8(), "/v1/memory");
assertTrue(cursor.isNull(5));
assertTrue(cursor.isNull(6));
assertEquals(cursor.getLong(7), 200);
assertEquals(cursor.getLong(8), 0);
assertEquals(cursor.getLong(9), 1000);
assertEquals(cursor.getLong(10), 10);
assertTrue(cursor.isNull(11));
assertTrue(cursor.advanceNextPosition());
assertEquals(cursor.getSlice(0).toStringUtf8(), address.toString());
assertEquals(cursor.getSlice(2).toStringUtf8(), "127.0.0.1");
assertEquals(cursor.getSlice(3).toStringUtf8(), "GET");
assertEquals(cursor.getSlice(4).toStringUtf8(), "/v1/service/presto/general");
assertEquals(cursor.getSlice(5).toStringUtf8(), "foo");
assertEquals(cursor.getSlice(6).toStringUtf8(), "ffffffff-ffff-ffff-ffff-ffffffffffff");
assertEquals(cursor.getLong(7), 200);
assertEquals(cursor.getLong(8), 0);
assertEquals(cursor.getLong(9), 37);
assertEquals(cursor.getLong(10), 1094);
assertEquals(cursor.getSlice(11).toStringUtf8(), "a7229d56-5cbd-4e23-81ff-312ba6be0f12");
}
use of com.facebook.presto.spi.RecordCursor in project presto by prestodb.
the class PrestoThriftBlock method convertColumnToBlock.
private static Block convertColumnToBlock(RecordSet recordSet, int columnIndex, int positions) {
Type type = recordSet.getColumnTypes().get(columnIndex);
BlockBuilder output = type.createBlockBuilder(null, positions);
Class<?> javaType = type.getJavaType();
RecordCursor cursor = recordSet.cursor();
for (int position = 0; position < positions; position++) {
checkState(cursor.advanceNextPosition(), "cursor has less values than expected");
if (cursor.isNull(columnIndex)) {
output.appendNull();
} else {
if (javaType == boolean.class) {
type.writeBoolean(output, cursor.getBoolean(columnIndex));
} else if (javaType == long.class) {
type.writeLong(output, cursor.getLong(columnIndex));
} else if (javaType == double.class) {
type.writeDouble(output, cursor.getDouble(columnIndex));
} else if (javaType == Slice.class) {
Slice slice = cursor.getSlice(columnIndex);
type.writeSlice(output, slice, 0, slice.length());
} else {
type.writeObject(output, cursor.getObject(columnIndex));
}
}
}
checkState(!cursor.advanceNextPosition(), "cursor has more values than expected");
return output.build();
}
use of com.facebook.presto.spi.RecordCursor in project presto by prestodb.
the class PrestoThriftTypeUtils method fromLongBasedColumn.
public static PrestoThriftBlock fromLongBasedColumn(RecordSet recordSet, int columnIndex, int positions, BiFunction<boolean[], long[], PrestoThriftBlock> result) {
if (positions == 0) {
return result.apply(null, null);
}
boolean[] nulls = null;
long[] longs = null;
RecordCursor cursor = recordSet.cursor();
for (int position = 0; position < positions; position++) {
checkState(cursor.advanceNextPosition(), "cursor has less values than expected");
if (cursor.isNull(columnIndex)) {
if (nulls == null) {
nulls = new boolean[positions];
}
nulls[position] = true;
} else {
if (longs == null) {
longs = new long[positions];
}
longs[position] = cursor.getLong(columnIndex);
}
}
checkState(!cursor.advanceNextPosition(), "cursor has more values than expected");
return result.apply(nulls, longs);
}
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()) {
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();
}
}
Aggregations