Skip to content

refactor: replace CometPlainVector boolean flags with VectorKind enum - #7830

Queued
adityavaish wants to merge 5 commits into
apache:mainfrom
adityavaish:adityavaish-refactor-cometplainvector-enum
Queued

adityavaish wants to merge 5 commits into
apache:mainfrom
adityavaish:adityavaish-refactor-cometplainvector-enum

Conversation

@adityavaish

@adityavaish adityavaish commented Dec 2, 2026 •

Copy link
Copy Markdown

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: f7e1b13184a1fe2f87ebdb8ca9a0bde0e4451e78

Dimension Assessment Rationale
Change confidence 9.5/10 ↑ Replaces boolean flags and sentinel dispatch checks with VectorKind using direct == comparisons (VARIABLE_WIDTH first), javap -c confirms zero $SwitchMap / ordinal() overhead, Java spotless: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

CometPlainVector previously dispatched on vector kinds using boolean flags (isBaseFixedWidthVector, isLargeVarWidth), -1 sentinel checks (offsetBufferAddress != -1, valueBufferAddress == -1), and an instanceof BaseFixedWidthVector check in getBinary. Replacing those dispatch checks with a single VectorKind enum makes vector-kind classification explicit and extensible.

What changes are included in this PR?

  • spark/src/main/java/org/apache/comet/vector/CometPlainVector.java:
    • Introduced private enum VectorKind { NULL, FIXED_WIDTH, VARIABLE_WIDTH, LARGE_VARIABLE_WIDTH, OTHER } classified once in vectorKindOf(ValueVector), followed by single assignments to valueBufferAddress and offsetBufferAddress (which still hold -1L when a vector kind has no corresponding buffer).
    • Replaced the boolean-flag, sentinel, and instanceof dispatch checks in getUTF8String, getBinary, and isNullAt with direct VectorKind reference comparisons (==), checking the common VARIABLE_WIDTH case first to avoid javac $SwitchMap overhead.
    • Widened the fixed-width byte-offset calculation in getUTF8String and getBinary from int (rowId * length) to long ((long) rowId * length) to prevent 32-bit signed overflow past 2 GiB.
  • spark/src/test/java/org/apache/comet/vector/TestCometPlainVector.java:
    • Added unit tests covering LargeVarCharVector, LargeVarBinaryVector, FixedSizeBinaryVector (plain and UUID decoding), NullVector, and IntervalMonthDayNanoVector.

How are these changes tested?

  • .\mvnw.cmd -B spotless:check "-DspotlessFiles=.*\.java" — passed (6/6 Java files in comet-common, 98/98 Java files in comet-spark; 0 violations).
  • javap -c spark\target\classes\org\apache\comet\vector\CometPlainVector.class — verified no $SwitchMap, ordinal(), or synthetic switch class is generated; both getUTF8String and getBinary test VectorKind.VARIABLE_WIDTH with a single getfield + 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).

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 comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines 67 to 90
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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +200 to +203
case FIXED_WIDTH:
BaseFixedWidthVector fixedWidthVector = (BaseFixedWidthVector) valueVector;
length = fixedWidthVector.getTypeWidth();
offset = (long) rowId * length;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 comphead left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @adityavaish, the PR goes into right direction, please check some nits!

@sunchao sunchao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

adityavaish and others added 3 commits December 2, 2026 17:18
…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
@adityavaish

Copy link
Copy Markdown
Author

Thanks @comphead and @sunchao for the review and 2 GiB boundary check! Addressed all comments in 3d4619a / f7e1b13:

  • Replaced \switch (vectorKind)\ in \getUTF8String/\getBinary\ with direct ==\ checks starting with \VectorKind.VARIABLE_WIDTH\ (verified via \javap -c\ that no \\ is generated) and unified bracing style.
  • Computed \�ectorKind\ first via \�ectorKindOf(ValueVector)\ so \�alueBufferAddress\ and \offsetBufferAddress\ are each assigned once.
  • Kept the (long) rowId * length\ widening fix and updated the PR description accordingly.

@comphead could you please approve the workflow runs on this updated commit (and queue for merge once CI is green)?

@github-actions github-actions Bot added enhancement New feature or request area:ffi Arrow FFI / JNI boundary labels Dec 3, 2026
@sunchao
sunchao added this pull request to the merge queue Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ffi Arrow FFI / JNI boundary enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor CometPlainVector by replacing boolean flags with an enum

4 participants