Refine search
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); }
private static Object extractObject(RecordCursor cursor, int field, Type type) { if (cursor.isNull(field)) { return null; } Class<?> javaType = type.getJavaType(); if (javaType == boolean.class) { return cursor.getBoolean(field); } else if (javaType == long.class) { return cursor.getLong(field); } else if (javaType == double.class) { return cursor.getDouble(field); } else if (javaType == Slice.class) { return cursor.getSlice(field).toStringUtf8(); } throw new AssertionError("Unsupported type: " + type); }
@Override public Type getType(int field) { return delegate.getType(field); }
@Override public boolean advanceNextPosition() { checkState(!closed); while (currentCursor == null || !currentCursor.advanceNextPosition()) { if (!iterator.hasNext()) { return false; } if (currentCursor != null) { currentCursor.close(); } currentCursor = iterator.next(); } return true; }
@Test public void testCursorMixedOrder() { RecordSet recordSet = new JdbcRecordSet(jdbcClient, session, split, ImmutableList.of( columnHandles.get("value"), columnHandles.get("value"), columnHandles.get("text"))); try (RecordCursor cursor = recordSet.cursor()) { assertEquals(cursor.getType(0), BIGINT); assertEquals(cursor.getType(1), BIGINT); assertEquals(cursor.getType(2), VARCHAR); Map<String, Long> data = new LinkedHashMap<>(); while (cursor.advanceNextPosition()) { assertEquals(cursor.getLong(0), cursor.getLong(1)); data.put(cursor.getSlice(2).toStringUtf8(), cursor.getLong(0)); } assertEquals(data, ImmutableMap.<String, Long>builder() .put("one", 1L) .put("two", 2L) .put("three", 3L) .put("ten", 10L) .put("eleven", 11L) .put("twelve", 12L) .build()); } }
@Test public void testEmptyCursor() { ListBasedRecordSet recordSet = new ListBasedRecordSet(ImmutableList.of(), ImmutableList.of(BIGINT, INTEGER)); assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, INTEGER)); RecordCursor cursor = recordSet.cursor(); assertFalse(cursor.advanceNextPosition()); }
@Test public void testCursor() { ListBasedRecordSet recordSet = new ListBasedRecordSet( ImmutableList.of( Arrays.asList("1", null, "3"), Arrays.asList("ab", "c", null)), ImmutableList.of(BIGINT, VARCHAR)); assertEquals(recordSet.getColumnTypes(), ImmutableList.of(BIGINT, VARCHAR)); RecordCursor cursor = recordSet.cursor(); assertTrue(cursor.advanceNextPosition()); assertEquals(cursor.getType(0), BIGINT); assertEquals(cursor.getType(1), VARCHAR); assertThrows(IndexOutOfBoundsException.class, () -> cursor.getLong(2)); assertEquals(cursor.getLong(0), 1L); assertEquals(cursor.getSlice(1), Slices.utf8Slice("ab")); assertTrue(cursor.advanceNextPosition()); assertTrue(cursor.isNull(0)); assertEquals(cursor.getSlice(1), Slices.utf8Slice("c")); assertTrue(cursor.advanceNextPosition()); assertEquals(cursor.getLong(0), 3L); assertTrue(cursor.isNull(1)); assertFalse(cursor.advanceNextPosition()); assertThrows(IndexOutOfBoundsException.class, () -> cursor.getLong(0)); } }
@Test public void testGetRecords() try (RecordCursor cursor = recordSetProvider.getRecordSet(transaction, SESSION, cassandraSplit, columnHandles).cursor()) { while (cursor.advanceNextPosition()) { try { assertReadFields(cursor, tableMetadata.getColumns()); String keyValue = cursor.getSlice(columnIndex.get("key")).toStringUtf8(); assertTrue(keyValue.startsWith("key ")); int rowId = Integer.parseInt(keyValue.substring(4)); assertEquals(keyValue, String.format("key %d", rowId)); assertEquals(Bytes.toHexString(cursor.getSlice(columnIndex.get("typebytes")).getBytes()), String.format("0x%08X", rowId)); assertEquals(cursor.getSlice(columnIndex.get("typeinteger")).toStringUtf8(), String.valueOf(rowId)); assertEquals(cursor.getLong(columnIndex.get("typelong")), 1000 + rowId); assertEquals(cursor.getSlice(columnIndex.get("typeuuid")).toStringUtf8(), String.format("00000000-0000-0000-0000-%012d", rowId)); assertEquals(cursor.getSlice(columnIndex.get("typetimestamp")).toStringUtf8(), Long.valueOf(DATE.getTime()).toString()); long newCompletedBytes = cursor.getCompletedBytes(); assertTrue(newCompletedBytes >= completedBytes); completedBytes = newCompletedBytes;
@Test public void test() { TypeManager typeManager = new TypeRegistry(); FunctionRegistry functionRegistry = new FunctionRegistry(typeManager, new BlockEncodingManager(typeManager), new FeaturesConfig()); ArrayType arrayOfBigintType = new ArrayType(BIGINT); FieldSetFilteringRecordSet fieldSetFilteringRecordSet = new FieldSetFilteringRecordSet( functionRegistry, new InMemoryRecordSet( ImmutableList.of(BIGINT, BIGINT, TIMESTAMP_WITH_TIME_ZONE, TIMESTAMP_WITH_TIME_ZONE, arrayOfBigintType, arrayOfBigintType), ImmutableList.of( ImmutableList.of( 100L, 100L, // test same time in different time zone to make sure equal check was done properly packDateTimeWithZone(100, getTimeZoneKeyForOffset(123)), packDateTimeWithZone(100, getTimeZoneKeyForOffset(234)), // test structural type arrayBlockOf(BIGINT, 12, 34, 56), arrayBlockOf(BIGINT, 12, 34, 56)))), ImmutableList.of(ImmutableSet.of(0, 1), ImmutableSet.of(2, 3), ImmutableSet.of(4, 5))); RecordCursor recordCursor = fieldSetFilteringRecordSet.cursor(); assertTrue(recordCursor.advanceNextPosition()); } }
private List<Long> readTimeStampsFrom(RecordSet recordSet) { ImmutableList.Builder<Long> result = ImmutableList.builder(); try (RecordCursor cursor = recordSet.cursor()) { while (cursor.advanceNextPosition()) { for (int i = 0; i < recordSet.getColumnTypes().size(); i++) { cursor.isNull(i); } if (cursor.isNull(0)) { return result.build(); } assertTrue(recordSet.getColumnTypes().get(0) instanceof TimestampType); result.add(cursor.getLong(0)); } } return result.build(); }
assertEquals(cursor.getType(i), columnHandles.get(i).getColumnType()); 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");
private static void assertRecordCursor(List<Type> types, Iterator<?>[] valuesByField, RecordCursor cursor) { while (cursor.advanceNextPosition()) { for (int field = 0; field < types.size(); field++) { assertTrue(valuesByField[field].hasNext()); Object expected = valuesByField[field].next(); Object actual = getActualCursorValue(cursor, types.get(field), field); assertEquals(actual, expected); } } }
@Test public void testRecordSetProvider() throws Exception { for (SchemaTableName schemaTableName : metadata.listTables(SESSION, JMX_SCHEMA_NAME)) { RecordSet recordSet = getRecordSet(schemaTableName); try (RecordCursor cursor = recordSet.cursor()) { while (cursor.advanceNextPosition()) { for (int i = 0; i < recordSet.getColumnTypes().size(); i++) { cursor.isNull(i); } } } } }
if (!cursor.isNull(columnIndex)) { Type type = column.getType(); if (BOOLEAN.equals(type)) { cursor.getBoolean(columnIndex); cursor.getLong(columnIndex); cursor.getLong(columnIndex); cursor.getLong(columnIndex); cursor.getDouble(columnIndex); cursor.getLong(columnIndex); cursor.getSlice(columnIndex); fail("Unknown primitive type " + columnIndex);
boolean hasNextPosition = delegate.advanceNextPosition(); if (!hasNextPosition) { return false; if (delegate.isNull(index)) { scratch[i] = null; continue; scratch[i] = delegate.getBoolean(index); scratch[i] = delegate.getLong(index); scratch[i] = delegate.getDouble(index); scratch[i] = delegate.getSlice(index); scratch[i] = (Block) delegate.getObject(index);
.collect(toImmutableList()); RecordCursor cursor = data.cursor(); while (true) { if (!cursor.advanceNextPosition()) { if (batch.size() > 0) { batch.execute(); Type type = columns.get(column).getType(); if (BOOLEAN.equals(type)) { batch.bind(column, cursor.getBoolean(column)); batch.bind(column, cursor.getLong(column)); batch.bind(column, (int) cursor.getLong(column)); batch.bind(column, cursor.getDouble(column)); batch.bind(column, cursor.getSlice(column).toStringUtf8()); long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
private static IndexedTable indexTable(RecordSet recordSet, final List<String> outputColumns, List<String> keyColumns) { List<Integer> keyPositions = keyColumns.stream() .map(columnName -> { int position = outputColumns.indexOf(columnName); checkState(position != -1); return position; }) .collect(toImmutableList()); ImmutableListMultimap.Builder<MaterializedTuple, MaterializedTuple> indexedValuesBuilder = ImmutableListMultimap.builder(); List<Type> outputTypes = recordSet.getColumnTypes(); List<Type> keyTypes = extractPositionValues(outputTypes, keyPositions); RecordCursor cursor = recordSet.cursor(); while (cursor.advanceNextPosition()) { List<Object> values = extractValues(cursor, outputTypes); List<Object> keyValues = extractPositionValues(values, keyPositions); indexedValuesBuilder.put(new MaterializedTuple(keyValues), new MaterializedTuple(values)); } return new IndexedTable(keyColumns, keyTypes, outputColumns, outputTypes, indexedValuesBuilder.build()); }
@Override public void coerce(RecordCursor delegate, int field) { try { long value = Long.parseLong(delegate.getSlice(field).toStringUtf8()); if (minValue <= value && value <= maxValue) { setLong(value); } else { setIsNull(true); } } catch (NumberFormatException e) { setIsNull(true); } } }
private Comparable<?> getPrestoValue(RecordCursor recordCursor, List<Column> columns, int columnId) { if (recordCursor.isNull(columnId)) { return null; } Column column = columns.get(columnId); ColumnType.Base baseType = column.getType().getBase(); switch (baseType) { case IDENTIFIER: case INTEGER: case DATE: case TIME: case DECIMAL: return recordCursor.getLong(columnId); case VARCHAR: case CHAR: return recordCursor.getSlice(columnId).toStringAscii(); } throw new UnsupportedOperationException(format("Unsupported TPCDS base type [%s]", baseType)); } }