-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Optimize partitioned exchange for RowType channels #12762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raunaqmorarka
merged 1 commit into
trinodb:master
from
starburstdata:ls/020-poo-row-type
Jul 4, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
core/trino-main/src/main/java/io/trino/operator/output/RowPositionsAppender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.operator.output; | ||
|
|
||
| import io.trino.spi.block.Block; | ||
| import io.trino.spi.block.RowBlock; | ||
| import io.trino.spi.block.RunLengthEncodedBlock; | ||
| import io.trino.spi.type.RowType; | ||
| import it.unimi.dsi.fastutil.ints.IntArrayList; | ||
| import org.openjdk.jol.info.ClassLayout; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static io.airlift.slice.SizeOf.sizeOf; | ||
| import static io.trino.operator.output.PositionsAppenderUtil.calculateBlockResetSize; | ||
| import static io.trino.operator.output.PositionsAppenderUtil.calculateNewArraySize; | ||
| import static io.trino.spi.block.RowBlock.fromFieldBlocks; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class RowPositionsAppender | ||
| implements PositionsAppender | ||
| { | ||
| private static final int INSTANCE_SIZE = ClassLayout.parseClass(RowPositionsAppender.class).instanceSize(); | ||
| private final PositionsAppender[] fieldAppenders; | ||
| private int initialEntryCount; | ||
| private boolean initialized; | ||
|
|
||
| private int positionCount; | ||
| private boolean hasNullRow; | ||
| private boolean hasNonNullRow; | ||
| private boolean[] rowIsNull = new boolean[0]; | ||
| private long retainedSizeInBytes; | ||
| private long sizeInBytes; | ||
|
|
||
| public static RowPositionsAppender createRowAppender( | ||
| PositionsAppenderFactory positionsAppenderFactory, | ||
| RowType type, | ||
| int expectedPositions, | ||
| long maxPageSizeInBytes) | ||
| { | ||
| PositionsAppender[] fields = new PositionsAppender[type.getFields().size()]; | ||
| for (int i = 0; i < fields.length; i++) { | ||
| fields[i] = positionsAppenderFactory.create(type.getFields().get(i).getType(), expectedPositions, maxPageSizeInBytes); | ||
| } | ||
| return new RowPositionsAppender(fields, expectedPositions); | ||
| } | ||
|
|
||
| private RowPositionsAppender(PositionsAppender[] fieldAppenders, int expectedPositions) | ||
| { | ||
| this.fieldAppenders = requireNonNull(fieldAppenders, "fields is null"); | ||
| this.initialEntryCount = expectedPositions; | ||
| updateRetainedSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public void append(IntArrayList positions, Block block) | ||
| { | ||
| if (positions.isEmpty()) { | ||
| return; | ||
| } | ||
| ensureCapacity(positions.size()); | ||
| RowBlock sourceRowBlock = (RowBlock) block; | ||
| IntArrayList nonNullPositions; | ||
| if (sourceRowBlock.mayHaveNull()) { | ||
| nonNullPositions = processNullablePositions(positions, sourceRowBlock); | ||
| hasNullRow |= nonNullPositions.size() < positions.size(); | ||
| hasNonNullRow |= nonNullPositions.size() > 0; | ||
| } | ||
| else { | ||
| // the source Block does not have nulls | ||
| nonNullPositions = processNonNullablePositions(positions, sourceRowBlock); | ||
| hasNonNullRow = true; | ||
| } | ||
|
|
||
| List<Block> fieldBlocks = sourceRowBlock.getChildren(); | ||
| for (int i = 0; i < fieldAppenders.length; i++) { | ||
| fieldAppenders[i].append(nonNullPositions, fieldBlocks.get(i)); | ||
| } | ||
|
|
||
| positionCount += positions.size(); | ||
| updateSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public void appendRle(RunLengthEncodedBlock rleBlock) | ||
| { | ||
| int rlePositionCount = rleBlock.getPositionCount(); | ||
| ensureCapacity(rlePositionCount); | ||
| RowBlock sourceRowBlock = (RowBlock) rleBlock.getValue(); | ||
| if (sourceRowBlock.isNull(0)) { | ||
| // append rlePositionCount nulls | ||
| Arrays.fill(rowIsNull, positionCount, positionCount + rlePositionCount, true); | ||
| hasNullRow = true; | ||
| } | ||
| else { | ||
| // append not null row value | ||
| List<Block> fieldBlocks = sourceRowBlock.getChildren(); | ||
| int fieldPosition = sourceRowBlock.getFieldBlockOffset(0); | ||
| for (int i = 0; i < fieldAppenders.length; i++) { | ||
| fieldAppenders[i].appendRle(new RunLengthEncodedBlock(fieldBlocks.get(i).getSingleValueBlock(fieldPosition), rlePositionCount)); | ||
| } | ||
| hasNonNullRow = true; | ||
| } | ||
| positionCount += rlePositionCount; | ||
| updateSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public Block build() | ||
| { | ||
| Block[] fieldBlocks = new Block[fieldAppenders.length]; | ||
| for (int i = 0; i < fieldAppenders.length; i++) { | ||
| fieldBlocks[i] = fieldAppenders[i].build(); | ||
| } | ||
| Block result; | ||
| if (hasNonNullRow) { | ||
| result = fromFieldBlocks(positionCount, hasNullRow ? Optional.of(rowIsNull) : Optional.empty(), fieldBlocks); | ||
| } | ||
| else { | ||
| Block nullRowBlock = fromFieldBlocks(1, Optional.of(new boolean[] {true}), fieldBlocks); | ||
| result = new RunLengthEncodedBlock(nullRowBlock, positionCount); | ||
| } | ||
|
|
||
| reset(); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public long getRetainedSizeInBytes() | ||
| { | ||
| long size = retainedSizeInBytes; | ||
| for (PositionsAppender field : fieldAppenders) { | ||
| size += field.getRetainedSizeInBytes(); | ||
| } | ||
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public long getSizeInBytes() | ||
| { | ||
| return sizeInBytes; | ||
| } | ||
|
|
||
| private void reset() | ||
| { | ||
| initialEntryCount = calculateBlockResetSize(positionCount); | ||
| initialized = false; | ||
| rowIsNull = new boolean[0]; | ||
| positionCount = 0; | ||
| sizeInBytes = 0; | ||
| hasNonNullRow = false; | ||
| hasNullRow = false; | ||
| updateRetainedSize(); | ||
| } | ||
|
|
||
| private IntArrayList processNullablePositions(IntArrayList positions, RowBlock sourceRowBlock) | ||
| { | ||
| int[] nonNullPositions = new int[positions.size()]; | ||
| int nonNullPositionsCount = 0; | ||
|
|
||
| for (int i = 0; i < positions.size(); i++) { | ||
| int position = positions.getInt(i); | ||
| boolean positionIsNull = sourceRowBlock.isNull(position); | ||
| nonNullPositions[nonNullPositionsCount] = sourceRowBlock.getFieldBlockOffset(position); | ||
| nonNullPositionsCount += positionIsNull ? 0 : 1; | ||
| rowIsNull[positionCount + i] = positionIsNull; | ||
| } | ||
|
|
||
| return IntArrayList.wrap(nonNullPositions, nonNullPositionsCount); | ||
| } | ||
|
|
||
| private IntArrayList processNonNullablePositions(IntArrayList positions, RowBlock sourceRowBlock) | ||
| { | ||
| int[] nonNullPositions = new int[positions.size()]; | ||
| for (int i = 0; i < positions.size(); i++) { | ||
| nonNullPositions[i] = sourceRowBlock.getFieldBlockOffset(positions.getInt(i)); | ||
| } | ||
| return IntArrayList.wrap(nonNullPositions); | ||
| } | ||
|
|
||
| private void ensureCapacity(int additionalCapacity) | ||
| { | ||
| if (rowIsNull.length <= positionCount + additionalCapacity) { | ||
| int newSize; | ||
| if (initialized) { | ||
| newSize = calculateNewArraySize(rowIsNull.length); | ||
| } | ||
| else { | ||
| newSize = initialEntryCount; | ||
| initialized = true; | ||
| } | ||
|
|
||
| int newCapacity = Math.max(newSize, positionCount + additionalCapacity); | ||
lukasz-stec marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rowIsNull = Arrays.copyOf(rowIsNull, newCapacity); | ||
| updateRetainedSize(); | ||
| } | ||
| } | ||
|
|
||
| private void updateSize() | ||
| { | ||
| long size = (Integer.BYTES + Byte.BYTES) * (long) positionCount; | ||
| for (PositionsAppender field : fieldAppenders) { | ||
| size += field.getSizeInBytes(); | ||
| } | ||
| sizeInBytes = size; | ||
| } | ||
|
|
||
| private void updateRetainedSize() | ||
| { | ||
| retainedSizeInBytes = INSTANCE_SIZE + sizeOf(rowIsNull); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.