Skip to content

Commit 62ff0b9

Browse files
authored
fix(query): fix match function panic in native format (#15402)
* fix(query): fix match function panic in native format * fix tests
1 parent ff5c3f4 commit 62ff0b9

File tree

5 files changed

+185
-44
lines changed

5 files changed

+185
-44
lines changed

src/query/sql/src/planner/optimizer/rule/rewrite/rule_push_down_prewhere.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::sync::Arc;
1717
use databend_common_exception::ErrorCode;
1818
use databend_common_exception::Result;
1919
use databend_common_expression::TableSchemaRef;
20+
use databend_common_expression::SEARCH_MATCHED_COL_NAME;
21+
use databend_common_expression::SEARCH_SCORE_COL_NAME;
2022

2123
use crate::optimizer::extract::Matcher;
2224
use crate::optimizer::rule::Rule;
@@ -77,6 +79,13 @@ impl RulePushDownPrewhere {
7779
.index_of(column.column.column_name.as_str())
7880
.is_ok())
7981
{
82+
if column.column.column_name == SEARCH_SCORE_COL_NAME
83+
|| column.column.column_name == SEARCH_MATCHED_COL_NAME
84+
{
85+
return Err(ErrorCode::StorageUnsupported(
86+
"Prewhere don't support search functions".to_string(),
87+
));
88+
}
8089
self.columns.insert(column.column.index);
8190
return Ok(());
8291
}

tests/sqllogictests/suites/ee/04_ee_inverted_index/04_0000_inverted_index_base.test

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -188,50 +188,6 @@ SELECT id, score(), content FROM t WHERE match(content, 'fly')
188188
----
189189

190190

191-
# Test pruning status
192-
193-
statement ok
194-
CREATE TABLE t_small_blocks (id int, content string) row_per_block=2
195-
196-
statement ok
197-
CREATE INVERTED INDEX IF NOT EXISTS inverted_idx2 ON t_small_blocks(content) tokenizer = 'chinese'
198-
199-
statement ok
200-
INSERT INTO t_small_blocks VALUES
201-
(1, 'The quick brown fox jumps over the lazy dog'),
202-
(2, 'A picture is worth a thousand words'),
203-
(3, 'The early bird catches the worm'),
204-
(4, 'Actions speak louder than words'),
205-
(5, 'Time flies like an arrow; fruit flies like a banana'),
206-
(6, 'Beauty is in the eye of the beholder'),
207-
(7, 'When life gives you lemons, make lemonade'),
208-
(8, 'Put all your eggs in one basket'),
209-
(9, 'You can not judge a book by its cover'),
210-
(10, 'An apple a day keeps the doctor away')
211-
212-
query IT
213-
SELECT id, content FROM t_small_blocks WHERE query('content:"early bird"')
214-
----
215-
3 The early bird catches the worm
216-
217-
query T
218-
EXPLAIN SELECT id, content FROM t_small_blocks WHERE query('content:"early bird"')
219-
----
220-
Filter
221-
├── output columns: [t_small_blocks.id (#0), t_small_blocks.content (#1)]
222-
├── filters: [t_small_blocks._search_matched (#2)]
223-
├── estimated rows: 10.00
224-
└── TableScan
225-
├── table: default.test_index.t_small_blocks
226-
├── output columns: [id (#0), content (#1), _search_matched (#2)]
227-
├── read rows: 2
228-
├── read size: < 1 KiB
229-
├── partitions total: 5
230-
├── partitions scanned: 1
231-
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 5 to 5, inverted pruning: 5 to 1>]
232-
├── push downs: [filters: [t_small_blocks._search_matched (#2)], limit: NONE]
233-
└── estimated rows: 10.00
234-
235191
statement ok
236192
CREATE TABLE books(
237193
id int,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
## Copyright 2023 Databend Cloud
2+
##
3+
## Licensed under the Elastic License, Version 2.0 (the "License");
4+
## you may not use this file except in compliance with the License.
5+
## You may obtain a copy of the License at
6+
##
7+
## https://www.elastic.co/licensing/elastic-license
8+
##
9+
## Unless required by applicable law or agreed to in writing, software
10+
## distributed under the License is distributed on an "AS IS" BASIS,
11+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
## See the License for the specific language governing permissions and
13+
## limitations under the License.
14+
15+
statement ok
16+
DROP DATABASE IF EXISTS test_inverted_index_db
17+
18+
statement ok
19+
CREATE DATABASE test_inverted_index_db
20+
21+
statement ok
22+
USE test_inverted_index_db
23+
24+
statement ok
25+
DROP TABLE IF EXISTS t1
26+
27+
statement ok
28+
CREATE TABLE t1 (id int, content string) row_per_block=2 storage_format='parquet'
29+
30+
statement ok
31+
CREATE INVERTED INDEX IF NOT EXISTS idx1 ON t1(content)
32+
33+
statement ok
34+
INSERT INTO t1 VALUES
35+
(1, 'The quick brown fox jumps over the lazy dog'),
36+
(2, 'A picture is worth a thousand words'),
37+
(3, 'The early bird catches the worm'),
38+
(4, 'Actions speak louder than words'),
39+
(5, 'Time flies like an arrow; fruit flies like a banana'),
40+
(6, 'Beauty is in the eye of the beholder'),
41+
(7, 'When life gives you lemons, make lemonade'),
42+
(8, 'Put all your eggs in one basket'),
43+
(9, 'You can not judge a book by its cover'),
44+
(10, 'An apple a day keeps the doctor away')
45+
46+
query T
47+
EXPLAIN SELECT id, content FROM t1 WHERE query('content:"early bird"')
48+
----
49+
Filter
50+
├── output columns: [t1.id (#0), t1.content (#1)]
51+
├── filters: [t1._search_matched (#2)]
52+
├── estimated rows: 10.00
53+
└── TableScan
54+
├── table: default.test_inverted_index_db.t1
55+
├── output columns: [id (#0), content (#1), _search_matched (#2)]
56+
├── read rows: 2
57+
├── read size: < 1 KiB
58+
├── partitions total: 5
59+
├── partitions scanned: 1
60+
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 5 to 5, inverted pruning: 5 to 1>]
61+
├── push downs: [filters: [t1._search_matched (#2)], limit: NONE]
62+
└── estimated rows: 10.00
63+
64+
statement ok
65+
DROP TABLE IF EXISTS t2
66+
67+
statement ok
68+
CREATE TABLE t2 (id int, content string) row_per_block=2 storage_format='native'
69+
70+
statement ok
71+
CREATE INVERTED INDEX IF NOT EXISTS idx1 ON t2(content)
72+
73+
statement ok
74+
INSERT INTO t2 VALUES
75+
(1, 'The quick brown fox jumps over the lazy dog'),
76+
(2, 'A picture is worth a thousand words'),
77+
(3, 'The early bird catches the worm'),
78+
(4, 'Actions speak louder than words'),
79+
(5, 'Time flies like an arrow; fruit flies like a banana'),
80+
(6, 'Beauty is in the eye of the beholder'),
81+
(7, 'When life gives you lemons, make lemonade'),
82+
(8, 'Put all your eggs in one basket'),
83+
(9, 'You can not judge a book by its cover'),
84+
(10, 'An apple a day keeps the doctor away')
85+
86+
query T
87+
EXPLAIN SELECT id, content FROM t2 WHERE query('content:"early bird"')
88+
----
89+
Filter
90+
├── output columns: [t2.id (#0), t2.content (#1)]
91+
├── filters: [t2._search_matched (#2)]
92+
├── estimated rows: 10.00
93+
└── TableScan
94+
├── table: default.test_inverted_index_db.t2
95+
├── output columns: [id (#0), content (#1), _search_matched (#2)]
96+
├── read rows: 2
97+
├── read size: < 1 KiB
98+
├── partitions total: 5
99+
├── partitions scanned: 1
100+
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 5 to 5, inverted pruning: 5 to 1>]
101+
├── push downs: [filters: [t2._search_matched (#2)], limit: NONE]
102+
└── estimated rows: 10.00
103+
104+
statement ok
105+
USE default
106+
107+
statement ok
108+
DROP DATABASE IF EXISTS test_inverted_index_db

tests/sqllogictests/suites/mode/standalone/explain/explain.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,12 +1438,45 @@ EvalScalar
14381438
└── limit: NONE
14391439

14401440

1441+
statement ok
1442+
drop table if exists t3;
1443+
1444+
statement ok
1445+
CREATE TABLE t3(a int, b map(string, string) null);
1446+
1447+
statement ok
1448+
INSERT INTO t3 VALUES (1, {'k1':'a', 'k2':'b'}), (2, null), (3, {'k3':'z'});
1449+
1450+
statement ok
1451+
INSERT INTO t3 VALUES (4, {'k1':'a', 'k2':'m'}), (5, null), (6, {'k3':'z'});
1452+
1453+
query I
1454+
EXPLAIN SELECT * FROM t3 WHERE b['k2'] = 'm';
1455+
----
1456+
Filter
1457+
├── output columns: [t3.a (#0), t3.b (#1)]
1458+
├── filters: [is_true(get(t3.b (#1), 'k2') = 'm')]
1459+
├── estimated rows: 1.20
1460+
└── TableScan
1461+
├── table: default.default.t3
1462+
├── output columns: [a (#0), b (#1)]
1463+
├── read rows: 3
1464+
├── read size: < 1 KiB
1465+
├── partitions total: 2
1466+
├── partitions scanned: 1
1467+
├── pruning stats: [segments: <range pruning: 2 to 2>, blocks: <range pruning: 2 to 2, bloom pruning: 2 to 1>]
1468+
├── push downs: [filters: [is_true(get(t3.b (#1), 'k2') = 'm')], limit: NONE]
1469+
└── estimated rows: 6.00
1470+
14411471
statement ok
14421472
drop table t1;
14431473

14441474
statement ok
14451475
drop table t2;
14461476

1477+
statement ok
1478+
drop table t3;
1479+
14471480
statement ok
14481481
CREATE TABLE customers AS SELECT
14491482
number % 100 AS customer_id,

tests/sqllogictests/suites/mode/standalone/explain_native/explain.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,3 +1078,38 @@ Sort
10781078
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
10791079
├── push downs: [filters: [], limit: NONE]
10801080
└── estimated rows: 3.00
1081+
1082+
statement ok
1083+
drop table if exists t3;
1084+
1085+
statement ok
1086+
CREATE TABLE t3(a int, b map(string, string) null);
1087+
1088+
statement ok
1089+
INSERT INTO t3 VALUES (1, {'k1':'a', 'k2':'b'}), (2, null), (3, {'k3':'z'});
1090+
1091+
statement ok
1092+
INSERT INTO t3 VALUES (4, {'k1':'a', 'k2':'m'}), (5, null), (6, {'k3':'z'});
1093+
1094+
query I
1095+
EXPLAIN SELECT * FROM t3 WHERE b['k2'] = 'm';
1096+
----
1097+
TableScan
1098+
├── table: default.default.t3
1099+
├── output columns: [a (#0), b (#1)]
1100+
├── read rows: 3
1101+
├── read size: < 1 KiB
1102+
├── partitions total: 2
1103+
├── partitions scanned: 1
1104+
├── pruning stats: [segments: <range pruning: 2 to 2>, blocks: <range pruning: 2 to 2, bloom pruning: 2 to 1>]
1105+
├── push downs: [filters: [is_true(get(t3.b (#1), 'k2') = 'm')], limit: NONE]
1106+
└── estimated rows: 1.20
1107+
1108+
statement ok
1109+
drop table t1;
1110+
1111+
statement ok
1112+
drop table t2;
1113+
1114+
statement ok
1115+
drop table t3;

0 commit comments

Comments
 (0)