← Back to Pull requests

feat(spark): support map/array input and an options argument in to_json #395

Open
unknown opened this pull request with 334 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 21, 2026 16:12 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 #338.
  • Rationale for this change

    Spark's to_json accepts a map or an array as the value to serialize, and an optional map(...) of options as a second argument. Both were rejected at planning time: to_json(map('a', 1)) failed with "expects a struct argument", and a second argument failed with "but received 2".

    What changes are included in this PR?

  • to_json accepts a struct, a map, any Arrow list type, or an untyped NULL. The encoder already serialized maps and lists when nested, so only the planning check changed; the root type gets the same validation as nested types.

  • to_json(expr, options): the second argument is a constant map of strings, read as Spark's JSONOptions does (case-insensitive names, true/false in any case).

  • The supported options are an enum, ToJsonOption, named after Spark's JSON data source options

  • (https://spark.apache.org/docs/latest/sql-data-sources-json.html#data-source-option). This PR implements one, ignoreNullFields. Any other option is an error that lists the supported ones.
  • The private helper struct_to_json is renamed to_json, matching apache/datafusion#27518.

  • The function reference is updated.
  • Are these changes tested?

    Yes.

  • Unit tests in to_json.rs for the planning check, map/list input as a column and as a scalar (NULL rows, sliced input, LargeList, FixedSizeList), option parsing, and ignoreNullFields.

  • New records in json/to_json.slt, written without [...] literals so that Spark can parse them.

  • Expected values were checked against Spark 4.2.0 with the PySpark runner in crates/sqllogictest/scripts. It found one difference, to_json(NULL), which Spark answers with NULL; this PR matches that.
  • Known differences from Spark, marked # xfail spark: intended in the SLT:

  • an option that is not supported is an error, while Spark applies it or ignores a name it does not know;

  • an interval element and an array map key are rejected, as they already are inside a struct.
  • Are there any user-facing changes?

  • to_json accepts map and array input and the ignoreNullFields option.

  • to_json(NULL) returns NULL instead of a planning error.

  • The rejection message now reads "to_json expects a struct, map or array

  • argument".

    No change to the public API: expr_fn::to_json(input) still takes one argument.

    Diff: 334 lines (+250 / -84) across 4 files Reviewer: cj-zhukov Review duration: 14294m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.42) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 334 lines diff across 4 files
    0.42
    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.348
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #395"
    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: 42.2% (MEDIUM) · Higher = larger blast radius
    42.2%
    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: 22.5% · Safe to merge
    22.5%