-
Notifications
You must be signed in to change notification settings - Fork 458
[common] Introduce MAP type for ARROW, COMPACTED and INDEXED formats #2190
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
XuQianJin-Stars
wants to merge
2
commits into
apache:main
Choose a base branch
from
XuQianJin-Stars:feature/issue-1973-support-map-format
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.
+3,631
−138
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
152 changes: 152 additions & 0 deletions
152
fluss-common/src/main/java/org/apache/fluss/row/BinaryMap.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,152 @@ | ||
| /* | ||
| * 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.row; | ||
|
|
||
| import org.apache.fluss.annotation.PublicEvolving; | ||
| import org.apache.fluss.memory.MemorySegment; | ||
| import org.apache.fluss.row.array.PrimitiveBinaryArray; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static org.apache.fluss.utils.Preconditions.checkArgument; | ||
|
|
||
| /** | ||
| * Binary implementation of {@link InternalMap} backed by {@link MemorySegment}s. | ||
| * | ||
| * <p>The binary layout of {@link BinaryMap}: | ||
| * | ||
| * <pre> | ||
| * [4 byte(keyArray size in bytes)] + [Key BinaryArray] + [Value BinaryArray]. | ||
| * </pre> | ||
| * | ||
| * <p>Influenced by Apache Spark UnsafeMapData. | ||
| * | ||
| * @since 0.9 | ||
| */ | ||
| @PublicEvolving | ||
| public class BinaryMap extends BinarySection implements InternalMap { | ||
|
||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private transient BinaryArray keys; | ||
| private transient BinaryArray values; | ||
|
|
||
| private final transient Supplier<BinaryArray> keyArraySupplier; | ||
| private final transient Supplier<BinaryArray> valueArraySupplier; | ||
| private final transient Supplier<BinaryMap> nestedMapSupplier; | ||
|
|
||
| public BinaryMap() { | ||
| this( | ||
| (Supplier<BinaryArray> & Serializable) PrimitiveBinaryArray::new, | ||
| (Supplier<BinaryArray> & Serializable) PrimitiveBinaryArray::new, | ||
| (Supplier<BinaryMap> & Serializable) BinaryMap::new); | ||
| } | ||
|
|
||
| public BinaryMap( | ||
| Supplier<BinaryArray> keyArraySupplier, | ||
| Supplier<BinaryArray> valueArraySupplier, | ||
| Supplier<BinaryMap> nestedMapSupplier) { | ||
| this.keyArraySupplier = keyArraySupplier; | ||
| this.valueArraySupplier = valueArraySupplier; | ||
| this.nestedMapSupplier = nestedMapSupplier; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return keys.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public void pointTo(MemorySegment[] segments, int offset, int sizeInBytes) { | ||
| // Read the numBytes of key array from the first 4 bytes. | ||
| final int keyArrayBytes = BinarySegmentUtils.getInt(segments, offset); | ||
| assert keyArrayBytes >= 0 : "keyArraySize (" + keyArrayBytes + ") should >= 0"; | ||
| final int valueArrayBytes = sizeInBytes - keyArrayBytes - 4; | ||
| assert valueArrayBytes >= 0 : "valueArraySize (" + valueArrayBytes + ") should >= 0"; | ||
|
|
||
| // see BinarySection.readObject, on this call stack, keys and values are not initialized | ||
| if (keys == null) { | ||
| keys = createKeyArrayInstance(); | ||
| } | ||
| keys.pointTo(segments, offset + 4, keyArrayBytes); | ||
| if (values == null) { | ||
| values = createValueArrayInstance(); | ||
| } | ||
| values.pointTo(segments, offset + 4 + keyArrayBytes, valueArrayBytes); | ||
|
|
||
| assert keys.size() == values.size(); | ||
|
|
||
| this.segments = segments; | ||
| this.offset = offset; | ||
| this.sizeInBytes = sizeInBytes; | ||
| } | ||
|
|
||
| /** Creates a {@link BinaryArray} instance for keys with the nested data type information. */ | ||
| protected BinaryArray createKeyArrayInstance() { | ||
| return keyArraySupplier.get(); | ||
| } | ||
|
|
||
| /** Creates a {@link BinaryArray} instance for values with the nested data type information. */ | ||
| protected BinaryArray createValueArrayInstance() { | ||
| return valueArraySupplier.get(); | ||
| } | ||
|
|
||
| /** Creates a nested {@link BinaryMap} with the nested data type information. */ | ||
| protected BinaryMap createNestedMapInstance() { | ||
| return nestedMapSupplier.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public BinaryArray keyArray() { | ||
| return keys; | ||
| } | ||
|
|
||
| @Override | ||
| public BinaryArray valueArray() { | ||
| return values; | ||
| } | ||
|
|
||
| public BinaryMap copy() { | ||
| return copy(createNestedMapInstance()); | ||
| } | ||
|
|
||
| public BinaryMap copy(BinaryMap reuse) { | ||
| byte[] bytes = BinarySegmentUtils.copyToBytes(segments, offset, sizeInBytes); | ||
| reuse.pointTo(MemorySegment.wrap(bytes), 0, sizeInBytes); | ||
| return reuse; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return BinarySegmentUtils.hash(segments, offset, sizeInBytes); | ||
| } | ||
|
|
||
| public static BinaryMap valueOf(BinaryArray key, BinaryArray value, BinaryMap reuse) { | ||
| checkArgument(key.segments.length == 1 && value.getSegments().length == 1); | ||
| byte[] bytes = new byte[4 + key.sizeInBytes + value.sizeInBytes]; | ||
| MemorySegment segment = MemorySegment.wrap(bytes); | ||
| segment.putInt(0, key.sizeInBytes); | ||
| key.getSegments()[0].copyTo(key.getOffset(), segment, 4, key.sizeInBytes); | ||
| value.getSegments()[0].copyTo( | ||
| value.getOffset(), segment, 4 + key.sizeInBytes, value.sizeInBytes); | ||
| reuse.pointTo(segment, 0, bytes.length); | ||
| return reuse; | ||
| } | ||
| } | ||
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
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.
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 cast to
(long)is applied only toelementSize, not to the entire multiplication result. This could still cause integer overflow whenelementSize * lengthexceedsInteger.MAX_VALUEbefore the cast is applied. The cast should wrap the entire multiplication expression:(long) elementSize * length.