fix: support scalar string argument in native lpad/rpad #7450
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
rich7420
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/rpadcan 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 nativeread_side_paddingkernel. Every arm there expects the string to be an array, so the query fails with:@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_padding2gets new arms for a scalar string argument,Utf8andLargeUtf8, which call a genericspark_read_side_padding_scalar::<T>helper. The padding loop now takes the strings as an iterator, anasciiflag and a capacity floor. The array arms passstring_array.iter(), so their path is the same as onmain. The scalar arm passesstd::iter::repeat_n(value, n)with no capacity floor, so the scalar is never expanded into an array. Expanding it would needlen(s) * rowsbytes of input and reserve as much output, which overflows the 32-bit offsets of aUtf8array for a wide string (for example 1 MiB × 4096 rows). Truncation,len <= 0and empty pad behave as before.Unsupported argumentserror (#7452).StringLPad/StringRPad.The serde only changes the
strunsupported reason, from "Scalar values" to "Literal values", since it rejects only a literalstrand the kernel now handles astrthat is a scalar at runtime. Literal strings and non-literal padding still use codegen dispatch, as documented inexpressions.md. Lifting the literalstrrestriction is tracked in https://github.com/apache/datafusion-comet/issues/7525.How are these changes tested?
New unit tests in
read_side_padding.rs:lpad,rpad,rpadtruncation, andread_side_paddingLargeUtf8scalar stringlpad,rpadandread_side_paddingoffset overflowbefore the second commit)These failed on
mainwith theUnsupported argumentserror above, and pass with this change.SQL file tests in
expressions/string/string_lpad.sqlandstring_rpad.sql, usingexpect_native(lpad|rpad)on the existingtest_lpad/test_rpadtables.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.IFandCASE WHEN, withlenas the length (scalar string, array length)Locally (macOS, Rust 1.99.1):
cargo test -p datafusion-comet-spark-expr --lib read_side_padding: 20 passedcargo clippy -p datafusion-comet-spark-expr --all-targets -- -D warnings: cleancargo fmt --all -- --check: cleanI did not run
CometSqlFileTestSuitelocally. The fork CI on the default Spark 4.1 profile passed, includingstring_lpad.sqlandstring_rpad.sqlin 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.