uqa-execution 0.4.6

Volcano physical operators with row-batch pipelines
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Query-table row adaptation and reconstruction from a retained base and evaluated private rows.

use std::sync::Arc;
use uqa_core::memory::{BudgetedMap, BudgetedVec, MemoryReservation};
use uqa_core::{DocId, FieldName, Value};
use uqa_sql::{ast::ColumnDef, schema::retention::RetainedColumns, ColumnType, SQLError};
use uqa_storage::inverted_index::RetainedInvertedIndexBuilder;
use uqa_storage::{
    read_control::StorageReadControl,
    vector_index::{RetainedVectorIndexBuilder, RetainedVectorIndexesBuilder, VectorIndexes},
};
use uqa_storage::{
    DocumentStore, InvertedIndex, RetainedDocumentStoreBuilder, StorageBackendError,
};

mod documents;
mod layout;
mod vector_metadata;
mod vectors;
use super::document_changes::DocumentChanges;
use layout::RowLayout;
pub use vector_metadata::{retain_vector_indexes, VectorDimensions};
use vectors::{SnapshotVector, VectorSources};

#[cfg(test)]
mod tests;

/// The selected catalog supplies immutable column definitions, field revisions and registered vector dimensions. The reconstruction does not consult live catalog state.
pub struct SnapshotSchema<'a> {
    pub columns: Arc<Vec<ColumnDef>>,
    pub text_fields: &'a [FieldName],
    pub text_revisions: &'a dyn InvertedIndex,
    pub vector_dimensions: &'a dyn VectorDimensions,
}

pub struct MaterializedTable {
    pub documents: Box<dyn DocumentStore>,
    pub text: Box<dyn InvertedIndex>,
    pub vectors: VectorIndexes,
    pub document_count: u64,
}

struct SnapshotBuilder {
    text: RetainedInvertedIndexBuilder,
    vectors: BudgetedVec<(FieldName, SnapshotVector, MemoryReservation)>,
    control: StorageReadControl,
}

/// Retain the immutable base instead of copying its documents into another complete store. Private replacements and row-layout metadata are shared by nested views, and projected reads avoid unrelated fields. Text/vector reconstruction still retains its resulting indexes.
pub fn retain(
    source: Arc<dyn DocumentStore>,
    source_columns: &[ColumnDef],
    schema: &SnapshotSchema<'_>,
    changes: DocumentChanges,
    control: &StorageReadControl,
) -> Result<MaterializedTable, SQLError> {
    retain_with_vector_indexes(source, source_columns, schema, changes, None, control)
}

/// Retain compatible physical indexes from the fixed source, or the selected new definition when every base row has an evaluated replacement or deletion. Canonical sources stay with their evaluated rows; unsupported selections retain the existing reconstruction path.
pub fn retain_with_vector_indexes(
    source: Arc<dyn DocumentStore>,
    source_columns: &[ColumnDef],
    schema: &SnapshotSchema<'_>,
    changes: DocumentChanges,
    base_vectors: Option<&dyn VectorDimensions>,
    control: &StorageReadControl,
) -> Result<MaterializedTable, SQLError> {
    let cancellation = control.cancellation();
    cancellation.check()?;
    let columns = RetainedColumns::capture(&schema.columns, control.memory(), cancellation)?;
    let layout = RowLayout::new(source_columns, columns.clone(), control)
        .map_err(|error| snapshot_error("base row layout", &error))?;
    let documents = documents::RetainedDocuments::new(
        source,
        layout,
        RowLayout::new(&schema.columns, columns, control)
            .map_err(|error| snapshot_error("private row layout", &error))?,
        changes,
        control,
    )
    .map_err(|error| snapshot_error("retained documents", &error))?;
    let sources = base_vectors.map(|indexes| documents.vector_sources(indexes, source_columns));
    let mut result = SnapshotBuilder::with_vector_sources(schema, sources.as_ref(), control)?;
    let document_count = u64::try_from(
        documents
            .len()
            .map_err(|error| snapshot_error("document count", &error))?,
    )
    .map_err(|_| SQLError::Internal("query snapshot document count overflow".into()))?;
    let fields = index_fields_for_reconstruction(schema, Some(&result), control)?;
    if fields.is_empty() {
        return result.finish(Box::new(documents), document_count);
    }
    let mut after = None;
    loop {
        cancellation.check()?;
        let ids = documents
            .id_page(after, crate::DEFAULT_BATCH_SIZE)
            .map_err(|error| snapshot_error("document ids", &error))?;
        let Some(last) = ids.last().copied() else {
            break;
        };
        after = Some(last);
        result.index_projection(&documents, &ids, &fields, schema)?;
    }
    cancellation.check()?;
    result.finish(Box::new(documents), document_count)
}

#[cfg(test)]
fn index_fields<'a>(
    schema: &'a SnapshotSchema<'_>,
    control: &StorageReadControl,
) -> Result<BudgetedVec<&'a str>, SQLError> {
    index_fields_for_reconstruction(schema, None, control)
}

