Skip to content

Commit 0fbae07

Browse files
feat: enable empty by in range select (apache#8)
1 parent 602d787 commit 0fbae07

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/parser/mod.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use alloc::{
2020
vec,
2121
vec::Vec,
2222
};
23+
#[cfg(feature = "bigdecimal-sql")]
24+
use bigdecimal::BigDecimal;
2325
use core::fmt;
2426
use std::collections::BTreeSet;
2527

@@ -5769,9 +5771,25 @@ impl<'a> Parser<'a> {
57695771
value.verify_duration()?;
57705772
let by = if self.parse_keyword(Keyword::BY) {
57715773
self.expect_token(&Token::LParen)?;
5772-
let by = self.parse_comma_separated(Parser::parse_expr)?;
5773-
self.expect_token(&Token::RParen)?;
5774-
by
5774+
if self.consume_token(&Token::RParen) {
5775+
// for case like `by ()`
5776+
// The user explicitly specifies that the aggregation key is empty. In this case, there is no aggregation key.
5777+
// All data will be aggregated into a group, which is equivalent to using a random constant as the aggregation key.
5778+
// Therefore, in this case, the constant 1 is used directly as the aggregation key.
5779+
// `()` == `(1)`
5780+
#[cfg(not(feature = "bigdecimal-sql"))]
5781+
{
5782+
vec![Expr::Value(Value::Number("1".into(), false))]
5783+
}
5784+
#[cfg(feature = "bigdecimal-sql")]
5785+
{
5786+
vec![Expr::Value(Value::Number(BigDecimal::from(1), false))]
5787+
}
5788+
} else {
5789+
let by = self.parse_comma_separated(Parser::parse_expr)?;
5790+
self.expect_token(&Token::RParen)?;
5791+
by
5792+
}
57755793
} else {
57765794
vec![]
57775795
};

tests/sqlparser_common.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7415,6 +7415,10 @@ fn parse_range_select() {
74157415
assert_sql("SELECT rate(metrics) RANGE '5m', sum(metrics) RANGE '10m' FILL MAX, sum(metrics) RANGE '10m' FROM t ALIGN '1h' by ((a+1)/2, b) FILL NULL;",
74167416
"SELECT range_fn(rate(metrics), '5m', 'NULL', '2', (a + 1) / 2, b, '1h'), range_fn(sum(metrics), '10m', 'MAX', '2', (a + 1) / 2, b, '1h'), range_fn(sum(metrics), '10m', 'NULL', '2', (a + 1) / 2, b, '1h') FROM t GROUP BY a, b");
74177417

7418+
// explicit empty by
7419+
assert_sql("SELECT rate(metrics) RANGE '5m', sum(metrics) RANGE '10m' FILL MAX, sum(metrics) RANGE '10m' FROM t ALIGN '1h' by () FILL NULL;",
7420+
"SELECT range_fn(rate(metrics), '5m', 'NULL', '1', 1, '1h'), range_fn(sum(metrics), '10m', 'MAX', '1', 1, '1h'), range_fn(sum(metrics), '10m', 'NULL', '1', 1, '1h') FROM t");
7421+
74187422
// expression1
74197423
assert_sql(
74207424
"SELECT avg(a/2 + 1) RANGE '5m' FILL NULL FROM t ALIGN '1h' FILL NULL;",

0 commit comments

Comments
 (0)