Support COUNT(DISTINCT x) and similar

This commit is contained in:
Jamie Brandon 2019-05-22 15:44:41 +01:00 committed by Nikhil Benesch
parent 4f944dd4aa
commit 72ced4bffe
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
3 changed files with 65 additions and 4 deletions

View file

@ -197,11 +197,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 = ''";
@ -662,6 +695,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))
);
@ -690,7 +724,8 @@ fn parse_window_functions() {
asc: Some(false)
}],
window_frame: None,
})
}),
distinct: false,
},
expr_from_projection(&select.projection[0])
);
@ -762,6 +797,7 @@ fn parse_delimited_identifiers() {
name: SQLObjectName(vec![r#""myfun""#.to_string()]),
args: vec![],
over: None,
distinct: false,
},
expr_from_projection(&select.projection[1]),
);