Skip to content

Commit 0077e2b

Browse files
committed
add unit test
1 parent fd59b5a commit 0077e2b

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

src/ast/mod.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,44 @@ impl fmt::Display for Expr {
382382
Expr::Exists(s) => write!(f, "EXISTS ({})", s),
383383
Expr::Subquery(s) => write!(f, "({})", s),
384384
Expr::ListAgg(listagg) => write!(f, "{}", listagg),
385+
Expr::GroupingSets(sets) => {
386+
write!(f, "GROUPING SETS (")?;
387+
let mut sep = "";
388+
for set in sets {
389+
write!(f, "{}", sep)?;
390+
sep = ", ";
391+
write!(f, "({})", display_comma_separated(set))?;
392+
}
393+
write!(f, ")")
394+
}
395+
Expr::Cube(sets) => {
396+
write!(f, "CUBE (")?;
397+
let mut sep = "";
398+
for set in sets {
399+
write!(f, "{}", sep)?;
400+
sep = ", ";
401+
if set.len() == 1 {
402+
write!(f, "{}", set[0])?;
403+
} else {
404+
write!(f, "({})", display_comma_separated(set))?;
405+
}
406+
}
407+
write!(f, ")")
408+
}
409+
Expr::Rollup(sets) => {
410+
write!(f, "ROLLUP (")?;
411+
let mut sep = "";
412+
for set in sets {
413+
write!(f, "{}", sep)?;
414+
sep = ", ";
415+
if set.len() == 1 {
416+
write!(f, "{}", set[0])?;
417+
} else {
418+
write!(f, "({})", display_comma_separated(set))?;
419+
}
420+
}
421+
write!(f, ")")
422+
}
385423
Expr::Substring {
386424
expr,
387425
substring_from,
@@ -1728,4 +1766,93 @@ mod tests {
17281766
let window_frame = WindowFrame::default();
17291767
assert_eq!(WindowFrameBound::Preceding(None), window_frame.start_bound);
17301768
}
1769+
1770+
#[test]
1771+
fn test_grouping_sets_display() {
1772+
// a and b in different group
1773+
let grouping_sets = Expr::GroupingSets(vec![
1774+
vec![Expr::Identifier(Ident::new("a"))],
1775+
vec![Expr::Identifier(Ident::new("b"))],
1776+
]);
1777+
assert_eq!("GROUPING SETS ((a), (b))", format!("{}", grouping_sets));
1778+
1779+
// a and b in the same group
1780+
let grouping_sets = Expr::GroupingSets(vec![vec![
1781+
Expr::Identifier(Ident::new("a")),
1782+
Expr::Identifier(Ident::new("b")),
1783+
]]);
1784+
assert_eq!("GROUPING SETS ((a, b))", format!("{}", grouping_sets));
1785+
1786+
// (a, b) and (c, d) in different group
1787+
let grouping_sets = Expr::GroupingSets(vec![
1788+
vec![
1789+
Expr::Identifier(Ident::new("a")),
1790+
Expr::Identifier(Ident::new("b")),
1791+
],
1792+
vec![
1793+
Expr::Identifier(Ident::new("c")),
1794+
Expr::Identifier(Ident::new("d")),
1795+
],
1796+
]);
1797+
assert_eq!(
1798+
"GROUPING SETS ((a, b), (c, d))",
1799+
format!("{}", grouping_sets)
1800+
);
1801+
}
1802+
1803+
#[test]
1804+
fn test_rollup_display() {
1805+
let rollup = Expr::Rollup(vec![vec![Expr::Identifier(Ident::new("a"))]]);
1806+
assert_eq!("ROLLUP (a)", format!("{}", rollup));
1807+
1808+
let rollup = Expr::Rollup(vec![vec![
1809+
Expr::Identifier(Ident::new("a")),
1810+
Expr::Identifier(Ident::new("b")),
1811+
]]);
1812+
assert_eq!("ROLLUP ((a, b))", format!("{}", rollup));
1813+
1814+
let rollup = Expr::Rollup(vec![
1815+
vec![Expr::Identifier(Ident::new("a"))],
1816+
vec![Expr::Identifier(Ident::new("b"))],
1817+
]);
1818+
assert_eq!("ROLLUP (a, b)", format!("{}", rollup));
1819+
1820+
let rollup = Expr::Rollup(vec![
1821+
vec![Expr::Identifier(Ident::new("a"))],
1822+
vec![
1823+
Expr::Identifier(Ident::new("b")),
1824+
Expr::Identifier(Ident::new("c")),
1825+
],
1826+
vec![Expr::Identifier(Ident::new("d"))],
1827+
]);
1828+
assert_eq!("ROLLUP (a, (b, c), d)", format!("{}", rollup));
1829+
}
1830+
1831+
#[test]
1832+
fn test_cube_display() {
1833+
let cube = Expr::Cube(vec![vec![Expr::Identifier(Ident::new("a"))]]);
1834+
assert_eq!("CUBE (a)", format!("{}", cube));
1835+
1836+
let cube = Expr::Cube(vec![vec![
1837+
Expr::Identifier(Ident::new("a")),
1838+
Expr::Identifier(Ident::new("b")),
1839+
]]);
1840+
assert_eq!("CUBE ((a, b))", format!("{}", cube));
1841+
1842+
let cube = Expr::Cube(vec![
1843+
vec![Expr::Identifier(Ident::new("a"))],
1844+
vec![Expr::Identifier(Ident::new("b"))],
1845+
]);
1846+
assert_eq!("CUBE (a, b)", format!("{}", cube));
1847+
1848+
let cube = Expr::Cube(vec![
1849+
vec![Expr::Identifier(Ident::new("a"))],
1850+
vec![
1851+
Expr::Identifier(Ident::new("b")),
1852+
Expr::Identifier(Ident::new("c")),
1853+
],
1854+
vec![Expr::Identifier(Ident::new("d"))],
1855+
]);
1856+
assert_eq!("CUBE (a, (b, c), d)", format!("{}", cube));
1857+
}
17311858
}

0 commit comments

Comments
 (0)