uqa-execution 0.6.3

Volcano physical operators with row-batch pipelines
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//
//! Demand-driven local and materialized table scans.

use super::TableScanContext;
use crate::query::{
    local_table::{
        table_lock_origin_with_control, HierarchyRowSource, LocalTableRowSource,
        LocalTableScanConfig,
    },
    CteScope,
};
use uqa_core::{
    memory::{Produced, ProductionControl, ProductionMap, ProductionVec},
    Value,
};
mod schema;
use schema::{append_scan_metadata, projected_scan_schema_with_control};
use uqa_sql::{
    plan::{
        source_projection::{ColumnPrune, QualifierFilters, SourceProjection},
        SourcePlan,
    },
    semantics::{
        source_filters::{has_filters_for_qualifier, qualifier_filter_with_control},
        TABLE_OID_COLUMN, XMIN_COLUMN,
    },
    SQLError, SQLParam,
};
type StreamingLocalTableScan<'a> = (Box<dyn crate::PhysicalOperator + 'a>, bool);

pub fn try_streaming_local_table_scan<'a, S: Clone>(
    context: TableScanContext<'_>,
    source: &SourcePlan,
    ctes: &CteScope<S>,
    prune: Option<&ColumnPrune>,
    filters: Option<&QualifierFilters>,
    params: &[SQLParam],
) -> Result<Option<StreamingLocalTableScan<'a>>, SQLError> {
    try_streaming_local_table_scan_with_control(
        context,
        source,
        ctes,
        prune,
        filters,
        params,
        &ProductionControl::uncontrolled(),
    )
}

