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 crate::ScalarExpr;
10use std::collections::{BTreeMap, BTreeSet};
11
12#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
13pub struct RelationMetadataProjection(u8);
14
15impl RelationMetadataProjection {
16    const DOC_ID: u8 = 1;
17    const SCORE: u8 = 2;
18
19    pub fn request_doc_id(&mut self) {
20        self.0 |= Self::DOC_ID;
21    }
22
23    pub fn request_score(&mut self) {
24        self.0 |= Self::SCORE;
25    }
26
27    pub fn includes_doc_id(self) -> bool {
28        self.0 & Self::DOC_ID != 0
29    }
30
31    pub fn includes_score(self) -> bool {
32        self.0 & Self::SCORE != 0
33    }
34
35    pub fn is_empty(self) -> bool {
36        self.0 == 0
37    }
38}
39
40#[derive(Debug, Clone, Default)]
41pub struct SourceProjection {
42    columns: BTreeSet<String>,
43    retain_all: bool,
44    metadata: RelationMetadataProjection,
45}
46
47impl SourceProjection {
48    pub fn retaining_all() -> Self {
49        Self {
50            retain_all: true,
51            ..Self::default()
52        }
53    }
54
55    pub fn contains(&self, column: &str) -> bool {
56        self.retain_all || self.columns.contains(column)
57    }
58
59    pub fn retain_all(&mut self) {
60        self.retain_all = true;
61    }
62
63    pub fn insert(&mut self, column: String) {
64        self.columns.insert(column);
65    }
66
67    pub fn extend(&mut self, columns: impl IntoIterator<Item = String>) {
68        self.columns.extend(columns);
69    }
70
71    pub fn explicit_columns(self) -> Option<BTreeSet<String>> {
72        (!self.retain_all).then_some(self.columns)
73    }
74
75    pub fn metadata(&self) -> RelationMetadataProjection {
76        self.metadata
77    }
78
79    pub fn metadata_mut(&mut self) -> &mut RelationMetadataProjection {
80        &mut self.metadata
81    }
82}
83
84pub type ColumnPrune = BTreeMap<String, SourceProjection>;
85pub type QualifierFilters = BTreeMap<String, Vec<ScalarExpr>>;