← Back to Pull requests

feat: port Spark findWiderCommonType and use it in if and array #420

Open
unknown opened this pull request with 404 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 25, 2026 23: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?

  • Part of #388.
  • Rationale for this change

    if and array coerced their arguments with DataFusion's rules rather than Spark's, so
    they resolved mixed-type arguments differently from Spark. For example, array(1::INT, 'a')
    is ARRAY<STRING> in Spark, and with ANSI mode on array(1::INT, '2') is ARRAY<BIGINT>,
    because ANSI casts the string to the other type. Neither function followed
    enable_ansi_mode, and a mismatch such as if(true, 1::INT, array(1::INT)) did not raise
    Spark's DATA_DIFF_TYPES error.

    What changes are included in this PR?

  • type_coercion.rs: ports Spark's findWiderCommonType, findWiderTypeForTwo (with both

  • the TypeCoercion and AnsiTypeCoercion string promotion) and
    findWiderTypeWithoutStringPromotion.
  • New helper merge_arg_types: computes the common type of a range of arguments, casts them

  • to it and raises DATA_DIFF_TYPES if there is none. It writes the type list in the format
    of the Spark expression: ["INT", "ARRAY<INT>"] for If, ("INT" or "ARRAY<INT>") for
    CreateArray (TypeUtils.checkForSameTypeInputExpr).
  • if and array use the helper and follow datafusion.execution.enable_ansi_mode.

  • greatest/least call the ported findWiderTypeWithoutStringPromotion instead of their

  • own fold. Behavior is unchanged.
  • Supporting fixes needed by the new rules: dictionary types are treated as their value

  • type, and force_nullable marks a string cast to a non-string, non-binary type (which ANSI
    promotion produces) as nullable, like Spark's Cast.forceNullable.

    Are these changes tested?

    Yes:

  • Unit tests in type_coercion.rs: string promotion with and without ANSI, argument order,

  • nested types and map keys, dictionaries, decimals, empty/NULL inputs, and both error formats.
  • New .slt cases in conditional/if.slt and array/array.slt, with ANSI mode on and off.

  • The new cases fail before this change and pass after it.

    The rules and error messages were checked against the Spark v4.2.0 source: TypeCoercion,
    AnsiTypeCoercion, AnsiStringPromotionTypeCoercion, TypeCoercionHelper, TypeUtils,
    If, CreateArray, Cast.forceNullable and error-conditions.json. I wrote the expected
    results by hand from those rules and then ran them. I did not run them against a real Spark
    with the PySpark runner.

    cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings,
    cargo test -p datafusion-spark and cargo test -p datafusion-spark-sqllogictest all pass.

    Are there any user-facing changes?

    Yes, in how if and array coerce mixed-type arguments: they now follow Spark's rules,
    which depend on ANSI mode, and report Spark's DATA_DIFF_TYPES error when there is no common
    type. The new helper functions are pub(crate); SparkIf and SparkArray gain a private
    field and keep their public constructors, so there are no breaking changes to the public API.

    Diff: 404 lines (+303 / -101) across 5 files Reviewer: andygrove Review duration: 8357m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.45) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 404 lines diff across 5 files
    0.45
    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%
    42%
    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.359
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #420"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% 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: 44.8% (MEDIUM) · Higher = larger blast radius
    44.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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 25.5% · Safe to merge
    25.5%