|
| 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 | + |
| 18 | +use crate::analyzer::AnalyzerRule; |
| 19 | +use datafusion_common::config::ConfigOptions; |
| 20 | +use datafusion_common::Result; |
| 21 | +use datafusion_expr::expr::AggregateFunction; |
| 22 | +use datafusion_expr::utils::COUNT_STAR_EXPANSION; |
| 23 | +use datafusion_expr::{aggregate_function, lit, Aggregate, Expr, LogicalPlan, Window}; |
| 24 | +use std::ops::Deref; |
| 25 | +use std::sync::Arc; |
| 26 | + |
| 27 | +pub struct CountWildcardRule {} |
| 28 | + |
| 29 | +impl Default for CountWildcardRule { |
| 30 | + fn default() -> Self { |
| 31 | + CountWildcardRule::new() |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl CountWildcardRule { |
| 36 | + pub fn new() -> Self { |
| 37 | + CountWildcardRule {} |
| 38 | + } |
| 39 | +} |
| 40 | +impl AnalyzerRule for CountWildcardRule { |
| 41 | + fn analyze(&self, plan: &LogicalPlan, _: &ConfigOptions) -> Result<LogicalPlan> { |
| 42 | + let new_plan = match plan { |
| 43 | + LogicalPlan::Window(window) => { |
| 44 | + let inputs = plan.inputs(); |
| 45 | + let window_expr = window.clone().window_expr; |
| 46 | + let window_expr = handle_wildcard(window_expr).unwrap(); |
| 47 | + LogicalPlan::Window(Window { |
| 48 | + input: Arc::new(inputs.get(0).unwrap().deref().clone()), |
| 49 | + window_expr, |
| 50 | + schema: plan.schema().clone(), |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + LogicalPlan::Aggregate(aggregate) => { |
| 55 | + let inputs = plan.inputs(); |
| 56 | + let aggr_expr = aggregate.clone().aggr_expr; |
| 57 | + let aggr_expr = handle_wildcard(aggr_expr).unwrap(); |
| 58 | + LogicalPlan::Aggregate( |
| 59 | + Aggregate::try_new_with_schema( |
| 60 | + Arc::new(inputs.get(0).unwrap().deref().clone()), |
| 61 | + aggregate.clone().group_expr, |
| 62 | + aggr_expr, |
| 63 | + plan.schema().clone(), |
| 64 | + ) |
| 65 | + .unwrap(), |
| 66 | + ) |
| 67 | + } |
| 68 | + _ => plan.clone(), |
| 69 | + }; |
| 70 | + Ok(new_plan) |
| 71 | + } |
| 72 | + |
| 73 | + fn name(&self) -> &str { |
| 74 | + "count_wildcard_rule" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +//handle Count(Expr:Wildcard) with DataFrame API |
| 79 | +pub fn handle_wildcard(exprs: Vec<Expr>) -> Result<Vec<Expr>> { |
| 80 | + let exprs: Vec<Expr> = exprs |
| 81 | + .iter() |
| 82 | + .map(|expr| match expr { |
| 83 | + Expr::AggregateFunction(AggregateFunction { |
| 84 | + fun: aggregate_function::AggregateFunction::Count, |
| 85 | + args, |
| 86 | + distinct, |
| 87 | + filter, |
| 88 | + }) if args.len() == 1 => match args[0] { |
| 89 | + Expr::Wildcard => Expr::AggregateFunction(AggregateFunction { |
| 90 | + fun: aggregate_function::AggregateFunction::Count, |
| 91 | + args: vec![lit(COUNT_STAR_EXPANSION)], |
| 92 | + distinct: *distinct, |
| 93 | + filter: filter.clone(), |
| 94 | + }), |
| 95 | + _ => expr.clone(), |
| 96 | + }, |
| 97 | + _ => expr.clone(), |
| 98 | + }) |
| 99 | + .collect(); |
| 100 | + Ok(exprs) |
| 101 | +} |
0 commit comments