Skip to content

Support for ClickHouse array types (e.g. [1,2,3]) #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ impl fmt::Display for ObjectName {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Array {
pub elem: Vec<Expr>,
pub named: bool,
}

impl fmt::Display for Array {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}[{}]",
if self.named { "ARRAY" } else { "" },
display_comma_separated(&self.elem)
)
}
}

/// An SQL expression of any type.
///
/// The parser does not distinguish between expressions of different types
Expand Down Expand Up @@ -298,7 +316,7 @@ pub enum Expr {
indexs: Vec<Expr>,
},
/// An array expression e.g. `ARRAY[1, 2]`
Array(Vec<Expr>),
Array(Array),
}

impl fmt::Display for Expr {
Expand Down Expand Up @@ -482,7 +500,7 @@ impl fmt::Display for Expr {
Ok(())
}
Expr::Array(set) => {
write!(f, "ARRAY[{}]", display_comma_separated(set))
write!(f, "{}", set)
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl<'a> Parser<'a> {
Keyword::TRIM => self.parse_trim_expr(),
Keyword::INTERVAL => self.parse_literal_interval(),
Keyword::LISTAGG => self.parse_listagg_expr(),
Keyword::ARRAY => self.parse_array_expr(),
Keyword::ARRAY => self.parse_array_expr(true),
Keyword::NOT => Ok(Expr::UnaryOp {
op: UnaryOperator::Not,
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
Expand Down Expand Up @@ -450,6 +450,7 @@ impl<'a> Parser<'a> {
_ => Ok(Expr::Identifier(w.to_ident())),
},
}, // End of Token::Word
Token::LBracket => self.parse_array_expr(false),
tok @ Token::Minus | tok @ Token::Plus => {
let op = if tok == Token::Plus {
UnaryOperator::Plus
Expand Down Expand Up @@ -825,11 +826,13 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_array_expr(&mut self) -> Result<Expr, ParserError> {
self.expect_token(&Token::LBracket)?;
pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
if named {
self.expect_token(&Token::LBracket)?;
}
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
self.expect_token(&Token::RBracket)?;
Ok(Expr::Array(exprs))
Ok(Expr::Array(Array { elem: exprs, named }))
}

/// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`.
Expand Down
11 changes: 7 additions & 4 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,13 @@ fn parse_array_index_expr() {
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Nested(Box::new(Expr::Cast {
expr: Box::new(Expr::Array(vec![Expr::Array(vec![
num[2].clone(),
num[3].clone(),
])])),
expr: Box::new(Expr::Array(Array {
elem: vec![Expr::Array(Array {
elem: vec![num[2].clone(), num[3].clone(),],
named: true,
})],
named: true,
})),
data_type: DataType::Array(Box::new(DataType::Array(Box::new(DataType::Int(
None
)))))
Expand Down
25 changes: 21 additions & 4 deletions tests/sqpparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#[macro_use]
mod test_utils;

use test_utils::*;

use sqlparser::ast::Expr::{Identifier, MapAccess};
Expand All @@ -30,7 +31,7 @@ fn parse_map_access_expr() {
&MapAccess {
column: Box::new(Identifier(Ident {
value: "string_values".to_string(),
quote_style: None
quote_style: None,
})),
keys: vec![Expr::Function(Function {
name: ObjectName(vec!["indexOf".into()]),
Expand All @@ -40,16 +41,32 @@ fn parse_map_access_expr() {
)))),
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
Value::SingleQuotedString("endpoint".to_string())
)))
))),
],
over: None,
distinct: false
})]
distinct: false,
})],
},
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_array_expr() {
let sql = "SELECT ['1', '2'] FROM test";
let select = clickhouse().verified_only_select(sql);
assert_eq!(
&Expr::Array(Array {
elem: vec![
Expr::Value(Value::SingleQuotedString("1".to_string())),
Expr::Value(Value::SingleQuotedString("2".to_string()))
],
named: false,
}),
expr_from_projection(only(&select.projection))
)
}

fn clickhouse() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {})],
Expand Down