← Back to Pull requests

feat: add Spark-compatible array_position #63

Closed
unknown closed this pull request without merging on Nov 16, 2026 12:31 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 03:56 UTC
Author

Closes #220.

Summary

Ports array_position from Comet (native/spark-expr/src/array_funcs/array_position.rs).

  • Returns the 1-based position of the first match as a BIGINT, 0 when not found, and NULL when the array or the element is NULL. DataFusion's built-in returns UInt64, returns NULL when the element is not found, and matches a NULL element against the first NULL in the array.

  • NULL elements inside the array never match but still count toward the position.

  • Floats compare as Spark does at any depth (inside arrays, fixed-size lists and structs): NaN = NaN and -0.0 = 0.0. NULL nested inside compared values equals NULL.

  • Supports List and LargeList (fixed-size lists are coerced to List).

  • Type coercion follows Spark's findTightestCommonType in a custom coerce_types: numbers widen, an integer widens to a decimal only if the decimal holds all its values, and a date widens to a timestamp. Everything else fails at planning time with Spark's error classes:

  • - ARRAY_FUNCTION_DIFF_TYPES: string and number (either way), two different decimals, decimal and double, struct fields with different names, order or types.
    - NULL_TYPE: a NULL-typed array, or a common type of NULL (array_position(array(), NULL)).
    - UNEXPECTED_INPUT_TYPE: a non-array first argument.
    - INVALID_ORDERING_TYPE: map elements at any depth.

    Comparison uses arrow's make_comparator after canonicalizing NaN and -0.0 with normalize_array. That helper and the rest of the float normalization from array_contains now live in a shared array/float_normalize.rs module used by both functions; it also descends into maps now, since array_contains still accepts map elements.

    Known differences from Spark, both covered by tests:

  • Struct field types are not widened. This matches Spark with ANSI mode off; with ANSI mode on, Spark widens them. coerce_types cannot see the session config.

  • Struct field names must match exactly. Spark ignores case, but DataFusion cannot cast between struct fields whose names differ only in case.
  • Documents the function with #[user_doc], following the guidelines in CONTRIBUTING.md.

    Testing

    array/array_position.slt (93 queries, 24 of them query error with message regexes). Expected values were checked against pyspark==4.2.0 with the PySpark SLT runner: all records match except the two known differences above and the array(1, NULL)[1] query, where arrays here are 1-based and Spark's are 0-based (commented in the file).

  • cargo clippy --all-targets --all-features -- -D warnings: clean

  • cargo test -p datafusion-spark: pass

  • cargo test -p datafusion-spark-sqllogictest: 246/246 files pass
  • Not done: the full Spark source audit across 3.5/4.0/4.1.

    🤖 Generated with Claude Code

    Diff: 354 lines (+265 / -89) across 4 files Reviewer: kosiew Review duration: 6275m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 354 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #63"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.4% · Safe to merge
    18.4%