uqa-execution 0.5.0

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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Foreign-table ALTER execution and publication through retained catalog write guards.
use super::{
    publication::dependencies::CatalogPublicationChanges,
    relation_alteration::{
        rewrite_relation_rename_dependents, validate_relation_alter_authority,
        RelationRenameDependencies, RoleTransferContext,
    },
    sequences::role_ownership::{
        table_owned_sequence_owner_updates, OwnedSequenceSecurityCatalog,
        OwnedSequenceSecurityWrite, SequenceSecurityPublication,
    },
};
use crate::catalog::security::roles::dependencies::{prepare_role_owner, RoleDependencyCandidate};
use crate::catalog::{foreign::StoredForeignTable, security::BoundTableSecurity};
use crate::row_locks::{
    binding::{bind_relation, RelationBinding, RelationDefinitionSession},
    RelationLockMode,
};
use std::{collections::BTreeMap, ops::DerefMut};
use uqa_core::RelationIdentity;
use uqa_sql::{
    ast::{AlterForeignTableAction, AlterForeignTableStmt},
    catalog::security::ownership::OwnerChangeAuthority,
    catalog::security::table::{rewrite_acl_owner, validate_table_security_invariants},
    schema::relation_alteration::{self, RelationAlterNames},
    SQLError,
};
use uqa_storage::StorageBackendResult;

pub type ForeignTableRegistryWrite<'a> =
    Box<dyn DerefMut<Target = BTreeMap<RelationIdentity, StoredForeignTable>> + 'a>;
pub type ForeignSecurityRegistryWrite<'a> =
    Box<dyn DerefMut<Target = BTreeMap<RelationIdentity, BoundTableSecurity>> + 'a>;
pub type ForeignMemoryRegistryWrite<'a> =
    Box<dyn DerefMut<Target = BTreeMap<RelationIdentity, Vec<uqa_fdw::Row>>> + 'a>;

pub trait ForeignTableAlterCatalog {
    fn contains_table(&self, relation: &RelationIdentity) -> bool;
    fn security(&self, relation: &RelationIdentity) -> Option<BoundTableSecurity>;
    fn table(&self, relation: &RelationIdentity) -> Option<StoredForeignTable>;
}
pub trait ForeignTableAlterAccess {
    fn ensure_owner(&self, name: &str) -> Result<String, SQLError>;
}
pub trait ForeignTableOwnerWriter {
    fn prepare_writer(&self) -> Result<(), SQLError>;
}
pub trait ForeignTableAlterPublication {
    fn persist_definition(
        &self,
        relation: &RelationIdentity,
        table: &StoredForeignTable,
    ) -> StorageBackendResult<()>;
    fn persist_rename(
        &self,
        from: &RelationIdentity,
        to: &RelationIdentity,
    ) -> StorageBackendResult<Option<bool>>;
    fn tables_write(&self) -> ForeignTableRegistryWrite<'_>;
    fn security_write(&self) -> ForeignSecurityRegistryWrite<'_>;
    fn memory_tables_write(&self) -> ForeignMemoryRegistryWrite<'_>;
    fn sequence_security_write(&self) -> OwnedSequenceSecurityWrite<'_>;
    fn persist_security(
        &self,
        relation: &RelationIdentity,
        security: &BoundTableSecurity,
    ) -> Result<(), SQLError>;
}
mod columns;

