uqa_execution/catalog/
graph.rs1use super::{CatalogReadView, RelationNameResolution};
8use uqa_core::RelationIdentity;
9use uqa_graph::{GraphLabelInfo, LabelKind};
10use uqa_sql::catalog::{age_agtype, age_graphid};
11use uqa_sql::{ColumnType, SQLError};
12
13pub struct GraphCatalogEntry {
15 pub name: String,
16 pub labels: Vec<GraphLabelInfo>,
17}
18
19#[derive(Clone)]
20pub struct AgeLabelRelation {
21 pub graph: String,
22 pub label: GraphLabelInfo,
23}
24
25impl AgeLabelRelation {
26 pub fn canonical_name(&self) -> String {
27 format!(
28 "{}.{}",
29 uqa_sql::expr::quote_ident(&self.graph),
30 uqa_sql::expr::quote_ident(&self.label.name)
31 )
32 }
33}
34
35pub fn graph_catalog_entries(
36 catalog: &CatalogReadView,
37) -> Result<Vec<GraphCatalogEntry>, SQLError> {
38 catalog
39 .read_graph_names()?
40 .into_iter()
41 .map(|name| {
42 let labels = catalog.graph_labels(&name)?.ok_or_else(|| {
43 SQLError::Internal(format!("graph `{name}` disappeared from catalog snapshot"))
44 })?;
45 Ok(GraphCatalogEntry { name, labels })
46 })
47 .collect()
48}
49
50pub fn resolve_age_label_relation(
54 catalog: &CatalogReadView,
55 resolution: &RelationNameResolution,
56 name: &str,
57) -> Result<Option<AgeLabelRelation>, SQLError> {
58 let (schema, label_name) = RelationIdentity::parse_reference(name).map_err(|error| {
59 SQLError::Internal(format!("invalid AGE label relation `{name}`: {error}"))
60 })?;
61 let graph_names =
62 schema.map_or_else(|| resolution.search_path().to_vec(), |schema| vec![schema]);
63 for graph_name in graph_names {
64 let Some(labels) = catalog.graph_labels(&graph_name)? else {
65 continue;
66 };
67 if let Some(label) = labels.into_iter().find(|label| label.name == label_name) {
68 return Ok(Some(AgeLabelRelation {
69 graph: graph_name,
70 label,
71 }));
72 }
73 }
74 Ok(None)
75}
76
77pub fn resolve_age_label_relation_name(
78 catalog: &CatalogReadView,
79 resolution: &RelationNameResolution,
80 name: &str,
81) -> Result<Option<String>, SQLError> {
82 Ok(resolve_age_label_relation(catalog, resolution, name)?
83 .map(|relation| relation.canonical_name()))
84}
85
86pub fn age_label_relation_schema(
87 catalog: &CatalogReadView,
88 resolution: &RelationNameResolution,
89 name: &str,
90) -> Result<Option<Vec<(String, ColumnType)>>, SQLError> {
91 let Some(relation) = resolve_age_label_relation(catalog, resolution, name)? else {
92 return Ok(None);
93 };
94 let columns = match relation.label.kind {
95 LabelKind::Vertex => vec![
96 ("id".into(), age_graphid()),
97 ("properties".into(), age_agtype()),
98 ],
99 LabelKind::Edge => vec![
100 ("id".into(), age_graphid()),
101 ("start_id".into(), age_graphid()),
102 ("end_id".into(), age_graphid()),
103 ("properties".into(), age_agtype()),
104 ],
105 };
106 Ok(Some(columns))
107}
108
109impl AgeLabelRelation {
110 pub fn includes_graphid(&self, graphid: u64) -> bool {
111 self.label.id == self.label.kind.default_label_id()
112 || uqa_graph::graphid_label_id(graphid) == self.label.id
113 }
114}