Skip to content

Commit

Permalink
fix: Fix wrong optimization on total count queries
Browse files Browse the repository at this point in the history
  • Loading branch information
waralexrom committed Apr 16, 2024
1 parent fa085ab commit 4d350da
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions datafusion/src/optimizer/projection_push_down.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the Apache Software Foundation (ASF) under one
// Licensed to the Apache Software Foundation (ASF) under onCount
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
Expand All @@ -22,11 +22,12 @@ use crate::cube_ext::alias::LogicalAlias;
use crate::error::{DataFusionError, Result};
use crate::execution::context::ExecutionProps;
use crate::logical_plan::{
build_join_schema, exprlist_to_fields, Column, DFField, DFSchema, DFSchemaRef, Expr,
LogicalPlan, LogicalPlanBuilder, ToDFSchema,
build_join_schema, exprlist_to_fields, lit, Column, DFField, DFSchema, DFSchemaRef,
Expr, LogicalPlan, LogicalPlanBuilder, ToDFSchema,
};
use crate::optimizer::optimizer::OptimizerRule;
use crate::optimizer::utils;
use crate::physical_plan::aggregates;
use crate::sql::utils::find_sort_exprs;
use arrow::datatypes::Schema;
use arrow::error::Result as ArrowResult;
Expand Down Expand Up @@ -307,6 +308,18 @@ fn optimize_plan(
}
})?;

//We cannot left Aggregate without both group expr and aggr expr so we add simple count
//here
let new_aggr_expr = if group_expr.is_empty() && new_aggr_expr.is_empty() {
vec![Expr::AggregateFunction {
fun: aggregates::AggregateFunction::Count,
args: vec![lit(1_u8)],
distinct: false,
}]
} else {
new_aggr_expr
};

let new_input = Arc::new(optimize_plan(
optimizer,
input,
Expand Down

0 comments on commit 4d350da

Please sign in to comment.