Skip to content

Commit 39dd457

Browse files
committed
Use upstream arrow-rs record_batch! and create_array! macros
Removes DataFusion's custom `record_batch!` and `create_array!` macro implementations in favor of the upstream versions from arrow-rs added in apache/arrow-rs#6588. Changes: - Replace custom macro definitions with re-exports from arrow::array - Update syntax from vec![...] to array literal [...] across 67 usages - Add arrow_schema aliases in test modules for macro compatibility - Add record_batch_vec! legacy macro for cases where upstream macro cannot be used (variables, vec repetition syntax) - Use record_batch_vec! in examples and tests that pass variables - Add datafusion-common dependency to ffi_example_table_provider The record_batch_vec! macro is a temporary solution until arrow-rs supports variables and repetition syntax. See upstream issue: apache/arrow-rs#6553 Closes #13037
1 parent 2d004af commit 39dd457

18 files changed

Lines changed: 167 additions & 186 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datafusion-examples/examples/ffi/ffi_example_table_provider/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ publish = false
2525
abi_stable = "0.11.3"
2626
arrow = { workspace = true }
2727
datafusion = { workspace = true }
28+
datafusion-common = { workspace = true }
2829
datafusion-ffi = { workspace = true }
2930
ffi_module_interface = { path = "../ffi_module_interface" }
3031

datafusion-examples/examples/ffi/ffi_example_table_provider/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::sync::Arc;
2020
use abi_stable::{export_root_module, prefix_type::PrefixTypeTrait};
2121
use arrow::array::RecordBatch;
2222
use arrow::datatypes::{DataType, Field, Schema};
23-
use datafusion::{common::record_batch, datasource::MemTable};
23+
use datafusion::datasource::MemTable;
2424
use datafusion_ffi::table_provider::FFI_TableProvider;
2525
use ffi_module_interface::{TableProviderModule, TableProviderModuleRef};
2626

@@ -29,7 +29,10 @@ fn create_record_batch(start_value: i32, num_values: usize) -> RecordBatch {
2929
let a_vals: Vec<i32> = (start_value..end_value).collect();
3030
let b_vals: Vec<f64> = a_vals.iter().map(|v| *v as f64).collect();
3131

32-
record_batch!(("a", Int32, a_vals), ("b", Float64, b_vals)).unwrap()
32+
// TODO: Use arrow::record_batch! once it supports variables
33+
// See https://github.com/apache/arrow-rs/issues/6553
34+
datafusion_common::record_batch_old!(("a", Int32, a_vals), ("b", Float64, b_vals))
35+
.unwrap()
3336
}
3437

3538
/// Here we only wish to create a simple table provider as an example.

datafusion-examples/examples/memory_pool_execution_plan.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
//! - Handle memory pressure by spilling to disk
2525
//! - Release memory when done
2626
27-
use arrow::record_batch::RecordBatch;
27+
use arrow::array::RecordBatch;
28+
use arrow::datatypes::{DataType, Field, Schema};
2829
use arrow_schema::SchemaRef;
29-
use datafusion::common::record_batch;
3030
use datafusion::common::{exec_datafusion_err, internal_err};
31+
use datafusion::common::{record_batch, record_batch_old};
3132
use datafusion::datasource::{memory::MemTable, DefaultTableSource};
3233
use datafusion::error::Result;
3334
use datafusion::execution::memory_pool::{MemoryConsumer, MemoryReservation};
@@ -57,30 +58,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5758
let config = SessionConfig::new().with_coalesce_batches(false);
5859
let ctx = SessionContext::new_with_config_rt(config, runtime.clone());
5960

61+
let schema = Arc::new(Schema::new(vec![
62+
Field::new("id", DataType::Int32, true),
63+
Field::new("name", DataType::Utf8, true),
64+
]));
65+
6066
// Create smaller batches to ensure we get multiple RecordBatches from the scan
6167
// Make each batch smaller than the memory limit to force multiple batches
62-
let batch1 = record_batch!(
68+
// TODO: Use arrow::record_batch! once it supports vec repetition syntax
69+
// See https://github.com/apache/arrow-rs/issues/6553
70+
let batch1 = record_batch_old!(
6371
("id", Int32, vec![1; 800]),
6472
("name", Utf8, vec!["Alice"; 800])
6573
)?;
6674

67-
let batch2 = record_batch!(
75+
let batch2 = record_batch_old!(
6876
("id", Int32, vec![2; 800]),
6977
("name", Utf8, vec!["Bob"; 800])
7078
)?;
7179

72-
let batch3 = record_batch!(
80+
let batch3 = record_batch_old!(
7381
("id", Int32, vec![3; 800]),
7482
("name", Utf8, vec!["Charlie"; 800])
7583
)?;
7684

77-
let batch4 = record_batch!(
85+
let batch4 = record_batch_old!(
7886
("id", Int32, vec![4; 800]),
7987
("name", Utf8, vec!["David"; 800])
8088
)?;
8189

82-
let schema = batch1.schema();
83-
8490
// Create a single MemTable with all batches in one partition to preserve order but ensure streaming
8591
let mem_table = Arc::new(MemTable::try_new(
8692
Arc::clone(&schema),
@@ -257,6 +263,9 @@ impl ExecutionPlan for BufferingExecutionPlan {
257263
partition: usize,
258264
context: Arc<TaskContext>,
259265
) -> Result<SendableRecordBatchStream> {
266+
// Required for arrow_array::record_batch! macro to work
267+
use arrow::datatypes as arrow_schema;
268+
260269
// Register memory consumer with the context's memory pool
261270
let reservation = MemoryConsumer::new("MyExternalBatchBufferer")
262271
.with_can_spill(true)
@@ -286,10 +295,9 @@ impl ExecutionPlan for BufferingExecutionPlan {
286295

287296
// Since this is a simplified example, return an empty batch
288297
// In a real implementation, you would create a batch stream from the processed results
289-
record_batch!(("id", Int32, vec![5]), ("name", Utf8, vec!["Eve"]))
290-
.map_err(|e| {
291-
exec_datafusion_err!("Failed to create final RecordBatch: {e}")
292-
})
298+
record_batch!(("id", Int32, [5]), ("name", Utf8, ["Eve"])).map_err(|e| {
299+
exec_datafusion_err!("Failed to create final RecordBatch: {e}")
300+
})
293301
}),
294302
)))
295303
}

datafusion/common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub mod utils;
6464

6565
/// Reexport arrow crate
6666
pub use arrow;
67+
/// Reexport arrow-rs macros for creating arrays and record batches
68+
pub use arrow::array::{create_array, record_batch};
6769
pub use column::Column;
6870
pub use dfschema::{
6971
qualified_name, DFSchema, DFSchemaRef, ExprSchema, SchemaExt, ToDFSchema,

0 commit comments

Comments
 (0)