Skip to main content

uqa_execution/mutation/returning/
destination.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Deliver each final RETURNING row before the mutation advances to another row.
8
9use crate::{
10    query::consumer::{QueryConsumerControl, QueryRowConsumer},
11    OwnedPhysicalRow, RowSchema,
12};
13use uqa_sql::SQLError;
14
15#[derive(Clone, Copy)]
16pub struct ReturningDestination<'a> {
17    consumer: &'a dyn QueryRowConsumer,
18    schema: &'a RowSchema,
19}
20
21impl<'a> ReturningDestination<'a> {
22    pub fn new(
23        consumer: &'a dyn QueryRowConsumer,
24        schema: &'a RowSchema,
25    ) -> Result<Self, SQLError> {
26        consumer.begin(schema.columns(), schema)?;
27        Ok(Self { consumer, schema })
28    }
29
30    pub(super) fn consume(self, row: OwnedPhysicalRow) -> Result<QueryConsumerControl, SQLError> {
31        if row.schema.columns() != self.schema.columns() {
32            return Err(SQLError::Internal(
33                "RETURNING values do not match their bound output columns".into(),
34            ));
35        }
36        let values = row.row.project_slots(row.schema.layout_slots());
37        self.consumer
38            .consume(OwnedPhysicalRow::new(self.schema.clone(), values))
39    }
40}