@Override public final void load(LazyBlock lazyBlock) { if (loaded) { return; } checkState(batchId == expectedBatchId); try { Block block = parquetReader.readPrimitive(columnDescriptor, type); lazyBlock.setBlock(block); } catch (IOException e) { throw new HdfsCursorException(); } loaded = true; }
@ScalarFunction("parse_agent") @Description("Returns Map, which has keys such as 'category', 'name', 'os', 'version', 'vendor' and 'os_version'") @SqlType("map<varchar,varchar>") public Block parseAgent(@TypeParameter("map<varchar,varchar>") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice slice) { String argument = slice.toStringUtf8(); Map<String, String> stringMap = Classifier.parse(argument); if (pageBuilder.isFull()) { pageBuilder.reset(); } BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0); BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry(); for (Map.Entry<String, String> entry : stringMap.entrySet()) { VARCHAR.writeSlice(singleMapBlockBuilder, Slices.utf8Slice(entry.getKey())); VARCHAR.writeSlice(singleMapBlockBuilder, Slices.utf8Slice(entry.getValue())); } blockBuilder.closeEntry(); pageBuilder.declarePosition(); return (Block) mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1); }
@Test public void testPoly_contains() throws Exception { double[] poly = new double[]{ 45, 9.5, 45.5, 9.5, 45.5, 9, 46, 9, 46, 10, 45, 10 }; Block blockPoly = toBlock(poly); assertFalse(PolyContains.contains(DoubleType.DOUBLE, blockPoly, 6, 3)); assertFalse(PolyContains.contains(DoubleType.DOUBLE, blockPoly, 45, 9)); assertTrue(PolyContains.contains(DoubleType.DOUBLE, blockPoly, 45.7, 9.7)); }
@Override public void addInput(Page page) { requireNonNull(page, "page is null"); checkState(state == State.RUNNING, "Operator is %s", state); Block[] blocks = new Block[inputChannels.size()]; for (int outputChannel = 0; outputChannel < inputChannels.size(); outputChannel++) { blocks[outputChannel] = page.getBlock(inputChannels.get(outputChannel)); } Block sampleWeightBlock = null; if (sampleWeightChannel.isPresent()) { sampleWeightBlock = page.getBlock(sampleWeightChannel.get()); } pageSink.appendPage(new Page(blocks), sampleWeightBlock); rowCount += page.getPositionCount(); }
@Test public void testRoundTrip() { BlockBuilder expectedBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 5); VARCHAR.writeString(expectedBlockBuilder, "alice"); VARCHAR.writeString(expectedBlockBuilder, "bob"); VARCHAR.writeString(expectedBlockBuilder, "charlie"); VARCHAR.writeString(expectedBlockBuilder, "dave"); Block expectedBlock = expectedBlockBuilder.build(); Page expectedPage = new Page(expectedBlock, expectedBlock, expectedBlock); DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024); writePages(blockEncodingManager, sliceOutput, expectedPage, expectedPage, expectedPage); List<Type> types = ImmutableList.<Type>of(VARCHAR, VARCHAR, VARCHAR); Iterator<Page> pageIterator = readPages(blockEncodingManager, sliceOutput.slice().getInput()); assertPageEquals(types, pageIterator.next(), expectedPage); assertPageEquals(types, pageIterator.next(), expectedPage); assertPageEquals(types, pageIterator.next(), expectedPage); assertFalse(pageIterator.hasNext()); }
public static Block toArray(Type arrayType, ConnectorSession connectorSession, Slice json) { try { List<?> array = (List<?>) stackRepresentationToObject(connectorSession, json, arrayType); if (array == null) { return null; } Type elementType = ((ArrayType) arrayType).getElementType(); BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), array.size()); for (Object element : array) { appendToBlockBuilder(elementType, element, blockBuilder); } return blockBuilder.build(); } catch (RuntimeException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to " + arrayType, e); } }
private static IntComparator IntBlockCompare(Type type, Block block) { return new AbstractIntComparator() { @Override public int compare(int left, int right) { if (block.isNull(left) && block.isNull(right)) { return 0; } if (block.isNull(left)) { return -1; } if (block.isNull(right)) { return 1; } return type.compareTo(block, left, block, right); } }; }
private static Page[] offsetColumns(Page[] pages, int offset) { Page[] newPages = new Page[pages.length]; for (int i = 0; i < pages.length; i++) { Page page = pages[i]; Block[] newBlocks = new Block[page.getChannelCount() + offset]; for (int channel = 0; channel < offset; channel++) { newBlocks[channel] = createNullRLEBlock(page.getPositionCount()); } for (int channel = 0; channel < page.getBlocks().length; channel++) { newBlocks[channel + offset] = page.getBlocks()[channel]; } newPages[i] = new Page(page.getPositionCount(), newBlocks); } return newPages; }
public static Block createTestBlock() { BlockBuilder blockBuilder = TIMESTAMP.createBlockBuilder(new BlockBuilderStatus(), 15); TIMESTAMP.writeLong(blockBuilder, 1111); TIMESTAMP.writeLong(blockBuilder, 1111); TIMESTAMP.writeLong(blockBuilder, 1111); TIMESTAMP.writeLong(blockBuilder, 2222); TIMESTAMP.writeLong(blockBuilder, 2222); TIMESTAMP.writeLong(blockBuilder, 2222); TIMESTAMP.writeLong(blockBuilder, 2222); TIMESTAMP.writeLong(blockBuilder, 2222); TIMESTAMP.writeLong(blockBuilder, 3333); TIMESTAMP.writeLong(blockBuilder, 3333); TIMESTAMP.writeLong(blockBuilder, 4444); return blockBuilder.build(); }
@UsedByGeneratedCode public static long arrayPosition(Type type, MethodHandle equalMethodHandle, Block array, boolean element) { int size = array.getPositionCount(); for (int i = 0; i < size; i++) { if (!array.isNull(i)) { boolean arrayValue = type.getBoolean(array, i); try { if ((boolean) equalMethodHandle.invokeExact(arrayValue, element)) { return i + 1; // result is 1-based (instead of 0) } } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, Error.class); Throwables.propagateIfInstanceOf(t, PrestoException.class); throw new PrestoException(INTERNAL_ERROR, t); } } } return 0; }
@Override public final void load(LazyBlock lazyBlock) { if (loaded) { return; } checkState(batchId == expectedBatchId); try { Block block = parquetReader.readBlock(columnDescriptor, batchSize, type); lazyBlock.setBlock(block); } catch (IOException e) { throw new PrestoException(HIVE_CURSOR_ERROR, e); } loaded = true; }
@UsedByGeneratedCode public static Object subscript(MethodHandle keyEqualsMethod, Type keyType, Type valueType, Block map, double key) { for (int position = 0; position < map.getPositionCount(); position += 2) { try { if ((boolean) keyEqualsMethod.invokeExact(keyType.getDouble(map, position), key)) { return readNativeValue(valueType, map, position + 1); // position + 1: value position } } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, Error.class); Throwables.propagateIfInstanceOf(t, PrestoException.class); throw new PrestoException(INTERNAL_ERROR, t); } } return null; }
public static void combine(KeyValuePairsState state, KeyValuePairsState otherState) { if (state.get() != null && otherState.get() != null) { Block keys = otherState.get().getKeys(); Block values = otherState.get().getValues(); KeyValuePairs pairs = state.get(); long startSize = pairs.estimatedInMemorySize(); for (int i = 0; i < keys.getPositionCount(); i++) { try { pairs.add(keys, values, i, i); } catch (ExceededMemoryLimitException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("The result of map_agg may not exceed %s", e.getMaxMemory())); } } state.addMemoryUsage(pairs.estimatedInMemorySize() - startSize); } else if (state.get() == null) { state.set(otherState.get()); } }
@Override public void addInput(Page page) { requireNonNull(page, "page is null"); checkState(!finishing, "Operator is finishing"); checkState(outputPage == null, "Operator still has pending output"); operatorContext.setMemoryReservation(markDistinctHash.getEstimatedSize()); Block markerBlock = markDistinctHash.markDistinctRows(page); // add the new boolean column to the page Block[] sourceBlocks = page.getBlocks(); Block[] outputBlocks = new Block[sourceBlocks.length + 1]; // +1 for the single boolean output channel System.arraycopy(sourceBlocks, 0, outputBlocks, 0, sourceBlocks.length); outputBlocks[sourceBlocks.length] = markerBlock; outputPage = new Page(outputBlocks); }
public static void input(BlockComparator comparator, Type valueType, Type keyType, MinMaxByNState state, Block value, Block key, int blockIndex, long n) { TypedKeyValueHeap heap = state.getTypedKeyValueHeap(); if (heap == null) { if (n <= 0) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "third argument of max_by/min_by must be a positive integer"); } heap = new TypedKeyValueHeap(comparator, keyType, valueType, Ints.checkedCast(n)); state.setTypedKeyValueHeap(heap); } long startSize = heap.getEstimatedSize(); if (!key.isNull(blockIndex)) { heap.add(key, value, blockIndex); } state.addMemoryUsage(heap.getEstimatedSize() - startSize); }
public void addPage(Page page) { // ignore empty pages if (page.getPositionCount() == 0) { return; } positionCount += page.getPositionCount(); int pageIndex = (channels.length > 0) ? channels[0].size() : 0; for (int i = 0; i < channels.length; i++) { Block block = page.getBlock(i); channels[i].add(block); pagesMemorySize += block.getRetainedSizeInBytes(); } for (int position = 0; position < page.getPositionCount(); position++) { long sliceAddress = encodeSyntheticAddress(pageIndex, position); valueAddresses.add(sliceAddress); } estimatedSize = calculateEstimatedSize(); }
@Override protected List<Object> computeNext() { position++; if (position >= page.getPositionCount()) { return endOfData(); } List<Object> values = new ArrayList<>(page.getChannelCount()); for (int channel = 0; channel < page.getChannelCount(); channel++) { Type type = types.get(channel); Block block = page.getBlock(channel); values.add(type.getObjectValue(session, block, position)); } return Collections.unmodifiableList(values); }
@Test public void testComplexSerialization() { StateCompiler compiler = new StateCompiler(); AccumulatorStateFactory<TestComplexState> factory = compiler.generateStateFactory(TestComplexState.class); AccumulatorStateSerializer<TestComplexState> serializer = compiler.generateStateSerializer(TestComplexState.class); TestComplexState singleState = factory.createSingleState(); TestComplexState deserializedState = factory.createSingleState(); singleState.setBoolean(true); singleState.setLong(1); singleState.setDouble(2.0); singleState.setByte((byte) 3); BlockBuilder builder = VarcharType.VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 1); serializer.serialize(singleState, builder); Block block = builder.build(); serializer.deserialize(block, 0, deserializedState); assertEquals(deserializedState.getBoolean(), singleState.getBoolean()); assertEquals(deserializedState.getLong(), singleState.getLong()); assertEquals(deserializedState.getDouble(), singleState.getDouble()); assertEquals(deserializedState.getByte(), singleState.getByte()); }
public static Block getHashBlock(List<? extends Type> hashTypes, Block... hashBlocks) { checkArgument(hashTypes.size() == hashBlocks.length); int[] hashChannels = new int[hashBlocks.length]; for (int i = 0; i < hashBlocks.length; i++) { hashChannels[i] = i; } HashGenerator hashGenerator = new InterpretedHashGenerator(ImmutableList.copyOf(hashTypes), hashChannels); int positionCount = hashBlocks[0].getPositionCount(); BlockBuilder builder = BIGINT.createFixedSizeBlockBuilder(positionCount); Page page = new Page(hashBlocks); for (int i = 0; i < positionCount; i++) { BIGINT.writeLong(builder, hashGenerator.hashPosition(i, page)); } return builder.build(); }
static List<Page> dropChannel(List<Page> pages, List<Integer> channels) { List<Page> actualPages = new ArrayList<>(); for (Page page : pages) { int channel = 0; Block[] blocks = new Block[page.getChannelCount() - channels.size()]; for (int i = 0; i < page.getChannelCount(); i++) { if (channels.contains(i)) { continue; } blocks[channel++] = page.getBlock(i); } actualPages.add(new Page(blocks)); } return actualPages; }
public static Block createTestBlock() { BlockBuilder blockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), 15); BIGINT.writeLong(blockBuilder, 1111); BIGINT.writeLong(blockBuilder, 1111); BIGINT.writeLong(blockBuilder, 1111); BIGINT.writeLong(blockBuilder, 2222); BIGINT.writeLong(blockBuilder, 2222); BIGINT.writeLong(blockBuilder, 2222); BIGINT.writeLong(blockBuilder, 2222); BIGINT.writeLong(blockBuilder, 2222); BIGINT.writeLong(blockBuilder, 3333); BIGINT.writeLong(blockBuilder, 3333); BIGINT.writeLong(blockBuilder, 4444); return blockBuilder.build(); }
private Page getNonLazyPage(Page page) { ImmutableSet.Builder<Integer> builder = ImmutableSet.builder(); for (ProjectionFunction projection : projections) { builder.addAll(projection.getInputChannels()); } Set<Integer> inputChannels = builder.build(); if (inputChannels.isEmpty()) { return page; } Block[] blocks = page.getBlocks(); for (int inputChannel : inputChannels) { Block block = page.getBlock(inputChannel); if (block instanceof LazyBlock) { blocks[inputChannel] = ((LazyBlock) block).getBlock(); } } return new Page(blocks); }
public static Boolean contains(Type elementType, MethodHandle equals, Block arrayBlock, double value) { boolean foundNull = false; for (int i = 0; i < arrayBlock.getPositionCount(); i++) { if (arrayBlock.isNull(i)) { foundNull = true; continue; } try { if ((boolean) equals.invokeExact(elementType.getDouble(arrayBlock, i), value)) { return true; } } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, Error.class); Throwables.propagateIfInstanceOf(t, PrestoException.class); throw new PrestoException(INTERNAL_ERROR, t); } } if (foundNull) { return null; } return false; }
@Test public void testReuse() throws Exception { BytesWritable value = new BytesWritable(); byte[] first = "hello world".getBytes(UTF_8); value.set(first, 0, first.length); byte[] second = "bye".getBytes(UTF_8); value.set(second, 0, second.length); Type type = new TypeToken<Map<BytesWritable, Integer>>() {}.getType(); ObjectInspector inspector = getInspector(type); Block actual = getBlockObject(new MapType(VARCHAR, BIGINT), ImmutableMap.of(value, 0), inspector); Block expected = mapBlockOf(VARCHAR, BIGINT, "bye", 0); assertBlockEquals(actual, expected); }
@Override public final void load(LazyBlock lazyBlock) { if (loaded) { return; } checkState(batchId == expectedBatchId); try { Block block = recordReader.readBlock(type, columnIndex); lazyBlock.setBlock(block); } catch (IOException e) { if (e instanceof OrcCorruptionException) { throw new PrestoException(HIVE_BAD_DATA, e); } throw new PrestoException(HIVE_CURSOR_ERROR, e); } loaded = true; }
public HDFSPageSource( ParquetReader parquetReader, ParquetDataSource dataSource, MessageType fileSchema, MessageType requestedSchema, long totalBytes, List<HDFSColumnHandle> columns, TypeManager typeManager) { checkArgument(totalBytes >= 0, "totalBytes is negative"); this.parquetReader = requireNonNull(parquetReader, "parquetReader is null"); this.dataSource = requireNonNull(dataSource, "dataSource is null"); this.fileSchema = requireNonNull(fileSchema, "fileSchema is null"); this.requestedSchema = requireNonNull(requestedSchema, "requestedSchema is null"); this.totalBytes = totalBytes; this.columnSize = columns.size(); this.constantBlocks = new Block[columnSize]; ImmutableList.Builder<String> namesBuilder = ImmutableList.builder(); ImmutableList.Builder<Type> typesBuilder = ImmutableList.builder(); for (int columnIndex = 0; columnIndex < columnSize; columnIndex++) { HDFSColumnHandle column = columns.get(columnIndex); String name = column.getName(); Type type = typeManager.getType(column.getType().getTypeSignature()); namesBuilder.add(name); typesBuilder.add(type); if (getParquetType(column, fileSchema) == null) { constantBlocks[columnIndex] = RunLengthEncodedBlock.create(type, null, MAX_VECTOR_LENGTH); } } columnNames = namesBuilder.build(); types = typesBuilder.build(); }
private static Block serializeObject(Type type, BlockBuilder builder, Object object) { if (!isStructuralType(type)) { serializePrimitive(type, builder, object); return null; } else if (isArrayType(type)) { return serializeList(type, builder, object); } else if (isMapType(type)) { return serializeMap(type, builder, object); } else if (isRowType(type)) { return serializeStruct(type, builder, object); } throw new RuntimeException("Unknown object type: " + type); }
private static Block serializeList(Type type, BlockBuilder builder, Object object) { List<?> list = (List) object; if (list == null) { requireNonNull(builder, "parent builder is null").appendNull(); return null; } List<Type> typeParameters = type.getTypeParameters(); checkArgument(typeParameters.size() == 1, "list must have exactly 1 type parameter"); Type elementType = typeParameters.get(0); BlockBuilder currentBuilder; if (builder != null) { currentBuilder = builder.beginBlockEntry(); } else { currentBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), list.size()); } for (Object element : list) { serializeObject(elementType, currentBuilder, element); } if (builder != null) { builder.closeEntry(); return null; } else { Block resultBlock = currentBuilder.build(); return resultBlock; } }
private static Block serializeMap(Type type, BlockBuilder builder, Object object) { Map<?, ?> map = (Map) object; if (map == null) { requireNonNull(builder, "parent builder is null").appendNull(); return null; } List<Type> typeParameters = type.getTypeParameters(); checkArgument(typeParameters.size() == 2, "map must have exactly 2 type parameter"); Type keyType = typeParameters.get(0); Type valueType = typeParameters.get(1); BlockBuilder currentBuilder; if (builder != null) { currentBuilder = builder.beginBlockEntry(); } else { currentBuilder = new InterleavedBlockBuilder(typeParameters, new BlockBuilderStatus(), map.size()); } for (Map.Entry<?, ?> entry : map.entrySet()) { // Hive skips map entries with null keys if (entry.getKey() != null) { serializeObject(keyType, currentBuilder, entry.getKey()); serializeObject(valueType, currentBuilder, entry.getValue()); } } if (builder != null) { builder.closeEntry(); return null; } else { Block resultBlock = currentBuilder.build(); return resultBlock; } }
@Override public void appendTo(Block block, int position, BlockBuilder blockBuilder) { if (block.isNull(position)) { blockBuilder.appendNull(); } else { block.writeBytesTo(position, 0, block.getLength(position), blockBuilder); blockBuilder.closeEntry(); } }
@Override public Object getObjectValue(ConnectorSession session, Block block, int position) { if (block.isNull(position)) { return null; } return new SqlVarbinary(block.getSlice(position, 0, block.getLength(position)).getBytes()); }
@Override public void deserialize(Block block, int index, HyperLogLogState state) { if (!block.isNull(index)) { Slice slice = HyperLogLogType.HYPER_LOG_LOG.getSlice(block, index); state.setHyperLogLog(HyperLogLog.fromBytes(slice.getBytes()).toDenseHLL()); } }
@TypeParameter(StandardTypes.DOUBLE) @SqlType(StandardTypes.BOOLEAN) @Nullable public static Boolean contains( @TypeParameter(StandardTypes.DOUBLE) Type elementType, @SqlType("array(double)") Block arrayBlock, @SqlType(StandardTypes.DOUBLE) double lng, @SqlType(StandardTypes.DOUBLE) double lat) { double[] array= new double[arrayBlock.getPositionCount()] ; Polygon poly = new Polygon(); for (int i = 0; i < arrayBlock.getPositionCount(); i++) { if (arrayBlock.isNull(i)) { continue; } array[i]=elementType.getDouble(arrayBlock, i); } poly.startPath(array[0], array[1]); for (int i = 2; i < array.length; i += 2) { poly.lineTo(array[i], array[i + 1]); } return OperatorContains.local().execute(poly, new Point(lng,lat), null, null); }
@SqlType("array(double)") public static Block geohash_decode(@SqlType(StandardTypes.VARCHAR) Slice geohash) { BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(new BlockBuilderStatus(), 2); LatLong coordinates = GeoHash.decodeHash(geohash.toStringUtf8()); DOUBLE.writeDouble(blockBuilder, coordinates.getLat()); DOUBLE.writeDouble(blockBuilder, coordinates.getLon()); return blockBuilder.build(); }
private static Block toBlock(double[] poly) { FixedWidthBlockBuilder blockBuilder = new FixedWidthBlockBuilder(8, new BlockBuilderStatus(), poly.length); for (double d : poly) { blockBuilder.writeLong(Double.doubleToLongBits(d)); blockBuilder.closeEntry(); } return blockBuilder.build(); }
@Test public void testDouble() throws Exception { InternalAggregationFunction doubleAgg = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("checksum", AGGREGATE, VARBINARY, DOUBLE)); Block block = createDoublesBlock(null, 2.0, null, 3.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN); assertAggregation(doubleAgg, 1.0, expectedChecksum(DoubleType.DOUBLE, block), block); }
public static void input(Type type, SliceState state, Block block, int position) { if (state.getSlice() != null) { return; } state.setSlice(type.getSlice(block, position)); }
private static boolean filter(int position, Block discountBlock, Block shipDateBlock, Block quantityBlock) { return !shipDateBlock.isNull(position) && DATE.getLong(shipDateBlock, position) >= MIN_SHIP_DATE && !shipDateBlock.isNull(position) && DATE.getLong(shipDateBlock, position) < MAX_SHIP_DATE && !discountBlock.isNull(position) && DOUBLE.getDouble(discountBlock, position) >= 0.05 && !discountBlock.isNull(position) && DOUBLE.getDouble(discountBlock, position) <= 0.07 && !quantityBlock.isNull(position) && BIGINT.getLong(quantityBlock, position) < 24; }
@Override public int hashPosition(int position, Page page) { Block[] blocks = page.getBlocks(); int result = HashGenerationOptimizer.INITIAL_HASH_VALUE; for (int i = 0; i < hashChannels.length; i++) { Type type = hashChannelTypes.get(i); result = (int) CombineHashFunction.getHash(result, TypeUtils.hashPosition(type, blocks[hashChannels[i]], position)); } return result; }
public static Block createStringsBlock(Iterable<String> values) { BlockBuilder builder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 100); for (String value : values) { if (value == null) { builder.appendNull(); } else { VARCHAR.writeString(builder, value); } } return builder.build(); }