You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: Enhance Arrow introduction guide with batch size config and improved examples
- Add configuring batch size section with Rust and SQL examples
- Add reference link for datafusion.execution.batch_size configuration
- Improve introduction with clearer Arrow explanation
- Enhance code examples with better imports and error handling
- Add links to arrow2 guide for deeper understanding
- Improve extension points section with ExecutionPlan details
- Reorganize Further Reading section with better categorization
- Hope I haven't overseen any comments
Addresses feedback from PR apache#18051
Copy file name to clipboardExpand all lines: docs/source/user-guide/arrow-introduction.md
+77-70Lines changed: 77 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,40 +17,24 @@
17
17
under the License.
18
18
-->
19
19
20
-
# A Gentle Introduction to Arrow & RecordBatches (for DataFusion users)
20
+
# Introduction to `Arrow` & RecordBatches
21
21
22
22
```{contents}
23
23
:local:
24
24
:depth: 2
25
25
```
26
26
27
-
This guide helps DataFusion users understand [Arrow]and its RecordBatch format. While you may never need to work with Arrow directly, this knowledge becomes valuable when using DataFusion's extension points or debugging performance issues.
27
+
This guide helps DataFusion users understand [Apache Arrow]—a language-independent, in-memory columnar format and development platform for analytics. It defines a standardized columnar representation that enables different systems and languages (e.g., Rust and Python) to share data with zero-copy interchange, avoiding serialization overhead. A core building block is the [`RecordBatch`] format. While you may never need to work with Arrow directly, this knowledge becomes valuable when using DataFusion's extension points or debugging performance issues.
28
28
29
29
**Why Arrow is central to DataFusion**: Arrow provides the unified type system that makes DataFusion possible. When you query a CSV file, join it with a Parquet file, and aggregate results from JSON—it all works seamlessly because every data source is converted to Arrow's common representation. This unified type system, combined with Arrow's columnar format, enables DataFusion to execute efficient vectorized operations across any combination of data sources while benefiting from zero-copy data sharing between query operators.
30
30
31
31
## Why Columnar? The Arrow Advantage
32
32
33
-
Apache Arrow is an open **specification** that defines how analytical data should be organized in memory. Think of it as a blueprint that different systems agree to follow, not a database or programming language.
33
+
Apache Arrow is an open **specification** that defines a common way to organize analytical data in memory. Think of it as a set of best practices that different systems agree to follow, not a database or programming language.
34
34
35
35
### Row-oriented vs Columnar Layout
36
36
37
-
Traditional databases often store data row-by-row:
38
-
39
-
```
40
-
Row 1: [id: 1, name: "Alice", age: 30]
41
-
Row 2: [id: 2, name: "Bob", age: 25]
42
-
Row 3: [id: 3, name: "Carol", age: 35]
43
-
```
44
-
45
-
Arrow organizes the same data by column:
46
-
47
-
```
48
-
Column "id": [1, 2, 3]
49
-
Column "name": ["Alice", "Bob", "Carol"]
50
-
Column "age": [30, 25, 35]
51
-
```
52
-
53
-
Visual comparison:
37
+
Quick visual: row-major (left) vs Arrow's columnar layout (right). For a deeper primer, see the [arrow2 guide].
54
38
55
39
```
56
40
Traditional Row Storage: Arrow Columnar Storage:
@@ -71,23 +55,44 @@ Traditional Row Storage: Arrow Columnar Storage:
71
55
-**Cache Efficiency**: Scanning specific columns doesn't load unnecessary data into CPU cache
72
56
-**Zero-Copy Data Sharing**: Systems can share Arrow data without conversion overhead
73
57
74
-
Arrow has become the universal standard for in-memory analytics precisely because of its **columnar format**—systems that natively store or process data in Arrow (DataFusion, Polars, InfluxDB 3.0), and runtimes that convert to Arrow for interchange (DuckDB, Spark, pandas), all organize data by column rather than by row. This cross-language, cross-platform adoption of the columnar model enables seamless data flow between systems with minimal conversion overhead.
58
+
Arrow is widely adopted for in-memory analytics precisely because of its **columnar format**—systems that natively store or process data in Arrow (DataFusion, Polars, InfluxDB 3.0), and runtimes that convert to Arrow for interchange (DuckDB, Spark, pandas), all organize data by column rather than by row. This cross-language, cross-platform adoption of the columnar model enables seamless data flow between systems with minimal conversion overhead.
75
59
76
60
Within this columnar design, Arrow's standard unit for packaging data is the **RecordBatch**—the key to making columnar format practical for real-world query engines.
77
61
78
62
## What is a RecordBatch? (And Why Batch?)
79
63
80
-
A **[`RecordBatch`]**cleverly combines the benefits of columnar storage with the practical need to process data in chunks. It represents a horizontal slice of a table, but critically, each column _within_ that slice remains a contiguous array.
64
+
A **[`RecordBatch`]**represents a horizontal slice of a table—a collection of equal-length columnar arrays that conform to a defined schema. Each column within the slice is a contiguous Arrow array, and all columns have the same number of rows (length). This chunked, immutable unit enables efficient streaming and parallel execution.
81
65
82
66
Think of it as having two perspectives:
83
67
84
68
-**Columnar inside**: Each column (`id`, `name`, `age`) is a contiguous array optimized for vectorized operations
85
-
-**Row-oriented outside**: The batch represents a chunk of rows (e.g., rows 1-1000), making it a manageable unit for streaming
69
+
-**Row-chunked externally**: The batch represents a chunk of rows (e.g., rows 1-1000), making it a manageable unit for streaming
86
70
87
71
RecordBatches are **immutable snapshots**—once created, they cannot be modified. Any transformation produces a _new_ RecordBatch, enabling safe parallel processing without locks or coordination overhead.
88
72
89
73
This design allows DataFusion to process streams of row-based chunks while gaining maximum performance from the columnar layout. Let's see how this works in practice.
90
74
75
+
### Configuring Batch Size
76
+
77
+
DataFusion uses a default batch size of 8192 rows per RecordBatch, balancing memory efficiency with vectorization benefits. You can adjust this via the session configuration or the [`datafusion.execution.batch_size`] configuration setting:
78
+
79
+
```rust
80
+
usedatafusion::execution::config::SessionConfig;
81
+
usedatafusion::prelude::*;
82
+
83
+
letconfig=SessionConfig::new().with_batch_size(8192); // default value
84
+
letctx=SessionContext::with_config(config);
85
+
```
86
+
87
+
You can also query and modify this setting using SQL:
88
+
89
+
```sql
90
+
SHOW datafusion.execution.batch_size;
91
+
SETdatafusion.execution.batch_size TO 1024;
92
+
```
93
+
94
+
See [configuration settings] for more details.
95
+
91
96
## From files to Arrow
92
97
93
98
When you call [`read_csv`], [`read_parquet`], [`read_json`] or [`read_avro`], DataFusion decodes those formats into Arrow arrays and streams them to operators as RecordBatches.
// let df = ctx.read_avro("data.avro", AvroReadOptions::default()).await?; // requires "avro" feature
109
114
110
115
letbatches=df
@@ -139,14 +144,15 @@ In this pipeline, [`RecordBatch`]es are the "packages" of columnar data that flo
139
144
140
145
Sometimes you need to create Arrow data programmatically rather than reading from files. This example shows the core building blocks: creating typed arrays (like [`Int32Array`] for numbers), defining a [`Schema`] that describes your columns, and assembling them into a [`RecordBatch`].
141
146
142
-
Note: You'll see [`Arc`] used frequently in the code—DataFusion's async architecture requires wrapping Arrow arrays in `Arc` (atomically reference-counted pointers) to safely share data across tasks. [`ArrayRef`] is simply a type alias for `Arc<dyn Array>`.
147
+
Note: You'll see [`Arc`] used frequently in the code—Arrow arrays are wrapped in `Arc` (atomically reference-counted pointers) to enable cheap, thread-safe sharing across operators and tasks. [`ArrayRef`] is simply a type alias for `Arc<dyn Array>`.
@@ -205,8 +211,8 @@ When working with Arrow and RecordBatches, watch out for these common issues:
205
211
206
212
-**Schema consistency**: All batches in a stream must share the exact same [`Schema`]. For example, you can't have one batch where a column is [`Int32`] and the next where it's [`Int64`], even if the values would fit
207
213
-**Immutability**: Arrays are immutable—to "modify" data, you must build new arrays or new RecordBatches. For instance, to change a value in an array, you'd create a new array with the updated value
-**Type mismatches**: Mixed input types across files may require explicit casts. For example, a string column `"123"` from a CSV file won't automatically join with an integer column `123` from a Parquet file—you'll need to cast one to match the other
214
+
-**Row by Row Processing**: Avoid iterating over Arrays element by element when possible, and use Arrow's built-in compute functions instead
215
+
-**Type mismatches**: Mixed input types across files may require explicit casts. For example, a string column `"123"` from a CSV file won't automatically join with an integer column `123` from a Parquet file—you'll need to cast one to match the other. Use Arrow's [`cast`] kernel where appropriate
210
216
-**Batch size assumptions**: Don't assume a particular batch size; always iterate until the stream ends. One file might produce 8192-row batches while another produces 1024-row batches
211
217
212
218
## When Arrow knowledge is needed (Extension Points)
@@ -219,66 +225,67 @@ These APIs are where you can plug your own code into the engine, and they often
219
225
220
226
-**[User-Defined Functions (UDFs)]**: If you need to perform a custom transformation on your data that isn't built into DataFusion, you can write a UDF. Your function will receive data as Arrow arrays (inside a [`RecordBatch`]) and must produce an Arrow array as its output.
221
227
222
-
-**[Custom Optimizer Rules and Operators]**: For advanced use cases, you can even add your own rules to the query optimizer or implement entirely new physical operators (like a special type of join). These also operate on the Arrow-based query plans.
228
+
-**[Custom Optimizer Rules and Physical Operators]**: For advanced use cases, you can add your own optimizer rules or implement entirely new physical operators by implementing the [`ExecutionPlan`] trait. While optimizer rules primarily reason about schemas and plan properties, ExecutionPlan implementations directly produce and consume streams of [`RecordBatch`]es during query execution.
223
229
224
230
In short, knowing Arrow is key to unlocking the full power of DataFusion's modular and extensible architecture.
225
231
226
-
## Next Steps: Working with DataFrames
227
-
228
-
Now that you understand Arrow's RecordBatch format, you're ready to work with DataFusion's high-level APIs. The [DataFrame API](dataframe.md) provides a familiar, ergonomic interface for building queries without needing to think about Arrow internals most of the time.
229
-
230
-
The DataFrame API handles all the Arrow details under the hood - reading files into RecordBatches, applying transformations, and producing results. You only need to drop down to the Arrow level when implementing custom data sources, UDFs, or other extension points.
231
-
232
-
**Recommended reading order:**
233
-
234
-
1.[DataFrame API](dataframe.md) - High-level query building interface
235
-
2.[Library User Guide: DataFrame API](../library-user-guide/using-the-dataframe-api.md) - Detailed examples and patterns
236
-
3.[Custom Table Providers](../library-user-guide/custom-table-providers.md) - When you need Arrow knowledge
237
-
238
232
## Further reading
239
233
240
234
**Arrow Documentation:**
241
235
242
-
-[Arrow Format Introduction](https://arrow.apache.org/docs/format/Intro.html) - Official Arrow specification
243
-
-[Arrow Columnar Format](https://arrow.apache.org/docs/format/Columnar.html) - In-depth look at the memory layout
236
+
-[Arrow Format Introduction](https://arrow.apache.org/docs/format/Intro.html) - Understand the Arrow specification and why it enables zero-copy data sharing
237
+
-[Arrow Columnar Format](https://arrow.apache.org/docs/format/Columnar.html) - Deep dive into memory layout for performance optimization
238
+
-[Arrow Rust Documentation](https://docs.rs/arrow/latest/arrow/) - Complete API reference for the Rust implementation
239
+
240
+
**DataFusion Extension Guides:**
241
+
242
+
-[Custom Table Providers](../library-user-guide/custom-table-providers.md) - Build custom data sources that stream RecordBatches
243
+
-[Adding UDFs](../library-user-guide/functions/adding-udfs.md) - Create efficient user-defined functions working with Arrow arrays
244
+
-[Extending Operators](../library-user-guide/extending-operators.md) - Implement custom ExecutionPlan operators for specialized processing
244
245
245
-
**DataFusion API References:**
246
+
**Key API References:**
246
247
247
-
-[RecordBatch](https://docs.rs/arrow-array/latest/arrow_array/struct.RecordBatch.html) - Core Arrow data structure
-[RecordBatch](https://docs.rs/arrow-array/latest/arrow_array/struct.RecordBatch.html) - The fundamental data structure for streaming columnar data
249
+
-[ExecutionPlan](https://docs.rs/datafusion/latest/datafusion/physical_plan/trait.ExecutionPlan.html) - Trait for implementing custom physical operators
250
+
-[TableProvider](https://docs.rs/datafusion/latest/datafusion/datasource/trait.TableProvider.html) - Interface for custom data sources
251
+
-[ScalarUDF](https://docs.rs/datafusion/latest/datafusion/logical_expr/struct.ScalarUDF.html) - Building scalar user-defined functions
252
+
-[MemTable](https://docs.rs/datafusion/latest/datafusion/datasource/struct.MemTable.html) - Working with in-memory Arrow data
251
253
252
254
**Academic Paper:**
253
255
254
-
-[Apache Arrow DataFusion: A Fast, Embeddable, Modular Analytic Query Engine](https://dl.acm.org/doi/10.1145/3626246.3653368) - Published at SIGMOD 2024
0 commit comments