Skip to content

Commit

Permalink
fix: support tailing commas in SQL (#2913)
Browse files Browse the repository at this point in the history
* fix: support tailing commas in SQL

* fix: broken ci
  • Loading branch information
Taylor-lagrange authored Dec 12, 2023
1 parent 8a74bd3 commit 3463555
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/sql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -37,6 +37,7 @@ impl<'a> ParserContext<'a> {
let mut stmts: Vec<Statement> = 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 };
Expand Down Expand Up @@ -67,6 +68,7 @@ impl<'a> ParserContext<'a> {

pub fn parse_function(sql: &'a str, dialect: &dyn Dialect) -> Result<Expr> {
let mut parser = Parser::new(dialect)
.with_options(ParserOptions::new().with_trailing_commas(true))
.try_with_sql(sql)
.context(SyntaxSnafu)?;

Expand Down
36 changes: 30 additions & 6 deletions tests-integration/src/tests/promql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/standalone/common/select/dummy.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/standalone/common/select/dummy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 3463555

Please sign in to comment.