Skip to main content

uqa_execution/
window.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! SQL window function evaluation. Window calls that partition and order their rows the same way share one sort, as the functions of one `WindowAgg` do, and each sorted partition is evaluated row by row with the frame semantics of `nodeWindowAgg.c`.
8
9use crate::scalar::plan::{PlanSubqueryArena, QueryExpressionContext};
10use crate::spill::BufferedIndexedSpill;
11use crate::RowSchemaExecution;
12use crate::{
13    eval_scalar, Batch, ExternalSort, PhysicalOperator, PhysicalRow, RowSchema, ScalarEvalContext,
14    ScalarExpr, ScalarFrameBound, ScalarOrder, ScalarSubqueryRunner, ScalarWindowSpec, SortKey,
15    SpillBuffer, SpillScan, WindowExecutor,
16};
17use uqa_core::Value;
18use uqa_sql::ast::{ColumnType, FrameExclusion, FrameMode, NullsOrder};
19use uqa_sql::plan::ProjectionPlan;
20use uqa_sql::semantics::projection_columns;
21use uqa_sql::{SQLError, SQLParam};
22
23mod aggregates;
24mod frame;
25mod functions;
26mod partition;
27mod planning;
28
29pub use planning::expr_has_window;
30use planning::rewrite_window_expr;
31
32use frame::{BoundKind, CurrentRow, FrameSpec};
33use functions::{WindowFunction, WindowFunctionState};
34use partition::PartitionRows;
35
36#[derive(Clone)]
37struct WindowSlot {
38    column: uqa_sql::ast::InternalColumnRef,
39    name: String,
40    args: Vec<ScalarExpr>,
41    spec: ScalarWindowSpec,
42    /// An aggregate's `FILTER (WHERE ...)` condition.
43    filter: Option<ScalarExpr>,
44}
45
46impl WindowSlot {
47    fn call(&self) -> ScalarExpr {
48        ScalarExpr::WindowCall {
49            name: self.name.clone(),
50            args: self.args.clone(),
51            spec: self.spec.clone(),
52            filter: self.filter.clone().map(Box::new),
53            modifiers: uqa_sql::ast::WindowCallModifiers::default(),
54        }
55    }
56}
57
58/// Window calls that partition and order their rows alike, evaluated over one sort.
59struct WindowPass {
60    partition_by: Vec<ScalarExpr>,
61    order_by: Vec<ScalarOrder>,
62    slots: Vec<WindowSlot>,
63}
64
65impl WindowPass {
66    fn sort_keys(&self) -> Vec<SortKey> {
67        let mut keys = self
68            .partition_by
69            .iter()
70            .cloned()
71            .map(|expr| SortKey {
72                expr,
73                descending: false,
74                nulls_first: None,
75            })
76            .collect::<Vec<_>>();
77        keys.extend(self.order_by.iter().map(|order| SortKey {
78            expr: order.expr.clone(),
79            descending: order.descending,
80            nulls_first: order.nulls.map(|nulls| matches!(nulls, NullsOrder::First)),
81        }));
82        keys
83    }
84}
85
86pub struct PreparedWindowPlan {
87    passes: Vec<WindowPass>,
88    projections: Vec<ProjectionPlan>,
89}
90
91impl PreparedWindowPlan {
92    pub fn projections(&self) -> &[ProjectionPlan] {
93        &self.projections
94    }
95
96    /// Move every partition or ordering key that `returns_set` classifies as set-returning into an internal column the input must supply, and return those keys in first-use order. A window's PARTITION BY and ORDER BY are processed like a target list, so `PostgreSQL` evaluates their set-returning calls in a projection below the window, which expands its input rows; identical keys share one column. Every target-list subexpression equal to a key then reads the key's column, as `set_plan_references` makes an upper target list read an equal lower output, so a call that the target list repeats is evaluated once instead of expanding the rows again.
97    pub fn take_set_returning_keys(
98        &mut self,
99        returns_set: &mut dyn FnMut(&ScalarExpr) -> Result<bool, SQLError>,
100    ) -> Result<Vec<(uqa_sql::ast::InternalColumnRef, ScalarExpr)>, SQLError> {
101        let relation = uqa_sql::ast::InternalRelationId::allocate();
102        let mut keys: Vec<(uqa_sql::ast::InternalColumnRef, ScalarExpr)> = Vec::new();
103        for pass in &mut self.passes {
104            let expressions = pass
105                .partition_by
106                .iter_mut()
107                .chain(pass.order_by.iter_mut().map(|order| &mut order.expr));
108            for expression in expressions {
109                if !returns_set(expression)? {
110                    continue;
111                }
112                let column = match keys.iter().find(|(_, key)| key == expression) {
113                    Some((column, _)) => *column,
114                    None => {
115                        let column = relation.column(keys.len());
116                        keys.push((column, expression.clone()));
117                        column
118                    }
119                };
120                *expression = ScalarExpr::InternalColumn(column);
121            }
122            for slot in &mut pass.slots {
123                slot.spec.partition_by.clone_from(&pass.partition_by);
124                slot.spec.order_by.clone_from(&pass.order_by);
125            }
126        }
127        for projection in &mut self.projections {
128            read_set_returning_keys(&mut projection.expr, &keys);
129        }
130        Ok(keys)
131    }
132
133    /// The input schema with one internal column per window call, in pass order.
134    pub fn output_schema(
135        &self,
136        context: &dyn uqa_sql::FunctionTypeResolver,
137        input: &RowSchema,
138        params: &[SQLParam],
139    ) -> Result<RowSchema, SQLError> {
140        let mut schema = input.clone();
141        for pass in &self.passes {
142            let types = slot_types(pass, context, &schema, params)?;
143            schema = pass_output_schema(&schema, &types);
144        }
145        Ok(schema)
146    }
147}
148
149/// Make every subexpression of `expression` equal to one of the set-returning window `keys` read that key's internal column. The visit is pre-order, so a repeated call is replaced as a whole before its arguments are visited.
150pub fn read_set_returning_keys(
151    expression: &mut ScalarExpr,
152    keys: &[(uqa_sql::ast::InternalColumnRef, ScalarExpr)],
153) {
154    if keys.is_empty() {
155        return;
156    }
157    expression.visit_mut(&mut |node| {
158        if let Some((column, _)) = keys.iter().find(|(_, key)| key == node) {
159            *node = ScalarExpr::InternalColumn(*column);
160        }
161    });
162}
163
164pub struct PhysicalWindowExecutor<'a> {
165    context: std::sync::Arc<dyn QueryExpressionContext + 'a>,
166    plan: PreparedWindowPlan,
167    params: &'a [SQLParam],
168
169    schema: RowSchema,
170    work_mem_bytes: usize,
171    input: Option<SpillBuffer>,
172}
173
174impl<'a> PhysicalWindowExecutor<'a> {
175    pub fn new(
176        context: std::sync::Arc<dyn QueryExpressionContext + 'a>,
177        plan: PreparedWindowPlan,
178        params: &'a [SQLParam],
179
180        schema: RowSchema,
181        work_mem_bytes: usize,
182    ) -> Self {
183        Self {
184            context,
185            plan,
186            params,
187
188            schema,
189            work_mem_bytes,
190            input: Some(SpillBuffer::new((work_mem_bytes / 3).max(1))),
191        }
192    }
193}
194
195impl WindowExecutor for PhysicalWindowExecutor<'_> {
196    fn consume(&mut self, batch: Batch) -> crate::ExecResult<()> {
197        self.input
198            .as_mut()
199            .ok_or_else(|| crate::ExecError::Other("window executor already finalized".into()))?
200            .push(batch)?;
201        Ok(())
202    }
203
204    fn finish(&mut self) -> crate::ExecResult<SpillBuffer> {
205        let mut input = self
206            .input
207            .take()
208            .ok_or_else(|| crate::ExecError::Other("window executor already finalized".into()))?;
209        let mut schema = self.schema.clone();
210        for pass in &self.plan.passes {
211            let types = slot_types(pass, self.context.as_ref(), &schema, self.params)?;
212            input = execute_window_pass(
213                self.context.as_ref(),
214                pass,
215                input,
216                &schema,
217                &types,
218                self.work_mem_bytes,
219                self.params,
220            )?;
221            schema = pass_output_schema(&schema, &types);
222        }
223        Ok(input)
224    }
225}
226
227/// Each pass sorts into a compact layout before appending its window values.
228/// Keep the declared layout and the next pass's input aligned with those rows.
229fn pass_output_schema(
230    input: &RowSchema,
231    types: &[(uqa_sql::ast::InternalColumnRef, Option<ColumnType>)],
232) -> RowSchema {
233    let (partition, _) = input.canonical_projection();
234    RowSchema::append_internal_typed(&partition, types)
235}
236
237pub fn has_window(projections: &[ProjectionPlan]) -> bool {
238    projections.iter().any(|p| expr_has_window(&p.expr))
239}
240
241pub fn prepare_window_plan(projections: &[ProjectionPlan]) -> PreparedWindowPlan {
242    let labels = projection_columns(projections);
243    let mut slots = Vec::new();
244    let mut rewritten = Vec::with_capacity(projections.len());
245    for (idx, projection) in projections.iter().enumerate() {
246        let (expr, changed) = rewrite_window_expr(&projection.expr, &mut slots);
247        let mut projection = projection.clone();
248        projection.expr = expr;
249        if changed && projection.alias.is_none() {
250            projection.alias = Some(labels[idx].clone());
251        }
252        rewritten.push(projection);
253    }
254    let mut passes: Vec<WindowPass> = Vec::new();
255    for slot in slots {
256        match passes.iter_mut().find(|pass| {
257            pass.partition_by == slot.spec.partition_by && pass.order_by == slot.spec.order_by
258        }) {
259            Some(pass) => pass.slots.push(slot),
260            None => passes.push(WindowPass {
261                partition_by: slot.spec.partition_by.clone(),
262                order_by: slot.spec.order_by.clone(),
263                slots: vec![slot],
264            }),
265        }
266    }
267    PreparedWindowPlan {
268        passes,
269        projections: rewritten,
270    }
271}
272
273fn slot_types(
274    pass: &WindowPass,
275    context: &dyn uqa_sql::FunctionTypeResolver,
276    schema: &RowSchema,
277    params: &[SQLParam],
278) -> Result<Vec<(uqa_sql::ast::InternalColumnRef, Option<ColumnType>)>, SQLError> {
279    pass.slots
280        .iter()
281        .map(|slot| {
282            crate::scalar_type_with_resolver(&slot.call(), schema, params, context)
283                .map(|ty| (slot.column, ty))
284        })
285        .collect()
286}
287
288/// One pass's window calls prepared for evaluation, with the frame offsets each still has to evaluate.
289struct PreparedSlot {
290    state: WindowFunctionState,
291    start_offset: Option<FrameOffset>,
292    end_offset: Option<FrameOffset>,
293}
294
295/// A frame offset expression and the type `transformFrameOffset` coerces it to.
296struct FrameOffset {
297    expression: ScalarExpr,
298    target: ColumnType,
299}
300
301fn execute_window_pass(
302    context: &dyn QueryExpressionContext,
303    pass: &WindowPass,
304    input: SpillBuffer,
305    schema: &RowSchema,
306    types: &[(uqa_sql::ast::InternalColumnRef, Option<ColumnType>)],
307    work_mem_bytes: usize,
308    params: &[SQLParam],
309) -> Result<SpillBuffer, SQLError> {
310    let phase_budget = (work_mem_bytes / 3).max(1);
311    let mut slots = pass
312        .slots
313        .iter()
314        .map(|slot| prepare_slot(context, slot, schema, params, phase_budget))
315        .collect::<Result<Vec<_>, _>>()?;
316    let scan: Box<dyn PhysicalOperator + '_> = Box::new(SpillScan::new(schema.clone(), input));
317    let keys = pass.sort_keys();
318    let evaluator = context.expression_evaluator(params);
319    let key_types = keys
320        .iter()
321        .map(|key| evaluator.expression_type(&key.expr, schema))
322        .collect::<Result<Vec<_>, _>>()?;
323    let (partition_types, order_types) = key_types.split_at(pass.partition_by.len());
324    let mut sorted = ExternalSort::new(scan, keys, evaluator, None, phase_budget);
325    sorted.open().map_err(exec_to_sql_error)?;
326
327    let hook = context;
328    let subquery_arena = PlanSubqueryArena::new(context.subquery_plans(), Some(hook));
329    let partition_schema = sorted.row_schema().clone();
330    let output_schema = RowSchema::append_internal_typed(&partition_schema, types);
331    let mut output = SpillBuffer::new(phase_budget);
332    let mut offsets_evaluated = false;
333
334    let execution = (|| -> Result<(), SQLError> {
335        let mut partition = BufferedIndexedSpill::new(partition_schema.clone(), phase_budget);
336        let mut partition_key: Option<Vec<Value>> = None;
337        while let Some(batch) = sorted.next().map_err(exec_to_sql_error)? {
338            for row in batch.rows {
339                let key = evaluate_all(
340                    &pass.partition_by,
341                    &batch.schema,
342                    &row,
343                    params,
344                    hook,
345                    &subquery_arena,
346                )?;
347                let same_partition = match &partition_key {
348                    Some(current) => crate::relational::equal_typed_sort_key_values(
349                        current,
350                        &key,
351                        partition_types,
352                        uqa_sql::expr::EngineHook::enum_labels(hook),
353                    )
354                    .map_err(exec_to_sql_error)?,
355                    None => true,
356                };
357                if !same_partition {
358                    emit_partition(
359                        pass,
360                        order_types,
361                        &mut slots,
362                        &mut partition,
363                        (&output_schema, &mut output),
364                        (params, hook, &subquery_arena),
365                        &mut offsets_evaluated,
366                    )?;
367                    partition = BufferedIndexedSpill::new(partition_schema.clone(), phase_budget);
368                }
369                partition_key = Some(key);
370                partition.push(&row).map_err(exec_to_sql_error)?;
371            }
372        }
373        if partition.is_empty() {
374            // `calculate_frame_offsets` runs before the first input row is read, so an invalid constant offset is reported even when there are no rows.
375            if !offsets_evaluated {
376                let empty = RowSchema::default();
377                let row = PhysicalRow::default();
378                let view = empty.view(&row);
379                let evaluation = ScalarEvalContext::from_row_lookup(&view, params)
380                    .with_function_hook(hook)
381                    .with_subquery_runner(&subquery_arena);
382                for slot in &mut slots {
383                    if slot.offsets_reference_rows() {
384                        continue;
385                    }
386                    slot.evaluate_offsets(&evaluation)?;
387                }
388            }
389        } else {
390            emit_partition(
391                pass,
392                order_types,
393                &mut slots,
394                &mut partition,
395                (&output_schema, &mut output),
396                (params, hook, &subquery_arena),
397                &mut offsets_evaluated,
398            )?;
399        }
400        Ok(())
401    })();
402    let close = sorted.close().map_err(exec_to_sql_error);
403    combine_execution_and_close(execution, close, "window sort")?;
404    Ok(output)
405}
406
407fn prepare_slot(
408    context: &dyn QueryExpressionContext,
409    slot: &WindowSlot,
410    schema: &RowSchema,
411    params: &[SQLParam],
412    budget_bytes: usize,
413) -> Result<PreparedSlot, SQLError> {
414    let spec = &slot.spec;
415    let (mode, start, end, exclusion) = spec.frame.as_ref().map_or(
416        // Without a frame clause the frame is RANGE UNBOUNDED PRECEDING, which ends with the current row's last peer.
417        (
418            FrameMode::Range,
419            BoundKind::UnboundedPreceding,
420            BoundKind::CurrentRow,
421            FrameExclusion::NoOthers,
422        ),
423        |frame| {
424            (
425                frame.mode,
426                bound_kind(&frame.start),
427                bound_kind(&frame.end),
428                frame.exclusion,
429            )
430        },
431    );
432    let (ascending, nulls_first) = spec.order_by.first().map_or((true, false), |order| {
433        (
434            !order.descending,
435            order
436                .nulls
437                .map_or(order.descending, |nulls| nulls == NullsOrder::First),
438        )
439    });
440    let offset = |bound: Option<&ScalarFrameBound>| -> Result<Option<FrameOffset>, SQLError> {
441        let Some(ScalarFrameBound::Preceding(expression) | ScalarFrameBound::Following(expression)) =
442            bound
443        else {
444            return Ok(None);
445        };
446        let target = if mode == FrameMode::Range {
447            let order = spec.order_by.first().ok_or_else(|| {
448                SQLError::Internal("RANGE frame offset has no ordering column".into())
449            })?;
450            let order_type =
451                crate::scalar_type_with_resolver(&order.expr, schema, params, context)?;
452            let offset_type =
453                crate::scalar_type_with_resolver(expression, schema, params, context)?;
454            let offset_type = uqa_sql::effective_overload_argument_type_with_params(
455                expression,
456                offset_type,
457                params,
458            );
459            uqa_sql::range_frame_offset_type(order_type.as_ref(), offset_type.as_ref())?
460        } else {
461            ColumnType::BigInteger
462        };
463        Ok(Some(FrameOffset {
464            expression: (**expression).clone(),
465            target,
466        }))
467    };
468    let start_offset = offset(spec.frame.as_ref().map(|frame| &frame.start))?;
469    let end_offset = offset(spec.frame.as_ref().map(|frame| &frame.end))?;
470    let frame = FrameSpec {
471        mode,
472        start,
473        end,
474        exclusion,
475        start_offset: None,
476        end_offset: None,
477        ascending,
478        nulls_first,
479    };
480    let function = window_function(context, slot, schema, params, budget_bytes)?;
481    Ok(PreparedSlot {
482        state: WindowFunctionState::new(function, frame),
483        start_offset,
484        end_offset,
485    })
486}
487
488const fn bound_kind(bound: &ScalarFrameBound) -> BoundKind {
489    match bound {
490        ScalarFrameBound::UnboundedPreceding => BoundKind::UnboundedPreceding,
491        ScalarFrameBound::UnboundedFollowing => BoundKind::UnboundedFollowing,
492        ScalarFrameBound::CurrentRow => BoundKind::CurrentRow,
493        ScalarFrameBound::Preceding(_) => BoundKind::Preceding,
494        ScalarFrameBound::Following(_) => BoundKind::Following,
495    }
496}
497
498fn window_function(
499    context: &dyn QueryExpressionContext,
500    slot: &WindowSlot,
501    schema: &RowSchema,
502    params: &[SQLParam],
503    budget_bytes: usize,
504) -> Result<WindowFunction, SQLError> {
505    let name = uqa_sql::semantics::builtin_function_dispatch_name(&slot.name);
506    let bind = |expression: &ScalarExpr| {
507        crate::bind_type_introspection_with_resolver(expression.clone(), schema, params, context)
508    };
509    let argument = |position: usize| {
510        slot.args
511            .get(position)
512            .map(|expression| crate::scalar::PreparedExpressions::scalar(bind(expression)))
513    };
514    let required = |position: usize| {
515        argument(position).ok_or_else(|| SQLError::BadArity {
516            name: name.clone(),
517            expected: format!(">={}", position + 1),
518            actual: slot.args.len(),
519        })
520    };
521    Ok(match name.as_str() {
522        "row_number" => WindowFunction::RowNumber,
523        "rank" => WindowFunction::Rank,
524        "dense_rank" => WindowFunction::DenseRank,
525        "percent_rank" => WindowFunction::PercentRank,
526        "cume_dist" => WindowFunction::CumeDist,
527        "ntile" => WindowFunction::Ntile(required(0)?),
528        "lag" | "lead" => WindowFunction::Shift(Box::new(functions::Shift {
529            forward: name == "lead",
530            target: required(0)?,
531            offset: argument(1),
532            default: argument(2),
533        })),
534        "first_value" => WindowFunction::FirstValue(required(0)?),
535        "last_value" => WindowFunction::LastValue(required(0)?),
536        "nth_value" => WindowFunction::NthValue {
537            target: required(0)?,
538            position: required(1)?,
539        },
540        _ => {
541            let call = ScalarExpr::Func {
542                order_syntax: uqa_sql::ast::FunctionOrderSyntax::Ordinary,
543                name: slot.name.clone(),
544                binding: None,
545                args: slot.args.iter().map(bind).collect(),
546                distinct: false,
547                order_by: Vec::new(),
548                filter: None,
549            };
550            let template = crate::aggregation::aggregate_accumulator_templates(
551                context,
552                std::slice::from_ref(&call),
553                schema,
554                params,
555            )?
556            .pop()
557            .ok_or_else(|| SQLError::Internal("window aggregate lost its accumulator".into()))?;
558            let ScalarExpr::Func { args, .. } = call else {
559                unreachable!()
560            };
561            let filter = slot.filter.as_ref().map(bind);
562            WindowFunction::Aggregate(Box::new(aggregates::WindowAggregate::new(
563                (&slot.name, &args, filter.as_ref()),
564                template,
565                budget_bytes,
566            )))
567        }
568    })
569}
570
571impl PreparedSlot {
572    fn offsets_reference_rows(&self) -> bool {
573        [&self.start_offset, &self.end_offset]
574            .into_iter()
575            .flatten()
576            .any(|offset| {
577                let mut found = false;
578                offset.expression.visit(&mut |part| {
579                    found |= matches!(
580                        part,
581                        ScalarExpr::Column(_)
582                            | ScalarExpr::QualifiedColumn { .. }
583                            | ScalarExpr::Position(_)
584                            | ScalarExpr::InternalColumn(_)
585                    );
586                });
587                found
588            })
589    }
590
591    /// `calculate_frame_offsets`: each offset is evaluated once, must not be NULL, and a `ROWS` or `GROUPS` count must not be negative.
592    fn evaluate_offsets(&mut self, context: &ScalarEvalContext<'_>) -> Result<(), SQLError> {
593        let mode = self.state.frame_mut().mode;
594        let start = self
595            .start_offset
596            .as_ref()
597            .map(|offset| frame_offset_value(offset, mode, "starting", context))
598            .transpose()?;
599        let end = self
600            .end_offset
601            .as_ref()
602            .map(|offset| frame_offset_value(offset, mode, "ending", context))
603            .transpose()?;
604        let frame = self.state.frame_mut();
605        frame.start_offset = start;
606        frame.end_offset = end;
607        Ok(())
608    }
609}
610
611fn frame_offset_value(
612    offset: &FrameOffset,
613    mode: FrameMode,
614    which: &str,
615    context: &ScalarEvalContext<'_>,
616) -> Result<Value, SQLError> {
617    let value = eval_scalar(&offset.expression, context)?;
618    if matches!(value, Value::Null) {
619        return Err(SQLError::Routine {
620            sqlstate: "22004".into(),
621            message: format!("frame {which} offset must not be null"),
622        });
623    }
624    let value = uqa_sql::expr::cast_value(&value, &offset.target.sql_name())?;
625    if mode != FrameMode::Range && matches!(value, Value::Int(count) if count < 0) {
626        return Err(SQLError::Routine {
627            sqlstate: "22013".into(),
628            message: format!("frame {which} offset must not be negative"),
629        });
630    }
631    Ok(value)
632}
633
634/// Evaluate every window call of a pass for each row of one sorted partition and append the results to the row.
635fn emit_partition(
636    pass: &WindowPass,
637    order_types: &[Option<ColumnType>],
638    slots: &mut [PreparedSlot],
639    partition: &mut BufferedIndexedSpill,
640    (schema, output): (&RowSchema, &mut SpillBuffer),
641    (params, hook, subqueries): (
642        &[SQLParam],
643        &dyn uqa_sql::expr::EngineHook,
644        &dyn ScalarSubqueryRunner,
645    ),
646    offsets_evaluated: &mut bool,
647) -> Result<(), SQLError> {
648    let mut rows = PartitionRows::new(
649        partition,
650        &pass.order_by,
651        order_types,
652        params,
653        hook,
654        subqueries,
655    )?;
656    if rows.len() == 0 {
657        return Ok(());
658    }
659    if !*offsets_evaluated {
660        // Offsets may use an enclosing query's columns, which every row of the input carries.
661        for slot in slots.iter_mut() {
662            rows.with_context(0, |context| slot.evaluate_offsets(context))?;
663        }
664        *offsets_evaluated = true;
665    }
666    for slot in slots.iter_mut() {
667        slot.state.begin_partition();
668    }
669    let mut current = CurrentRow::new();
670    let mut pending = Vec::with_capacity(crate::batch::DEFAULT_BATCH_SIZE);
671    for position in 0..rows.len() {
672        if position > 0 {
673            for slot in slots.iter_mut() {
674                slot.state.advance();
675            }
676            current.advance(&mut rows)?;
677        }
678        let values = slots
679            .iter_mut()
680            .map(|slot| slot.state.value(&mut current, &mut rows))
681            .collect::<Result<Vec<_>, _>>()?;
682        pending.push(rows.row(position)?.append_values(values));
683        if pending.len() == crate::batch::DEFAULT_BATCH_SIZE {
684            output
685                .push(Batch::from_physical_rows(
686                    schema.clone(),
687                    std::mem::take(&mut pending),
688                ))
689                .map_err(exec_to_sql_error)?;
690            pending = Vec::with_capacity(crate::batch::DEFAULT_BATCH_SIZE);
691        }
692    }
693    if !pending.is_empty() {
694        output
695            .push(Batch::from_physical_rows(schema.clone(), pending))
696            .map_err(exec_to_sql_error)?;
697    }
698    Ok(())
699}
700
701fn evaluate_all(
702    expressions: &[ScalarExpr],
703    schema: &RowSchema,
704    row: &PhysicalRow,
705    params: &[SQLParam],
706    hook: &dyn uqa_sql::expr::EngineHook,
707    subqueries: &dyn ScalarSubqueryRunner,
708) -> Result<Vec<Value>, SQLError> {
709    expressions
710        .iter()
711        .map(|expression| evaluate_on_row(expression, schema, row, params, hook, subqueries))
712        .collect()
713}
714
715fn evaluate_on_row(
716    expression: &ScalarExpr,
717    schema: &RowSchema,
718    row: &PhysicalRow,
719    params: &[SQLParam],
720    eval_hook: &dyn uqa_sql::expr::EngineHook,
721    subquery_runner: &dyn ScalarSubqueryRunner,
722) -> Result<Value, SQLError> {
723    let view = schema.view(row);
724    let context = ScalarEvalContext::from_row_lookup(&view, params)
725        .with_function_hook(eval_hook)
726        .with_subquery_runner(subquery_runner)
727        .with_physical_outer_row(schema, row);
728    eval_scalar(expression, &context)
729}
730
731fn combine_execution_and_close(
732    execution: Result<(), SQLError>,
733    close: Result<(), SQLError>,
734    operator: &str,
735) -> Result<(), SQLError> {
736    match (execution, close) {
737        (Ok(()), Ok(())) => Ok(()),
738        (Ok(()), Err(error)) | (Err(error), Ok(())) => Err(error),
739        (Err(execution_error), Err(close_error)) => Err(SQLError::Internal(format!(
740            "{execution_error}; closing {operator} after failure also failed: {close_error}"
741        ))),
742    }
743}
744
745fn exec_to_sql_error(error: crate::ExecError) -> SQLError {
746    match error {
747        crate::ExecError::SQL(error) => error,
748        crate::ExecError::Other(message) => SQLError::Internal(message),
749    }
750}
751
752#[cfg(test)]
753mod tests;