← Back to Pull requests

[SPARK-48873][SQL] Use UnsafeRow in JSON parser. #47310

Closed
unknown closed this pull request without merging on Jul 16, 2024 08:13 UTC
View on GitHub
unknown unknown commented on Jul 11, 2024 23:53 UTC
Author

What changes were proposed in this pull request?

It uses UnsafeRow to represent struct result in the JSON parser. It saves memory compared to the current GenericInternalRow. The change is guarded by a flag and disabled by default.

The benchmark shows that enabling the flag brings ~10% slowdown. This is basically expected because converting to UnsafeRow requires some work. The purpose of the PR is to provide an alternative to save memory.

I did the following experiment. It generates a big .gz JSON file containing a single large array. Each array element is a struct with 50 string fields and will be parsed into a row by the JSON reader.

s = b'{"field00":null,"field01":"field01_<v>","field02":"field02_<v>","field03":"field03_<v>","field04":"field04_<v>","field05":"field05_<v>","field06":"field06_<v>","field07":"field07_<v>","field08":"field08_<v>","field09":"field09_<v>","field10":null,"field11":"field11_<v>","field12":"field12_<v>","field13":"field13_<v>","field14":"field14_<v>","field15":"field15_<v>","field16":"field16_<v>","field17":"field17_<v>","field18":"field18_<v>","field19":"field19_<v>","field20":null,"field21":"field21_<v>","field22":"field22_<v>","field23":"field23_<v>","field24":"field24_<v>","field25":"field25_<v>","field26":"field26_<v>","field27":"field27_<v>","field28":"field28_<v>","field29":"field29_<v>","field30":null,"field31":"field31_<v>","field32":"field32_<v>","field33":"field33_<v>","field34":"field34_<v>","field35":"field35_<v>","field36":"field36_<v>","field37":"field37_<v>","field38":"field38_<v>","field39":"field39_<v>","field40":null,"field41":"field41_<v>","field42":"field42_<v>","field43":"field43_<v>","field44":"field44_<v>","field45":"field45_<v>","field46":"field46_<v>","field47":"field47_<v>","field48":"field48_<v>","field49":"field49_<v>"}'

import gzip

def write(n):
  with gzip.open(f'json{n}.gz', 'w') as f:
    f.write(b'[')
    for i in range(n):
        if i != 0:
            f.write(b',')
        f.write(s.replace(b'<v>', str(i).encode('ascii')))
    f.write(b']')

write(100000)

Then it processes the file in Spark shell with the following command:

./bin/spark-shell --conf spark.driver.memory=1g --conf spark.executor.memory=1g  --master "local[1]"

> val schema = "field00 string, field01 string, field02 string, field03 string, field04 string, field05 string, field06 string, field07 string, field08 string, field09 string, field10 string, field11 string, field12 string, field13 string, field14 string, field15 string, field16 string, field17 string, field18 string, field19 string, field20 string, field21 string, field22 string, field23 string, field24 string, field25 string, field26 string, field27 string, field28 string, field29 string, field30 string, field31 string, field32 string, field33 string, field34 string, field35 string, field36 string, field37 string, field38 string, field39 string, field40 string, field41 string, field42 string, field43 string, field44 string, field45 string, field46 string, field47 string, field48 string, field49 string"
> spark.conf.set("spark.sql.json.useUnsafeRow", "false")
> spark.read.schema(schema).option("multiline", "true").json("json100000.gz").selectExpr("sum(hash(struct(*)))").collect()

When the flag is off (the current behavior), the query can process 2.5e5 rows but fails to process 3e5 rows. When the flag is on, the query can process 8e5 rows but fails to process 9e5 rows. We can say this change reduces the memory consumption to about 1/3.

Why are the changes needed?

It reduces the memory requirement of JSON-related query.

Does this PR introduce _any_ user-facing change?

No.

How was this patch tested?

A new JSON unit test with the config flag on.

Was this patch authored or co-authored using generative AI tooling?

No.

Diff: 484 lines (+363 / -121) across 6 files Reviewer: sadikovi Review duration: 6259m
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.47) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 484 lines diff across 6 files
0.47
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.370
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #47310"
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: 47.3% (MEDIUM) · Higher = larger blast radius
47.3%
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: 20.5% · Safe to merge
20.5%
Repository