#[expect(
    clippy::too_many_lines,
    reason = "preserves source schema and row identity"
)]
pub fn try_streaming_local_table_scan_with_control<'a, S: Clone>(
    context: TableScanContext<'_>,
    source: &SourcePlan,
    ctes: &CteScope<S>,
    prune: Option<&ColumnPrune>,
    filters: Option<&QualifierFilters>,
    params: &[SQLParam],
    control: &ProductionControl<'_>,
) -> Result<Option<StreamingLocalTableScan<'a>>, SQLError> {
    control.check()?;
    let SourcePlan::Table {
        executable_query: _,
        name,
        qualifier,
        alias,
        column_aliases,
        bound_columns,
        include_descendants,
    } = source
    else {
        return Ok(None);
    };
    let qualifier = alias.as_deref().unwrap_or(qualifier);
    if let Some(materialized) = ctes.materialized_for_scan(name) {
        if has_filters_for_qualifier(filters, qualifier) {
            return Ok(None);
        }
        let scan = materialized.into_operator_with_control(control)?;
        let selection = crate::ColumnSelection::with_scan_projection_with_control(
            scan,
            qualifier,
            column_aliases,
            prune.and_then(|prune| prune.get(qualifier)),
            ctes.lock_identities.emit,
            control,
        )?;
        return Ok(Some((selection, false)));
    }
    let catalog = ctes.catalog_read_view()?;
    let resolution = ctes.relation_name_resolution_with_control(control)?;
    if catalog
        .view_resolved_with_control(&resolution, name, control)?
        .is_some()
        || catalog
            .foreign_table_resolved_with_control(&resolution, name, control)?
            .is_some()
    {
        return Ok(None);
    }
    let Some(root_name) = catalog.table_name_resolved_with_control(&resolution, name, control)?
    else {
        return Ok(None);
    };
    let root_table = catalog
        .table_resolved_with_control(&resolution, &root_name, control)?
        .ok_or_else(|| SQLError::UnknownTable(name.clone()))?;
    let wanted = prune.and_then(|prune| prune.get(qualifier));
    let metadata = wanted.map(SourceProjection::metadata).unwrap_or_default();
    let table_columns = uqa_sql::semantics::bound_source_column_names_with_control(
        root_table.columns.iter().map(|column| column.name.as_str()),
        bound_columns.as_deref(),
        control,
    )?;
    if column_aliases.len() > table_columns.len() {
        return Err(SQLError::Routine {
            sqlstate: "42P10".into(),
            message: format!(
                "table \"{qualifier}\" has {} columns available but {} columns specified",
                table_columns.len(),
                column_aliases.len()
            ),
        });
    }
    let open_document = root_table.columns.is_empty() && !root_table.columns_declared;
    let mut selected = ProductionVec::new(*control);
    for column in table_columns.iter() {
        selected.push_copy(column.as_str())?;
    }
    if open_document {
        if let Some(wanted) = wanted {
            for field in requested_document_fields(wanted) {
                selected.push_copy(field)?;
            }
        }
    }
    // Aliases retain their declaration positions even when a projection omits preceding fields.
    let mut visible_names = ProductionMap::new(*control);
    let mut columns = ProductionVec::new(*control);
    let mut schema = ProductionVec::new(*control);
    for (position, physical) in selected.iter().enumerate() {
        let visible = column_aliases
            .get(position)
            .map_or(*physical, String::as_str);
        visible_names.insert_copy(*physical, visible)?;
        if wanted.is_none_or(|wanted| wanted.contains(visible)) {
            columns.push_produced(control.copy_text(physical)?)?;
            schema.push_produced(control.copy_text(visible)?)?;
        }
    }
    let include_table_oid = wanted.as_ref().is_some_and(|wanted| {
        wanted
            .requested_columns()
            .any(|column| column == TABLE_OID_COLUMN)
    });
    let include_xmin = wanted.as_ref().is_some_and(|wanted| {
        wanted
            .requested_columns()
            .any(|column| column == XMIN_COLUMN)
    });
    if include_xmin {
        columns.push_produced(control.copy_text(XMIN_COLUMN)?)?;
        schema.push_produced(control.copy_text(XMIN_COLUMN)?)?;
    }
    if include_table_oid {
        schema.push_produced(control.copy_text(TABLE_OID_COLUMN)?)?;
    }
    let columns = columns.finish()?;
    let schema = schema.finish()?;
    let identity = uqa_core::RelationIdentity::try_borrowed_reference(&root_name)
        .map_err(|error| SQLError::Internal(error.to_string()))?
        .identity_with_control(uqa_core::RelationName::decoded("public"), control)?;
    let row_type = root_table
        .catalog_oids
        .row_type_reference_with_control(&identity, control)?;
    let mut physical_schema = projected_scan_schema_with_control(
        qualifier,
        &columns,
        &schema,
        &root_table.columns,
        include_table_oid,
        row_type.as_deref(),
        control,
    )?;
    if open_document {
        physical_schema = crate::RowSchema::with_wildcard_hidden_positions_with_control(
            &physical_schema,
            0..schema.len(),
            control,
        )?;
    }
    physical_schema = append_scan_metadata(&physical_schema, qualifier, metadata, control)?;
    let table_names = catalog.hierarchy_scan_tables_with_control(
        &resolution,
        name,
        *include_descendants,
        control,
    )?;
    let mut sources = ProductionVec::new(*control);
    let mut filter_pushed = false;
    for table_name in table_names.iter() {
        control.check()?;
        let table = context.tables.table(table_name)?;
        let table_snapshot = catalog
            .table_resolved_with_control(&resolution, table_name, control)?
            .ok_or_else(|| SQLError::UnknownTable(table_name.clone()))?;
        let column_definitions = table_snapshot.columns.clone();
        let lock_origin = table_lock_origin_with_control(
            &catalog,
            &resolution,
            table_name,
            qualifier,
            ctes.lock_identities.emit,
            control,
        )?;
        // Candidate selection and row evaluation must share the selected SQL operand types. A lossy column cast cannot use an index over the uncast values.
        let predicate_expression = qualifier_filter_with_control(filters, qualifier, control)?
            .map(|predicate| {
                uqa_sql::bind_type_introspection_owned(
                    predicate,
                    &physical_schema,
                    params,
                    None,
                    control,
                )
            })
            .transpose()?;
        let predicate = predicate_expression
            .as_ref()
            .filter(|predicate| !expression_references_tableoid(predicate))
            .map(|predicate| {
                crate::ProjectedPredicate::compile_bound_with_control(
                    predicate,
                    &physical_schema,
                    params,
                    control,
                )
            })
            .transpose()?
            .flatten();
        filter_pushed |= predicate.is_some();
        let recheck_pins = lock_origin
            .as_ref()
            .and_then(|(origin_qualifier, storage_name)| {
                ctes.recheck_docs_for_scan(origin_qualifier, storage_name)
            });
        // A filter that names its rows by identity, or by an integer primary key whose values are the identities, reads only those rows, from storage or from the changes above it, instead of scanning the table.
        let mut candidates = if recheck_pins.is_none() {
            predicate_expression.as_ref().and_then(|filter| {
                crate::query::key_candidates::key_candidates(
                    filter,
                    params,
                    crate::query::key_candidates::IdentityColumns::new(
                        &column_definitions,
                        table.maps_integer_keys(),
                        |name| visible_names.get(name).copied().unwrap_or(name),
                    ),
                )
            })
        } else {
            None
        }
        .map(|ids| {
            crate::query::SelectedIds::from_sorted(
                ids,
                context.runtime.work_mem_bytes()?,
                &context.runtime.cancellation_token(),
            )
        })
        .transpose()?;
        if candidates.is_none() && recheck_pins.is_none() {
            if let Some(filter) = &predicate_expression {
                candidates = crate::query::index_candidates::IndexCandidates {
                    reads: context.tables,
                    table: table_name,
                    columns: &column_definitions,
                    visible: &|name| visible_names.get(name).copied().unwrap_or(name).to_owned(),
                    params,
                    command_visible: ctes.reads_command_overlay(),
                    cancellation: &context.runtime.cancellation_token(),
                    bytes: context.runtime.work_mem_bytes()?,
                }
                .select(filter)?;
            }
        }
        let command_changes = if ctes.reads_command_overlay() {
            context.tables.command_overlay_changes(table_name)?
        } else {
            None
        };
        let estimated_cardinality = match &candidates {
            Some(candidates) => u64::try_from(candidates.len()).unwrap_or(u64::MAX),
            None => context.tables.table_row_estimate(table_name)?,
        };
        let table_oid =
            include_table_oid.then(|| Value::Int(i64::from(table_snapshot.catalog_oids.relation)));
        let name = control.copy_text(table_name)?;
        let copied_columns = copy_names(&columns, control)?;
        let copied_schema = copy_names(&schema, control)?;
        let (table_name, name_memory) = name.into_parts();
        let (columns, column_memory) = copied_columns.into_parts();
        let (schema, schema_memory) = copied_schema.into_parts();
        let config = control.finish(
            LocalTableScanConfig {
                cancellation: context.runtime.cancellation_token(),
                serializable: context.tables.serializable_read(&table_name)?,
                table_name,
                table,
                column_definitions,
                columns,
                schema,
                physical_schema: physical_schema.clone(),
                metadata,
                table_oid,
                predicate,
                estimated_cardinality,
                lock_origin,
                recheck_pins,
                candidates,
                command_changes,
            },
            control.combine(control.combine(name_memory, column_memory), schema_memory),
        )?;
        let source = LocalTableRowSource::from_produced(config)
            .with_scan_batch_bytes(context.runtime.work_mem_bytes()?);
        sources.push_produced(control.finish(source, control.empty_reservation())?)?;
    }
    let sources = sources.finish()?;
    let source: Box<dyn crate::RowSource> = if sources.len() == 1 {
        let (mut sources, _memory) = sources.into_parts();
        sources
            .pop()
            .expect("single-table scan owns its source")
            .into_boxed(control)?
    } else {
        HierarchyRowSource::boxed_with_control(sources, control)?
    };
    Ok(Some((
        crate::TableScan::boxed_with_control(source, control)?,
        filter_pushed,
    )))
}

