Skip to content

Commit

Permalink
refactor find_eq_db_table to util::find_eq_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed May 17, 2023
1 parent d358085 commit 37e2bdd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 53 deletions.
27 changes: 1 addition & 26 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_db_table;

pub struct ColumnsTable {
table_info: TableInfo,
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`, Column `database`
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
29 changes: 2 additions & 27 deletions src/query/storages/system/src/tables_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use common_expression::types::NumberDataType;
use common_expression::types::StringType;
use common_expression::utils::FromData;
use common_expression::DataBlock;
use common_expression::Expr;
use common_expression::FromOptData;
use common_expression::Scalar;
use common_expression::TableDataType;
Expand All @@ -39,6 +38,7 @@ use common_meta_app::schema::TableMeta;

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

pub struct TablesTable<const WITH_HISTORY: bool> {
table_info: TableInfo,
Expand Down Expand Up @@ -117,7 +117,7 @@ where TablesTable<T>: HistoryAware
let mut db_name = Vec::new();
if let Some(filter) = &push_downs.filter {
let expr = filter.as_expr(&BUILTIN_FUNCTIONS);
find_eq_db_database(&expr, &mut |col_name, scalar| {
find_eq_db_table(&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 @@ -310,28 +310,3 @@ where TablesTable<T>: HistoryAware
AsyncOneBlockSystemTable::create(TablesTable::<T> { table_info })
}
}

pub fn find_eq_db_database(expr: &Expr<String>, visitor: &mut impl FnMut(&str, &Scalar)) {
match expr {
Expr::Constant { .. } | Expr::ColumnRef { .. } => {}
Expr::Cast { expr, .. } => find_eq_db_database(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 `database`
for arg in args {
find_eq_db_database(arg, visitor)
}
}
}
}
}
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_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`, Column `database`
for arg in args {
find_eq_db_table(arg, visitor)
}
}
}
}
}

0 comments on commit 37e2bdd

Please sign in to comment.