Skip to content

Commit 6782364

Browse files
committed
refactor: cleanup AggregateFunctionPlanner
1 parent 07bee49 commit 6782364

File tree

4 files changed

+61
-303
lines changed

4 files changed

+61
-303
lines changed
Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,80 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
118
use datafusion_expr::{
219
expr, lit,
320
planner::{PlannerResult, RawAggregateFunction, UserDefinedSQLPlanner},
21+
utils::COUNT_STAR_EXPANSION,
422
Expr,
523
};
624

25+
fn is_wildcard(expr: &Expr) -> bool {
26+
matches!(expr, Expr::Wildcard { qualifier: None })
27+
}
28+
729
pub struct AggregateFunctionPlanner;
830

9-
impl UserDefinedSQLPlanner for AggregateFunctionPlanner {
10-
fn plan_aggregate_function(
31+
impl AggregateFunctionPlanner {
32+
fn plan_count(
1133
&self,
1234
aggregate_function: RawAggregateFunction,
1335
) -> datafusion_common::Result<PlannerResult<RawAggregateFunction>> {
14-
let RawAggregateFunction {
15-
udf,
16-
args,
17-
distinct,
18-
filter,
19-
order_by,
20-
null_treatment,
21-
} = aggregate_function.clone();
22-
23-
if udf.name() == "count" && args.is_empty() {
36+
if aggregate_function.args.is_empty() {
37+
return Ok(PlannerResult::Planned(Expr::AggregateFunction(
38+
expr::AggregateFunction::new_udf(
39+
aggregate_function.udf,
40+
vec![lit(COUNT_STAR_EXPANSION).alias("count()")],
41+
aggregate_function.distinct,
42+
aggregate_function.filter,
43+
aggregate_function.order_by,
44+
aggregate_function.null_treatment,
45+
),
46+
)));
47+
}
48+
49+
if aggregate_function.udf.name() == "count"
50+
&& aggregate_function.args.len() == 1
51+
&& is_wildcard(&aggregate_function.args[0])
52+
{
2453
return Ok(PlannerResult::Planned(Expr::AggregateFunction(
2554
expr::AggregateFunction::new_udf(
26-
udf.clone(),
27-
vec![lit(1).alias("")],
28-
distinct,
29-
filter.clone(),
30-
order_by.clone(),
31-
null_treatment.clone(),
55+
aggregate_function.udf,
56+
vec![lit(COUNT_STAR_EXPANSION).alias("*")],
57+
aggregate_function.distinct,
58+
aggregate_function.filter,
59+
aggregate_function.order_by,
60+
aggregate_function.null_treatment,
3261
),
3362
)));
3463
}
3564

36-
Ok(PlannerResult::Original(aggregate_function.clone()))
65+
Ok(PlannerResult::Original(aggregate_function))
66+
}
67+
}
68+
69+
impl UserDefinedSQLPlanner for AggregateFunctionPlanner {
70+
fn plan_aggregate_function(
71+
&self,
72+
aggregate_function: RawAggregateFunction,
73+
) -> datafusion_common::Result<PlannerResult<RawAggregateFunction>> {
74+
if aggregate_function.udf.name() == "count" {
75+
return self.plan_count(aggregate_function);
76+
}
77+
78+
Ok(PlannerResult::Original(aggregate_function))
3779
}
3880
}

datafusion/optimizer/src/analyzer/count_wildcard_rule.rs

Lines changed: 0 additions & 280 deletions
This file was deleted.

0 commit comments

Comments
 (0)