Repository navigation
refactor: replace CometPlainVector boolean flags with VectorKind enum - #7830
adityavaish wants to merge 5 commits into
Conversation
Replaces isBaseFixedWidthVector, isLargeVarWidth, and the -1 buffer address sentinel checks in CometPlainVector with a VectorKind enum, and adds unit tests covering large variable-width, fixed-size binary/UUID, null, and interval vectors. Refs apache#4433 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
comphead
left a comment
There was a problem hiding this comment.
I compared every getter against upstream/main. The enum maps one to one onto the old flag and sentinel combinations (BaseFixedWidthVector and the two variable-width bases are disjoint, so no old combination is lost) and NULL and OTHER behave as before. The new tests pin the existing behavior for large, fixed-width, UUID, null and interval vectors and do not duplicate the Scala suites. The open question is cost on the per-row string and binary read path, which the PR does not measure.
| } | ||
| } else { | ||
| throw new IllegalStateException("Unsupported UTF8 vector type: " + valueVector.getName()); | ||
| switch (vectorKind) { |
There was a problem hiding this comment.
getUTF8String and getBinary run once per row. Before, the common case was one long compare on offsetBufferAddress plus a boolean. Now switch (vectorKind) goes through javac's synthetic $SwitchMap array indexed by ordinal(), which is an extra array load with a bounds check per call, and the JIT does not fold a static array element into a constant. I expect this to be small but I did not decompile or benchmark it. Please add a before and after number for reading a string column and a binary column row by row (an existing benchmark under spark/src/test/scala/org/apache/spark/sql/benchmark would do), or keep the hot VARIABLE_WIDTH check as a plain comparison and use the enum only for the rare kinds.
There was a problem hiding this comment.
Good point—replaced switch (vectorKind) in both getUTF8String and getBinary with if / else if / else direct reference comparisons (==) starting with VectorKind.VARIABLE_WIDTH in 3d4619ac2 (merged in f7e1b1318). Verified with javap -c that javac emits no $SwitchMap / ordinal() lookup or synthetic class—the common VARIABLE_WIDTH path is a single getfield + getstatic + if_acmpne.
Auto-replied by the GitHub Copilot app
| if (vector instanceof NullVector) { | ||
| this.valueBufferAddress = -1; | ||
| } else { | ||
| // NullType doesn't have a data buffer. | ||
| this.vectorKind = VectorKind.NULL; | ||
| this.valueBufferAddress = -1L; | ||
| this.offsetBufferAddress = -1L; | ||
| } else if (vector instanceof BaseFixedWidthVector) { | ||
| this.vectorKind = VectorKind.FIXED_WIDTH; | ||
| this.valueBufferAddress = vector.getDataBuffer().memoryAddress(); | ||
| this.offsetBufferAddress = -1L; | ||
| } else if (vector instanceof BaseVariableWidthVector) { | ||
| this.vectorKind = VectorKind.VARIABLE_WIDTH; | ||
| this.valueBufferAddress = vector.getDataBuffer().memoryAddress(); | ||
| } | ||
| isBaseFixedWidthVector = valueVector instanceof BaseFixedWidthVector; | ||
| if (vector instanceof BaseVariableWidthVector) { | ||
| this.offsetBufferAddress = | ||
| ((BaseVariableWidthVector) vector).getOffsetBuffer().memoryAddress(); | ||
| this.isLargeVarWidth = false; | ||
| } else if (vector instanceof BaseLargeVariableWidthVector) { | ||
| this.vectorKind = VectorKind.LARGE_VARIABLE_WIDTH; | ||
| this.valueBufferAddress = vector.getDataBuffer().memoryAddress(); | ||
| this.offsetBufferAddress = | ||
| ((BaseLargeVariableWidthVector) vector).getOffsetBuffer().memoryAddress(); | ||
| this.isLargeVarWidth = true; | ||
| } else { | ||
| this.offsetBufferAddress = -1; | ||
| this.isLargeVarWidth = false; | ||
| this.vectorKind = VectorKind.OTHER; | ||
| this.valueBufferAddress = vector.getDataBuffer().memoryAddress(); | ||
| this.offsetBufferAddress = -1L; | ||
| } |
There was a problem hiding this comment.
Three of the four branches repeat this.valueBufferAddress = vector.getDataBuffer().memoryAddress(), and -1L is still written to valueBufferAddress and offsetBufferAddress for NULL and OTHER. So the sentinels are not gone, and the PR text that says this removes sentinel coupling is only half true. Computing vectorKind first and then assigning each address once would be shorter and would make it clear which kinds own which buffer.
There was a problem hiding this comment.
Refactored in 3d4619ac2: extracted vectorKindOf(ValueVector) so the constructor computes vectorKind first and assigns valueBufferAddress and offsetBufferAddress once each. Also updated the PR description to state accurately that -1L is still stored when a kind has no buffer, while dispatch checks now use vectorKind.
Auto-replied by the GitHub Copilot app
| case FIXED_WIDTH: | ||
| BaseFixedWidthVector fixedWidthVector = (BaseFixedWidthVector) valueVector; | ||
| length = fixedWidthVector.getTypeWidth(); | ||
| offset = (long) rowId * length; |
There was a problem hiding this comment.
The PR description says this removes the hot-path instanceof BaseFixedWidthVector from getBinary. The FIXED_WIDTH case still casts valueVector and calls getTypeWidth(), and before the change that instanceof was only reached after the offset check failed, so variable-width reads never paid for it. Please drop that claim from the description. Also, (long) rowId * length here and at line 169 changes the old int multiplication to long. That is an improvement, but it is a behavior change that the title and description do not mention. Either say so or keep the refactor behavior-identical. Finally, case VARIABLE_WIDTH declares int intOffset without braces while getUTF8String braces each case, so please use one style.
There was a problem hiding this comment.
Updated the PR description to drop the hot-path instanceof claim and explicitly document the (long) rowId * length 2 GiB offset-overflow fix (kept per @sunchao's verification across the 2 GiB boundary). Switching both getters to if / else if / else in 3d4619ac2 also made their bracing style consistent.
Auto-replied by the GitHub Copilot app
comphead
left a comment
There was a problem hiding this comment.
thanks @adityavaish, the PR goes into right direction, please check some nits!
sunchao
left a comment
There was a problem hiding this comment.
I’d keep the (long) rowId casts in both getters. I tested a 16-byte FixedSizeBinaryVector across the 2 GiB boundary. At row 134,217,728, the old getBinary and getUTF8String both read a marker placed 2 GiB before the data buffer. Both now read the expected value at +2 GiB. All eight TestCometPlainVector tests passed with Arrow 20. I found no additional correctness issues.
Automated review by Codex.
…uctor Addresses review feedback on apache#7830: - Replace switch(vectorKind) in getUTF8String/getBinary with if/else if direct enum reference checks (testing VARIABLE_WIDTH first) to eliminate javac $SwitchMap/ordinal() lookup overhead and unify bracing style. - Compute vectorKind once via vectorKindOf(vector) and assign valueBufferAddress and offsetBufferAddress cleanly without repeating getDataBuffer().memoryAddress(). - Retain (long) rowId * length in fixed-width offset calculations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ector-enum' into adityavaish-refactor-cometplainvector-enum
|
Thanks @comphead and @sunchao for the review and 2 GiB boundary check! Addressed all comments in 3d4619a / f7e1b13:
@comphead could you please approve the workflow runs on this updated commit (and queue for merge once CI is green)? |
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
f7e1b13184a1fe2f87ebdb8ca9a0bde0e4451e78VectorKindusing direct==comparisons (VARIABLE_WIDTHfirst),javap -cconfirms zero$SwitchMap/ordinal()overhead, Javaspotless:check(104/104 Java files),TestCometPlainVector(8/8), and 5 Scala vector/Arrow suites (78/78 tests) all passed.↓ Full Linux CI suite is pending workflow approval.
Which issue does this PR close?
Closes #4433.
Rationale for this change
CometPlainVectorpreviously dispatched on vector kinds using boolean flags (isBaseFixedWidthVector,isLargeVarWidth),-1sentinel checks (offsetBufferAddress != -1,valueBufferAddress == -1), and aninstanceof BaseFixedWidthVectorcheck ingetBinary. Replacing those dispatch checks with a singleVectorKindenum makes vector-kind classification explicit and extensible.What changes are included in this PR?
spark/src/main/java/org/apache/comet/vector/CometPlainVector.java:private enum VectorKind { NULL, FIXED_WIDTH, VARIABLE_WIDTH, LARGE_VARIABLE_WIDTH, OTHER }classified once invectorKindOf(ValueVector), followed by single assignments tovalueBufferAddressandoffsetBufferAddress(which still hold-1Lwhen a vector kind has no corresponding buffer).instanceofdispatch checks ingetUTF8String,getBinary, andisNullAtwith directVectorKindreference comparisons (==), checking the commonVARIABLE_WIDTHcase first to avoid javac$SwitchMapoverhead.getUTF8StringandgetBinaryfromint(rowId * length) tolong((long) rowId * length) to prevent 32-bit signed overflow past 2 GiB.spark/src/test/java/org/apache/comet/vector/TestCometPlainVector.java:LargeVarCharVector,LargeVarBinaryVector,FixedSizeBinaryVector(plain and UUID decoding),NullVector, andIntervalMonthDayNanoVector.How are these changes tested?
.\mvnw.cmd -B spotless:check "-DspotlessFiles=.*\.java"— passed (6/6 Java files incomet-common, 98/98 Java files incomet-spark; 0 violations).javap -c spark\target\classes\org\apache\comet\vector\CometPlainVector.class— verified no$SwitchMap,ordinal(), or synthetic switch class is generated; bothgetUTF8StringandgetBinarytestVectorKind.VARIABLE_WIDTHwith a singlegetfield+getstatic+if_acmpne..\mvnw.cmd -B test-compile surefire:test "-Dtest=org.apache.comet.vector.TestCometPlainVector" "-Dscalastyle.skip=true" "-Dspotless.check.skip=true"— passed (8/8 tests)..\mvnw.cmd -B test "-Dtest=none" "-Dsuites=org.apache.spark.sql.comet.util.UtilsSuite,org.apache.comet.vector.CometVectorUtilsSuite,org.apache.comet.DirectColumnarToRowConverterSuite,org.apache.spark.sql.comet.execution.arrow.CometArrowStreamSuite,org.apache.spark.sql.comet.execution.arrow.CachedBatchRowIteratorSuite" "-Dscalastyle.skip=true" "-Dspotless.check.skip=true"— passed (5/5 suites, 78/78 tests).