fn index_fields_for_reconstruction<'a>(
    schema: &'a SnapshotSchema<'_>,
    builder: Option<&SnapshotBuilder>,
    control: &StorageReadControl,
) -> Result<BudgetedVec<&'a str>, SQLError> {
    control.cancellation().check()?;
    let mut selected = BudgetedMap::new(control.memory());
    for field in schema.text_fields {
        control.cancellation().check()?;
        selected
            .insert(field.as_str(), ())
            .map_err(|error| snapshot_error("index field selection", &error.into()))?;
    }
    schema.vector_dimensions.visit(&mut |field, _| {
        control.cancellation().check()?;
        if builder.is_some_and(|builder| {
            builder.vectors.iter().any(|(name, index, _)| {
                name == field && matches!(index, SnapshotVector::Physical(_))
            })
        }) {
            return Ok(());
        }
        selected
            .insert(field, ())
            .map_err(|error| snapshot_error("index field selection", &error.into()))?;
        Ok(())
    })?;
    let mut fields = BudgetedVec::new(control.memory());
    fields
        .reserve(selected.len())
        .map_err(|error| snapshot_error("index field projection", &error.into()))?;
    for (field, ()) in selected.iter() {
        control.cancellation().check()?;
        fields
            .push(*field)
            .map_err(|error| snapshot_error("index field projection", &error.into()))?;
    }
    Ok(fields)
}

/// Copy a mutable source into a controlled immutable corpus, or share an already retained source. Provider row pages keep their payload reservations through corpus adoption. The existing read adapter maps original column identities and shares evaluated private rows and selected definitions; capture does not construct defaults or unrequested generated values for every row. Text/vector reconstruction evaluates its requested fields. Owned read outputs retain their separate producer boundaries.
pub fn materialize(
    source: &dyn DocumentStore,
    source_columns: &[ColumnDef],
    schema: &SnapshotSchema<'_>,
    changes: DocumentChanges,
    control: &StorageReadControl,
) -> Result<MaterializedTable, SQLError> {
    let cancellation = control.cancellation();
    cancellation.check()?;
    if let Some(source) = source
        .retained_snapshot()
        .map_err(|error| snapshot_error("retained documents", &error))?
    {
        return retain(source, source_columns, schema, changes, control);
    }
    let mut retained = RetainedDocumentStoreBuilder::new(control);
    let mut after = None;
    loop {
        cancellation.check()?;
        let ids = uqa_storage::document_store::read_document_ids(
            source,
            after,
            crate::DEFAULT_BATCH_SIZE,
            control,
        )
        .map_err(|error| snapshot_error("document ids", &error))?;
        let Some(last) = ids.last().copied() else {
            break;
        };
        after = Some(last);
        let mut selected = uqa_core::memory::BudgetedVec::new(control.memory());
        for id in ids.iter().copied() {
            cancellation.check()?;
            if !changes.contains_change(id) {
                selected
                    .push(id)
                    .map_err(|error| snapshot_error("document selection", &error.into()))?;
            }
        }
        if selected.is_empty() {
            continue;
        }
        let (documents, _page_memory) =
            uqa_storage::document_store::read_stored_documents(source, &selected, control)
                .map_err(|error| snapshot_error("documents", &error))?
                .into_parts();
        for (id, document) in selected.iter().copied().zip(documents) {
            cancellation.check()?;
            if let Some(document) = document {
                retained
                    .add_retained_document(id, document)
                    .map_err(|error| snapshot_error("document", &error))?;
            }
        }
    }
    cancellation.check()?;
    let documents = retained
        .finish()
        .map_err(|error| snapshot_error("documents", &error))?;
    retain(
        Arc::new(documents),
        source_columns,
        schema,
        changes,
        control,
    )
}

pub fn empty(
    schema: &SnapshotSchema<'_>,
    control: &StorageReadControl,
) -> Result<MaterializedTable, SQLError> {
    let documents = RetainedDocumentStoreBuilder::new(control)
        .finish()
        .map_err(|error| snapshot_error("documents", &error))?;
    SnapshotBuilder::new(schema, control)?.finish(Box::new(documents), 0)
}

impl SnapshotBuilder {
    fn new(schema: &SnapshotSchema<'_>, control: &StorageReadControl) -> Result<Self, SQLError> {
        Self::with_vector_sources(schema, None, control)
    }

