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 mut close_states = Ok(());
403    for slot in &mut slots {
404        close_states = crate::aggregation::state::with_cleanup(close_states, slot.state.close());
405    }
406    let execution = crate::aggregation::state::with_cleanup(execution, close_states);
407    let close = sorted.close().map_err(exec_to_sql_error);
408    combine_execution_and_close(execution, close, "window sort")?;
409    Ok(output)
410}
411
412fn prepare_slot(
413    context: &dyn QueryExpressionContext,
414    slot: &WindowSlot,
415    schema: &RowSchema,
416    params: &[SQLParam],
417    budget_bytes: usize,
418) -> Result<PreparedSlot, SQLError> {
419    let spec = &slot.spec;
420    let (mode, start, end, exclusion) = spec.frame.as_ref().map_or(
421        // Without a frame clause the frame is RANGE UNBOUNDED PRECEDING, which ends with the current row's last peer.
422        (
423            FrameMode::Range,
424            BoundKind::UnboundedPreceding,
425            BoundKind::CurrentRow,
426            FrameExclusion::NoOthers,
427        ),
428        |frame| {
429            (
430                frame.mode,
431                bound_kind(&frame.start),
432                bound_kind(&frame.end),
433                frame.exclusion,
434            )
435        },
436    );
437    let (ascending, nulls_first) = spec.order_by.first().map_or((true, false), |order| {
438        (
439            !order.descending,
440            order
441                .nulls
442                .map_or(order.descending, |nulls| nulls == NullsOrder::First),
443        )
444    });
445    let offset = |bound: Option<&ScalarFrameBound>| -> Result<Option<FrameOffset>, SQLError> {
446        let Some(ScalarFrameBound::Preceding(expression) | ScalarFrameBound::Following(expression)) =
447            bound
448        else {
449            return Ok(None);
450        };
451        let target = if mode == FrameMode::Range {
452            let order = spec.order_by.first().ok_or_else(|| {
453                SQLError::Internal("RANGE frame offset has no ordering column".into())
454            })?;
455            let order_type =
456                crate::scalar_type_with_resolver(&order.expr, schema, params, context)?;
457            let offset_type =
458                crate::scalar_type_with_resolver(expression, schema, params, context)?;
459            let offset_type = uqa_sql::effective_overload_argument_type_with_params(
460                expression,
461                offset_type,
462                params,
463            );
464            uqa_sql::range_frame_offset_type(order_type.as_ref(), offset_type.as_ref())?
465        } else {
466            ColumnType::BigInteger
467        };
468        Ok(Some(FrameOffset {
469            expression: (**expression).clone(),
470            target,
471        }))
472    };
473    let start_offset = offset(spec.frame.as_ref().map(|frame| &frame.start))?;
474    let end_offset = offset(spec.frame.as_ref().map(|frame| &frame.end))?;
475    let frame = FrameSpec {
476        mode,
477        start,
478        end,
479        exclusion,
480        start_offset: None,
481        end_offset: None,
482        ascending,
483        nulls_first,
484    };
485    let function = window_function(context, slot, schema, params, budget_bytes)?;
486    Ok(PreparedSlot {
487        state: WindowFunctionState::new(function, frame),
488        start_offset,
489        end_offset,
490    })
491}
492
493const fn bound_kind(bound: &ScalarFrameBound) -> BoundKind {
494    match bound {
495        ScalarFrameBound::UnboundedPreceding => BoundKind::UnboundedPreceding,
496        ScalarFrameBound::UnboundedFollowing => BoundKind::UnboundedFollowing,
497        ScalarFrameBound::CurrentRow => BoundKind::CurrentRow,
498        ScalarFrameBound::Preceding(_) => BoundKind::Preceding,
499        ScalarFrameBound::Following(_) => BoundKind::Following,
500    }
501}
502
503fn window_function(
504    context: &dyn QueryExpressionContext,
505    slot: &WindowSlot,
506    schema: &RowSchema,
507    params: &[SQLParam],
508    budget_bytes: usize,
509) -> Result<WindowFunction, SQLError> {
510    let name = uqa_sql::semantics::builtin_function_dispatch_name(&slot.name);
511    let bind = |expression: &ScalarExpr| {
512        crate::bind_type_introspection_with_resolver(expression.clone(), schema, params, context)
513    };
514    let argument = |position: usize| {
515        slot.args
516            .get(position)
517            .map(|expression| crate::scalar::PreparedExpressions::scalar(bind(expression)))
518    };
519    let required = |position: usize| {
520        argument(position).ok_or_else(|| SQLError::BadArity {
521            name: name.to_string(),
522            expected: format!(">={}", position + 1),
523            actual: slot.args.len(),
524        })
525    };
526    Ok(match name.as_ref() {
527        "row_number" => WindowFunction::RowNumber,
528        "rank" => WindowFunction::Rank,
529        "dense_rank" => WindowFunction::DenseRank,
530        "percent_rank" => WindowFunction::PercentRank,
531        "cume_dist" => WindowFunction::CumeDist,
532        "ntile" => WindowFunction::Ntile(required(0)?),
533        "lag" | "lead" => WindowFunction::Shift(Box::new(functions::Shift {
534            forward: name == "lead",
535            target: required(0)?,
536            offset: argument(1),
537            default: argument(2),
538        })),
539        "first_value" => WindowFunction::FirstValue(required(0)?),
540        "last_value" => WindowFunction::LastValue(required(0)?),
541        "nth_value" => WindowFunction::NthValue {
542            target: required(0)?,
543            position: required(1)?,
544        },
545        _ => {
546            let call = ScalarExpr::Func {
547                order_syntax: uqa_sql::ast::FunctionOrderSyntax::Ordinary,
548                name: slot.name.clone(),
549                binding: None,
550                args: slot.args.iter().map(bind).collect(),
551                distinct: false,
552                order_by: Vec::new(),
553                filter: None,
554            };
555            let template = crate::aggregation::aggregate_accumulator_templates(
556                context,
557                std::slice::from_ref(&call),
558                schema,
559                params,
560            )?
561            .pop()
562            .ok_or_else(|| SQLError::Internal("window aggregate lost its accumulator".into()))?;
563            let ScalarExpr::Func { args, .. } = call else {
564                unreachable!()
565            };
566            let filter = slot.filter.as_ref().map(bind);
567            WindowFunction::Aggregate(Box::new(aggregates::WindowAggregate::new(
568                (&slot.name, &args, filter.as_ref()),
569                template,
570                budget_bytes,
571            )))
572        }
573    })
574}
575
576impl PreparedSlot {
577    fn offsets_reference_rows(&self) -> bool {
578        [&self.start_offset, &self.end_offset]
579            .into_iter()
580            .flatten()
581            .any(|offset| {
582                let mut found = false;
583                offset.expression.visit(&mut |part| {
584                    found |= matches!(
585                        part,
586                        ScalarExpr::Column(_)
587                            | ScalarExpr::QualifiedColumn { .. }
588                            | ScalarExpr::Position(_)
589                            | ScalarExpr::InternalColumn(_)
590                    );
591                });
592                found
593            })
594    }
595
596    /// `calculate_frame_offsets`: each offset is evaluated once, must not be NULL, and a `ROWS` or `GROUPS` count must not be negative.
597    fn evaluate_offsets(&mut self, context: &ScalarEvalContext<'_>) -> Result<(), SQLError> {
598        let mode = self.state.frame_mut().mode;
599        let start = self
600            .start_offset
601            .as_ref()
602            .map(|offset| frame_offset_value(offset, mode, "starting", context))
603            .transpose()?;
604        let end = self
605            .end_offset
606            .as_ref()
607            .map(|offset| frame_offset_value(offset, mode, "ending", context))
608            .transpose()?;
609        let frame = self.state.frame_mut();
610        frame.start_offset = start;
611        frame.end_offset = end;
612        Ok(())
613    }
614}
615
616fn frame_offset_value(
617    offset: &FrameOffset,
618    mode: FrameMode,
619    which: &str,
620    context: &ScalarEvalContext<'_>,
621) -> Result<Value, SQLError> {
622    let value = eval_scalar(&offset.expression, context)?;
623    if matches!(value, Value::Null) {
624        return Err(SQLError::Routine {
625            sqlstate: "22004".into(),
626            message: format!("frame {which} offset must not be null"),
627        });
628    }
629    let value = uqa_sql::expr::cast_value(&value, &offset.target.sql_name())?;
630    if mode != FrameMode::Range && matches!(value, Value::Int(count) if count < 0) {
631        return Err(SQLError::Routine {
632            sqlstate: "22013".into(),
633            message: format!("frame {which} offset must not be negative"),
634        });
635    }
636    Ok(value)
637}
638
639/// Evaluate every window call of a pass for each row of one sorted partition and append the results to the row.
640fn emit_partition(
641    pass: &WindowPass,
642    order_types: &[Option<ColumnType>],
643    slots: &mut [PreparedSlot],
644    partition: &mut BufferedIndexedSpill,
645    (schema, output): (&RowSchema, &mut SpillBuffer),
646    (params, hook, subqueries): (
647        &[SQLParam],
648        &dyn uqa_sql::expr::EngineHook,
649        &dyn ScalarSubqueryRunner,
650    ),
651    offsets_evaluated: &mut bool,
652) -> Result<(), SQLError> {
653    let mut rows = PartitionRows::new(
654        partition,
655        &pass.order_by,
656        order_types,
657        params,
658        hook,
659        subqueries,
660    )?;
661    if rows.len() == 0 {
662        return Ok(());
663    }
664    if !*offsets_evaluated {
665        // Offsets may use an enclosing query's columns, which every row of the input carries.
666        for slot in slots.iter_mut() {
667            rows.with_context(0, |context| slot.evaluate_offsets(context))?;
668        }
669        *offsets_evaluated = true;
670    }
671    for slot in slots.iter_mut() {
672        slot.state.begin_partition()?;
673    }
674    let mut current = CurrentRow::new();
675    let mut pending = Vec::with_capacity(crate::batch::DEFAULT_BATCH_SIZE);
676    for position in 0..rows.len() {
677        if position > 0 {
678            for slot in slots.iter_mut() {
679                slot.state.advance();
680            }
681            current.advance(&mut rows)?;
682        }
683        let values = slots
684            .iter_mut()
685            .map(|slot| slot.state.value(&mut current, &mut rows))
686            .collect::<Result<Vec<_>, _>>()?;
687        pending.push(rows.row(position)?.append_values(values));
688        if pending.len() == crate::batch::DEFAULT_BATCH_SIZE {
689            output
690                .push(Batch::from_physical_rows(
691                    schema.clone(),
692                    std::mem::take(&mut pending),
693                ))
694                .map_err(exec_to_sql_error)?;
695            pending = Vec::with_capacity(crate::batch::DEFAULT_BATCH_SIZE);
696        }
697    }
698    if !pending.is_empty() {
699        output
700            .push(Batch::from_physical_rows(schema.clone(), pending))
701            .map_err(exec_to_sql_error)?;
702    }
703    Ok(())
704}
705
706fn evaluate_all(
707    expressions: &[ScalarExpr],
708    schema: &RowSchema,
709    row: &PhysicalRow,
710    params: &[SQLParam],
711    hook: &dyn uqa_sql::expr::EngineHook,
712    subqueries: &dyn ScalarSubqueryRunner,
713) -> Result<Vec<Value>, SQLError> {
714    expressions
715        .iter()
716        .map(|expression| evaluate_on_row(expression, schema, row, params, hook, subqueries))
717        .collect()
718}
719
720fn evaluate_on_row(
721    expression: &ScalarExpr,
722    schema: &RowSchema,
723    row: &PhysicalRow,
724    params: &[SQLParam],
725    eval_hook: &dyn uqa_sql::expr::EngineHook,
726    subquery_runner: &dyn ScalarSubqueryRunner,
727) -> Result<Value, SQLError> {
728    let view = schema.view(row);
729    let context = ScalarEvalContext::from_row_lookup(&view, params)
730        .with_function_hook(eval_hook)
731        .with_subquery_runner(subquery_runner)
732        .with_physical_outer_row(schema, row);
733    eval_scalar(expression, &context)
734}
735
736fn combine_execution_and_close(
737    execution: Result<(), SQLError>,
738    close: Result<(), SQLError>,
739    operator: &str,
740) -> Result<(), SQLError> {
741    match (execution, close) {
742        (Ok(()), Ok(())) => Ok(()),
743        (Ok(()), Err(error)) | (Err(error), Ok(())) => Err(error),
744        (Err(execution_error), Err(close_error)) => {
745            Err(execution_error
746                .with_cleanup(close_error, format!("close {operator} after failure")))
747        }
748    }
749}
750
751fn exec_to_sql_error(error: crate::ExecError) -> SQLError {
752    match error {
753        crate::ExecError::SQL(error) => error,
754        crate::ExecError::Other(message) => SQLError::Internal(message),
755    }
756}
757
758#[cfg(test)]
759mod tests;