-
Notifications
You must be signed in to change notification settings - Fork 321
perf: cache offsetBufferAddress in CometPlainVector for variable-width vectors #4364
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
base: main
Are you sure you want to change the base?
Changes from all commits
a36b4f7
c837e74
b957bf2
0a2c84f
0cb8e20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| /** A column vector whose elements are plainly decoded. */ | ||
| public class CometPlainVector extends CometDecodedVector { | ||
| private final long valueBufferAddress; | ||
| private final long offsetBufferAddress; | ||
| private final boolean isBaseFixedWidthVector; | ||
|
|
||
| private byte booleanByteCache; | ||
|
|
@@ -59,6 +60,12 @@ public CometPlainVector( | |
| } | ||
|
|
||
| isBaseFixedWidthVector = valueVector instanceof BaseFixedWidthVector; | ||
| if (vector instanceof BaseVariableWidthVector) { | ||
| this.offsetBufferAddress = | ||
| ((BaseVariableWidthVector) vector).getOffsetBuffer().memoryAddress(); | ||
| } else { | ||
| this.offsetBufferAddress = -1; | ||
| } | ||
| this.isReused = isReused; | ||
| } | ||
|
|
||
|
|
@@ -124,13 +131,11 @@ public double getDouble(int rowId) { | |
| @Override | ||
| public UTF8String getUTF8String(int rowId) { | ||
| if (isNullAt(rowId)) return null; | ||
| if (!isBaseFixedWidthVector) { | ||
| BaseVariableWidthVector varWidthVector = (BaseVariableWidthVector) valueVector; | ||
| long offsetBufferAddress = varWidthVector.getOffsetBuffer().memoryAddress(); | ||
| if (offsetBufferAddress != -1) { | ||
| int offset = Platform.getInt(null, offsetBufferAddress + rowId * 4L); | ||
| int length = Platform.getInt(null, offsetBufferAddress + (rowId + 1L) * 4L) - offset; | ||
| return UTF8String.fromAddress(null, valueBufferAddress + offset, length); | ||
| } else { | ||
| } else if (isBaseFixedWidthVector) { | ||
| BaseFixedWidthVector fixedWidthVector = (BaseFixedWidthVector) valueVector; | ||
| int length = fixedWidthVector.getTypeWidth(); | ||
| int offset = rowId * length; | ||
|
|
@@ -143,6 +148,8 @@ public UTF8String getUTF8String(int rowId) { | |
| } else { | ||
| return UTF8String.fromString(convertToUuid(result).toString()); | ||
| } | ||
| } else { | ||
| throw new RuntimeException("Unsupported UTF8 vector type: " + valueVector.getName()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to write a test for this error branch?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, Thanks for review. I'll add this in next commit.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into this and I don't think this branch is reachable through the real construction path.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this branch reachable in practice? |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -151,9 +158,7 @@ public byte[] getBinary(int rowId) { | |
| if (isNullAt(rowId)) return null; | ||
| int offset; | ||
| int length; | ||
| if (valueVector instanceof BaseVariableWidthVector) { | ||
| BaseVariableWidthVector varWidthVector = (BaseVariableWidthVector) valueVector; | ||
| long offsetBufferAddress = varWidthVector.getOffsetBuffer().memoryAddress(); | ||
| if (offsetBufferAddress != -1) { | ||
| offset = Platform.getInt(null, offsetBufferAddress + rowId * 4L); | ||
| length = Platform.getInt(null, offsetBufferAddress + (rowId + 1L) * 4L) - offset; | ||
| } else if (valueVector instanceof BaseFixedWidthVector) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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.comet.vector; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.VarBinaryVector; | ||
| import org.apache.arrow.vector.VarCharVector; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| public class TestCometPlainVector { | ||
|
|
||
| @Test | ||
| public void testGetUTF8StringWithVariableWidthVector() { | ||
| try (RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { | ||
| VarCharVector vector = new VarCharVector("strings", allocator); | ||
| vector.allocateNew(); | ||
| vector.setSafe(0, bytes("alpha")); | ||
| vector.setSafe(1, bytes("")); | ||
| vector.setSafe(2, bytes("spark")); | ||
| vector.setValueCount(4); // row 3 is null (validity bit not set) | ||
|
|
||
| try (CometPlainVector cv = new CometPlainVector(vector, false)) { | ||
| assertEquals("alpha", cv.getUTF8String(0).toString()); | ||
| assertEquals("", cv.getUTF8String(1).toString()); | ||
| assertEquals("spark", cv.getUTF8String(2).toString()); | ||
| assertNull(cv.getUTF8String(3)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetBinaryWithVariableWidthVector() { | ||
| try (RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) { | ||
| VarBinaryVector vector = new VarBinaryVector("bytes", allocator); | ||
| vector.allocateNew(); | ||
| vector.setSafe(0, new byte[] {1, 2, 3}, 0, 3); | ||
| vector.setSafe(1, new byte[0], 0, 0); | ||
| vector.setSafe(2, new byte[] {4, 5}, 0, 2); | ||
| vector.setValueCount(4); // row 3 is null (validity bit not set) | ||
|
|
||
| try (CometPlainVector cv = new CometPlainVector(vector, false)) { | ||
| assertArrayEquals(new byte[] {1, 2, 3}, cv.getBinary(0)); | ||
| assertArrayEquals(new byte[0], cv.getBinary(1)); | ||
| assertArrayEquals(new byte[] {4, 5}, cv.getBinary(2)); | ||
| assertNull(cv.getBinary(3)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static byte[] bytes(String s) { | ||
| return s.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
| } |
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.
Consider a
private final boolean isVariableWidthVectoralongsideisBaseFixedWidthVectorinstead of usingoffsetBufferAddress != -1as the dispatch sentinel. The-1pattern matches the existingvalueBufferAddressprecedent, so this is purely taste.Alternatively, instead of an ever growing list of booleans, can we do an enum (maybe that's a bigger refactor and out of scope)?