Skip to content

Commit

Permalink
refactor(ast): add derive-visitor for expr and query (databendlabs#14814
Browse files Browse the repository at this point in the history
)

* refactor(ast): add derive-visitor for expr and query

* fix

* fix

* fix

* fix
  • Loading branch information
andylokandy authored Mar 3, 2024
1 parent ed38356 commit 9509cd7
Show file tree
Hide file tree
Showing 44 changed files with 6,409 additions and 5,410 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ tonic-reflection = { version = "0.10.2" }
typetag = "0.2.3"
uuid = { version = "1.1.2", features = ["serde", "v4"] }
walkdir = "2.3.2"
derive-visitor = "0.3.0"

# Future and async
futures = "0.3.24"
Expand Down
1 change: 1 addition & 0 deletions src/query/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ databend-common-io = { path = "../../common/io" }
databend-common-meta-app = { path = "../../meta/app" }

# Crates.io dependencies
derive-visitor = { workspace = true }
enum-as-inner = "0.5.1"
ethnum = { workspace = true }
fast-float = "0.2.0"
Expand Down
46 changes: 39 additions & 7 deletions src/query/ast/src/ast/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ use std::fmt::Display;
use std::fmt::Formatter;

use databend_common_exception::Span;
use derive_visitor::Drive;
use derive_visitor::DriveMut;

use crate::parser::quote::quote_ident;

// Identifier of table name or column name.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub struct Identifier {
#[drive(skip)]
pub span: Span,
#[drive(skip)]
pub name: String,
#[drive(skip)]
pub quote: Option<char>,
}

Expand Down Expand Up @@ -60,10 +65,13 @@ impl Display for Identifier {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub struct ColumnPosition {
#[drive(skip)]
pub span: Span,
#[drive(skip)]
pub pos: usize,
#[drive(skip)]
pub name: String,
}

Expand All @@ -86,7 +94,7 @@ impl Display for ColumnPosition {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub enum ColumnID {
Name(Identifier),
Position(ColumnPosition),
Expand All @@ -110,7 +118,23 @@ impl Display for ColumnID {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub struct DatabaseRef {
pub catalog: Option<Identifier>,
pub database: Identifier,
}

impl Display for DatabaseRef {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(catalog) = &self.catalog {
write!(f, "{}.", catalog)?;
}
write!(f, "{}", self.database)?;
Ok(())
}
}

#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub struct TableRef {
pub catalog: Option<Identifier>,
pub database: Option<Identifier>,
Expand All @@ -121,7 +145,7 @@ impl Display for TableRef {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
assert!(self.catalog.is_none() || (self.catalog.is_some() && self.database.is_some()));
if let Some(catalog) = &self.catalog {
write!(f, "{catalog}.")?;
write!(f, "{}.", catalog)?;
}
if let Some(database) = &self.database {
write!(f, "{}.", database)?;
Expand All @@ -131,7 +155,7 @@ impl Display for TableRef {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub struct ColumnRef {
pub database: Option<Identifier>,
pub table: Option<Identifier>,
Expand All @@ -140,13 +164,21 @@ pub struct ColumnRef {

impl Display for ColumnRef {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
assert!(self.database.is_none() || (self.database.is_some() && self.table.is_some()));

if f.alternate() {
write!(f, "{}", self.column)?;
return Ok(());
}

if let Some(database) = &self.database {
write!(f, "{}.", database)?;
}
if let Some(table) = &self.table {
write!(f, "{}.", table)?;
}
write!(f, "{}", self.column)
write!(f, "{}", self.column)?;
Ok(())
}
}

Expand Down
Loading

0 comments on commit 9509cd7

Please sign in to comment.