Merge pull request #67804 from ClickHouse/vdimir/fix_fliter_pushdown_no_keys

Fix filter pushdown for aggregation without keys
This commit is contained in:
vdimir 2024-08-06 14:35:50 +00:00 committed by GitHub
commit a180bf311c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 0 deletions

View File

@ -442,6 +442,15 @@ size_t tryPushDownFilter(QueryPlan::Node * parent_node, QueryPlan::Nodes & nodes
const auto & params = aggregating->getParams();
const auto & keys = params.keys;
/** The filter is applied either to aggregation keys or aggregation result
* (columns under aggregation is not available in outer scope, so we can't have a filter for them).
* The filter for the aggregation result is not pushed down, so the only valid case is filtering aggregation keys.
* In case keys are empty, do not push down the filter.
* Also with empty keys we can have an issue with `empty_result_for_aggregation_by_empty_set`,
* since we can gen a result row when everything is filtered.
*/
if (keys.empty())
return 0;
const bool filter_column_is_not_among_aggregation_keys
= std::find(keys.begin(), keys.end(), filter->getFilterColumnName()) == keys.end();

View File

@ -0,0 +1,6 @@
---
1 1
---
3 3
---
---

View File

@ -0,0 +1,26 @@
select * from ( select sum(last_seen) as dates_seen, materialize(1) as last_seen ) where last_seen > 2;
select * from ( select sum(last_seen) as dates_seen, materialize(2) as last_seen ) where last_seen < 2;
select * from ( select sum(last_seen) as dates_seen, materialize(2) as last_seen GROUP BY 'a' ) where last_seen < 2;
select '---';
select * from ( select sum(last_seen) as dates_seen, 1 as last_seen UNION ALL select sum(last_seen) as dates_seen, 3 as last_seen ) where last_seen < 2;
select '---';
select * from ( select sum(last_seen) as dates_seen, 1 as last_seen UNION ALL select sum(last_seen) as dates_seen, 3 as last_seen ) where last_seen > 2;
select '---';
with activity as (
select
groupUniqArrayState(toDate('2025-01-01 01:00:00')) as dates_seen,
toDateTime('2025-01-01 01:00:00') as last_seen
union all
select
groupUniqArrayState(toDate('2023-11-11 11:11:11')) as dates_seen,
toDateTime('2023-11-11 11:11:11') as last_seen
)
select last_seen from activity
where last_seen < toDateTime('2020-01-01 00:00:00');
select '---';