From 9ea67ca534de481eccc7b34f1bf3a40f9d9e03c8 Mon Sep 17 00:00:00 2001 From: Amogh-Bharadwaj Date: Mon, 6 May 2024 21:39:56 +0530 Subject: [PATCH 1/2] support clickhouse for sqlparser --- src/ast/mod.rs | 2 ++ src/keywords.rs | 1 + src/parser/mod.rs | 3 +++ tests/sqlparser_postgres.rs | 29 +++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index bf5c035..e47e429 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -2876,6 +2876,7 @@ pub enum PeerType { Eventhubs, PubSub, Elasticsearch, + Clickhouse, } impl fmt::Display for PeerType { @@ -2892,6 +2893,7 @@ impl fmt::Display for PeerType { PeerType::Eventhubs => write!(f, "EVENTHUBS"), PeerType::PubSub => write!(f, "PUBSUB"), PeerType::Elasticsearch => write!(f, "ELASTICSEARCH"), + PeerType::Clickhouse => write!(f, "CLICKHOUSE"), } } } diff --git a/src/keywords.rs b/src/keywords.rs index a3c32df..0b9cc03 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -147,6 +147,7 @@ define_keywords!( CHARSET, CHAR_LENGTH, CHECK, + CLICKHOUSE, CLOB, CLONE, CLOSE, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b7cebe0..353bc25 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9828,6 +9828,7 @@ impl<'a> Parser<'a> { Keyword::EVENTHUBS, Keyword::PUBSUB, Keyword::ELASTICSEARCH, + Keyword::CLICKHOUSE ]) { Some(Keyword::BIGQUERY) => Ok(PeerType::Bigquery), Some(Keyword::MONGO) => Ok(PeerType::Mongo), @@ -9840,6 +9841,7 @@ impl<'a> Parser<'a> { Some(Keyword::EVENTHUBS) => Ok(PeerType::Eventhubs), Some(Keyword::PUBSUB) => Ok(PeerType::PubSub), Some(Keyword::ELASTICSEARCH) => Ok(PeerType::Elasticsearch), + Some(Keyword::CLICKHOUSE) => Ok(PeerType::Clickhouse), other => { let supported_peer_types = [ "BIGQUERY", @@ -9853,6 +9855,7 @@ impl<'a> Parser<'a> { "EVENTHUBS", "PUBSUB", "ELASTICSEARCH", + "CLICKHOUSE" ]; let err = format!( "expected peertype as one of {}, got {:#?}", diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index cb701a4..ed0433b 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -3678,6 +3678,35 @@ fn parse_create_s3_peer() { } } +#[test] +fn parse_create_clickhouse_peer() { + match pg().verified_stmt( + "CREATE PEER clickhouse_1 FROM CLICKHOUSE WITH \ + (host = 'http://clickhouse-server:8123')", + ) { + Statement::CreatePeer { + if_not_exists: _, + peer_name: _, + peer_type, + with_options, + } => { + assert_eq!(peer_type, PeerType::Clickhouse); + assert_eq!( + with_options, + vec![ + SqlOption { + name: Ident::new("host"), + value: Expr::Value(Value::SingleQuotedString(String::from( + "http://clickhouse-server:8123" + ))) + }, + ] + ); + } + _ => unreachable!(), + } +} + #[test] fn parse_create_single_mirror() { match pg().verified_stmt("CREATE MIRROR IF NOT EXISTS test_mirror FROM p1 TO p2 WITH TABLE MAPPING ({from : s1.t1, to : s2.t2}) WITH (key1 = 'value1')") { From f11c4fcfd0a5644d6b20508e3bd79df68a6cc114 Mon Sep 17 00:00:00 2001 From: Amogh-Bharadwaj Date: Mon, 6 May 2024 21:42:45 +0530 Subject: [PATCH 2/2] lint --- src/parser/mod.rs | 4 ++-- tests/sqlparser_postgres.rs | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 353bc25..087aee3 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9828,7 +9828,7 @@ impl<'a> Parser<'a> { Keyword::EVENTHUBS, Keyword::PUBSUB, Keyword::ELASTICSEARCH, - Keyword::CLICKHOUSE + Keyword::CLICKHOUSE, ]) { Some(Keyword::BIGQUERY) => Ok(PeerType::Bigquery), Some(Keyword::MONGO) => Ok(PeerType::Mongo), @@ -9855,7 +9855,7 @@ impl<'a> Parser<'a> { "EVENTHUBS", "PUBSUB", "ELASTICSEARCH", - "CLICKHOUSE" + "CLICKHOUSE", ]; let err = format!( "expected peertype as one of {}, got {:#?}", diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index ed0433b..53bc135 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -3693,14 +3693,12 @@ fn parse_create_clickhouse_peer() { assert_eq!(peer_type, PeerType::Clickhouse); assert_eq!( with_options, - vec![ - SqlOption { - name: Ident::new("host"), - value: Expr::Value(Value::SingleQuotedString(String::from( - "http://clickhouse-server:8123" - ))) - }, - ] + vec![SqlOption { + name: Ident::new("host"), + value: Expr::Value(Value::SingleQuotedString(String::from( + "http://clickhouse-server:8123" + ))) + },] ); } _ => unreachable!(),