← Back to Pull requests

fix: reject untyped null in array_contains #401

Open
unknown opened this pull request with 574 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 22, 2026 18:53 UTC
Author

<!--
SPDX-License-Identifier: Apache-2.0
SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
-->

Which issue does this PR close?

  • Closes #367.
  • Rationale for this change

    In Apache Spark, array_contains rejects untyped NULL inputs during query analysis / planning with error class [DATATYPE_MISMATCH.NULL_TYPE]:

    [DATATYPE_MISMATCH.NULL_TYPE] Cannot resolve "array_contains(NULL, 1)" due to data type mismatch: Null typed values cannot be used as arguments of "array_contains". SQLSTATE: 42K09
    

    Previously, datafusion-spark configured SparkArrayContains with DataFusion's generic Signature::array_and_element, which bypassed analysis-time validation for untyped NULL and returned NULL at runtime (annotated as xfail spark in crates/sqllogictest/test_files/array/array_contains.slt).

    Typed NULL expressions, such as CAST(NULL AS ARRAY<INT>) or CAST(NULL AS INT), are valid in Spark and return NULL at runtime.

    Furthermore, Spark's ArrayContains.inputTypes replaces generic coercion with TypeCoercion.findWiderTypeWithoutStringPromotionForTwo to find the wider type of array elements and the search needle (supporting decimal widening), while rejecting unorderable types and mismatched types without implicit string promotion.

    What changes are included in this PR?

    1. Configured SparkArrayContains with Signature::user_defined(Volatility::Immutable).
    2. Implemented ScalarUDFImpl::coerce_types for SparkArrayContains aligning with Spark's ArrayContains.checkInputDataTypes:
    - Symmetrically rejects untyped DataType::Null arguments at planning time with [DATATYPE_MISMATCH.NULL_TYPE] Null typed values cannot be used as arguments of \array_contains\. SQLSTATE: 42K09.
    - Validates array types (List, LargeList, FixedSizeList), raising [DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE] if the first argument is not an array.
    - Replaces generic element coercion with Spark's decimal-compatible type_coercion::wider_type (TypeCoercion.findWiderTypeWithoutStringPromotionForTwo) and struct_field_types_match, raising [DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES] on type mismatch or unsupported promotion (e.g., strings vs numbers).
    - Checks is_orderable(&common), raising [DATATYPE_MISMATCH.INVALID_ORDERING_TYPE] for unorderable types (e.g. Map, Interval).
    3. Preserves typed NULL expressions (CAST(NULL AS ...)), which pass analysis and evaluate to NULL at execution time.
    4. Added 12 Rust unit tests in crates/spark/src/function/array/array_contains.rs covering untyped NULL rejection, same-type coercion, decimal widening, nested decimal widening, type mismatch, non-array input, invalid ordering types, and arity validation.
    5. Updated crates/sqllogictest/test_files/array/array_contains.slt to:
    - Remove the two # xfail spark markers and assert DATATYPE_MISMATCH.NULL_TYPE errors for untyped NULLs.
    - Assert NULL for typed NULL arguments.
    - Add a comprehensive "Type coercion" section covering numeric widening, decimal widening, nested arrays, floats, dictionaries, structs, and error cases (strings vs numbers, unorderable map elements, non-array first argument).

    Are these changes tested?

  • Unit tests: Added 12 unit tests in crates/spark/src/function/array/array_contains.rs verifying all error and coercion branches.

  • Sqllogictests: Updated crates/sqllogictest/test_files/array/array_contains.slt and verified with cargo test -p datafusion-spark-sqllogictest -- array_contains across both simplify and evaluate passes (2 of 2 passed).

  • Code Quality: Verified with cargo fmt --check and cargo clippy -p datafusion-spark --all-targets -- -D warnings.

  • Verified against Apache Spark 3.5 / 4.2 behavior as documented in issue #367 and maintainer review comments.
  • Are there any user-facing changes?

  • array_contains now rejects untyped NULL arguments at query planning time with [DATATYPE_MISMATCH.NULL_TYPE] ... SQLSTATE: 42K09, matching Apache Spark behavior.

  • Element coercion is replaced with Spark's findWiderTypeWithoutStringPromotionForTwo semantics: arguments that do not widen under Spark rules (such as strings paired with numeric types) are rejected at planning time with [DATATYPE_MISMATCH.ARRAY_FUNCTION_DIFF_TYPES], while decimals widen correctly (e.g., integer with decimal, decimal with decimal, decimal with float).
  • Diff: 574 lines (+430 / -144) across 7 files Reviewer: andygrove Review duration: 11364m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 574 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.380
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #401"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 26.5% · Safe to merge
    26.5%