1use crate::batch::{Batch, PhysicalRow, RowSchema, DEFAULT_BATCH_SIZE};
17use crate::physical::{BackwardScanSupport, ExecResult, PhysicalOperator, PhysicalOrder};
18use crate::RowSchemaExecution;
19use uqa_sql::ast::ColumnType;
20use uqa_sql::ResultRow;
21
22pub trait RowSource: Send {
27 fn schema(&self) -> &[String];
29
30 fn supports_batched_consumption(&self) -> bool {
32 false
33 }
34
35 fn physical_schema(&self) -> Option<&RowSchema> {
39 None
40 }
41
42 fn estimated_cardinality(&self) -> Option<u64> {
44 None
45 }
46
47 fn output_ordering(&self) -> &[PhysicalOrder] {
49 &[]
50 }
51
52 fn next_row(&mut self) -> ExecResult<Option<ResultRow>>;
54
55 fn next_batch(&mut self, max_rows: usize) -> ExecResult<Vec<ResultRow>> {
59 let mut rows = Vec::with_capacity(max_rows);
60 while rows.len() < max_rows {
61 match self.next_row()? {
62 Some(row) => rows.push(row),
63 None => break,
64 }
65 }
66 Ok(rows)
67 }
68
69 fn next_physical_batch(&mut self, max_rows: usize) -> ExecResult<Vec<PhysicalRow>> {
73 let schema = RowSchema::new(self.schema().to_vec());
74 self.next_batch(max_rows).map(|rows| {
75 rows.into_iter()
76 .map(|row| PhysicalRow::from_result_row(&schema, row))
77 .collect()
78 })
79 }
80
81 fn consume_into_aggregate(
85 &mut self,
86 _executor: &mut dyn crate::relational::AggregateExecutor,
87 ) -> ExecResult<bool> {
88 Ok(false)
89 }
90
91 fn collect_materialized(
93 &mut self,
94 _sink: &mut crate::query::output::MaterializedResultSink<'_>,
95 ) -> ExecResult<bool> {
96 Ok(false)
97 }
98}
99
100pub struct VecSource {
103 schema: Vec<String>,
104 physical_schema: RowSchema,
105 rows: std::vec::IntoIter<ResultRow>,
106}
107
108pub struct PhysicalVecSource {
110 schema: RowSchema,
111 rows: std::vec::IntoIter<PhysicalRow>,
112}
113
114pub struct RowIteratorScan<'a> {
118 schema: RowSchema,
119 rows: Box<dyn Iterator<Item = ExecResult<ResultRow>> + Send + 'a>,
120 exhausted: bool,
121}
122
123pub struct PhysicalRowIteratorScan<'a> {
127 schema: RowSchema,
128 rows: crate::SQLRowStream<'a, PhysicalRow>,
129 batch_size: std::num::NonZeroUsize,
130 exhausted: bool,
131}
132
133impl<'a> RowIteratorScan<'a> {
134 pub fn new(
135 schema: Vec<String>,
136 rows: Box<dyn Iterator<Item = ExecResult<ResultRow>> + Send + 'a>,
137 ) -> Self {
138 Self {
139 schema: RowSchema::new(schema),
140 rows,
141 exhausted: false,
142 }
143 }
144
145 pub fn with_types(
146 schema: Vec<String>,
147 types: Vec<Option<ColumnType>>,
148 rows: Box<dyn Iterator<Item = ExecResult<ResultRow>> + Send + 'a>,
149 ) -> Self {
150 Self {
151 schema: RowSchema::with_types(schema, types),
152 rows,
153 exhausted: false,
154 }
155 }
156
157 pub fn with_row_schema(
158 schema: RowSchema,
159 rows: Box<dyn Iterator<Item = ExecResult<ResultRow>> + Send + 'a>,
160 ) -> Self {
161 Self {
162 schema,
163 rows,
164 exhausted: false,
165 }
166 }
167}
168
169impl PhysicalOperator for RowIteratorScan<'_> {
170 fn row_schema(&self) -> &RowSchema {
171 &self.schema
172 }
173
174 fn supports_batched_consumption(&self) -> bool {
175 true
176 }
177
178 fn open(&mut self) -> ExecResult<()> {
179 self.exhausted = false;
180 Ok(())
181 }
182
183 fn next(&mut self) -> ExecResult<Option<Batch>> {
184 self.next_with_demand(
185 std::num::NonZeroUsize::new(DEFAULT_BATCH_SIZE).expect("nonempty scan batch"),
186 )
187 }
188
189 fn next_with_demand(&mut self, demand: std::num::NonZeroUsize) -> ExecResult<Option<Batch>> {
190 if self.exhausted {
191 return Ok(None);
192 }
193 let mut batch = Vec::with_capacity(DEFAULT_BATCH_SIZE.min(demand.get()));
194 while batch.len() < DEFAULT_BATCH_SIZE.min(demand.get()) {
195 match self.rows.next() {
196 Some(Ok(row)) => batch.push(row),
197 Some(Err(error)) => return Err(error),
198 None => {
199 self.exhausted = true;
200 break;
201 }
202 }
203 }
204 if batch.is_empty() {
205 Ok(None)
206 } else {
207 Ok(Some(Batch::new(self.schema.clone(), batch)))
208 }
209 }
210
211 fn close(&mut self) -> ExecResult<()> {
212 self.exhausted = true;
213 Ok(())
214 }
215}
216
217impl<'a> PhysicalRowIteratorScan<'a> {
218 pub fn new(
219 schema: RowSchema,
220 rows: Box<dyn Iterator<Item = ExecResult<PhysicalRow>> + Send + 'a>,
221 ) -> Self {
222 Self::from_stream(
223 schema,
224 crate::SQLRowStream::new(
225 rows.map(|row| row.map_err(crate::physical::physical_exec_error)),
226 ),
227 )
228 }
229
230 pub fn from_stream(schema: RowSchema, rows: crate::SQLRowStream<'a, PhysicalRow>) -> Self {
231 Self {
232 schema,
233 rows,
234 batch_size: std::num::NonZeroUsize::new(DEFAULT_BATCH_SIZE)
235 .expect("default scan batch is nonempty"),
236 exhausted: false,
237 }
238 }
239
240 pub fn with_batch_size(mut self, size: std::num::NonZeroUsize) -> Self {
242 self.batch_size = size;
243 self
244 }
245}
246
247impl PhysicalOperator for PhysicalRowIteratorScan<'_> {
248 fn row_schema(&self) -> &RowSchema {
249 &self.schema
250 }
251
252 fn supports_batched_consumption(&self) -> bool {
253 true
254 }
255
256 fn backward_scan_support(&self) -> BackwardScanSupport {
257 BackwardScanSupport::Materialize
258 }
259
260 fn open(&mut self) -> ExecResult<()> {
261 self.exhausted = false;
262 Ok(())
263 }
264
265 fn next(&mut self) -> ExecResult<Option<Batch>> {
266 self.next_with_demand(
267 std::num::NonZeroUsize::new(self.batch_size.get()).expect("nonempty scan batch"),
268 )
269 }
270
271 fn next_with_demand(&mut self, demand: std::num::NonZeroUsize) -> ExecResult<Option<Batch>> {
272 if self.exhausted {
273 return Ok(None);
274 }
275 let mut batch = Vec::with_capacity(self.batch_size.get().min(demand.get()));
276 while batch.len() < self.batch_size.get().min(demand.get()) {
277 match self.rows.next() {
278 Some(Ok(row)) => batch.push(row),
279 Some(Err(error)) => return Err(error.into()),
280 None => {
281 self.exhausted = true;
282 break;
283 }
284 }
285 }
286 if batch.is_empty() {
287 Ok(None)
288 } else {
289 Ok(Some(Batch::from_physical_rows(self.schema.clone(), batch)))
290 }
291 }
292
293 fn close(&mut self) -> ExecResult<()> {
294 self.exhausted = true;
295 self.rows.close().map_err(Into::into)
296 }
297}
298
299impl VecSource {
300 pub fn new(schema: Vec<String>, rows: Vec<ResultRow>) -> Self {
301 let physical_schema = RowSchema::from_named_columns(schema.clone());
302 Self {
303 schema,
304 physical_schema,
305 rows: rows.into_iter(),
306 }
307 }
308
309 pub fn with_row_schema(physical_schema: RowSchema, rows: Vec<ResultRow>) -> Self {
310 Self {
311 schema: physical_schema.columns().to_vec(),
312 physical_schema,
313 rows: rows.into_iter(),
314 }
315 }
316
317 pub fn with_types(
318 schema: Vec<String>,
319 types: Vec<Option<ColumnType>>,
320 rows: Vec<ResultRow>,
321 ) -> Self {
322 let physical_schema = RowSchema::with_types(schema.clone(), types);
323 Self {
324 schema,
325 physical_schema,
326 rows: rows.into_iter(),
327 }
328 }
329}
330
331impl RowSource for VecSource {
332 fn schema(&self) -> &[String] {
333 &self.schema
334 }
335
336 fn supports_batched_consumption(&self) -> bool {
337 true
338 }
339
340 fn physical_schema(&self) -> Option<&RowSchema> {
341 Some(&self.physical_schema)
342 }
343
344 fn estimated_cardinality(&self) -> Option<u64> {
345 u64::try_from(self.rows.len()).ok()
346 }
347
348 fn next_row(&mut self) -> ExecResult<Option<ResultRow>> {
349 Ok(self.rows.next())
350 }
351}
352
353impl PhysicalVecSource {
354 pub fn new(schema: RowSchema, rows: Vec<PhysicalRow>) -> Self {
355 Self {
356 schema,
357 rows: rows.into_iter(),
358 }
359 }
360}
361
362impl RowSource for PhysicalVecSource {
363 fn schema(&self) -> &[String] {
364 self.schema.columns()
365 }
366
367 fn supports_batched_consumption(&self) -> bool {
368 true
369 }
370
371 fn physical_schema(&self) -> Option<&RowSchema> {
372 Some(&self.schema)
373 }
374
375 fn estimated_cardinality(&self) -> Option<u64> {
376 u64::try_from(self.rows.len()).ok()
377 }
378
379 fn next_row(&mut self) -> ExecResult<Option<ResultRow>> {
380 Ok(self
381 .rows
382 .next()
383 .map(|row| self.schema.view(&row).to_result_row()))
384 }
385
386 fn next_physical_batch(&mut self, max_rows: usize) -> ExecResult<Vec<PhysicalRow>> {
387 Ok(self.rows.by_ref().take(max_rows).collect())
388 }
389}
390
391pub struct TableScan {
396 source: Option<Box<dyn RowSource>>,
397 schema: RowSchema,
398 ordering: Vec<PhysicalOrder>,
399 estimated_cardinality: Option<u64>,
400 exhausted: bool,
401}
402
403impl TableScan {
404 pub fn new(source: Box<dyn RowSource>) -> Self {
405 let schema = source
406 .physical_schema()
407 .cloned()
408 .unwrap_or_else(|| RowSchema::new(source.schema().to_vec()));
409 let ordering = source.output_ordering().to_vec();
410 let estimated_cardinality = source.estimated_cardinality();
411 Self {
412 source: Some(source),
413 schema,
414 ordering,
415 estimated_cardinality,
416 exhausted: false,
417 }
418 }
419
420 pub fn from_rows(schema: Vec<String>, rows: Vec<ResultRow>) -> Self {
421 Self::new(Box::new(VecSource::new(schema, rows)))
422 }
423
424 pub fn from_typed_rows(
425 schema: Vec<String>,
426 types: Vec<Option<ColumnType>>,
427 rows: Vec<ResultRow>,
428 ) -> Self {
429 Self::new(Box::new(VecSource::with_types(schema, types, rows)))
430 }
431
432 pub fn from_rows_with_schema(schema: RowSchema, rows: Vec<ResultRow>) -> Self {
433 Self::new(Box::new(VecSource::with_row_schema(schema, rows)))
434 }
435
436 pub fn from_physical_rows(schema: RowSchema, rows: Vec<PhysicalRow>) -> Self {
437 Self::new(Box::new(PhysicalVecSource::new(schema, rows)))
438 }
439}
440
441impl PhysicalOperator for TableScan {
442 fn row_schema(&self) -> &RowSchema {
443 &self.schema
444 }
445
446 fn supports_batched_consumption(&self) -> bool {
447 self.source
448 .as_ref()
449 .is_some_and(|source| source.supports_batched_consumption())
450 }
451
452 fn estimated_cardinality(&self) -> Option<u64> {
453 self.estimated_cardinality
454 }
455
456 fn output_ordering(&self) -> &[PhysicalOrder] {
457 &self.ordering
458 }
459
460 fn backward_scan_support(&self) -> BackwardScanSupport {
461 BackwardScanSupport::Materialize
462 }
463
464 fn consume_into_aggregate(
465 &mut self,
466 executor: &mut dyn crate::relational::AggregateExecutor,
467 ) -> ExecResult<bool> {
468 let Some(source) = self.source.as_mut() else {
469 return Ok(false);
470 };
471 source.consume_into_aggregate(executor)
472 }
473
474 fn collect_materialized(
475 &mut self,
476 sink: &mut crate::query::output::MaterializedResultSink<'_>,
477 ) -> ExecResult<bool> {
478 let Some(source) = self.source.as_mut() else {
479 return Ok(false);
480 };
481 let complete = source.collect_materialized(sink)?;
482 self.exhausted |= complete;
483 Ok(complete)
484 }
485
486 fn open(&mut self) -> ExecResult<()> {
487 self.exhausted = false;
488 Ok(())
489 }
490
491 fn next(&mut self) -> ExecResult<Option<Batch>> {
492 self.next_with_demand(
493 std::num::NonZeroUsize::new(DEFAULT_BATCH_SIZE).expect("nonempty scan batch"),
494 )
495 }
496
497 fn next_with_demand(&mut self, demand: std::num::NonZeroUsize) -> ExecResult<Option<Batch>> {
498 if self.exhausted {
499 return Ok(None);
500 }
501 let Some(src) = self.source.as_mut() else {
502 return Ok(None);
503 };
504 let buf = src.next_physical_batch(DEFAULT_BATCH_SIZE.min(demand.get()))?;
505 if buf.is_empty() {
506 self.exhausted = true;
507 return Ok(None);
508 }
509 Ok(Some(Batch::from_physical_rows(self.schema.clone(), buf)))
510 }
511
512 fn close(&mut self) -> ExecResult<()> {
513 self.source = None;
514 self.exhausted = true;
515 Ok(())
516 }
517}
518
519#[cfg(test)]
520mod tests {
521 use super::*;
522 use crate::physical::run_to_rows;
523 use uqa_core::Value;
524
525 fn row<const N: usize>(pairs: [(&str, Value); N]) -> ResultRow {
526 pairs.into_iter().map(|(k, v)| (k.to_string(), v)).collect()
527 }
528
529 #[test]
530 fn table_scan_drains_source() {
531 let rows = vec![
532 row([("id", Value::Int(1)), ("name", Value::Str("a".into()))]),
533 row([("id", Value::Int(2)), ("name", Value::Str("b".into()))]),
534 ];
535 let mut scan = TableScan::from_rows(vec!["id".into(), "name".into()], rows);
536 let (cols, rows) = run_to_rows(&mut scan).unwrap();
537 assert_eq!(cols, vec!["id", "name"]);
538 assert_eq!(rows.len(), 2);
539 }
540
541 #[test]
542 fn table_scan_empty_source_returns_no_batch() {
543 let mut scan = TableScan::from_rows(vec!["id".into()], Vec::new());
544 let (_cols, rows) = run_to_rows(&mut scan).unwrap();
545 assert_eq!(rows.len(), 0);
546 }
547
548 #[test]
549 fn iterator_scan_propagates_a_late_producer_error() {
550 let rows = vec![
551 Ok(row([("id", Value::Int(1))])),
552 Err(crate::ExecError::Other("late producer failure".into())),
553 ];
554 let mut scan = RowIteratorScan::new(vec!["id".into()], Box::new(rows.into_iter()));
555 let error = run_to_rows(&mut scan).unwrap_err();
556 assert!(error.to_string().contains("late producer failure"));
557 }
558}