Skip to content

Commit 99f88df

Browse files
committed
fix: indexes table filter pushdown does not distinguish between and & or
1 parent 7aac6a6 commit 99f88df

4 files changed

Lines changed: 50 additions & 80 deletions

File tree

src/query/storages/common/index/src/bloom_index.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -830,19 +830,7 @@ impl BloomIndexBuilder {
830830
}
831831
}
832832

833-
pub struct Visitor<T: EqVisitor>(T);
834-
835-
impl<T> Visitor<T>
836-
where T: EqVisitor
837-
{
838-
pub fn new(value: T) -> Self {
839-
Visitor(value)
840-
}
841-
842-
pub fn inner(self) -> T {
843-
self.0
844-
}
845-
}
833+
struct Visitor<T: EqVisitor>(T);
846834

847835
impl<T> ExprVisitor<String> for Visitor<T>
848836
where T: EqVisitor
@@ -1044,9 +1032,9 @@ where T: EqVisitor
10441032
}
10451033
}
10461034

1047-
pub type ResultRewrite = Result<ControlFlow<Option<Expr<String>>, Option<Expr<String>>>>;
1035+
type ResultRewrite = Result<ControlFlow<Option<Expr<String>>, Option<Expr<String>>>>;
10481036

1049-
pub trait EqVisitor {
1037+
trait EqVisitor {
10501038
fn enter_target(
10511039
&mut self,
10521040
span: Span,

src/query/storages/common/index/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ pub use bloom_index::BloomIndex;
2828
pub use bloom_index::BloomIndexBuilder;
2929
pub use bloom_index::BloomIndexMeta;
3030
pub use bloom_index::BloomIndexResult;
31-
pub use bloom_index::EqVisitor;
3231
pub use bloom_index::FilterEvalResult;
3332
pub use bloom_index::NgramArgs;
34-
pub use bloom_index::ResultRewrite;
35-
pub use bloom_index::Visitor;
3633
pub use eliminate_cast::eliminate_cast;
3734
pub use index::Index;
3835
pub use inverted_index::extract_component_fields;

src/query/storages/system/src/indexes_table.rs

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::ops::ControlFlow;
1615
use std::sync::Arc;
1716

18-
use databend_common_ast::Span;
1917
use databend_common_catalog::catalog::CATALOG_DEFAULT;
2018
use databend_common_catalog::plan::PushDownInfo;
2119
use databend_common_catalog::table::Table;
2220
use databend_common_exception::Result;
23-
use databend_common_expression::types::DataType;
2421
use databend_common_expression::types::StringType;
2522
use databend_common_expression::types::TimestampType;
26-
use databend_common_expression::visit_expr;
2723
use databend_common_expression::DataBlock;
28-
use databend_common_expression::Expr;
2924
use databend_common_expression::FromData;
3025
use databend_common_expression::Scalar;
3126
use databend_common_expression::TableDataType;
@@ -36,15 +31,13 @@ use databend_common_meta_app::schema::ListIndexesReq;
3631
use databend_common_meta_app::schema::TableIdent;
3732
use databend_common_meta_app::schema::TableInfo;
3833
use databend_common_meta_app::schema::TableMeta;
39-
use databend_common_storages_fuse::index::EqVisitor;
40-
use databend_common_storages_fuse::index::ResultRewrite;
41-
use databend_common_storages_fuse::index::Visitor;
4234
use databend_common_storages_fuse::TableContext;
4335
use futures::future::try_join_all;
4436
use log::warn;
4537

4638
use crate::table::AsyncOneBlockSystemTable;
4739
use crate::table::AsyncSystemTable;
40+
use crate::util::find_eq_or_filter;
4841

4942
const POINT_GET_TABLE_LIMIT: usize = 20;
5043

@@ -65,17 +58,46 @@ impl AsyncSystemTable for IndexesTable {
6558
ctx: Arc<dyn TableContext>,
6659
push_downs: Option<PushDownInfo>,
6760
) -> Result<DataBlock> {
68-
let mut database_names = None;
69-
let mut table_names = None;
61+
let mut filtered_db_names = None;
62+
let mut filtered_table_names = None;
63+
let mut invalid_optimize = false;
7064

7165
if let Some(filters) = push_downs.and_then(|info| info.filters) {
7266
let expr = filters.filter.as_expr(&BUILTIN_FUNCTIONS);
73-
let mut visitor = Visitor::new(TableFilterVisitor::default());
74-
visit_expr(&expr, &mut visitor)?;
7567

76-
let inner = visitor.inner();
77-
database_names = (!inner.database_names.is_empty()).then_some(inner.database_names);
78-
table_names = (!inner.table_names.is_empty()).then_some(inner.table_names);
68+
let mut databases: Vec<String> = Vec::new();
69+
let mut tables: Vec<String> = Vec::new();
70+
71+
invalid_optimize = find_eq_or_filter(
72+
&expr,
73+
&mut |col_name, scalar| {
74+
if col_name == "database" {
75+
if let Scalar::String(database) = scalar {
76+
if !databases.contains(database) {
77+
databases.push(database.clone());
78+
}
79+
}
80+
} else if col_name == "table" {
81+
if let Scalar::String(table) = scalar {
82+
if !tables.contains(table) {
83+
tables.push(table.clone());
84+
}
85+
}
86+
}
87+
Ok(())
88+
},
89+
invalid_optimize,
90+
);
91+
if !databases.is_empty() {
92+
filtered_db_names = Some(databases);
93+
}
94+
if !tables.is_empty() {
95+
filtered_table_names = Some(tables);
96+
}
97+
}
98+
if invalid_optimize {
99+
filtered_db_names = None;
100+
filtered_table_names = None;
79101
}
80102

81103
let tenant = ctx.get_tenant();
@@ -87,8 +109,8 @@ impl AsyncSystemTable for IndexesTable {
87109
let table_index_tables = self
88110
.list_table_index_tables(
89111
ctx.clone(),
90-
database_names.as_deref(),
91-
table_names.as_deref(),
112+
filtered_db_names.as_deref(),
113+
filtered_table_names.as_deref(),
92114
)
93115
.await?;
94116

@@ -294,48 +316,3 @@ impl IndexesTable {
294316
Ok(index_tables)
295317
}
296318
}
297-
298-
#[derive(Debug, Default)]
299-
struct TableFilterVisitor {
300-
database_names: Vec<String>,
301-
table_names: Vec<String>,
302-
}
303-
304-
impl EqVisitor for TableFilterVisitor {
305-
fn enter_target(
306-
&mut self,
307-
_: Span,
308-
col_name: &str,
309-
scalar: &Scalar,
310-
_: &DataType,
311-
_: &DataType,
312-
is_like: bool,
313-
) -> ResultRewrite {
314-
if is_like {
315-
return Ok(ControlFlow::Break(None));
316-
}
317-
let Some(name) = scalar.as_string() else {
318-
return Ok(ControlFlow::Break(None));
319-
};
320-
match col_name {
321-
"table" => &mut self.table_names,
322-
"database" => &mut self.database_names,
323-
_ => return Ok(ControlFlow::Break(None)),
324-
}
325-
.push(name.clone());
326-
327-
Ok(ControlFlow::Break(None))
328-
}
329-
330-
fn enter_map_column(
331-
&mut self,
332-
_: Span,
333-
_: &[Expr<String>],
334-
_: &Scalar,
335-
_: &DataType,
336-
_: &DataType,
337-
_: bool,
338-
) -> ResultRewrite {
339-
Ok(ControlFlow::Continue(None))
340-
}
341-
}

tests/sqllogictests/suites/ee/01_ee_system/01_0000_system_indexes.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,16 @@ idx2 NGRAM test_show_indexes_0 t1
135135
idx1 INVERTED test_show_indexes_1 t1
136136
idx2 NGRAM test_show_indexes_1 t1
137137

138+
query TTTT
139+
select name, type, database, table from system.indexes where database = 'test_show_indexes_0' and table = 't1';
140+
----
141+
idx1 INVERTED test_show_indexes_0 t1
142+
idx2 NGRAM test_show_indexes_0 t1
143+
138144
query TTTT
139145
select name, type, database, table from system.indexes where database = 'test_show_indexes_0' or table = 't1';
140146
----
141147
idx1 INVERTED test_show_indexes_0 t1
142148
idx2 NGRAM test_show_indexes_0 t1
149+
idx1 INVERTED test_show_indexes_1 t1
150+
idx2 NGRAM test_show_indexes_1 t1

0 commit comments

Comments
 (0)