Skip to content

Support COUNT(DISTINCT x) and similar #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub enum ASTNode {
name: SQLObjectName,
args: Vec<ASTNode>,
over: Option<SQLWindowSpec>,
// aggregate functions may specify eg `COUNT(DISTINCT x)`
distinct: bool,
},
/// CASE [<operand>] WHEN <condition> THEN <result> ... [ELSE <result>] END
/// Note we only recognize a complete single expression as <condition>, not
Expand Down Expand Up @@ -190,8 +192,18 @@ impl ToString for ASTNode {
format!("{} {}", operator.to_string(), expr.as_ref().to_string())
}
ASTNode::SQLValue(v) => v.to_string(),
ASTNode::SQLFunction { name, args, over } => {
let mut s = format!("{}({})", name.to_string(), comma_separated_string(args));
ASTNode::SQLFunction {
name,
args,
over,
distinct,
} => {
let mut s = format!(
"{}({}{})",
name.to_string(),
if *distinct { "DISTINCT " } else { "" },
comma_separated_string(args)
);
if let Some(o) = over {
s += &format!(" OVER ({})", o.to_string())
}
Expand Down
15 changes: 14 additions & 1 deletion src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ impl Parser {

pub fn parse_function(&mut self, name: SQLObjectName) -> Result<ASTNode, ParserError> {
self.expect_token(&Token::LParen)?;
let all = self.parse_keyword("ALL");
let distinct = self.parse_keyword("DISTINCT");
if all && distinct {
return parser_err!(format!(
"Cannot specify both ALL and DISTINCT in function: {}",
name.to_string(),
));
}
let args = self.parse_optional_args()?;
let over = if self.parse_keyword("OVER") {
// TBD: support window names (`OVER mywin`) in place of inline specification
Expand All @@ -279,7 +287,12 @@ impl Parser {
None
};

Ok(ASTNode::SQLFunction { name, args, over })
Ok(ASTNode::SQLFunction {
name,
args,
over,
distinct,
})
}

pub fn parse_window_frame(&mut self) -> Result<Option<SQLWindowFrame>, ParserError> {
Expand Down
38 changes: 37 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''";
Expand Down Expand Up @@ -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))
);
Expand Down Expand Up @@ -690,7 +724,8 @@ fn parse_window_functions() {
asc: Some(false)
}],
window_frame: None,
})
}),
distinct: false,
},
expr_from_projection(&select.projection[0])
);
Expand Down Expand Up @@ -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]),
);
Expand Down