Skip to content

Commit 904b33a

Browse files
authored
refactor: String column like % can rewrite to true (#18263)
optimize: String/Variant column like % can rewrite to true
1 parent b9f2227 commit 904b33a

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

src/query/functions/src/scalars/comparison.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,34 +1038,34 @@ fn register_like(registry: &mut FunctionRegistry) {
10381038
}
10391039

10401040
fn calc_like_domain(lhs: &StringDomain, pattern: String) -> Option<FunctionDomain<BooleanType>> {
1041-
let pattern_type = generate_like_pattern(pattern.as_bytes(), 1);
1042-
1043-
if matches!(pattern_type, LikePattern::OrdinalStr(_)) {
1044-
return Some(lhs.domain_eq(&StringDomain {
1041+
match generate_like_pattern(pattern.as_bytes(), 1) {
1042+
LikePattern::StartOfPercent(v) if v.is_empty() => {
1043+
Some(FunctionDomain::Domain(ALL_TRUE_DOMAIN))
1044+
}
1045+
LikePattern::OrdinalStr(_) => Some(lhs.domain_eq(&StringDomain {
10451046
min: pattern.clone(),
10461047
max: Some(pattern),
1047-
}));
1048-
}
1049-
1050-
if matches!(pattern_type, LikePattern::EndOfPercent(_)) {
1051-
let mut pat_str = pattern;
1052-
// remove the last char '%'
1053-
pat_str.pop();
1054-
let pat_len = pat_str.chars().count();
1055-
let other = StringDomain {
1056-
min: pat_str.clone(),
1057-
max: Some(pat_str),
1058-
};
1059-
let lhs = StringDomain {
1060-
min: lhs.min.chars().take(pat_len).collect(),
1061-
max: lhs
1062-
.max
1063-
.as_ref()
1064-
.map(|max| max.chars().take(pat_len).collect()),
1065-
};
1066-
return Some(lhs.domain_eq(&other));
1048+
})),
1049+
LikePattern::EndOfPercent(_) => {
1050+
let mut pat_str = pattern;
1051+
// remove the last char '%'
1052+
pat_str.pop();
1053+
let pat_len = pat_str.chars().count();
1054+
let other = StringDomain {
1055+
min: pat_str.clone(),
1056+
max: Some(pat_str),
1057+
};
1058+
let lhs = StringDomain {
1059+
min: lhs.min.chars().take(pat_len).collect(),
1060+
max: lhs
1061+
.max
1062+
.as_ref()
1063+
.map(|max| max.chars().take(pat_len).collect()),
1064+
};
1065+
Some(lhs.domain_eq(&other))
1066+
}
1067+
_ => None,
10671068
}
1068-
None
10691069
}
10701070

10711071
fn variant_vectorize_like_jsonb() -> impl Fn(

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ abcd
1414
abcd[
1515
abcde
1616

17+
query T
18+
explain select * from t1 where s like '%';
19+
----
20+
Filter
21+
├── output columns: [t1.s (#0)]
22+
├── filters: [is_true(like(t1.s (#0), '%'))]
23+
├── estimated rows: 5.00
24+
└── TableScan
25+
├── table: default.default.t1
26+
├── output columns: [s (#0)]
27+
├── read rows: 5
28+
├── read size: < 1 KiB
29+
├── partitions total: 1
30+
├── partitions scanned: 1
31+
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
32+
├── push downs: [filters: [is_true(like(t1.s (#0), '%'))], limit: NONE]
33+
└── estimated rows: 5.00
34+
1735
query T
1836
explain select * from t1 where s like 'abcd%' order by s;
1937
----
@@ -161,3 +179,27 @@ Filter
161179
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
162180
├── push downs: [filters: [is_true(like_any(t1.s (#0), '%b$%c%', '$'))], limit: NONE]
163181
└── estimated rows: 5.00
182+
183+
184+
statement ok
185+
create or replace table t1 (c varchar not null, c3 variant not null, c5 int);
186+
187+
statement ok
188+
insert into t1 values('c',parse_json('[1,2,3,["a","b","c"],{"k":"v"}]'), 1),('c1',parse_json('[1,2,3,["a","b","c"],{"k":"v1"}]'), 2)
189+
190+
query T
191+
explain select c from t1 where c like '%'
192+
----
193+
TableScan
194+
├── table: default.default.t1
195+
├── output columns: [c (#0)]
196+
├── read rows: 2
197+
├── read size: < 1 KiB
198+
├── partitions total: 1
199+
├── partitions scanned: 1
200+
├── pruning stats: [segments: <range pruning: 1 to 1>, blocks: <range pruning: 1 to 1>]
201+
├── push downs: [filters: [], limit: NONE]
202+
└── estimated rows: 2.00
203+
204+
statement ok
205+
drop table if exists t1;

0 commit comments

Comments
 (0)