Skip to main content

uqa_sql/plan/
source_projection.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Required SQL source columns and explicitly requested relation metadata.
8
9use std::collections::{BTreeMap, BTreeSet};
10
11#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
12pub struct RelationMetadataProjection(u8);
13
14impl RelationMetadataProjection {
15    const DOC_ID: u8 = 1;
16    const SCORE: u8 = 2;
17
18    pub fn request_doc_id(&mut self) {
19        self.0 |= Self::DOC_ID;
20    }
21
22    pub fn request_score(&mut self) {
23        self.0 |= Self::SCORE;
24    }
25
26    pub fn includes_doc_id(self) -> bool {
27        self.0 & Self::DOC_ID != 0
28    }
29
30    pub fn includes_score(self) -> bool {
31        self.0 & Self::SCORE != 0
32    }
33
34    pub fn is_empty(self) -> bool {
35        self.0 == 0
36    }
37}
38
39#[derive(Debug, Clone, Default)]
40pub struct SourceProjection {
41    columns: BTreeSet<String>,
42    retain_all: bool,
43    metadata: RelationMetadataProjection,
44}
45
46impl SourceProjection {
47    pub fn retaining_all() -> Self {
48        Self {
49            retain_all: true,
50            ..Self::default()
51        }
52    }
53
54    pub fn contains(&self, column: &str) -> bool {
55        self.retain_all || self.columns.contains(column)
56    }
57
58    pub fn retain_all(&mut self) {
59        self.retain_all = true;
60    }
61
62    pub fn insert(&mut self, column: String) {
63        self.columns.insert(column);
64    }
65
66    pub fn extend(&mut self, columns: impl IntoIterator<Item = String>) {
67        self.columns.extend(columns);
68    }
69
70    pub fn explicit_columns(self) -> Option<BTreeSet<String>> {
71        (!self.retain_all).then_some(self.columns)
72    }
73
74    /// Named field demands remain available when other expressions require the source's complete declared row.
75    pub fn requested_columns(&self) -> impl Iterator<Item = &str> {
76        self.columns.iter().map(String::as_str)
77    }
78
79    pub fn metadata(&self) -> RelationMetadataProjection {
80        self.metadata
81    }
82
83    pub fn metadata_mut(&mut self) -> &mut RelationMetadataProjection {
84        &mut self.metadata
85    }
86}
87
88pub type ColumnPrune = BTreeMap<String, SourceProjection>;
89mod qualifier_filters;
90pub use qualifier_filters::QualifierFilters;
91
92pub type CteOutputFilter = (
93    uqa_core::memory::Produced<String>,
94    crate::plan::OwnedScalarExpr,
95);
96pub type CteOutputFilters = crate::binding::production::BindingMap<CteOutputFilter>;