    fn with_vector_sources(
        schema: &SnapshotSchema<'_>,
        sources: Option<&VectorSources<'_>>,
        control: &StorageReadControl,
    ) -> Result<Self, SQLError> {
        control.cancellation().check()?;
        let revisions = schema.text_fields.iter().map(|field| {
            control.check()?;
            Ok((
                field.as_str(),
                schema.text_revisions.index_analyzer_revision(field)?,
                schema.text_revisions.search_analyzer_revision(field)?,
            ))
        });
        let text = RetainedInvertedIndexBuilder::from_revisions(
            schema
                .text_revisions
                .default_analyzer_binding()
                .map_err(|error| snapshot_error("default analyzer binding", &error))?,
            revisions,
            control,
        )
        .map_err(|error| snapshot_error("inverted index", &error))?;
        let mut vectors = BudgetedVec::new(control.memory());
        schema.vector_dimensions.visit(&mut |field, dimensions| {
            control.cancellation().check()?;
            vectors
                .reserve(1)
                .map_err(|error| snapshot_error("vector metadata", &error.into()))?;
            let (name, memory) = vector_metadata::copy_field(field, control)?;
            let physical = sources
                .map(|sources| sources.retain(field, dimensions, schema, control))
                .transpose()?
                .flatten();
            let index = physical.map_or_else(
                || {
                    SnapshotVector::Reconstructed(RetainedVectorIndexBuilder::new(
                        dimensions, control,
                    ))
                },
                SnapshotVector::Physical,
            );
            vectors
                .push((name, index, memory))
                .map_err(|error| snapshot_error("vector metadata", &error.into()))?;
            Ok(())
        })?;
        Ok(Self {
            text,
            vectors,
            control: control.clone(),
        })
    }

    fn finish(
        self,
        documents: Box<dyn DocumentStore>,
        document_count: u64,
    ) -> Result<MaterializedTable, SQLError> {
        let (builders, _builder_memory) = self.vectors.into_parts();
        let mut vectors = RetainedVectorIndexesBuilder::new(&self.control);
        for (field, index, memory) in builders {
            self.control.cancellation().check()?;
            match index {
                SnapshotVector::Reconstructed(index) => {
                    let index = index
                        .finish()
                        .map_err(|error| snapshot_error("vector index", &error))?;
                    vectors.insert_admitted(field, index, memory)
                }
                SnapshotVector::Physical(index) => vectors.insert_admitted(
                    field,
                    uqa_storage::ReadOnlySnapshot::new(index),
                    memory,
                ),
            }
            .map_err(|error| snapshot_error("vector index metadata", &error))?;
        }
        Ok(MaterializedTable {
            documents,
            text: Box::new(
                self.text
                    .finish()
                    .map_err(|error| snapshot_error("inverted index", &error))?,
            ),
            vectors: vectors
                .finish()
                .map_err(|error| snapshot_error("vector indexes", &error))?,
            document_count,
        })
    }

    fn index_projection(
        &mut self,
        documents: &dyn DocumentStore,
        ids: &[DocId],
        fields: &[&str],
        schema: &SnapshotSchema<'_>,
    ) -> Result<(), SQLError> {
        let mut failure = None;
        let visited = documents.for_each_fields_multi_ref_with_presence(
            ids,
            fields,
            &mut |id, present, values| {
                if !present {
                    return true;
                }
                let indexed = self.index_fields(
                    id,
                    |field| fields.binary_search(&field).ok().map(|slot| values[slot]),
                    schema,
                );
                if let Err(error) = indexed {
                    failure = Some(error);
                    return false;
                }
                true
            },
        );
        // Keep the first consumer failure when a provider notices cancellation while unwinding its borrowed projection.
        if let Some(error) = failure {
            return Err(error);
        }
        visited.map_err(|error| snapshot_error("index fields", &error))
    }

    fn index_fields<'a>(
        &mut self,
        id: DocId,
        mut value_for: impl FnMut(&str) -> Option<&'a Value>,
        schema: &SnapshotSchema<'_>,
    ) -> Result<(), SQLError> {
        let fields = schema
            .text_fields
            .iter()
            .filter_map(|field| match value_for(field) {
                Some(Value::Str(value)) => Some((field.as_str(), value.as_str())),
                _ => None,
            });
        self.text
            .add_document(id, fields)
            .map_err(|error| snapshot_error("inverted index", &error))?;
        for (field, index, _) in self.vectors.iter_mut() {
            let SnapshotVector::Reconstructed(index) = index else {
                continue;
            };
            let Some(value) = value_for(field) else {
                continue;
            };
            let fallback = ColumnType::Vector(index.dimensions());
            let ty = schema
                .columns
                .iter()
                .find(|column| column.name == *field)
                .map(|column| &column.ty)
                .filter(|ty| matches!(ty, ColumnType::Tensor(_) | ColumnType::Vector(_)))
                .unwrap_or(&fallback);
            let vectors = uqa_sql::assignment::vectors::index_vectors_for_type_budgeted(
                value,
                ty,
                self.control.memory(),
                &mut || self.control.cancellation().check(),
            )?;
            index
                .add_document(id, vectors)
                .map_err(|error| snapshot_error("vector index", &error))?;
        }
        Ok(())
    }
}

fn snapshot_error(component: &str, error: &StorageBackendError) -> SQLError {
    crate::storage_errors::storage_error(&format!("construct query {component} snapshot"), error)
}