Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ async fn test_unsupported_aggregation_keeps_connection_usable() {
"SELECT count(DISTINCT value) FROM sharded",
"unsupported aggregation count: count(DISTINCT ...) is not yet supported",
),
(
// The cast must not hide DISTINCT and let per-shard counts be summed.
"SELECT count(DISTINCT value)::int FROM sharded",
"unsupported aggregation count: count(DISTINCT ...) is not yet supported",
),
(
"SELECT count(*) FROM sharded HAVING count(*) > 0 LIMIT (SELECT 1)",
"cross-shard HAVING is unsupported: \
Expand Down
34 changes: 23 additions & 11 deletions pgdog/src/frontend/router/parser/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,21 +363,19 @@ impl Aggregate {
.iter()
.enumerate()
.filter_map(|(idx, node)| {
let func = Function::try_from(node.val()).ok()?;
let function = AggregateFunction::from_name(func.name, schema)?;

// FIXME: This doesn't correctly handle distinct behind type
// casts any other nodes that Function::try_from handles
let distinct = if let Node::FuncCall(func) = node.val() {
func.agg_distinct
} else {
false
};
// The call may sit behind a cast, e.g. `count(DISTINCT x)::int`.
let call = Function::extract_func_call(node.val())?;
let name = call
.funcname()
.iter()
.filter_map(Node::as_str)
.next_back()?;
let function = AggregateFunction::from_name(name, schema)?;

Some(AggregateTarget {
column: idx,
function,
distinct,
distinct: call.agg_distinct,
})
})
.collect();
Expand Down Expand Up @@ -465,6 +463,20 @@ mod test {
assert!(!avg.is_distinct());
}

#[test]
fn test_parse_distinct_behind_cast() {
let aggr = parse(
"SELECT COUNT(DISTINCT price)::int, CAST(SUM(DISTINCT price) AS numeric)::text, \
COUNT(price)::int FROM menu",
);
let distinct = aggr
.targets()
.iter()
.map(AggregateTarget::is_distinct)
.collect::<Vec<_>>();
assert_eq!(distinct, vec![true, true, false]);
}

#[test]
fn test_parse_bool_and_bit_aggregates() {
let aggr = parse(
Expand Down
Loading