1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Auto engine selection: heuristic for picking the best execution mode.
/// The execution mode selected by the auto heuristic.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EngineMode {
/// Single-threaded, full table in RAM.
Columnar,
/// Multi-core parallel, full table in RAM.
Parallel,
/// Single-threaded streaming, bounded memory.
Stream,
/// Multi-core parallel streaming, bounded memory.
ParallelStreaming,
}
impl EngineMode {
/// Return the string representation used by Python adapters.
pub fn as_str(&self) -> &'static str {
match self {
EngineMode::Columnar => "columnar",
EngineMode::Parallel => "parallel",
EngineMode::Stream => "stream",
EngineMode::ParallelStreaming => "parallel_streaming",
}
}
}
/// Configuration for the auto engine selection heuristic.
pub struct AutoConfig {
/// Size of the input file in bytes.
pub file_size: u64,
/// Memory budget (e.g. 64 MiB). `None` = no budget (full table).
pub memory: Option<u64>,
/// Number of threads for parallel processing. `None` = 1 (single-threaded).
pub threads: Option<usize>,
/// Explicit schema (column names). `Some` = schema provided, `None` = discover.
pub schema: Option<Vec<String>>,
/// Whether the adapter has parallel support.
pub has_parallel: bool,
/// Whether the adapter has columnar support.
pub has_columnar: bool,
}
/// Resolve the best engine mode based on configuration.
///
/// The heuristic considers:
/// - `memory`: if provided, user wants streaming
/// - `threads`: if > 1, prefer parallel modes
/// - `schema`: if provided, streaming is 11% faster (no discovery overhead)
/// - `file_size`: small files use columnar, large files use parallel
pub fn resolve_engine(config: &AutoConfig) -> EngineMode {
let threads = config.threads.unwrap_or(1);
// 1. User explicitly requested streaming via memory=
if config.memory.is_some() {
if threads > 1 {
return EngineMode::ParallelStreaming;
}
return EngineMode::Stream;
}
// 2. User explicitly requested parallel via threads=
if threads > 1 {
// For parallel streaming, we need a memory budget.
// Without memory=, prefer parallel (full table) if it fits.
// If we can't determine available memory, use parallel streaming
// as the safer choice for large files.
if config.file_size >= 100 * 1024 * 1024 {
// Large file: prefer parallel streaming for bounded memory
return EngineMode::ParallelStreaming;
}
return EngineMode::Parallel;
}
// 3. Schema provided: prefer streaming for 11% boost on large files
if config.schema.is_some() && config.file_size >= 100 * 1024 * 1024 {
return EngineMode::Stream;
}
// 4. Default: file size based
if config.file_size < 8 * 1024 * 1024 {
// Small file: prefer columnar (no chunk overhead)
if config.has_columnar {
return EngineMode::Columnar;
}
return EngineMode::Stream;
}
// Large file: prefer parallel if available
if config.has_parallel {
return EngineMode::Parallel;
}
// Fallback
EngineMode::Stream
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memory_only() {
let config = AutoConfig {
file_size: 1_000_000_000,
memory: Some(64 * 1024 * 1024),
threads: None,
schema: None,
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::Stream);
}
#[test]
fn test_memory_and_threads() {
let config = AutoConfig {
file_size: 1_000_000_000,
memory: Some(64 * 1024 * 1024),
threads: Some(16),
schema: None,
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::ParallelStreaming);
}
#[test]
fn test_threads_only_large_file() {
let config = AutoConfig {
file_size: 1_000_000_000,
memory: None,
threads: Some(16),
schema: None,
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::ParallelStreaming);
}
#[test]
fn test_threads_only_small_file() {
let config = AutoConfig {
file_size: 50 * 1024 * 1024,
memory: None,
threads: Some(16),
schema: None,
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::Parallel);
}
#[test]
fn test_schema_large_file() {
let config = AutoConfig {
file_size: 500 * 1024 * 1024,
memory: None,
threads: None,
schema: Some(vec!["a".into(), "b".into()]),
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::Stream);
}
#[test]
fn test_small_file_columnar() {
let config = AutoConfig {
file_size: 5 * 1024 * 1024,
memory: None,
threads: None,
schema: None,
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::Columnar);
}
#[test]
fn test_small_file_no_columnar() {
let config = AutoConfig {
file_size: 5 * 1024 * 1024,
memory: None,
threads: None,
schema: None,
has_parallel: true,
has_columnar: false,
};
assert_eq!(resolve_engine(&config), EngineMode::Stream);
}
#[test]
fn test_medium_file_parallel() {
let config = AutoConfig {
file_size: 100 * 1024 * 1024,
memory: None,
threads: None,
schema: None,
has_parallel: true,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::Parallel);
}
#[test]
fn test_large_file_no_parallel() {
let config = AutoConfig {
file_size: 100 * 1024 * 1024,
memory: None,
threads: None,
schema: None,
has_parallel: false,
has_columnar: true,
};
assert_eq!(resolve_engine(&config), EngineMode::Stream);
}
}