|  | 
|  | 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 | + | 
| 1 | 18 | use datafusion_expr::{ | 
| 2 | 19 |     expr, lit, | 
| 3 | 20 |     planner::{PlannerResult, RawAggregateFunction, UserDefinedSQLPlanner}, | 
|  | 21 | +    utils::COUNT_STAR_EXPANSION, | 
| 4 | 22 |     Expr, | 
| 5 | 23 | }; | 
| 6 | 24 | 
 | 
|  | 25 | +fn is_wildcard(expr: &Expr) -> bool { | 
|  | 26 | +    matches!(expr, Expr::Wildcard { qualifier: None }) | 
|  | 27 | +} | 
|  | 28 | + | 
| 7 | 29 | pub struct AggregateFunctionPlanner; | 
| 8 | 30 | 
 | 
| 9 |  | -impl UserDefinedSQLPlanner for AggregateFunctionPlanner { | 
| 10 |  | -    fn plan_aggregate_function( | 
|  | 31 | +impl AggregateFunctionPlanner { | 
|  | 32 | +    fn plan_count( | 
| 11 | 33 |         &self, | 
| 12 | 34 |         aggregate_function: RawAggregateFunction, | 
| 13 | 35 |     ) -> 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 | +        { | 
| 24 | 53 |             return Ok(PlannerResult::Planned(Expr::AggregateFunction( | 
| 25 | 54 |                 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, | 
| 32 | 61 |                 ), | 
| 33 | 62 |             ))); | 
| 34 | 63 |         } | 
| 35 | 64 | 
 | 
| 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)) | 
| 37 | 79 |     } | 
| 38 | 80 | } | 
0 commit comments