Merge pull request #77 from benesch/count-distinct

Support COUNT(DISTINCT x) and similar
This commit is contained in:
Nickolay Ponomarev 2019-05-30 02:35:49 +03:00 committed by GitHub
commit 646479e56c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 4 deletions

View file

@ -211,11 +211,44 @@ fn parse_select_count_wildcard() {
name: SQLObjectName(vec!["COUNT".to_string()]),
args: vec![ASTNode::SQLWildcard],
over: None,
distinct: false,
},
expr_from_projection(only(&select.projection))
);
}
#[test]
fn parse_select_count_distinct() {
let sql = "SELECT COUNT(DISTINCT + x) FROM customer";
let select = verified_only_select(sql);
assert_eq!(
&ASTNode::SQLFunction {
name: SQLObjectName(vec!["COUNT".to_string()]),
args: vec![ASTNode::SQLUnary {
operator: SQLOperator::Plus,
expr: Box::new(ASTNode::SQLIdentifier("x".to_string()))
}],
over: None,
distinct: true,
},
expr_from_projection(only(&select.projection))
);
one_statement_parses_to(
"SELECT COUNT(ALL + x) FROM customer",
"SELECT COUNT(+ x) FROM customer",
);
let sql = "SELECT COUNT(ALL DISTINCT + x) FROM customer";
let res = parse_sql_statements(sql);
assert_eq!(
ParserError::ParserError(
"Cannot specify both ALL and DISTINCT in function: COUNT".to_string()
),
res.unwrap_err()
);
}
#[test]
fn parse_not() {
let sql = "SELECT id FROM customer WHERE NOT salary = ''";
@ -676,6 +709,7 @@ fn parse_scalar_function_in_projection() {
name: SQLObjectName(vec!["sqrt".to_string()]),
args: vec![ASTNode::SQLIdentifier("id".to_string())],
over: None,
distinct: false,
},
expr_from_projection(only(&select.projection))
);
@ -704,7 +738,8 @@ fn parse_window_functions() {
asc: Some(false)
}],
window_frame: None,
})
}),
distinct: false,
},
expr_from_projection(&select.projection[0])
);
@ -776,6 +811,7 @@ fn parse_delimited_identifiers() {
name: SQLObjectName(vec![r#""myfun""#.to_string()]),
args: vec![],
over: None,
distinct: false,
},
expr_from_projection(&select.projection[1]),
);