@Override public Block toBlock(Type desiredType) { checkArgument(desiredType.getTypeParameters().size() == 1 && BIGINT.equals(desiredType.getTypeParameters().get(0)), "type doesn't match: %s", desiredType); int numberOfRecords = numberOfRecords(); return ArrayBlock.fromElementBlock( numberOfRecords, Optional.of(nulls == null ? new boolean[numberOfRecords] : nulls), calculateOffsets(sizes, nulls, numberOfRecords), values != null ? values.toBlock(BIGINT) : new LongArrayBlock(0, Optional.empty(), new long[] {})); }
private static Block createArrayBlock(int positionCount, Block elementsBlock) { int[] offsets = new int[positionCount + 1]; int arraySize = elementsBlock.getPositionCount() / positionCount; for (int i = 0; i < offsets.length; i++) { offsets[i] = arraySize * i; } return ArrayBlock.fromElementBlock(positionCount, Optional.empty(), offsets, elementsBlock); }
private ColumnChunk readArray(GroupField field) throws IOException { List<Type> parameters = field.getType().getTypeParameters(); checkArgument(parameters.size() == 1, "Arrays must have a single type parameter, found %d", parameters.size()); Field elementField = field.getChildren().get(0).get(); ColumnChunk columnChunk = readColumnChunk(elementField); IntList offsets = new IntArrayList(); BooleanList valueIsNull = new BooleanArrayList(); calculateCollectionOffsets(field, offsets, valueIsNull, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels()); Block arrayBlock = ArrayBlock.fromElementBlock(valueIsNull.size(), Optional.of(valueIsNull.toBooleanArray()), offsets.toIntArray(), columnChunk.getBlock()); return new ColumnChunk(arrayBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels()); }
public void testCompactBlock() { Block emptyValueBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); int[] offsets = {0, 1, 1, 2, 4, 8, 16}; boolean[] valueIsNull = {false, true, false, false, false, false}; testCompactBlock(fromElementBlock(0, Optional.empty(), new int[1], emptyValueBlock)); testCompactBlock(fromElementBlock(valueIsNull.length, Optional.of(valueIsNull), offsets, compactValueBlock)); testIncompactBlock(fromElementBlock(valueIsNull.length - 1, Optional.of(valueIsNull), offsets, compactValueBlock)); // underlying value block is not compact testIncompactBlock(fromElementBlock(valueIsNull.length, Optional.of(valueIsNull), offsets, inCompactValueBlock)); }
@Override public Block apply(Block block) { if (elementCoercer == null) { return block; } ColumnarArray arrayBlock = toColumnarArray(block); Block elementsBlock = elementCoercer.apply(arrayBlock.getElementsBlock()); boolean[] valueIsNull = new boolean[arrayBlock.getPositionCount()]; int[] offsets = new int[arrayBlock.getPositionCount() + 1]; for (int i = 0; i < arrayBlock.getPositionCount(); i++) { valueIsNull[i] = arrayBlock.isNull(i); offsets[i + 1] = offsets[i] + arrayBlock.getLength(i); } return ArrayBlock.fromElementBlock(arrayBlock.getPositionCount(), Optional.of(valueIsNull), offsets, elementsBlock); } }
@TypeParameter("T") @SqlType("array(array(T))") public static Block ngrams(@SqlType("array(T)") Block array, @SqlType(INTEGER) long n) { checkCondition(n > 0, INVALID_FUNCTION_ARGUMENT, "N must be positive"); // n should not be larger than the array length int elementsPerRecord = toIntExact(min(array.getPositionCount(), n)); int totalRecords = array.getPositionCount() - elementsPerRecord + 1; int[] ids = new int[totalRecords * elementsPerRecord]; int[] offset = new int[totalRecords + 1]; for (int recordIndex = 0; recordIndex < totalRecords; recordIndex++) { for (int elementIndex = 0; elementIndex < elementsPerRecord; elementIndex++) { ids[recordIndex * elementsPerRecord + elementIndex] = recordIndex + elementIndex; } offset[recordIndex + 1] = (recordIndex + 1) * elementsPerRecord; } return ArrayBlock.fromElementBlock(totalRecords, Optional.empty(), offset, array.getPositions(ids, 0, totalRecords * elementsPerRecord)); } }
elements = elementType.createBlockBuilder(null, 0).build(); Block arrayBlock = ArrayBlock.fromElementBlock(nextBatchSize, Optional.ofNullable(nullVector), offsetVector, elements);
elements = elementType.createBlockBuilder(null, 0).build(); Block arrayBlock = ArrayBlock.fromElementBlock(nextBatchSize, Optional.ofNullable(nullVector), offsetVector, elements);