Skip to main content

rypipe_core/
parallel.rs

1use std::panic::{catch_unwind, AssertUnwindSafe};
2use std::sync::atomic::{AtomicU64, Ordering};
3use std::sync::Arc;
4use std::time::Instant;
5
6use arrow::record_batch::RecordBatch;
7use rayon::prelude::*;
8use rustc_hash::FxHashMap as HashMap;
9
10use crate::arrow_export::apply_compare_filter;
11use crate::decoder::{split_points_to_ranges, RecordParser, Splitter};
12use crate::engine::TableBuilder;
13use crate::merge::engines_to_record_batches;
14use crate::plan::ExecutionPlan;
15use crate::Result;
16
17// --- Chunk-time profiling ---
18// Reset before each parallel run; read via `chunk_profile()`.
19static CHUNK_TIME_SUM_NS: AtomicU64 = AtomicU64::new(0);
20static CHUNK_TIME_MAX_NS: AtomicU64 = AtomicU64::new(0);
21static CHUNK_COUNT: AtomicU64 = AtomicU64::new(0);
22static SPLIT_SCAN_NS: AtomicU64 = AtomicU64::new(0);
23
24/// Reset profiling counters (call before a parallel run).
25pub fn reset_chunk_profile() {
26    CHUNK_TIME_SUM_NS.store(0, Ordering::Relaxed);
27    CHUNK_TIME_MAX_NS.store(0, Ordering::Relaxed);
28    CHUNK_COUNT.store(0, Ordering::Relaxed);
29    SPLIT_SCAN_NS.store(0, Ordering::Relaxed);
30}
31
32/// Read the chunk-time profile as (split_scan_ns, sum_ns, max_ns, count).
33pub fn chunk_profile() -> (u64, u64, u64, u64) {
34    (
35        SPLIT_SCAN_NS.load(Ordering::Relaxed),
36        CHUNK_TIME_SUM_NS.load(Ordering::Relaxed),
37        CHUNK_TIME_MAX_NS.load(Ordering::Relaxed),
38        CHUNK_COUNT.load(Ordering::Relaxed),
39    )
40}
41
42/// Parallel executor that splits an input byte slice into chunks, parses each
43/// chunk independently, and returns the resulting Arrow record batches.
44pub struct ParallelExecutor;
45
46impl ParallelExecutor {
47    /// Parse `bytes` in parallel using `splitter` and `parser`.
48    ///
49    /// * Fast path (no `auto_dict`): each chunk is exported as its own
50    ///   `RecordBatch` and a vector of batches is returned. Row filters
51    ///   (`Equal`/`NotEqual`/`Compare`) are applied during parse per chunk.
52    /// * Merge path: chunk builders are merged sequentially so that
53    ///   auto-dictionary upgrades see the full cardinality, then a single
54    ///   `RecordBatch` is returned inside the vector.
55    pub fn parse<P>(
56        bytes: &[u8],
57        splitter: &dyn Splitter,
58        parser: P,
59        plan: Arc<ExecutionPlan>,
60        num_chunks: usize,
61    ) -> Result<Vec<RecordBatch>>
62    where
63        P: RecordParser + Clone + Send + Sync,
64    {
65        reset_chunk_profile();
66        let t_split = Instant::now();
67        let split_points = splitter.find_split_points_with(
68            bytes,
69            num_chunks,
70            plan.min_chunk_bytes
71                .unwrap_or(crate::decoder::MIN_CHUNK_BYTES),
72        );
73        let split_ns = t_split.elapsed().as_nanos() as u64;
74        SPLIT_SCAN_NS.store(split_ns, Ordering::Relaxed);
75        crate::profiling::add(crate::profiling::SPLIT, split_ns);
76        let mut ranges = split_points_to_ranges(&split_points, bytes.len());
77
78        // Guard against degenerate splitter output that produces no ranges.
79        if ranges.is_empty() {
80            ranges.push(0..bytes.len());
81        }
82
83        let est_row = splitter
84            .estimate_bytes_per_row(&bytes[..bytes.len().min(65536)])
85            .max(512);
86        let results: Vec<Result<TableBuilder>> = ranges
87            .into_par_iter()
88            .map(|range| {
89                let t_chunk = Instant::now();
90                let result = catch_unwind(AssertUnwindSafe(|| {
91                    let est = if !range.is_empty() {
92                        (range.len() / est_row).max(64)
93                    } else {
94                        64
95                    };
96                    let mut sink = TableBuilder::with_plan(est, Arc::clone(&plan));
97                    let t = Instant::now();
98                    parser.validate(&bytes[range.clone()])?;
99                    crate::profiling::add(
100                        crate::profiling::VALIDATE,
101                        t.elapsed().as_nanos() as u64,
102                    );
103                    let t = Instant::now();
104                    parser.parse_chunk_generic(&bytes[range.clone()], &mut sink)?;
105                    crate::profiling::add(crate::profiling::PARSE, t.elapsed().as_nanos() as u64);
106                    Ok(sink)
107                }))
108                .unwrap_or_else(|payload| {
109                    let msg = if let Some(s) = payload.downcast_ref::<&str>() {
110                        (*s).to_string()
111                    } else if let Some(s) = payload.downcast_ref::<String>() {
112                        s.clone()
113                    } else {
114                        "unknown panic".to_string()
115                    };
116                    Err(crate::Error::Parser(format!(
117                        "worker panicked during parallel parse: {msg}. \
118                         This usually indicates a bug in the parser (e.g., returning \
119                         Cow::Borrowed that outlives the input chunk, or an unwrap() \
120                         on None/Err during parsing). Check your RecordParser::parse_chunk \
121                         implementation for incorrect lifetime handling or missing error checks."
122                    )))
123                });
124                // Record chunk timing for get_par_profile()
125                let elapsed_ns = t_chunk.elapsed().as_nanos() as u64;
126                CHUNK_TIME_SUM_NS.fetch_add(elapsed_ns, Ordering::Relaxed);
127                CHUNK_COUNT.fetch_add(1, Ordering::Relaxed);
128                // Atomic max: CAS loop
129                loop {
130                    let prev = CHUNK_TIME_MAX_NS.load(Ordering::Relaxed);
131                    if elapsed_ns <= prev {
132                        break;
133                    }
134                    if CHUNK_TIME_MAX_NS
135                        .compare_exchange_weak(
136                            prev,
137                            elapsed_ns,
138                            Ordering::Relaxed,
139                            Ordering::Relaxed,
140                        )
141                        .is_ok()
142                    {
143                        break;
144                    }
145                }
146                result
147            })
148            .collect();
149
150        let mut engines: Vec<TableBuilder> = results.into_iter().collect::<Result<_>>()?;
151
152        if engines.is_empty() {
153            return Ok(Vec::new());
154        }
155
156        let plan = Arc::clone(&engines[0].plan);
157
158        // Fast path: no auto_dict and all chunks agree on column types.
159        // Compare filters are evaluated per-row during parse, so they do not
160        // force the merge path. Schema-inconsistent chunks fall through to the
161        // merge path, which surfaces a precise `Error::Merge` for irreconcilable
162        // type mismatches instead of an opaque Arrow schema error.
163        if !plan.auto_dict && schemas_consistent(&engines) {
164            let t = Instant::now();
165            let out = engines_to_record_batches(engines, &plan);
166            crate::profiling::add(crate::profiling::EXPORT, t.elapsed().as_nanos() as u64);
167            return out;
168        }
169
170        // Incremental dict path for auto_dict: per-chunk upgrade in parallel,
171        // then unify dictionaries (tiny serial) and remap, staying on fast path.
172        if plan.auto_dict {
173            // Per-chunk upgrade in parallel (was serial after merge)
174            engines.par_iter_mut().for_each(|e| e.auto_dict_upgrade());
175            if schemas_consistent(&engines) {
176                // Find first dict column that needs unification across chunks.
177                let mut unify_col: Option<String> = None;
178                for col_name in &engines[0].column_order {
179                    let first_idx = engines[0].field_index.get(col_name).copied();
180                    if let Some(idx) = first_idx {
181                        if engines[0].columns[idx].variant_key() == "dictionary" {
182                            let first_data = if let crate::columnar::ColumnBuilder::Dictionary {
183                                data,
184                                offsets,
185                                ..
186                            } = &engines[0].columns[idx]
187                            {
188                                (data, offsets)
189                            } else {
190                                continue;
191                            };
192                            for e in &engines[1..] {
193                                if let Some(&j) = e.field_index.get(col_name) {
194                                    if let crate::columnar::ColumnBuilder::Dictionary {
195                                        data,
196                                        offsets,
197                                        ..
198                                    } = &e.columns[j]
199                                    {
200                                        if data != first_data.0 || offsets != first_data.1 {
201                                            unify_col = Some(col_name.clone());
202                                            break;
203                                        }
204                                    }
205                                }
206                            }
207                            if unify_col.is_some() {
208                                break;
209                            }
210                        }
211                    }
212                }
213                if unify_col.is_none() {
214                    return engines_to_record_batches(engines, &plan);
215                }
216                let col_name = unify_col.unwrap();
217                // Build seed from first chunk, unify, remap.
218                let first_data = if let Some(&idx) = engines[0].field_index.get(&col_name) {
219                    if let crate::columnar::ColumnBuilder::Dictionary { data, offsets, .. } =
220                        &engines[0].columns[idx]
221                    {
222                        (data.clone(), offsets.clone())
223                    } else {
224                        return engines_to_record_batches(engines, &plan);
225                    }
226                } else {
227                    return engines_to_record_batches(engines, &plan);
228                };
229                // Build SeedDict from the contiguous buffer
230                let mut seed_values = Vec::new();
231                let mut seed_offsets = Vec::with_capacity(first_data.1.len());
232                seed_offsets.push(0);
233                for i in 0..(first_data.1.len() - 1) {
234                    let start = first_data.1[i] as usize;
235                    let end = first_data.1[i + 1] as usize;
236                    seed_values.extend_from_slice(&first_data.0[start..end]);
237                    seed_offsets.push(seed_values.len() as i32);
238                }
239                let seed = crate::dict::SeedDict {
240                    values: seed_values,
241                    offsets: seed_offsets,
242                    index: rustc_hash::FxHashMap::default(), // Will be populated by unify
243                };
244                let col_refs: Vec<&crate::columnar::ColumnBuilder> = engines
245                    .iter()
246                    .filter_map(|e| e.field_index.get(&col_name).map(|&idx| &e.columns[idx]))
247                    .collect();
248                let (unified_dict, remaps) = crate::dict::unify_dictionaries(&seed, &col_refs);
249                // Build unified data+offsets+index for replace_dict.
250                let mut new_index: rustc_hash::FxHashMap<Box<str>, i32> =
251                    rustc_hash::FxHashMap::default();
252                for i in 0..(unified_dict.offsets.len() - 1) {
253                    let start = unified_dict.offsets[i] as usize;
254                    let end = unified_dict.offsets[i + 1] as usize;
255                    let s = String::from_utf8_lossy(&unified_dict.data[start..end]);
256                    let k: Box<str> = s.into();
257                    new_index.insert(k, i as i32);
258                }
259                // Apply remap to each chunk, then replace dict.
260                for (e, remap) in engines.iter_mut().zip(remaps.iter()) {
261                    if let Some(idx) = e.field_index.get(&col_name).copied() {
262                        if let Some(codes) = e.columns[idx].dict_codes_mut() {
263                            codes.remap_codes(&remap.map);
264                        }
265                        e.columns[idx].replace_dict(
266                            unified_dict.data.clone(),
267                            unified_dict.offsets.clone(),
268                            new_index.clone(),
269                        );
270                    }
271                }
272                return engines_to_record_batches(engines, &plan);
273            }
274        }
275
276        // Merge path.
277        let t_merge = Instant::now();
278        let mut merged = TableBuilder::with_plan(engines.len().max(64) * 512, Arc::clone(&plan));
279        for engine in engines {
280            merged.extend(engine)?;
281        }
282        let batch = merged.finish()?;
283        crate::profiling::add(crate::profiling::MERGE, t_merge.elapsed().as_nanos() as u64);
284        if let Some(ref filter) = plan.filter {
285            return Ok(vec![apply_compare_filter(batch, filter)?]);
286        }
287        Ok(vec![batch])
288    }
289}
290
291/// True when every chunk builder agrees on each column's storage variant.
292/// Chunks missing a column entirely are fine (the export path null-fills).
293fn schemas_consistent(engines: &[TableBuilder]) -> bool {
294    let Some(first) = engines.first() else {
295        return true;
296    };
297    let base: HashMap<&str, &str> = first
298        .field_index
299        .iter()
300        .map(|(name, &idx)| (name.as_str(), first.columns[idx].variant_key()))
301        .collect();
302    engines[1..].iter().all(|e| {
303        e.field_index.iter().all(|(name, &idx)| {
304            let b = &e.columns[idx];
305            match base.get(name.as_str()) {
306                Some(key) => *key == b.variant_key(),
307                None => true,
308            }
309        })
310    })
311}