Skip to main content

hawdb_sql/pgq/
catalog.rs

1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::BTreeMap;
16
17use super::PgqDataType;
18
19pub trait PgqCatalog {
20    fn property_graph(&self, name: &[String]) -> Option<&PropertyGraphSchema>;
21}
22
23#[derive(Debug, Default, Clone, PartialEq, Eq)]
24pub struct PropertyGraphCatalog {
25    graphs: BTreeMap<Vec<String>, PropertyGraphSchema>,
26}
27
28impl PropertyGraphCatalog {
29    pub fn insert(&mut self, graph: PropertyGraphSchema) -> Option<PropertyGraphSchema> {
30        self.graphs.insert(graph.name.clone(), graph)
31    }
32}
33
34impl PgqCatalog for PropertyGraphCatalog {
35    fn property_graph(&self, name: &[String]) -> Option<&PropertyGraphSchema> {
36        self.graphs.get(name)
37    }
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct PropertyGraphSchema {
42    pub name: Vec<String>,
43    pub vertex_labels: BTreeMap<String, PropertyGraphElementSchema>,
44    pub edge_labels: BTreeMap<String, PropertyGraphElementSchema>,
45}
46
47impl PropertyGraphSchema {
48    pub fn new(name: impl IntoIterator<Item = impl Into<String>>) -> Self {
49        Self {
50            name: name.into_iter().map(Into::into).collect(),
51            vertex_labels: BTreeMap::new(),
52            edge_labels: BTreeMap::new(),
53        }
54    }
55
56    pub fn add_vertex_label(
57        &mut self,
58        name: impl Into<String>,
59        schema: PropertyGraphElementSchema,
60    ) {
61        self.vertex_labels.insert(name.into(), schema);
62    }
63
64    pub fn add_edge_label(&mut self, name: impl Into<String>, schema: PropertyGraphElementSchema) {
65        self.edge_labels.insert(name.into(), schema);
66    }
67}
68
69#[derive(Debug, Default, Clone, PartialEq, Eq)]
70pub struct PropertyGraphElementSchema {
71    pub properties: BTreeMap<String, PgqDataType>,
72}
73
74impl PropertyGraphElementSchema {
75    pub fn with_property(mut self, name: impl Into<String>, data_type: PgqDataType) -> Self {
76        self.properties.insert(name.into(), data_type);
77        self
78    }
79}
80
81#[derive(Debug, Default, Clone, PartialEq, Eq)]
82pub struct PgqBindingContext {
83    pub outer_columns: BTreeMap<Vec<String>, PgqDataType>,
84}
85
86impl PgqBindingContext {
87    pub fn with_outer_column(
88        mut self,
89        name: impl IntoIterator<Item = impl Into<String>>,
90        data_type: PgqDataType,
91    ) -> Self {
92        self.outer_columns
93            .insert(name.into_iter().map(Into::into).collect(), data_type);
94        self
95    }
96}