1use std::fmt::{self, Display};
16
17use datafusion_common::TableReference as DfTableReference;
18
19#[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}