Skip to main content

sqlload_python_host/crossing/main/
table_batches.rs

1//! Start reading a table generator into Arrow.
2
3use std::sync::Arc;
4use std::sync::mpsc::sync_channel;
5
6use arrow_schema::Schema;
7use pyo3::{Bound, PyAny};
8
9use crate::crossing::_helpers::parser::start_parsers;
10use crate::crossing::_helpers::producer::produce;
11use crate::crossing::models::{
12    ByteBudget, CrossingOptions, CrossingStats, ReadContext, TableBatches,
13};
14
15/// Read `iterable` (a table function's generator) on a background thread (calling `snapshot`, when given, for `ctx.state` as each piece crosses), batching dict yields, and parse what it yields into Arrow on others, expanding the objects `context` lists to flatten and running `sl.http` jobs with its connections; at most `options.max_queued + options.parse_threads` batches wait between them and the consumer, so the generator pauses when the consumer falls behind.
16pub fn table_batches(
17    iterable: &Bound<'_, PyAny>,
18    snapshot: Option<&Bound<'_, PyAny>>,
19    options: CrossingOptions,
20    context: ReadContext,
21) -> TableBatches {
22    let (batch_sender, batch_receiver) = sync_channel(0);
23    let stats = Arc::new(CrossingStats {
24        budget: ByteBudget::new(options.max_queued_bytes),
25        ..CrossingStats::default()
26    });
27    let dispatch = start_parsers(options, batch_sender, &stats, context);
28    let source = iterable.clone().unbind();
29    let snapshot = snapshot.map(|snapshot| snapshot.clone().unbind());
30    let producer_stats = stats.clone();
31    std::thread::spawn(move || produce((source, snapshot), options, dispatch, producer_stats));
32    TableBatches {
33        receiver: batch_receiver,
34        schema: Schema::empty(),
35        stats,
36        finished: false,
37    }
38}