pub struct ForeignTableAlterContext<'a> {
    pub deletion: &'a dyn crate::schema::deletion::CatalogRemovalInputs,
    pub schema_moves: super::relation_alteration::relocation::RelationSchemaContext<'a>,
    pub names: &'a dyn RelationAlterNames,
    pub catalog: &'a dyn ForeignTableAlterCatalog,
    pub authority: crate::catalog::security::table_inquiry::TablePrivilegeContext<'a>,
    pub creation: super::namespaces::relations::RelationCreationContext<'a>,
    pub locks: &'a dyn RelationDefinitionSession,
    pub writer: &'a dyn ForeignTableOwnerWriter,
    pub roles: RoleTransferContext<'a>,
    pub dependencies: &'a dyn RelationRenameDependencies,
    pub owned_sequences: &'a dyn OwnedSequenceSecurityCatalog,
    pub sequence_publication: &'a dyn SequenceSecurityPublication,
    pub publication: &'a dyn ForeignTableAlterPublication,
    pub changes: &'a dyn CatalogPublicationChanges,
    pub notices: &'a crate::query::NoticeQueue,
}
pub type ForeignTableAlterWrite<'a> =
    Box<dyn FnOnce(&ForeignTableAlterContext<'_>) -> Result<(), SQLError> + 'a>;
pub trait ForeignTableAlterTransactions {
    fn with_foreign_table_write(&self, write: ForeignTableAlterWrite<'_>) -> Result<(), SQLError>;
}

pub fn bound_foreign_table_security(
    catalog: &dyn ForeignTableAlterCatalog,
    name: &str,
) -> Result<(RelationIdentity, BoundTableSecurity), SQLError> {
    let relation = RelationIdentity::from_legacy_name(name)
        .map_err(|error| SQLError::Internal(format!("resolve foreign table `{name}`: {error}")))?;
    if !catalog.contains_table(&relation) {
        return Err(SQLError::Internal(format!(
            "foreign table `{name}` disappeared"
        )));
    }
    let security = catalog.security(&relation).ok_or_else(|| {
        SQLError::Internal(format!(
            "foreign table `{name}` has no loaded security metadata"
        ))
    })?;
    Ok((relation, security))
}

pub fn alter_foreign_table(
    transactions: &dyn ForeignTableAlterTransactions,
    statement: &AlterForeignTableStmt,
) -> Result<(), SQLError> {
    transactions.with_foreign_table_write(Box::new(|context| {
        let Some(binding) = bind_relation(
            context.locks,
            RelationLockMode::AccessExclusive,
            false,
            || {
                let Some(target) = relation_alteration::foreign_table_alter_target(
                    context.names.resolve_relation_kind(&statement.name)?,
                    statement,
                    &mut |message| {
                        context.notices.push(uqa_sql::SQLNotice::notice(message));
                    },
                )?
                else {
                    return Ok(None);
                };
                Ok(Some(RelationBinding {
                    name: target.canonical.clone(),
                    object_id: context
                        .catalog
                        .table(&target.relation)
                        .map(|table| table.object_id),
                    value: target,
                }))
            },
            |binding| {
                let target = &binding.value;
                validate_relation_alter_authority(
                    &context.authority,
                    &context.creation,
                    &target.relation,
                    target.kind,
                    matches!(statement.action, AlterForeignTableAction::RenameTo(_)),
                )?;
                target.require_kind("foreign table")
            },
        )?
        else {
            return Ok(());
        };
        let canonical = binding.name;
        match &statement.action {
            AlterForeignTableAction::DropColumns(columns) => {
                columns::drop_columns(context, &binding.value.relation, columns)
            }
            AlterForeignTableAction::OwnerTo(owner) => {
                alter_foreign_table_role_owner(context, &canonical, owner)
            }
            AlterForeignTableAction::RenameTo(new_name) => {
                context.locks.prepare_definition_write()?;
                rename_foreign_table(context, &binding.value.relation, new_name)
            }
            AlterForeignTableAction::SetSchema(schema) => {
                let relation = &binding.value.relation;
                let current = context
                    .catalog
                    .table(relation)
                    .ok_or_else(|| SQLError::Internal("moved foreign table disappeared".into()))?;
                if let Some(target) =
                    context
                        .schema_moves
                        .target(relation, schema, current.persistence)?
                {
                    let relocation =
                        context
                            .schema_moves
                            .prepare(relation, &target, current.object_id)?;
                    rename_foreign_table_to(context, relation, target.clone())?;
                    relocation.publish(&context.schema_moves)?;
                }
                Ok(())
            }
        }
    }))
}

fn alter_foreign_table_role_owner(
    context: &ForeignTableAlterContext<'_>,
    name: &str,
    requested_owner: &uqa_sql::ast::RoleSpecification,
) -> Result<(), SQLError> {
    let owner = context.roles.bind(requested_owner)?;
    let current_user = context.roles.session.current_role();
    let RoleDependencyCandidate {
        roles,
        memberships,
        value,
        ..
    } = prepare_role_owner(
        context.roles.lock_context(),
        &owner,
        || context.writer.prepare_writer(),
        |roles, memberships, new_owner| {
            let (relation, bound) = bound_foreign_table_security(context.catalog, name)?;
            let mut security = bound.resolve(roles).map_err(SQLError::Internal)?;
            if security.role_owner == new_owner {
                return Ok(None);
            }
            let authority = OwnerChangeAuthority {
                roles,
                memberships,
                current_user: &current_user,
                new_owner,
            };
            authority.require_owner_change(
                &security.role_owner,
                "foreign table",
                &relation.name,
            )?;
            authority.require_schema_create(context.roles.schemas, &relation.schema)?;
            let table = context.catalog.table(&relation).ok_or_else(|| {
                SQLError::Internal(format!("foreign table `{name}` disappeared before update"))
            })?;
            rewrite_acl_owner(&mut security, new_owner);
            let columns = table
                .columns
                .iter()
                .map(|column| column.name.clone())
                .collect::<Vec<_>>();
            validate_table_security_invariants(&security, Some(&columns), roles)
                .map_err(|error| SQLError::Internal(format!("foreign table `{name}` produced invalid ownership metadata after owner transfer: {error}")))?;
            Ok(Some((
                relation,
                table.object_id,
                BoundTableSecurity::bind(&security, roles).map_err(SQLError::Internal)?,
            )))
        },
    )?;
    let Some((relation, object_id, security)) = value else {
        return Ok(());
    };
    let sequence_updates = table_owned_sequence_owner_updates(
        context.owned_sequences,
        object_id,
        owner.require_name(&roles)?,
        &roles,
    )?;
    for (sequence, sequence_security) in &sequence_updates {
        context.sequence_publication.persist_security(
            &sequence.qualified_name(),
            sequence,
            sequence_security,
        )?;
    }
    context.publication.persist_security(&relation, &security)?;
    context
        .publication
        .security_write()
        .insert(relation.clone(), security);
    if !sequence_updates.is_empty() {
        let mut registry = context.publication.sequence_security_write();
        for (sequence, sequence_security) in sequence_updates {
            registry.insert(sequence, sequence_security);
        }
    }
    drop(memberships);
    drop(roles);
    context.changes.catalog_registry_changed();
    context.changes.prepared_relation_changed(&relation);
    Ok(())
}

fn rename_foreign_table(
    context: &ForeignTableAlterContext<'_>,
    relation: &RelationIdentity,
    new_name: &str,
) -> Result<(), SQLError> {
    let target = relation_alteration::relation_rename_target(
        context.names,
        relation,
        new_name,
        "ALTER FOREIGN TABLE RENAME TO",
    )?;
    rename_foreign_table_to(context, relation, target)
}

/// Move a foreign table into `schema` under its own name while its schema is renamed.
pub fn relocate_foreign_table(
    context: &ForeignTableAlterContext<'_>,
    relation: &RelationIdentity,
    schema: &str,
) -> Result<(), SQLError> {
    let target = RelationIdentity::new(schema, &relation.name);
    if context
        .names
        .relation_kind_at(&target.qualified_name())
        .map_err(|error| {
            SQLError::Internal(format!(
                "check schema rename target `{}`: {error}",
                target.qualified_name()
            ))
        })?
        .is_some()
    {
        return Err(SQLError::Internal(format!(
            "schema rename target `{}` already exists",
            target.qualified_name()
        )));
    }
    rename_foreign_table_to(context, relation, target)
}

fn rename_foreign_table_to(
    context: &ForeignTableAlterContext<'_>,
    relation: &RelationIdentity,
    target: RelationIdentity,
) -> Result<(), SQLError> {
    let current = context
        .catalog
        .table(relation)
        .ok_or_else(|| SQLError::Internal("renamed foreign table disappeared".into()))?;
    let array_name = super::types::relation_arrays::rename(
        &context.creation,
        relation,
        &target,
        current.row_type_array_name.as_deref(),
        current.relation_oids().array_type,
    )?;
    rewrite_relation_rename_dependents(context.dependencies, relation, &target).map_err(
        |error| {
            SQLError::Internal(format!(
                "rewrite dependencies while renaming foreign table `{}`: {error}",
                relation.qualified_name()
            ))
        },
    )?;
    if current.persistence != uqa_sql::ast::RelationPersistence::Temporary
        && context
            .publication
            .persist_rename(relation, &target)
            .map_err(|error| {
                SQLError::Internal(format!(
                    "persist foreign table rename `{}` to `{}`: {error}",
                    relation.qualified_name(),
                    target.qualified_name()
                ))
            })?
            == Some(false)
    {
        return Err(SQLError::Internal(format!(
            "foreign table `{}` disappeared during rename",
            relation.qualified_name()
        )));
    }
    let mut tables = context.publication.tables_write();
    let mut security = context.publication.security_write();
    if tables.contains_key(&target) || security.contains_key(&target) {
        return Err(SQLError::Internal(format!(
            "foreign table rename target `{}` appeared after preflight",
            target.qualified_name()
        )));
    }
    let mut table = tables.remove(relation).ok_or_else(|| {
        SQLError::Internal(format!(
            "foreign table `{}` disappeared during rename",
            relation.qualified_name()
        ))
    })?;
    let table_security = security.remove(relation).ok_or_else(|| {
        SQLError::Internal(format!(
            "foreign table `{}` lost its security metadata during rename",
            relation.qualified_name()
        ))
    })?;
    table.name = target.qualified_name();
    table.row_type_array_name = array_name;
    tables.insert(target.clone(), table);
    security.insert(target.clone(), table_security);
    drop(security);
    drop(tables);
    let renamed = context
        .catalog
        .table(&target)
        .ok_or_else(|| SQLError::Internal("renamed foreign table disappeared".into()))?;
    context
        .publication
        .persist_definition(&target, &renamed)
        .map_err(|error| uqa_sql::catalog::errors::storage_error("foreign array rename", &error))?;
    let mut memory_tables = context.publication.memory_tables_write();
    if let Some(rows) = memory_tables.remove(relation) {
        memory_tables.insert(target.clone(), rows);
    }
    drop(memory_tables);
    context.changes.catalog_registry_changed();
    context.changes.prepared_relation_changed(&target);
    Ok(())
}