[AURON #2624] Support Spark's array_except natively #2648
<!-- ccr-overview-v2 -->
🟢 Approval recommended
No active review issues block approval of the native array_except implementation.
No open issues 🎉
<details>
<summary><strong>What changed in this PR</strong></summary>
Adds native execution of Spark’s array_except in Auron, reducing fallback for supported atomic element types while preserving Spark’s ordering, deduplication, null, and floating-point semantics.
Changes:
array_distinct.| File | Description |
| ---- | ----------- |
| spark-extension/src/main/scala/org/apache/spark/sql/auron/NativeConverters.scala | Adds ArrayExcept conversion and shared set-array type checks. |
| spark-extension-shims-spark/src/test/scala/org/apache/auron/AuronFunctionSuite.scala | Tests Spark-compatible results, native execution, and fallback behavior. |
| native-engine/datafusion-ext-functions/src/spark_array.rs | Implements native array subtraction, extracts NaN handling, and adds unit tests. |
| native-engine/datafusion-ext-functions/src/lib.rs | Registers Spark_ArrayExcept in the native function dispatcher. |
</details>
<sub>Review effort: Balanced</sub>
The helper (and its call sites in array_distinct/union/except) only canonicalizes NaNs but never applies Spark 4.2+ signed-zero normalization. Your Spark test explicitly expects different -0.0 behavior when SPARK_RUNTIME_VERSION >= 4.2, so the native implementation will likely diverge on Spark 4.2+. Consider extending the float canonicalization step to also normalize signed zeros when targeting Spark 4.2+ (e.g., map -0.0 to 0.0 conditionally), or apply the existing Spark_NormalizeNanAndZero semantics to float array set ops for Spark 4.2+ so native results match Spark across versions.
Within the per-row loop, seen is repeatedly repopulated from the right-side elements but is created with the default capacity and may rehash as right sizes vary by row. Consider ensuring capacity before inserting (e.g., seen.reserve(right_end - right_start) after seen.clear()), or initializing with a reasonable capacity derived from the first row, to reduce rehash overhead on wide arrays.
Within the per-row loop, seen is repeatedly repopulated from the right-side elements but is created with the default capacity and may rehash as right sizes vary by row. Consider ensuring capacity before inserting (e.g., seen.reserve(right_end - right_start) after seen.clear()), or initializing with a reasonable capacity derived from the first row, to reduce rehash overhead on wide arrays.
Comparing Spark versions as strings is not a correct version ordering in general (e.g., \"3.10\" would compare less than \"3.2\"). It’s safer to use a numeric/semantic version comparator (or an existing SparkVersionUtil boolean like isSparkV31OrGreater, if available) to avoid incorrect feature gating as versions evolve.
[!NOTE]
Copilot was unable to run its full agentic suite in this review.
<!-- ccr-overview-v2 -->
Copilot review overview
<details open>
<summary><strong>4 open issues</strong></summary>
array_distinct/union/except) only canonicalizes NaNs but never… · Newseen is repeatedly repopulated from the right-side elements but is… · Newseen is repeatedly repopulated from the right-side elements but is… · New</details>
<sub>Review effort: Lite</sub>
/native-engine/datafusion-ext-commons/src/spark_float.rs contains Spark float specific behavior. Could this be added there since it is not specific to these operators?
Instead of checking the runtime most other cases I have seen add Spark version specific logic to /spark-extension-shims-spark/src/main/scala/org/apache/spark/sql/auron/ShimsImpl.scala and @sparkver. Can you use that pattern here? I know you didn't add this in the PR, but since it is being refactored now seems like a good time to do it.
Can you also add some of the tricky cases (non_canonical_nan, -0.0, etc) on the right to verify both sides get the same handling?
Overall the logic LGTM. Just a few minor comments.
<!-- ccr-overview-v2 -->
🟢 Approval recommended
The review reports no active issues with the native array_except change.
<details open>
<summary><strong>3 open issues</strong></summary>
array_distinct/union/except) only canonicalizes NaNs but never…seen is repeatedly repopulated from the right-side elements but is…seen is repeatedly repopulated from the right-side elements but is…</details>
<details>
<summary><strong>1 resolved since last review</strong></summary>
</details>
<sub>Review effort: Balanced</sub>
Good call, moved canonicalize_nans to spark_float.rs so other functions can share this impl. Updated array_distinct, array_except and arrays_overlap to use it.
Moved the check to Shims.hasNaNAwareArraySetOps with @sparkver
Added some tricky cases on the right side, these verify that both sides get the same handling
Thanks for the review @robreeves ! Addressed all the comments
@slfan1989 could you please approve the workflow runs for this PR as well? Thanks!
Could it canonicalize floats inline when constructing the hash set keys, rather than upfront? This would avoid a separate pass over both inputs. Since seen currently uses row-encoded keys, could it instead use native value keys with canonicalized float bits for floats and avoid row encoding altogether?
<!-- ccr-overview-v2 -->
🟢 Approval recommended
The native array_except change has no active review issues blocking approval.
<details open>
<summary><strong>3 open issues</strong></summary>
array_distinct/union/except) only canonicalizes NaNs but never…seen is repeatedly repopulated from the right-side elements but is…seen is repeatedly repopulated from the right-side elements but is…</details>
<sub>Review effort: Balanced</sub>
Good point, had explored it during design and was planning to add it in a follow up PR, but added it here
Floats now are canonicalized inline and keyed by their bits, so they skip the separate canonicalisation pass and row encoding.
LGTM. @slfan1989 PTAL
robreeves
zoddtheimmortal
Which issue does this PR close?
Closes #2624
Rationale for this change
Spark's
array_exceptcurrently falls back to Spark.What changes are included in this PR?
ArrayExceptto a new nativeSpark_ArrayExceptfunction.- null arrays give null;
- a single null element is kept unless the second array contains null;
- NaN equals NaN;
-
-0.0and0.0stay distinct; Spark 4.2 normalizes them before this function.ArrayDistinctintoisSupportedSetArrayType, shared by both functions.array_distinct's NaN canonicalization intocanonicalize_nans, shared by both functions.Are there any user-facing changes?
array_exceptcan execute natively for supported atomic element types. Nested element type keeps the fallback, and so does floating point arrays on Spark 3.0.How was this patch tested?
All passed.
AuronFunctionSuite: 97 succeeded, 0 failed on each Spark version.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5 on GitHub Copilot CLI 1.0.105
ASF guidance: https://www.apache.org/legal/generative-tooling.html