Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(planner): do not use data type in optimizer #11436

Merged
merged 5 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use common_exception::Result;
use common_expression::types::DataType;
use common_expression::types::NumberScalar;
use common_expression::Scalar;

Expand Down Expand Up @@ -78,47 +77,13 @@ impl Rule for RuleFoldCountAggregate {
let is_simple_count = agg.group_items.is_empty()
&& agg.aggregate_functions.iter().all(|agg| match &agg.scalar {
ScalarExpr::AggregateFunction(agg_func) => {
agg_func.func_name == "count"
&& (agg_func.args.is_empty()
|| !matches!(
agg_func.args[0].data_type(),
Ok(DataType::Nullable(_)) | Ok(DataType::Null)
))
&& !agg_func.distinct
agg_func.func_name == "count" && !agg_func.distinct
}
_ => false,
});

let simple_nullable_count = agg.group_items.is_empty()
&& agg.aggregate_functions.iter().all(|agg| match &agg.scalar {
ScalarExpr::AggregateFunction(agg_func) => {
agg_func.func_name == "count"
&& (agg_func.args.is_empty()
|| matches!(agg_func.args[0].data_type(), Ok(DataType::Nullable(_))))
&& !agg_func.distinct
}
_ => false,
});

if let (true, Some(card)) = (
if let (true, column_stats, Some(table_card)) = (
is_simple_count,
input_stat_info.statistics.precise_cardinality,
) {
let mut scalars = agg.aggregate_functions;
for item in scalars.iter_mut() {
item.scalar = ScalarExpr::ConstantExpr(ConstantExpr {
span: item.scalar.span(),
value: Scalar::Number(NumberScalar::UInt64(card)),
});
}
let eval_scalar = EvalScalar { items: scalars };
let dummy_table_scan = DummyTableScan;
state.add_result(SExpr::create_unary(
eval_scalar.into(),
SExpr::create_leaf(dummy_table_scan.into()),
));
} else if let (true, column_stats, Some(table_card)) = (
simple_nullable_count,
input_stat_info.statistics.column_stats,
input_stat_info.statistics.precise_cardinality,
) {
Expand All @@ -130,23 +95,22 @@ impl Rule for RuleFoldCountAggregate {
span: item.scalar.span(),
value: Scalar::Number(NumberScalar::UInt64(table_card)),
});
return Ok(());
} else {
let col_set = agg_func.args[0].used_columns();
for index in col_set {
let col_stat = column_stats.get(&index);
if let Some(card) = col_stat {
item.scalar = ScalarExpr::ConstantExpr(ConstantExpr {
span: item.scalar.span(),
value: Scalar::Number(NumberScalar::UInt64(
table_card - card.null_count,
)),
});
} else {
return Ok(());
}
} else if let ScalarExpr::BoundColumnRef(col) = &agg_func.args[0] {
if let Some(card) = column_stats.get(&col.column.index) {
item.scalar = ScalarExpr::ConstantExpr(ConstantExpr {
span: item.scalar.span(),
value: Scalar::Number(NumberScalar::UInt64(
table_card - card.null_count,
)),
});
} else {
return Ok(());
}
} else {
return Ok(());
}
} else {
return Ok(());
}
}
let eval_scalar = EvalScalar { items: scalars };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use common_exception::Result;
use common_expression::types::DataType;
use common_expression::Scalar;

use crate::binder::split_conjunctions;
Expand Down Expand Up @@ -51,15 +50,12 @@ fn predicate_scalar(scalar: &ScalarExpr) -> PredicateScalar {
}
}

