parse grouping sets, rollup, and cube for postgresql (#366)

* parse grouping sets, rollup, and cube

* add postgresql flag
This commit is contained in:
Jiayu Liu 2021-12-11 03:45:09 +08:00 committed by GitHub
parent d7e84be3e1
commit 40d67aab87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 261 additions and 5 deletions

View file

@ -20,13 +20,14 @@
#[macro_use]
mod test_utils;
use test_utils::{all_dialects, expr_from_projection, join, number, only, table, table_alias};
use matches::assert_matches;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
use sqlparser::dialect::{GenericDialect, PostgreSqlDialect, SQLiteDialect};
use sqlparser::keywords::ALL_KEYWORDS;
use sqlparser::parser::{Parser, ParserError};
use test_utils::{
all_dialects, expr_from_projection, join, number, only, table, table_alias, TestedDialects,
};
#[test]
fn parse_insert_values() {
@ -1039,6 +1040,65 @@ fn parse_select_group_by() {
);
}
#[test]
fn parse_select_group_by_grouping_sets() {
let dialects = TestedDialects {
dialects: vec![Box::new(PostgreSqlDialect {})],
};
let sql =
"SELECT brand, size, sum(sales) FROM items_sold GROUP BY size, GROUPING SETS ((brand), (size), ())";
let select = dialects.verified_only_select(sql);
assert_eq!(
vec![
Expr::Identifier(Ident::new("size")),
Expr::GroupingSets(vec![
vec![Expr::Identifier(Ident::new("brand"))],
vec![Expr::Identifier(Ident::new("size"))],
vec![],
])
],
select.group_by
);
}
#[test]
fn parse_select_group_by_rollup() {
let dialects = TestedDialects {
dialects: vec![Box::new(PostgreSqlDialect {})],
};
let sql = "SELECT brand, size, sum(sales) FROM items_sold GROUP BY size, ROLLUP (brand, size)";
let select = dialects.verified_only_select(sql);
assert_eq!(
vec![
Expr::Identifier(Ident::new("size")),
Expr::Rollup(vec![
vec![Expr::Identifier(Ident::new("brand"))],
vec![Expr::Identifier(Ident::new("size"))],
])
],
select.group_by
);
}
#[test]
fn parse_select_group_by_cube() {
let dialects = TestedDialects {
dialects: vec![Box::new(PostgreSqlDialect {})],
};
let sql = "SELECT brand, size, sum(sales) FROM items_sold GROUP BY size, CUBE (brand, size)";
let select = dialects.verified_only_select(sql);
assert_eq!(
vec![
Expr::Identifier(Ident::new("size")),
Expr::Cube(vec![
vec![Expr::Identifier(Ident::new("brand"))],
vec![Expr::Identifier(Ident::new("size"))],
])
],
select.group_by
);
}
#[test]
fn parse_select_having() {
let sql = "SELECT foo FROM bar GROUP BY foo HAVING COUNT(*) > 1";