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

feat(query): optimize show tables with pushdown filter only contain eq database #11490

Merged
merged 3 commits into from
May 17, 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
29 changes: 2 additions & 27 deletions src/query/storages/system/src/columns_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use common_expression::infer_table_schema;
use common_expression::types::StringType;
use common_expression::utils::FromData;
use common_expression::DataBlock;
use common_expression::Expr;
use common_expression::Scalar;
use common_expression::TableDataType;
use common_expression::TableField;
Expand All @@ -39,6 +38,7 @@ use common_storages_view::view_table::VIEW_ENGINE;

use crate::table::AsyncOneBlockSystemTable;
use crate::table::AsyncSystemTable;
use crate::util::find_eq_filter;

pub struct ColumnsTable {
table_info: TableInfo,
Expand Down Expand Up @@ -152,7 +152,7 @@ impl ColumnsTable {
if let Some(push_downs) = push_downs {
if let Some(filter) = push_downs.filter {
let expr = filter.as_expr(&BUILTIN_FUNCTIONS);
find_eq_db_table(&expr, &mut |col_name, scalar| {
find_eq_filter(&expr, &mut |col_name, scalar| {
if col_name == "database" {
if let Scalar::String(s) = scalar {
if let Ok(database) = String::from_utf8(s.clone()) {
Expand Down Expand Up @@ -219,28 +219,3 @@ impl ColumnsTable {
Ok(rows)
}
}

pub fn find_eq_db_table(expr: &Expr<String>, visitor: &mut impl FnMut(&str, &Scalar)) {
match expr {
Expr::Constant { .. } | Expr::ColumnRef { .. } => {}
Expr::Cast { expr, .. } => find_eq_db_table(expr, visitor),
Expr::FunctionCall { function, args, .. } => {
if function.signature.name == "eq" {
match args.as_slice() {
[Expr::ColumnRef { id, .. }, Expr::Constant { scalar, .. }]
| [Expr::Constant { scalar, .. }, Expr::ColumnRef { id, .. }] => {
visitor(id, scalar);
}
_ => {}
}
} else if function.signature.name == "and_filters" {
// only support this:
// 1. where xx and xx and xx
// 2. filter: Column `table`
for arg in args {
find_eq_db_table(arg, visitor)
}
}
}
}
}
1 change: 1 addition & 0 deletions src/query/storages/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod table_functions_table;
mod tables_table;
mod tracing_table;
mod users_table;
mod util;

pub use build_options_table::BuildOptionsTable;
pub use caches_table::CachesTable;
Expand Down
31 changes: 29 additions & 2 deletions src/query/storages/system/src/tables_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ use common_expression::types::StringType;
use common_expression::utils::FromData;
use common_expression::DataBlock;
use common_expression::FromOptData;
use common_expression::Scalar;
use common_expression::TableDataType;
use common_expression::TableField;
use common_expression::TableSchemaRef;
use common_expression::TableSchemaRefExt;
use common_functions::BUILTIN_FUNCTIONS;
use common_meta_app::schema::TableIdent;
use common_meta_app::schema::TableInfo;
use common_meta_app::schema::TableMeta;

use crate::table::AsyncOneBlockSystemTable;
use crate::table::AsyncSystemTable;
use crate::util::find_eq_filter;

pub struct TablesTable<const WITH_HISTORY: bool> {
table_info: TableInfo,
Expand Down Expand Up @@ -94,7 +97,7 @@ where TablesTable<T>: HistoryAware
async fn get_full_data(
&self,
ctx: Arc<dyn TableContext>,
_push_downs: Option<PushDownInfo>,
push_downs: Option<PushDownInfo>,
) -> Result<DataBlock> {
let tenant = ctx.get_tenant();
let catalog_mgr = CatalogManager::instance();
Expand All @@ -109,7 +112,31 @@ where TablesTable<T>: HistoryAware

let mut database_tables = vec![];
for (ctl_name, ctl) in ctls.into_iter() {
let dbs = ctl.list_databases(tenant.as_str()).await?;
let mut dbs = Vec::new();
if let Some(push_downs) = &push_downs {
let mut db_name = Vec::new();
if let Some(filter) = &push_downs.filter {
let expr = filter.as_expr(&BUILTIN_FUNCTIONS);
find_eq_filter(&expr, &mut |col_name, scalar| {
if col_name == "database" {
if let Scalar::String(s) = scalar {
if let Ok(database) = String::from_utf8(s.clone()) {
db_name.push(database);
}
}
}
});
for db in db_name {
if let Ok(database) = ctl.get_database(tenant.as_str(), db.as_str()).await {
dbs.push(database);
}
}
}
}

if dbs.is_empty() {
dbs = ctl.list_databases(tenant.as_str()).await?;
}
let ctl_name: &str = Box::leak(ctl_name.into_boxed_str());

for db in dbs {
Expand Down
41 changes: 41 additions & 0 deletions src/query/storages/system/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use common_expression::Expr;
use common_expression::Scalar;

pub fn find_eq_filter(expr: &Expr<String>, visitor: &mut impl FnMut(&str, &Scalar)) {
match expr {
Expr::Constant { .. } | Expr::ColumnRef { .. } => {}
Expr::Cast { expr, .. } => find_eq_filter(expr, visitor),
Expr::FunctionCall { function, args, .. } => {
if function.signature.name == "eq" {
match args.as_slice() {
[Expr::ColumnRef { id, .. }, Expr::Constant { scalar, .. }]
| [Expr::Constant { scalar, .. }, Expr::ColumnRef { id, .. }] => {
visitor(id, scalar);
}
_ => {}
}
} else if function.signature.name == "and_filters" {
// only support this:
// 1. where xx and xx and xx
// 2. filter: Column `table`, Column `database`
for arg in args {
find_eq_filter(arg, visitor)
}
}
}
}
}