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

fix(query): fix aggregating index conflict with min/max rewrite #17182

Merged
merged 11 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions src/query/ee/tests/it/aggregating_index/index_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,47 +747,6 @@ struct TestSuite {
/// ```
fn get_test_suites() -> Vec<TestSuite> {
vec![
// query: eval-scan, index: eval-scan
TestSuite {
query: "select to_string(c + 1) from t",
index: "select c + 1 from t",
is_index_scan: true,
},
TestSuite {
query: "select c + 1 from t",
index: "select c + 1 from t",
is_index_scan: true,
},
TestSuite {
query: "select a from t",
index: "select a from t",
is_index_scan: true,
},
TestSuite {
query: "select a as z from t",
index: "select a from t",
is_index_scan: true,
},
TestSuite {
query: "select a + 1, to_string(a) from t",
index: "select a from t",
is_index_scan: true,
},
TestSuite {
query: "select a + 1 as z, to_string(a) from t",
index: "select a from t",
is_index_scan: true,
},
TestSuite {
query: "select b from t",
index: "select a, b from t",
is_index_scan: true,
},
TestSuite {
query: "select a from t",
index: "select b, c from t",
is_index_scan: false,
},
// query: eval-filter-scan, index: eval-scan
TestSuite {
query: "select a from t where b > 1",
Expand Down
41 changes: 29 additions & 12 deletions src/query/sql/src/planner/optimizer/aggregate/stats_aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use databend_common_expression::types::DataType;

use crate::optimizer::SExpr;
use crate::plans::Aggregate;
use crate::plans::AggregateFunction;
use crate::plans::BoundColumnRef;
use crate::plans::ConstantExpr;
use crate::plans::DummyTableScan;
use crate::plans::EvalScalar;
Expand All @@ -28,7 +30,9 @@ use crate::plans::RelOp;
use crate::plans::RelOperator;
use crate::plans::ScalarExpr;
use crate::plans::ScalarItem;
use crate::ColumnBindingBuilder;
use crate::MetadataRef;
use crate::Visibility;

// Replace aggregate function with scalar from table's accurate stats function
pub struct RuleStatsAggregateOptimizer {
Expand Down Expand Up @@ -122,27 +126,40 @@ impl RuleStatsAggregateOptimizer {
{
if let Some((col_id, name)) = need_rewrite_agg {
if let Some(stat) = stats.get(col_id) {
if name.eq_ignore_ascii_case("min") && !stat.min.may_be_truncated {
let value_bound = if name.eq_ignore_ascii_case("min") {
&stat.min
} else {
&stat.max
};
if !value_bound.may_be_truncated {
eval_scalar_results.push(ScalarItem {
index: agg.index,
scalar: ScalarExpr::ConstantExpr(ConstantExpr {
value: stat.min.value.clone(),
span: None,
}),
});
continue;
} else if !stat.max.may_be_truncated {
eval_scalar_results.push(ScalarItem {
index: agg.index,
scalar: ScalarExpr::ConstantExpr(ConstantExpr {
value: stat.max.value.clone(),
span: None,
span: agg.scalar.span(),
value: value_bound.value.clone(),
}),
});
continue;
}
}
}

// Add other aggregate functions as derived column,
// this will be used in aggregating index rewrite.
let agg_func = AggregateFunction::try_from(agg.scalar.clone())?;
eval_scalar_results.push(ScalarItem {
index: agg.index,
scalar: ScalarExpr::BoundColumnRef(BoundColumnRef {
span: agg.scalar.span(),
column: ColumnBindingBuilder::new(
agg_func.display_name.clone(),
agg.index,
agg_func.return_type.clone(),
Visibility::Visible,
)
.build(),
}),
});
agg_results.push(agg.clone());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,6 @@ impl RuleTryApplyAggIndex {

fn normal_matchers() -> Vec<Matcher> {
vec![
// Expression
// |
// Scan
Matcher::MatchOp {
op_type: RelOp::EvalScalar,
children: vec![Matcher::MatchOp {
op_type: RelOp::Scan,
children: vec![],
}],
},
// Expression
// |
// Filter
Expand Down
66 changes: 47 additions & 19 deletions src/query/sql/src/planner/semantic/aggregating_index_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use databend_common_ast::ast::TableReference;
use databend_common_ast::parser::parse_expr;
use databend_common_ast::parser::tokenize_sql;
use databend_common_ast::parser::Dialect;
use databend_common_expression::FunctionKind;
use databend_common_expression::BLOCK_NAME_COL_NAME;
use databend_common_functions::aggregates::AggregateFunctionFactory;
use databend_common_functions::BUILTIN_FUNCTIONS;
Expand Down Expand Up @@ -63,7 +64,7 @@ impl AggregatingIndexRewriter {
..
} if !*distinct
&& SUPPORTED_AGGREGATING_INDEX_FUNCTIONS
.contains(&&*name.name.to_ascii_lowercase().to_lowercase())
.contains(&name.name.to_ascii_lowercase().to_lowercase().as_str())
&& window.is_none()
&& lambda.is_none() =>
{
Expand Down Expand Up @@ -197,15 +198,23 @@ impl AggregatingIndexRewriter {
#[derive(Debug, Clone, Default, Visitor)]
#[visitor(FunctionCall(enter), SelectStmt(enter), Query(enter))]
pub struct AggregatingIndexChecker {
has_agg_function: bool,
has_group_by: bool,
has_selection: bool,
not_support: bool,
}

impl AggregatingIndexChecker {
pub fn is_supported(&self) -> bool {
!self.not_support
// Must have at least one of aggregate function, group by, or selection.
// An aggregating index like `select a + 1 from t` are useless and take up extra storage space.
!self.not_support && (self.has_agg_function || self.has_group_by || self.has_selection)
}

fn enter_function_call(&mut self, func: &FunctionCall) {
if self.not_support {
return;
}
let FunctionCall {
distinct: _,
name,
Expand All @@ -215,29 +224,34 @@ impl AggregatingIndexChecker {
lambda: _,
} = func;

if self.not_support {
return;
}

// is agg func but not support now.
if AggregateFunctionFactory::instance().contains(&name.name)
&& !SUPPORTED_AGGREGATING_INDEX_FUNCTIONS.contains(&&*name.name.to_lowercase())
{
let name = name.name.to_lowercase();
let func_name = name.as_str();
if AggregateFunctionFactory::instance().contains(func_name) {
self.has_agg_function = true;
// is agg func but not support now.
if !SUPPORTED_AGGREGATING_INDEX_FUNCTIONS.contains(&func_name) {
self.not_support = true;
}
} else if let Some(func_property) = BUILTIN_FUNCTIONS.get_property(func_name) {
// set returning functions and non deterministic functions such as `now()` are not supported.
if func_property.kind == FunctionKind::SRF || func_property.non_deterministic {
self.not_support = true;
}
} else {
// Functions other than aggregate and scalar are not supported, such as window, UDF, etc.
self.not_support = true;
return;
}

self.not_support = BUILTIN_FUNCTIONS
.get_property(&name.name)
.map(|p| p.non_deterministic)
.unwrap_or(false);
}

fn enter_select_stmt(&mut self, stmt: &SelectStmt) {
if self.not_support {
return;
}
if stmt.having.is_some() || stmt.window_list.is_some() || stmt.qualify.is_some() {
if stmt.having.is_some()
|| stmt.window_list.is_some()
|| stmt.qualify.is_some()
|| stmt.top_n.is_some()
{
self.not_support = true;
return;
}
Expand All @@ -249,12 +263,26 @@ impl AggregatingIndexChecker {
return;
}
}
if stmt.from.len() != 1 {
self.not_support = true;
return;
}
// only support select from one table.
match &stmt.from[0] {
TableReference::Table { .. } => {}
_ => {
self.not_support = true;
return;
}
}
for target in &stmt.select_list {
if target.has_window() {
if target.is_star() {
self.not_support = true;
return;
}
}
self.has_selection = stmt.selection.is_some();
self.has_group_by = stmt.group_by.is_some();
}

fn enter_query(&mut self, query: &Query) {
Expand Down Expand Up @@ -288,7 +316,7 @@ impl RefreshAggregatingIndexRewriter {
..
} if !*distinct
&& SUPPORTED_AGGREGATING_INDEX_FUNCTIONS
.contains(&&*name.name.to_ascii_lowercase().to_lowercase())
.contains(&name.name.to_ascii_lowercase().to_lowercase().as_str())
&& window.is_none() =>
{
self.has_agg_function = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,14 @@ INSERT INTO t VALUES (1,1,4), (1,2,1), (1,2,4), (2,2,5)

# query: eval-scan, index: eval-scan

statement ok
statement error 1601
CREATE ASYNC AGGREGATING INDEX testi AS select c + 1 from t

statement ok
REFRESH AGGREGATING INDEX testi

query T
select to_string(c + 1) from t
----
5
2
5
6

statement ok
DROP AGGREGATING INDEX testi

# eval-filter-scan, index: eval-scan

statement ok
statement error 1601
CREATE ASYNC AGGREGATING INDEX testi AS select a, b from t

statement ok
REFRESH AGGREGATING INDEX testi

query T
select a from t where b > 1
----
1
1
2

statement ok
DROP AGGREGATING INDEX testi

# query: eval-agg-eval-scan, index: eval-scan
# No available case for index scan.

Expand All @@ -92,26 +65,9 @@ statement ok
DROP AGGREGATING INDEX testi

# query: eval-agg-eval-scan, index: eval-filter-scan
statement ok
statement error 1601
CREATE ASYNC AGGREGATING INDEX testi AS select a + 1, b from t

statement ok
REFRESH AGGREGATING INDEX testi

query I
select sum(a + 1) as s from t group by b order by s
----
2
7

query I
select sum(a + 1) as s from t
----
9

statement ok
DROP AGGREGATING INDEX testi

# query: eval-agg-eval-filter-scan, index: eval-filter-scan
statement ok
CREATE ASYNC AGGREGATING INDEX testi AS select a + 1, b from t where b > 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use test_index
statement ok
DROP AGGREGATING INDEX IF EXISTS testi;

statement ok
DROP AGGREGATING INDEX IF EXISTS testi2;

statement ok
CREATE TABLE t (a int, b int, c int)

Expand All @@ -34,6 +37,32 @@ INSERT INTO t VALUES (1,1,4), (1,2,1), (1,2,4), (2,2,5)
statement ok
CREATE AGGREGATING INDEX testi AS SELECT b, MAX(a), SUM(a) from t WHERE c > 1 GROUP BY b

statement ok
CREATE AGGREGATING INDEX testi2 AS SELECT MAX(a), MIN(b), AVG(c) from t

query I
SELECT b from t WHERE c > 1 GROUP BY b ORDER BY b
----
1
2

query II
SELECT b, SUM(a) from t WHERE c > 1 GROUP BY b ORDER BY b
----
1 1
2 3

query IIT
SELECT MAX(a), MIN(b), AVG(c) from t
----
2 1 3.5

statement ok
REFRESH AGGREGATING INDEX testi

statement ok
REFRESH AGGREGATING INDEX testi2

query I
SELECT b from t WHERE c > 1 GROUP BY b ORDER BY b
----
Expand All @@ -45,3 +74,8 @@ SELECT b, SUM(a) from t WHERE c > 1 GROUP BY b ORDER BY b
----
1 1
2 3

query IIT
SELECT MAX(a), MIN(b), AVG(c) from t
----
2 1 3.5
Loading