Commit 2347165
committed
[SPARK-57268][SQL] Add Apache Arrow as a native cache format for in-memory Dataset caching
### What changes were proposed in this pull request?
This PR adds Apache Arrow as a native cache format for Spark in-memory Dataset
caching, available alongside the existing `DefaultCachedBatchSerializer`. It is
one of the sub-tasks of [SPARK-56978](https://issues.apache.org/jira/browse/SPARK-56978)
(SPIP: Faster queries in local laptop mode for Apache Spark), specifically the
"Arrow-based `df.cache` reimplementation" item.
The new `ArrowCachedBatchSerializer` stores cached data in Apache Arrow IPC
streaming format. It is opt-in via `spark.sql.cache.serializer`:
```scala
spark.conf.set("spark.sql.cache.serializer",
"org.apache.spark.sql.execution.columnar.ArrowCachedBatchSerializer")
```
Main components:
- **`ArrowCachedBatch`** -- a `SimpleMetricsCachedBatch` holding `numRows`, the
serialized Arrow `RecordBatch` (IPC streaming format, optionally compressed),
and per-column statistics for partition pruning.
- **`ArrowCachedBatchSerializer`** -- the serializer:
- Write paths for both `InternalRow` and `ColumnarBatch` input, with a
zero-copy fast path when the input is already backed by `ArrowColumnVector`.
- Read paths for both `ColumnarBatch` output (wrapping Arrow vectors directly)
and `InternalRow` output. The row path uses pre-built typed
`ArrowColumnReader`s that write directly into an `UnsafeRowWriter` to avoid
per-row pattern matching, and falls back to a columnar-to-row path for
complex types (Array/Struct/Map/UDT/Variant/etc.).
- Optional background prefetch of the next batch (decompress/deserialize off
the consumer thread), controlled by a new config (off by default).
- Min/max statistics collection over Arrow vectors, kept consistent with the
row-based `ColumnStats` path (NaN handling, collation-aware string
comparison, null/decimal bounds).
- **`ArrowUtils.isSupportedByArrow`** -- recursive type-support check used by
`supportsColumnarInput`.
- **`ObjectColumnStats`** -- now skips `getSizeInBytes` for columnar complex
types (`ColumnarArray`/`ColumnarMap`/`ColumnarRow`), which are views into
`ColumnVector`s and do not expose a size.
- New config `spark.sql.execution.arrow.cache.prefetch.enabled` (default
`false`), Kryo registration for the new classes, and documentation
(`sql-arrow-cache-format.html`, linked from the SQL docs menu).
### Why are the changes needed?
The default cache format is row/column-encoded specifically for Spark. Using
Arrow as the cache format provides:
- Zero-copy columnar reads when the cached data is already in Arrow form (e.g.
re-caching Arrow-cached data with column projection).
- Interoperability with the Arrow ecosystem and off-heap memory management via
Arrow allocators.
- Min/max statistics for partition pruning, consistent with the default path.
In our benchmarks, the Arrow format is competitive with or faster than the
default format on columnar/primitive workloads, with the largest gains on the
zero-copy re-cache path. The default format can still be faster in some cases
(for example, at higher compression levels), so this is offered as an opt-in
alternative rather than a replacement. See the committed
`sql/core/benchmarks/ArrowCacheBenchmark-jdk{17,21,25}-results.txt` files,
generated by the `ArrowCacheBenchmark` suite via the GitHub Actions benchmark
workflow.
### Does this PR introduce _any_ user-facing change?
Yes, additively. A new opt-in cache serializer
(`ArrowCachedBatchSerializer`) and a new config
`spark.sql.execution.arrow.cache.prefetch.enabled` (default `false`) are added.
The default cache behavior is unchanged: `spark.sql.cache.serializer` still
defaults to `DefaultCachedBatchSerializer`.
### How was this patch tested?
- New `ArrowCachedBatchSerializerSuite` covering primitive and complex/nested
types, null handling, collation, NaN bounds, statistics correctness for both
the row and columnar (Arrow-vector) paths, columnar input from Parquet,
column projection, filter pushdown, and compression codecs (none/zstd/lz4),
plus a check that the Arrow serializer is actually used.
- `ArrowCachedBatchKryoRegistrationSuite` verifying Kryo registration.
- Added `ArrowCacheBenchmark` for performance comparison against the default
cache format. Result files for JDK 17/21/25 are generated in the consistent
GitHub Actions environment via the benchmark workflow.
Locally: `catalyst/compile` + `sql/Test/compile` pass; the two suites above run
green (0 failures).
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 4.8)
Closes #56334 from viirya/arrow-cache-format.
Authored-by: Liang-Chi Hsieh <viirya@gmail.com>
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>1 parent c369cf2 commit 2347165
16 files changed
Lines changed: 5644 additions & 5 deletions
File tree
- core/src/main/scala/org/apache/spark/serializer
- docs
- _data
- sql
- api/src/main/scala/org/apache/spark/sql/util
- catalyst/src/main/scala/org/apache/spark/sql/internal
- core
- benchmarks
- src
- main/scala/org/apache/spark/sql/execution/columnar
- test/scala/org/apache/spark/sql/execution
- benchmark
- columnar
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
620 | 620 | | |
621 | 621 | | |
622 | 622 | | |
| 623 | + | |
| 624 | + | |
623 | 625 | | |
624 | 626 | | |
625 | 627 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
83 | 85 | | |
84 | 86 | | |
85 | 87 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
| |||
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
41 | 86 | | |
42 | 87 | | |
43 | 88 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4943 | 4943 | | |
4944 | 4944 | | |
4945 | 4945 | | |
| 4946 | + | |
| 4947 | + | |
| 4948 | + | |
| 4949 | + | |
| 4950 | + | |
| 4951 | + | |
| 4952 | + | |
| 4953 | + | |
| 4954 | + | |
| 4955 | + | |
| 4956 | + | |
| 4957 | + | |
4946 | 4958 | | |
4947 | 4959 | | |
4948 | 4960 | | |
| |||
8905 | 8917 | | |
8906 | 8918 | | |
8907 | 8919 | | |
| 8920 | + | |
| 8921 | + | |
8908 | 8922 | | |
8909 | 8923 | | |
8910 | 8924 | | |
| |||
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
175 | 178 | | |
176 | 179 | | |
177 | 180 | | |
| |||
Lines changed: 85 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
Lines changed: 85 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
0 commit comments