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(bytes, num_chunks);
68        SPLIT_SCAN_NS.store(t_split.elapsed().as_nanos() as u64, Ordering::Relaxed);
69        let ranges = split_points_to_ranges(&split_points, bytes.len());
70
71        let est_row = splitter
72            .estimate_bytes_per_row(&bytes[..bytes.len().min(65536)])
73            .max(512);
74        let results: Vec<Result<TableBuilder>> = ranges
75            .into_par_iter()
76            .map(|range| {
77                let t_chunk = Instant::now();
78                let result = catch_unwind(AssertUnwindSafe(|| {
79                    let est = if !range.is_empty() {
80                        (range.len() / est_row).max(64)
81                    } else {
82                        64
83                    };
84                    let mut sink = TableBuilder::with_plan(est, Arc::clone(&plan));
85                    parser.validate(&bytes[range.clone()])?;
86                    parser.parse_chunk_generic(&bytes[range.clone()], &mut sink)?;
87                    Ok(sink)
88                }))
89                .unwrap_or_else(|payload| {
90                    let msg = if let Some(s) = payload.downcast_ref::<&str>() {
91                        (*s).to_string()
92                    } else if let Some(s) = payload.downcast_ref::<String>() {
93                        s.clone()
94                    } else {
95                        "unknown panic".to_string()
96                    };
97                    Err(crate::Error::Merge(format!(
98                        "worker panicked during parallel parse: {msg}"
99                    )))
100                });
101                // Record chunk timing for get_par_profile()
102                let elapsed_ns = t_chunk.elapsed().as_nanos() as u64;
103                CHUNK_TIME_SUM_NS.fetch_add(elapsed_ns, Ordering::Relaxed);
104                CHUNK_COUNT.fetch_add(1, Ordering::Relaxed);
105                // Atomic max: CAS loop
106                loop {
107                    let prev = CHUNK_TIME_MAX_NS.load(Ordering::Relaxed);
108                    if elapsed_ns <= prev {
109                        break;
110                    }
111                    if CHUNK_TIME_MAX_NS
112                        .compare_exchange_weak(
113                            prev,
114                            elapsed_ns,
115                            Ordering::Relaxed,
116                            Ordering::Relaxed,
117                        )
118                        .is_ok()
119                    {
120                        break;
121                    }
122                }
123                result
124            })
125            .collect();
126
127        let mut engines: Vec<TableBuilder> = results.into_iter().collect::<Result<_>>()?;
128
129        if engines.is_empty() {
130            return Ok(Vec::new());
131        }
132
133        let plan = Arc::clone(&engines[0].plan);
134
135        // Fast path: no auto_dict and all chunks agree on column types.
136        // Compare filters are evaluated per-row during parse, so they do not
137        // force the merge path. Schema-inconsistent chunks fall through to the
138        // merge path, which surfaces a precise `Error::Merge` for irreconcilable
139        // type mismatches instead of an opaque Arrow schema error.
140        if !plan.auto_dict && schemas_consistent(&engines) {
141            return engines_to_record_batches(engines, &plan);
142        }
143
144        // Incremental dict path for auto_dict: per-chunk upgrade in parallel,
145        // then unify dictionaries (tiny serial) and remap, staying on fast path.
146        if plan.auto_dict {
147            // Per-chunk upgrade in parallel (was serial after merge)
148            engines.par_iter_mut().for_each(|e| e.auto_dict_upgrade());
149            if schemas_consistent(&engines) {
150                // Find first dict column that needs unification across chunks.
151                let mut unify_col: Option<String> = None;
152                for col_name in &engines[0].column_order.clone() {
153                    let first_idx = engines[0].field_index.get(col_name).copied();
154                    if let Some(idx) = first_idx {
155                        if engines[0].columns[idx].variant_key() == "dictionary" {
156                            let first_dict =
157                                if let crate::columnar::ColumnBuilder::Dictionary { dict, .. } =
158                                    &engines[0].columns[idx]
159                                {
160                                    dict
161                                } else {
162                                    continue;
163                                };
164                            for e in &engines[1..] {
165                                if let Some(&j) = e.field_index.get(col_name) {
166                                    if let crate::columnar::ColumnBuilder::Dictionary {
167                                        dict, ..
168                                    } = &e.columns[j]
169                                    {
170                                        if dict != first_dict {
171                                            unify_col = Some(col_name.clone());
172                                            break;
173                                        }
174                                    }
175                                }
176                            }
177                            if unify_col.is_some() {
178                                break;
179                            }
180                        }
181                    }
182                }
183                if unify_col.is_none() {
184                    return engines_to_record_batches(engines, &plan);
185                }
186                let col_name = unify_col.unwrap();
187                // Build seed from first chunk, unify, remap.
188                let first_dict =
189                    if let crate::columnar::ColumnBuilder::Dictionary { dict, .. } =
190                        &engines[0].columns[engines[0].field_index[&col_name]]
191                    {
192                        dict.clone()
193                    } else {
194                        return engines_to_record_batches(engines, &plan);
195                    };
196                let seed = crate::dict::SeedDict::new(first_dict);
197                let col_refs: Vec<&crate::columnar::ColumnBuilder> = engines
198                    .iter()
199                    .filter_map(|e| e.field_index.get(&col_name).map(|&idx| &e.columns[idx]))
200                    .collect();
201                let (unified_dict, remaps) = crate::dict::unify_dictionaries(&seed, &col_refs);
202                // Build unified dict values and index for replace_dict.
203                let mut new_dict = Vec::with_capacity(unified_dict.offsets.len() - 1);
204                for w in unified_dict.offsets.windows(2) {
205                    let start = w[0] as usize;
206                    let end = w[1] as usize;
207                    let s = std::str::from_utf8(&unified_dict.data[start..end])
208                        .unwrap_or("")
209                        .to_owned();
210                    new_dict.push(s);
211                }
212                let new_index: HashMap<String, i32> = new_dict
213                    .iter()
214                    .enumerate()
215                    .map(|(i, s)| (s.clone(), i as i32))
216                    .collect();
217                // Apply remap to each chunk, then replace dict.
218                for (e, remap) in engines.iter_mut().zip(remaps.iter()) {
219                    if let Some(idx) = e.field_index.get(&col_name).copied() {
220                        if let Some(codes) = e.columns[idx].dict_codes_mut() {
221                            codes.remap_codes(&remap.map);
222                        }
223                        e.columns[idx].replace_dict(new_dict.clone(), new_index.clone());
224                    }
225                }
226                return engines_to_record_batches(engines, &plan);
227            }
228        }
229
230        // Merge path.
231        let mut merged = TableBuilder::with_plan(engines.len().max(64) * 512, Arc::clone(&plan));
232        for engine in engines {
233            merged.extend(engine)?;
234        }
235        let batch = merged.finish()?;
236        if let Some(ref filter) = plan.filter {
237            return Ok(vec![apply_compare_filter(batch, filter)?]);
238        }
239        Ok(vec![batch])
240    }
241}
242
243/// True when every chunk builder agrees on each column's storage variant.
244/// Chunks missing a column entirely are fine (the export path null-fills).
245fn schemas_consistent(engines: &[TableBuilder]) -> bool {
246    let Some(first) = engines.first() else {
247        return true;
248    };
249    let base: HashMap<&str, &str> = first
250        .field_index
251        .iter()
252        .map(|(name, &idx)| (name.as_str(), first.columns[idx].variant_key()))
253        .collect();
254    engines[1..].iter().all(|e| {
255        e.field_index.iter().all(|(name, &idx)| {
256            let b = &e.columns[idx];
257            match base.get(name.as_str()) {
258                Some(key) => *key == b.variant_key(),
259                None => true,
260            }
261        })
262    })
263}