From 34635558d215773c3922618bab6313cd9e2a64da Mon Sep 17 00:00:00 2001 From: WU Jingdi Date: Tue, 12 Dec 2023 19:56:23 +0800 Subject: [PATCH] fix: support tailing commas in SQL (#2913) * fix: support tailing commas in SQL * fix: broken ci --- src/sql/src/parser.rs | 4 ++- tests-integration/src/tests/promql_test.rs | 36 +++++++++++++++---- .../standalone/common/select/dummy.result | 9 +++++ .../cases/standalone/common/select/dummy.sql | 3 ++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/sql/src/parser.rs b/src/sql/src/parser.rs index 91e9b191627c..3dc5ce1983e7 100644 --- a/src/sql/src/parser.rs +++ b/src/sql/src/parser.rs @@ -16,7 +16,7 @@ use snafu::ResultExt; use sqlparser::ast::Ident; use sqlparser::dialect::Dialect; use sqlparser::keywords::Keyword; -use sqlparser::parser::{Parser, ParserError}; +use sqlparser::parser::{Parser, ParserError, ParserOptions}; use sqlparser::tokenizer::{Token, TokenWithLocation}; use crate::ast::{Expr, ObjectName}; @@ -37,6 +37,7 @@ impl<'a> ParserContext<'a> { let mut stmts: Vec = Vec::new(); let parser = Parser::new(dialect) + .with_options(ParserOptions::new().with_trailing_commas(true)) .try_with_sql(sql) .context(SyntaxSnafu)?; let mut parser_ctx = ParserContext { sql, parser }; @@ -67,6 +68,7 @@ impl<'a> ParserContext<'a> { pub fn parse_function(sql: &'a str, dialect: &dyn Dialect) -> Result { let mut parser = Parser::new(dialect) + .with_options(ParserOptions::new().with_trailing_commas(true)) .try_with_sql(sql) .context(SyntaxSnafu)?; diff --git a/tests-integration/src/tests/promql_test.rs b/tests-integration/src/tests/promql_test.rs index 52aa567793f9..517f58ae72b3 100644 --- a/tests-integration/src/tests/promql_test.rs +++ b/tests-integration/src/tests/promql_test.rs @@ -37,8 +37,20 @@ async fn create_insert_query_assert( lookback: Duration, expected: &str, ) { - let _ = instance.do_query(create, QueryContext::arc()).await; - let _ = instance.do_query(insert, QueryContext::arc()).await; + instance + .do_query(create, QueryContext::arc()) + .await + .into_iter() + .for_each(|v| { + let _ = v.unwrap(); + }); + instance + .do_query(insert, QueryContext::arc()) + .await + .into_iter() + .for_each(|v| { + let _ = v.unwrap(); + }); let query = PromQuery { query: promql.to_string(), @@ -69,8 +81,20 @@ async fn create_insert_tql_assert( tql: &str, expected: &str, ) { - let _ = instance.do_query(create, QueryContext::arc()).await; - let _ = instance.do_query(insert, QueryContext::arc()).await; + instance + .do_query(create, QueryContext::arc()) + .await + .into_iter() + .for_each(|v| { + let _ = v.unwrap(); + }); + instance + .do_query(insert, QueryContext::arc()) + .await + .into_iter() + .for_each(|v| { + let _ = v.unwrap(); + }); let query_output = instance .do_query(tql, QueryContext::arc()) @@ -181,7 +205,7 @@ const AGGREGATORS_CREATE_TABLE: &str = r#"create table http_requests ( "group" string, "value" double, ts timestamp TIME INDEX, - PRIMARY KEY (job, instance, group), + PRIMARY KEY (job, instance, "group"), );"#; // load 5m @@ -193,7 +217,7 @@ const AGGREGATORS_CREATE_TABLE: &str = r#"create table http_requests ( // http_requests{job="app-server", instance="1", group="production"} 0+60x10 // http_requests{job="app-server", instance="0", group="canary"} 0+70x10 // http_requests{job="app-server", instance="1", group="canary"} 0+80x10 -const AGGREGATORS_INSERT_DATA: &str = r#"insert into http_requests(job, instance, group, value, ts) values +const AGGREGATORS_INSERT_DATA: &str = r#"insert into http_requests(job, instance, "group", value, ts) values ('api-server', '0', 'production', 100, 0), ('api-server', '1', 'production', 200, 0), ('api-server', '0', 'canary', 300, 0), diff --git a/tests/cases/standalone/common/select/dummy.result b/tests/cases/standalone/common/select/dummy.result index ccc8e8c7173f..aaa688e07932 100644 --- a/tests/cases/standalone/common/select/dummy.result +++ b/tests/cases/standalone/common/select/dummy.result @@ -131,6 +131,15 @@ select TO_UNIXTIME(b) from test_unixtime; | 27 | +------------------------------+ +-- TEST tailing commas support +select a, b, from test_unixtime; + ++----+---------------------+ +| a | b | ++----+---------------------+ +| 27 | 1970-01-01T00:00:27 | ++----+---------------------+ + DROP TABLE test_unixtime; Affected Rows: 0 diff --git a/tests/cases/standalone/common/select/dummy.sql b/tests/cases/standalone/common/select/dummy.sql index f6e8858815b3..10832fd4d9b5 100644 --- a/tests/cases/standalone/common/select/dummy.sql +++ b/tests/cases/standalone/common/select/dummy.sql @@ -36,4 +36,7 @@ select b from test_unixtime; select TO_UNIXTIME(b) from test_unixtime; +-- TEST tailing commas support +select a, b, from test_unixtime; + DROP TABLE test_unixtime;