Repository navigation
[client] Prune Arrow buffers before client-side decompression. #4883
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
Open
loserwang1024
wants to merge
1
commit into
apache:main
Choose a base branch
from
loserwang1024:poc-test-scan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,021
−91
Open
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
200 changes: 200 additions & 0 deletions
200
fluss-common/src/main/java/org/apache/fluss/record/ArrowReadPlan.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,200 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.fluss.record; | ||
|
|
||
| import org.apache.fluss.annotation.Internal; | ||
| import org.apache.fluss.compression.ArrowCompressionFactory; | ||
| import org.apache.fluss.memory.MemorySegment; | ||
| import org.apache.fluss.row.ProjectedRow; | ||
| import org.apache.fluss.row.arrow.ArrowReader; | ||
| import org.apache.fluss.shaded.arrow.org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.fluss.shaded.arrow.org.apache.arrow.vector.TypeLayout; | ||
| import org.apache.fluss.shaded.arrow.org.apache.arrow.vector.VectorSchemaRoot; | ||
| import org.apache.fluss.shaded.arrow.org.apache.arrow.vector.ipc.message.ArrowRecordBatch; | ||
| import org.apache.fluss.shaded.arrow.org.apache.arrow.vector.types.pojo.Field; | ||
| import org.apache.fluss.types.RowType; | ||
| import org.apache.fluss.utils.ArrowUtils; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.annotation.concurrent.NotThreadSafe; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.BitSet; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Reads Arrow record batches of one source schema. The loaded vectors, the buffers selected before | ||
| * decompression and the output row mapping are all derived from the same selected source fields, so | ||
| * they cannot disagree. | ||
| * | ||
| * <p>The plan owns a {@link VectorSchemaRoot} that is reused across batches: each {@link | ||
| * #load(MemorySegment, int, int)} replaces the buffers of the previous one, and {@link #clear()} | ||
| * releases them once a batch has been consumed. | ||
| */ | ||
| @Internal | ||
| @NotThreadSafe | ||
| public final class ArrowReadPlan implements AutoCloseable { | ||
| private final BufferAllocator allocator; | ||
| private final RowType loadedRowType; | ||
| private final VectorSchemaRoot root; | ||
| // null when every source field is loaded and no buffer needs to be skipped | ||
| @Nullable private final int[] nodeIndexes; | ||
| @Nullable private final int[] bufferIndexes; | ||
| private final int nodeCount; | ||
| private final int bufferCount; | ||
| // null when loaded rows can be returned as-is | ||
| @Nullable private final int[] outputMapping; | ||
|
|
||
| /** | ||
| * Creates a plan reading batches of {@code sourceType} into {@code allocator}. | ||
| * | ||
| * @param sourceType the row type of the serialized batch | ||
| * @param outputToSource maps each output field to its source field (-1 if absent), or null if | ||
| * the output fields are the source fields | ||
| * @param selectedFields the output fields read by the caller; unselected ones are never loaded | ||
| */ | ||
| ArrowReadPlan( | ||
| RowType sourceType, | ||
| @Nullable int[] outputToSource, | ||
| int[] selectedFields, | ||
| BufferAllocator allocator) { | ||
| int sourceFieldCount = sourceType.getFieldCount(); | ||
| int[] mapping = outputToSource; | ||
| if (mapping == null) { | ||
| mapping = new int[sourceFieldCount]; | ||
| Arrays.setAll(mapping, i -> i); | ||
| } | ||
| BitSet sourceFields = new BitSet(sourceFieldCount); | ||
| for (int selectedField : selectedFields) { | ||
| if (mapping[selectedField] >= 0) { | ||
| sourceFields.set(mapping[selectedField]); | ||
| } | ||
| } | ||
| int[] selectedSourceFields = sourceFields.stream().toArray(); | ||
| boolean pruneBuffers = selectedSourceFields.length < sourceFieldCount; | ||
|
|
||
| this.allocator = allocator; | ||
| this.loadedRowType = pruneBuffers ? sourceType.project(selectedSourceFields) : sourceType; | ||
| this.outputMapping = | ||
| outputToSource == null && !pruneBuffers | ||
| ? null | ||
| : toLoadedPositions(mapping, selectedSourceFields, sourceFieldCount); | ||
|
|
||
| List<Field> flattenedFields = new ArrayList<>(); | ||
| BitSet selectedNodes = new BitSet(); | ||
| List<Field> fields = ArrowUtils.toArrowSchema(sourceType).getFields(); | ||
| for (int i = 0; i < fields.size(); i++) { | ||
| int start = flattenedFields.size(); | ||
| flattenField(fields.get(i), flattenedFields); | ||
| if (sourceFields.get(i)) { | ||
| selectedNodes.set(start, flattenedFields.size()); | ||
| } | ||
| } | ||
| BitSet selectedBuffers = new BitSet(); | ||
| int bufferIndex = 0; | ||
| for (int i = 0; i < flattenedFields.size(); i++) { | ||
| int count = TypeLayout.getTypeBufferCount(flattenedFields.get(i).getType()); | ||
| if (selectedNodes.get(i)) { | ||
| selectedBuffers.set(bufferIndex, bufferIndex + count); | ||
| } | ||
| bufferIndex += count; | ||
| } | ||
| this.nodeCount = flattenedFields.size(); | ||
| this.bufferCount = bufferIndex; | ||
| this.nodeIndexes = pruneBuffers ? selectedNodes.stream().toArray() : null; | ||
| this.bufferIndexes = pruneBuffers ? selectedBuffers.stream().toArray() : null; | ||
| this.root = VectorSchemaRoot.create(ArrowUtils.toArrowSchema(loadedRowType), allocator); | ||
| } | ||
|
|
||
| /** Returns the row type of the loaded vectors: the selected source fields in source order. */ | ||
| public RowType getLoadedRowType() { | ||
| return loadedRowType; | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes the Arrow batch and loads only the selected fields; buffers of unselected fields | ||
| * are neither copied nor decompressed. Buffers of a previously loaded batch are replaced. | ||
| */ | ||
| public ArrowReader load(MemorySegment segment, int arrowOffset, int arrowLength) { | ||
| try (ArrowRecordBatch batch = | ||
| nodeIndexes == null | ||
| ? ArrowUtils.deserializeArrowRecordBatch( | ||
| segment, arrowOffset, arrowLength, allocator) | ||
| : ArrowUtils.deserializeProjectedArrowRecordBatch( | ||
| segment, | ||
| arrowOffset, | ||
| arrowLength, | ||
| allocator, | ||
| nodeCount, | ||
| bufferCount, | ||
| nodeIndexes, | ||
| bufferIndexes)) { | ||
| new FlussVectorLoader(root, ArrowCompressionFactory.INSTANCE).load(batch); | ||
| return ArrowUtils.createArrowReader(root, loadedRowType); | ||
| } catch (RuntimeException | Error e) { | ||
| // The root is shared across batches, so partially loaded buffers must not linger. | ||
| root.clear(); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a row view exposing loaded rows at the output positions expected by the caller, or | ||
| * null if loaded rows already match them. Only selected output fields may be read; the others, | ||
| * like fields absent from the source schema, are reported as null. | ||
| */ | ||
| @Nullable | ||
| public ProjectedRow createOutputRow() { | ||
| return outputMapping == null ? null : ProjectedRow.from(outputMapping); | ||
| } | ||
|
|
||
| /** Releases the buffers of the last loaded batch while keeping the plan reusable. */ | ||
| public void clear() { | ||
| root.clear(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| root.close(); | ||
| } | ||
|
|
||
| private static int[] toLoadedPositions( | ||
| int[] outputToSource, int[] selectedSourceFields, int sourceFieldCount) { | ||
| int[] sourceToLoaded = new int[sourceFieldCount]; | ||
| Arrays.fill(sourceToLoaded, -1); | ||
| for (int i = 0; i < selectedSourceFields.length; i++) { | ||
| sourceToLoaded[selectedSourceFields[i]] = i; | ||
| } | ||
| int[] outputToLoaded = new int[outputToSource.length]; | ||
| for (int i = 0; i < outputToSource.length; i++) { | ||
| int sourceField = outputToSource[i]; | ||
| outputToLoaded[i] = sourceField < 0 ? -1 : sourceToLoaded[sourceField]; | ||
| } | ||
| return outputToLoaded; | ||
| } | ||
|
|
||
| private static void flattenField(Field field, List<Field> flattenedFields) { | ||
| flattenedFields.add(field); | ||
| for (Field child : field.getChildren()) { | ||
| flattenField(child, flattenedFields); | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The full IPC body is still deserialized and copied before pruning. Please describe this as skipping vector loading and decompression for unselected columns, rather than saying their buffers are not copied.