reifydb_core/interface/
identifier.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use reifydb_type::Fragment;
5use serde::{Deserialize, Serialize};
6
7// NOTE: ColumnIdentifier is kept temporarily for backward compatibility with the expression system.
8// It should be replaced with proper resolved types in the future.
9
10/// Column identifier with source qualification
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12pub struct ColumnIdentifier<'a> {
13	pub source: ColumnSource<'a>,
14	pub name: Fragment<'a>,
15}
16
17impl<'a> ColumnIdentifier<'a> {
18	pub fn with_source(namespace: Fragment<'a>, source: Fragment<'a>, name: Fragment<'a>) -> Self {
19		Self {
20			source: ColumnSource::Source {
21				namespace,
22				source,
23			},
24			name,
25		}
26	}
27
28	pub fn with_alias(alias: Fragment<'a>, name: Fragment<'a>) -> Self {
29		Self {
30			source: ColumnSource::Alias(alias),
31			name,
32		}
33	}
34
35	pub fn into_owned(self) -> ColumnIdentifier<'static> {
36		ColumnIdentifier {
37			source: self.source.into_owned(),
38			name: Fragment::Owned(self.name.into_owned()),
39		}
40	}
41
42	pub fn to_static(&self) -> ColumnIdentifier<'static> {
43		ColumnIdentifier {
44			source: self.source.to_static(),
45			name: Fragment::owned_internal(self.name.text()),
46		}
47	}
48}
49
50/// How a column is qualified in plans (always fully qualified)
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52pub enum ColumnSource<'a> {
53	/// Fully qualified by namespace.source
54	Source {
55		namespace: Fragment<'a>,
56		source: Fragment<'a>,
57	},
58	/// Qualified by alias (which maps to a fully qualified source)
59	Alias(Fragment<'a>),
60}
61
62impl<'a> ColumnSource<'a> {
63	pub fn into_owned(self) -> ColumnSource<'static> {
64		match self {
65			ColumnSource::Source {
66				namespace,
67				source,
68			} => ColumnSource::Source {
69				namespace: Fragment::Owned(namespace.into_owned()),
70				source: Fragment::Owned(source.into_owned()),
71			},
72			ColumnSource::Alias(alias) => ColumnSource::Alias(Fragment::Owned(alias.into_owned())),
73		}
74	}
75
76	pub fn to_static(&self) -> ColumnSource<'static> {
77		match self {
78			ColumnSource::Source {
79				namespace,
80				source,
81			} => ColumnSource::Source {
82				namespace: Fragment::owned_internal(namespace.text()),
83				source: Fragment::owned_internal(source.text()),
84			},
85			ColumnSource::Alias(alias) => ColumnSource::Alias(Fragment::owned_internal(alias.text())),
86		}
87	}
88
89	pub fn as_fragment(&self) -> &Fragment<'a> {
90		match self {
91			ColumnSource::Source {
92				source,
93				..
94			} => source,
95			ColumnSource::Alias(alias) => alias,
96		}
97	}
98}