feat: add support for array_position expression#3172
Open
andygrove wants to merge 20 commits intoapache:mainfrom
Open
feat: add support for array_position expression#3172andygrove wants to merge 20 commits intoapache:mainfrom
andygrove wants to merge 20 commits intoapache:mainfrom
Conversation
Implements Spark's array_position function which returns the 1-based position of an element in an array, returning 0 if not found. This required a custom Rust implementation because DataFusion's array_position returns UInt64 and null when not found, while Spark returns Int64 (LongType) and 0. Key implementation details: - Returns Int64 to match Spark's LongType - Returns 0 when element is not found (Spark behavior) - Returns null when array is null or search element is null - Supports both List and LargeList array types Closes apache#3153 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3172 +/- ##
============================================
+ Coverage 56.12% 59.96% +3.84%
- Complexity 976 1462 +486
============================================
Files 119 175 +56
Lines 11743 16180 +4437
Branches 2251 2684 +433
============================================
+ Hits 6591 9703 +3112
- Misses 4012 5128 +1116
- Partials 1140 1349 +209 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
# Conflicts: # docs/source/user-guide/latest/configs.md # native/spark-expr/src/comet_scalar_funcs.rs
Member
Author
|
Moving this to draft until #3328 is merged |
Move array_position tests from CometArrayExpressionSuite to a SQL file test and fall back to Spark when all arguments are literals. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…o feature/array-position # Conflicts: # native/spark-expr/src/array_funcs/mod.rs # native/spark-expr/src/comet_scalar_funcs.rs
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove stray array_repeat references from merge conflict resolution. Add NULL val row to test data and add tests for all supported array element types: boolean, tinyint, smallint, bigint, float, double, decimal, date, and timestamp. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
|
Just wondering can we reuse DF? the builtin function gets optimized in apache/datafusion#20532 |
Member
Author
The DF implementation isn't compatible with Spark though. |
… review feedback - Use typed array downcasting instead of ScalarValue for element comparison, improving performance from 0.4X to 0.7-0.8X of Spark - Add getSupportLevel override marking as Incompatible (NaN equality) - Add NaN edge case tests for float/double arrays - Add CometArrayExpressionBenchmark microbenchmark - Make spark_array_position function private - Update docs to mark array_position as supported
Treat NaN == NaN in float/double comparisons, matching Spark's ordering.equiv() behavior. This makes array_position Compatible rather than Incompatible.
Avoid per-row subarray allocation from list_array.value(row_index). Instead, downcast the flat values buffer once and iterate using offset ranges directly. Improves from 0.7-0.8X to 0.9X of Spark.
Switches benchmark to use SCAN_NATIVE_DATAFUSION for the Comet cases, avoiding JVM parquet reader overhead. Results now show Comet is 1.1-1.2X faster than Spark.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue does this PR close?
Closes #3157.
Closes #3153.
Rationale for this change
Spark's
array_positionfunction is not currently accelerated by Comet. Adding native support allows this expression to run on the native execution engine.What changes are included in this PR?
Adds native Comet support for Spark's
array_positionfunction, which returns the 1-based position of an element in an array, or 0 if not found.This required a custom Rust implementation because DataFusion's
array_positionreturnsUInt64andnullwhen not found, while Spark returnsInt64(LongType) and0.Key implementation details:
Int64to match Spark'sLongType0when element is not found (Spark behavior)nullwhen array isnullor search element isnullListandLargeListarray typesordering.equiv()semantics (NaN == NaN)Benchmark Results
Comet native execution is 1.1-1.2X faster than Spark for this expression when using the native DataFusion scan.
How are these changes tested?
SQL file-based tests in
spark/src/test/resources/sql-tests/expressions/array/array_position.sqlcovering:CometArrayExpressionBenchmark