fn normalize_predicate_scalar(
predicate_scalar: PredicateScalar,
return_type: DataType,
) -> ScalarExpr {
fn normalize_predicate_scalar(predicate_scalar: PredicateScalar) -> ScalarExpr {
match predicate_scalar {
PredicateScalar::And { args } => {
assert!(args.len() >= 2);
args.into_iter()
.map(|arg| normalize_predicate_scalar(arg, return_type.clone()))
.map(normalize_predicate_scalar)
.reduce(|lhs, rhs| {
ScalarExpr::FunctionCall(FunctionCall {
span: None,
Expand All @@ -73,7 +69,7 @@ fn normalize_predicate_scalar(
PredicateScalar::Or { args } => {
assert!(args.len() >= 2);
args.into_iter()
.map(|arg| normalize_predicate_scalar(arg, return_type.clone()))
.map(normalize_predicate_scalar)
.reduce(|lhs, rhs| {
ScalarExpr::FunctionCall(FunctionCall {
span: None,
Expand Down Expand Up @@ -136,10 +132,7 @@ impl Rule for RuleNormalizeDisjunctiveFilter {
if has_rewritten {
rewritten = true;
}
rewritten_predicates.push(normalize_predicate_scalar(
rewritten_predicate_scalar,
predicate.data_type()?,
));
rewritten_predicates.push(normalize_predicate_scalar(rewritten_predicate_scalar));
}
let mut split_predicates: Vec<ScalarExpr> = Vec::with_capacity(rewritten_predicates.len());
for predicate in rewritten_predicates.iter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use common_expression::types::number::NumberDataType;
use common_expression::types::DataType;
use common_functions::aggregates::AggregateFunctionFactory;

use crate::binder::wrap_cast;
use crate::optimizer::rule::Rule;
use crate::optimizer::rule::TransformResult;
use crate::optimizer::ColumnSet;
Expand All @@ -29,7 +30,6 @@ use crate::plans::Aggregate;
use crate::plans::AggregateFunction;
use crate::plans::AggregateMode;
use crate::plans::BoundColumnRef;
use crate::plans::CastExpr;
use crate::plans::EvalScalar;
use crate::plans::FunctionCall;
use crate::plans::Join;
Expand Down Expand Up @@ -1291,18 +1291,6 @@ fn modify_final_aggregate_function(agg: &mut AggregateFunction, args_index: usiz
}
}

fn cast_expr_if_needed(expr: ScalarExpr, target_data_type: DataType) -> ScalarExpr {
match expr.data_type() {
Ok(data_type) if data_type != target_data_type => ScalarExpr::CastExpr(CastExpr {
span: None,
is_try: false,
argument: Box::new(expr),
target_type: Box::new(target_data_type),
}),
_ => expr,
}
}

fn remove_group_by_items_and_aggregate_functions(
agg_final: &mut Aggregate,
columns_set: &ColumnSet,
Expand Down Expand Up @@ -1357,8 +1345,8 @@ fn create_avg_scalar_item(left_index: usize, right_index: usize) -> ScalarExpr {
visibility: Visibility::Visible,
},
}),
cast_expr_if_needed(
ScalarExpr::BoundColumnRef(BoundColumnRef {
wrap_cast(
&ScalarExpr::BoundColumnRef(BoundColumnRef {
span: None,
column: ColumnBinding {
database_name: None,
Expand All @@ -1372,7 +1360,7 @@ fn create_avg_scalar_item(left_index: usize, right_index: usize) -> ScalarExpr {
visibility: Visibility::Visible,
},
}),
DataType::Number(NumberDataType::UInt64),
&DataType::Number(NumberDataType::UInt64),
),
],
})
Expand Down Expand Up @@ -1507,7 +1495,7 @@ fn update_aggregate_and_eval(
column_binding.data_type = Box::new(DataType::Nullable(Box::new(
DataType::Number(NumberDataType::UInt64),
)));
eval_scalar_item.scalar = cast_expr_if_needed(eval_scalar_item.scalar.clone(), DataType::Number(NumberDataType::UInt64));
eval_scalar_item.scalar = wrap_cast(&eval_scalar_item.scalar, &DataType::Number(NumberDataType::UInt64));
}
success = true;
}
Expand Down Expand Up @@ -1551,8 +1539,8 @@ fn create_eager_count_multiply_scalar_item(
params: vec![],
arguments: vec![
new_scalar,
cast_expr_if_needed(
ScalarExpr::BoundColumnRef(BoundColumnRef {
wrap_cast(
&ScalarExpr::BoundColumnRef(BoundColumnRef {
span: None,
column: ColumnBinding {
database_name: None,
Expand All @@ -1566,7 +1554,7 @@ fn create_eager_count_multiply_scalar_item(
visibility: Visibility::Visible,
},
}),
DataType::Number(NumberDataType::UInt64),
&DataType::Number(NumberDataType::UInt64),
),
],
}),
Expand Down