Skip to main content

databend_common_ast/ast/statements/
virtual_column.rs

1// Copyright 2021 Datafuse Labs
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::Display;
16use std::fmt::Formatter;
17
18use derive_visitor::Drive;
19use derive_visitor::DriveMut;
20
21use crate::ast::Expr;
22use crate::ast::Identifier;
23use crate::ast::ShowLimit;
24use crate::ast::write_dot_separated_list;
25
26#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
27pub struct RefreshVirtualColumnStmt {
28    pub catalog: Option<Identifier>,
29    pub database: Option<Identifier>,
30    pub table: Identifier,
31    pub selection: Option<Box<Expr>>,
32    pub limit: Option<u64>,
33    pub overwrite: bool,
34}
35
36impl Display for RefreshVirtualColumnStmt {
37    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
38        write!(f, "REFRESH VIRTUAL COLUMN ON ")?;
39        write_dot_separated_list(
40            f,
41            self.catalog
42                .iter()
43                .chain(&self.database)
44                .chain(Some(&self.table)),
45        )?;
46        if let Some(selection) = &self.selection {
47            write!(f, " WHERE {selection}")?;
48        }
49        if let Some(limit) = self.limit {
50            write!(f, " LIMIT {limit}")?;
51        }
52        if self.overwrite {
53            write!(f, " OVERWRITE")?;
54        }
55        Ok(())
56    }
57}
58
59#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
60pub struct ShowVirtualColumnsStmt {
61    pub catalog: Option<Identifier>,
62    pub database: Option<Identifier>,
63    pub table: Option<Identifier>,
64    pub limit: Option<ShowLimit>,
65}
66
67impl Display for ShowVirtualColumnsStmt {
68    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
69        write!(f, "SHOW VIRTUAL COLUMNS")?;
70        if let Some(table) = &self.table {
71            write!(f, " FROM {}", table)?;
72        }
73        if let Some(database) = &self.database {
74            write!(f, " FROM ")?;
75            if let Some(catalog) = &self.catalog {
76                write!(f, "{catalog}.",)?;
77            }
78            write!(f, "{database}")?;
79        }
80
81        if let Some(limit) = &self.limit {
82            write!(f, " {limit}")?;
83        }
84
85        Ok(())
86    }
87}
88
89#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
90pub struct VacuumVirtualColumnStmt {
91    pub catalog: Option<Identifier>,
92    pub database: Option<Identifier>,
93    pub table: Identifier,
94}
95
96impl Display for VacuumVirtualColumnStmt {
97    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
98        write!(f, "VACUUM VIRTUAL COLUMN FROM ")?;
99        write_dot_separated_list(
100            f,
101            self.catalog
102                .iter()
103                .chain(&self.database)
104                .chain(Some(&self.table)),
105        )
106    }
107}