Skip to content
Closed
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
19 changes: 19 additions & 0 deletions supabase-wrappers/src/upper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,25 @@ pub(super) extern "C-unwind" fn get_foreign_upper_paths<
return;
}

// Nothing above a pushed-down aggregate re-checks the scan's restrictions,
// so every one of them must have reached the FDW as a qual.
let restrictions = (*input_rel).baserestrictinfo;
let num_restrictions = if restrictions.is_null() {
0
} else {
(*restrictions).length as usize
};
if state.quals.len() != num_restrictions {
debug2!("WHERE clause has conditions not extracted as quals, skipping pushdown");
return;
}

// The pushed-down scan returns a single grouping, not one per set
if !(*(*root).parse).groupingSets.is_null() {
debug2!("GROUPING SETS present, skipping aggregate pushdown");
return;
}

// Extract aggregates from the query
let aggregates = match extract_aggregates(root, output_rel, extra) {
Some(aggs) => aggs,
Expand Down
38 changes: 38 additions & 0 deletions wrappers/src/fdw/clickhouse_fdw/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,44 @@ mod tests {
c,
"SELECT id + 1 AS g, COUNT(*) FROM agg_test GROUP BY id + 1"
);

// WHERE condition that is not extracted as a qual: it never reaches
// the FDW, so only a local aggregate above the scan can apply it.
let cnt: i64 = c
.select(
"SELECT COUNT(*) FROM agg_test WHERE name || '' = 'alice'",
None,
&[],
)
.unwrap()
.first()
.get_one::<i64>()
.unwrap()
.unwrap();
assert_eq!(cnt, 2);
assert_not_pushed_down!(
c,
"SELECT COUNT(*) FROM agg_test WHERE name || '' = 'alice'"
);

// GROUPING SETS: two empty sets produce two rows of 4.
let total: i64 = c
.select(
"SELECT SUM(cnt)::bigint FROM (SELECT COUNT(*) AS cnt FROM agg_test \
GROUP BY GROUPING SETS ((), ())) s",
None,
&[],
)
.unwrap()
.first()
.get_one::<i64>()
.unwrap()
.unwrap();
assert_eq!(total, 8);
assert_not_pushed_down!(
c,
"SELECT COUNT(*) FROM agg_test GROUP BY GROUPING SETS ((), ())"
);
});
}

Expand Down