← Back to Pull requests

fix: support scalar string argument in native lpad/rpad #7450

Closed
unknown closed this pull request without merging on Nov 16, 2026 23:08 UTC
View on GitHub
unknown unknown commented on Nov 03, 2026 03:50 UTC
Author

Which issue does this PR close?

Closes #7245.
Closes #7452.

Related to #5889: as discussed in this comment, the padding fix is split out of that PR so it can land before the struct-typed scalar subquery support.

Rationale for this change

The string argument of lpad/rpad can reference columns and still evaluate to a scalar for a whole batch. For example, IF(id > 0, (SELECT max(s) FROM t), s) returns a scalar when every row in the batch takes the subquery branch. The serde only rejects a literal string, so this shape reaches the native read_side_padding kernel. Every arm there expects the string to be an array, so the query fails with:

Unsupported arguments [Scalar(Utf8("b")), ...] for function rpad/lpad/read_side_padding

@sunchao raised this in the #5889 review and suggested handling a runtime scalar string in the kernel.

What changes are included in this PR?

  • spark_read_side_padding2 gets new arms for a scalar string argument, Utf8 and LargeUtf8, which call a generic spark_read_side_padding_scalar::<T> helper. The padding loop now takes the strings as an iterator, an ascii flag and a capacity floor. The array arms pass string_array.iter(), so their path is the same as on main. The scalar arm passes std::iter::repeat_n(value, n) with no capacity floor, so the scalar is never expanded into an array. Expanding it would need len(s) * rows bytes of input and reserve as much output, which overflows the 32-bit offsets of a Utf8 array for a wide string (for example 1 MiB × 4096 rows). Truncation, len <= 0 and empty pad behave as before.

  • A NULL scalar length, which a scalar subquery returning no rows produces, now yields NULL rows instead of an Unsupported arguments error (#7452).

  • A dictionary-typed scalar string is rejected with an error rather than sent to the dictionary arm.

  • When every argument is a scalar, the result is returned as a scalar.

  • A NULL scalar string yields NULL, matching Spark's null-intolerant StringLPad/StringRPad.
  • The serde only changes the str unsupported reason, from "Scalar values" to "Literal values", since it rejects only a literal str and the kernel now handles a str that is a scalar at runtime. Literal strings and non-literal padding still use codegen dispatch, as documented in expressions.md. Lifting the literal str restriction is tracked in https://github.com/apache/datafusion-comet/issues/7525.

    How are these changes tested?

    New unit tests in read_side_padding.rs:

  • scalar string with a scalar length, for lpad, rpad, rpad truncation, and read_side_padding

  • scalar string with an array length

  • NULL scalar string

  • LargeUtf8 scalar string

  • NULL scalar length, with 2 and 3 arguments, for lpad, rpad and read_side_padding

  • 1 MiB scalar string × 4096 rows (panicked with offset overflow before the second commit)

  • non-ASCII scalar string with an array length

  • dictionary scalar string is rejected
  • These failed on main with the Unsupported arguments error above, and pass with this change.

    SQL file tests in expressions/string/string_lpad.sql and string_rpad.sql, using expect_native(lpad|rpad) on the existing test_lpad/test_rpad tables. IF(coalesce(len, 0) > -1000, (SELECT max(s) FROM t), s) takes the subquery branch for every row, so the string is a scalar for the whole batch. max(s) is 'é', so the non-ASCII path is covered.

  • IF and CASE WHEN, with len as the length (scalar string, array length)

  • 2-argument form, and a literal length

  • subquery returning NULL

  • mixed batch, where only some rows take the subquery branch so the string stays an array

  • length from a scalar subquery that returns no rows (NULL length), with 2 and 3 arguments

  • scalar string with a NULL scalar length, so the result is a NULL scalar
  • Locally (macOS, Rust 1.99.1):

  • cargo test -p datafusion-comet-spark-expr --lib read_side_padding: 20 passed

  • cargo clippy -p datafusion-comet-spark-expr --all-targets -- -D warnings: clean

  • cargo fmt --all -- --check: clean
  • I did not run CometSqlFileTestSuite locally. The fork CI on the default Spark 4.1 profile passed, including string_lpad.sql and string_rpad.sql in the [expressions] job: https://github.com/tlfua/datafusion-comet/actions/runs/42112117022. I did not run the other Spark profiles.

    AI disclosure: the change, the tests and this description were written with an AI coding assistant, and reviewed by me.

    Diff: 578 lines (+433 / -145) across 7 files Reviewer: rich7420 Review duration: 19877m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 578 lines diff across 7 files
    0.50
    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.381
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7450"
    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: 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: 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: 21.6% · Safe to merge
    21.6%