fn expression_references_tableoid(expression: &crate::ScalarExpr) -> bool {
    let mut references = false;
    let projectable = expression
        .try_visit_columns(&mut |column| {
            references |= column == TABLE_OID_COLUMN;
            Ok::<_, std::convert::Infallible>(())
        })
        .unwrap_or_else(|never| match never {});
    projectable && references
}

fn copy_names(
    names: &[String],
    control: &ProductionControl<'_>,
) -> Result<Produced<Vec<String>>, SQLError> {
    let mut output = ProductionVec::new(*control);
    for name in names {
        output.push_produced(control.copy_text(name)?)?;
    }
    Ok(output.finish()?)
}

fn requested_document_fields(projection: &SourceProjection) -> impl Iterator<Item = &str> {
    let metadata = projection.metadata();
    projection
        .requested_columns()
        .filter(move |column| match *column {
            XMIN_COLUMN | TABLE_OID_COLUMN => false,
            uqa_sql::semantics::DOC_ID_COLUMN => !metadata.includes_doc_id(),
            uqa_sql::semantics::SCORE_COLUMN => !metadata.includes_score(),
            _ => true,
        })
}

#[cfg(test)]
fn projected_scan_schema(
    qualifier: &str,
    columns: &[String],
    visible: &[String],
    definitions: &[uqa_sql::ast::ColumnDef],
    include_table_oid: bool,
    row_type: Option<uqa_sql::ast::CompositeTypeReference>,
) -> Result<crate::RowSchema, SQLError> {
    projected_scan_schema_with_control(
        qualifier,
        columns,
        visible,
        definitions,
        include_table_oid,
        row_type.as_ref(),
        &ProductionControl::uncontrolled(),
    )
}

#[cfg(test)]
mod tests;