table/
table_reference.rs

1// Copyright 2023 Greptime Team
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::fmt::{self, Display};
16
17use datafusion_common::TableReference as DfTableReference;
18
19/// Represents a resolved path to a table of the form “catalog.schema.table”
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21pub struct TableReference<'a> {
22    pub catalog: &'a str,
23    pub schema: &'a str,
24    pub table: &'a str,
25}
26
27pub type OwnedTableReference = TableReference<'static>;
28
29impl<'a> TableReference<'a> {
30    pub fn bare(table: &'a str) -> Self {
31        TableReference {
32            catalog: common_catalog::consts::DEFAULT_CATALOG_NAME,
33            schema: common_catalog::consts::DEFAULT_SCHEMA_NAME,
34            table,
35        }
36    }
37
38    pub fn full(catalog: &'a str, schema: &'a str, table: &'a str) -> Self {
39        TableReference {
40            catalog,
41            schema,
42            table,
43        }
44    }
45}
46
47impl Display for TableReference<'_> {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        write!(f, "{}.{}.{}", self.catalog, self.schema, self.table)
50    }
51}
52
53impl<'a> From<TableReference<'a>> for DfTableReference {
54    fn from(val: TableReference<'a>) -> Self {
55        DfTableReference::full(val.catalog, val.schema, val.table)
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_table_reference() {
65        let table_ref = TableReference {
66            catalog: "greptime",
67            schema: "public",
68            table: "test",
69        };
70
71        assert_eq!("greptime.public.test", table_ref.to_string());
72    }
73}