Skip to content

Commit

Permalink
feat: add like domain (#11265)
Browse files Browse the repository at this point in the history
* feat: add like domain

* feat: add like domain

* feat: add like domain

---------

Co-authored-by: BohuTANG <overred.shuttler@gmail.com>
  • Loading branch information
sundy-li and BohuTANG authored Apr 29, 2023
1 parent 867b922 commit ed9bdba
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/query/functions/src/scalars/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::sync::Arc;

use common_arrow::arrow::bitmap::MutableBitmap;
use common_expression::types::boolean::BooleanDomain;
use common_expression::types::string::StringDomain;
use common_expression::types::AnyType;
use common_expression::types::ArgType;
use common_expression::types::ArrayType;
Expand Down Expand Up @@ -441,7 +442,44 @@ fn register_like(registry: &mut FunctionRegistry) {

registry.register_passthrough_nullable_2_arg::<StringType, StringType, BooleanType, _, _>(
"like",
|_, _| FunctionDomain::Full,
|lhs, rhs| {
if rhs.max.as_ref() == Some(&rhs.min) {
let pattern_type = check_pattern_type(&rhs.min, false);
if pattern_type == PatternType::EndOfPercent
|| pattern_type == PatternType::OrdinalStr
{
let (min, max) = if pattern_type == PatternType::EndOfPercent {
let min = rhs.min[..rhs.min.len() - 1].to_vec();
let mut max = min.clone();

let l = max.len();
if max[l - 1] != u8::MAX {
max[l - 1] += 1;
} else {
return FunctionDomain::Full;
}
(min, max)
} else {
(rhs.min.clone(), rhs.min.clone())
};

let other = StringDomain {
min,
max: Some(max),
};
let gte = lhs.domain_gte(&other);
let lt = lhs.domain_lt(&other);

if let (FunctionDomain::Domain(lhs), FunctionDomain::Domain(rhs)) = (lt, gte) {
return FunctionDomain::Domain(BooleanDomain {
has_false: lhs.has_false || rhs.has_false,
has_true: lhs.has_true && rhs.has_true,
});
}
}
}
FunctionDomain::Full
},
vectorize_like(|str, pat, _, pattern_type| {
match pattern_type {
PatternType::OrdinalStr => str == pat,
Expand Down
3 changes: 3 additions & 0 deletions src/query/functions/tests/it/scalars/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ fn test_like(file: &mut impl Write) {
StringType::from_data(vec!["abc", "abd", "abe", "abf"]),
)];
run_ast(file, "lhs like 'a%'", &columns);
run_ast(file, "lhs like 'b%'", &columns);
run_ast(file, "lhs like 'c'", &columns);

let columns = [
(
"lhs",
Expand Down
48 changes: 48 additions & 0 deletions src/query/functions/tests/it/scalars/testdata/comparison.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,54 @@ evaluation (internal):
+--------+------------------------------------------------------------------------------+


ast : lhs like 'b%'
raw expr : like(lhs::String, 'b%')
checked expr : like<String, String>(lhs, "b%")
optimized expr : false
evaluation:
+--------+-----------------+---------+
| | lhs | Output |
+--------+-----------------+---------+
| Type | String | Boolean |
| Domain | {"abc"..="abf"} | {FALSE} |
| Row 0 | 'abc' | false |
| Row 1 | 'abd' | false |
| Row 2 | 'abe' | false |
| Row 3 | 'abf' | false |
+--------+-----------------+---------+
evaluation (internal):
+--------+------------------------------------------------------------------------------+
| Column | Data |
+--------+------------------------------------------------------------------------------+
| lhs | StringColumn { data: 0x616263616264616265616266, offsets: [0, 3, 6, 9, 12] } |
| Output | Boolean([0b____0000]) |
+--------+------------------------------------------------------------------------------+


ast : lhs like 'c'
raw expr : like(lhs::String, 'c')
checked expr : like<String, String>(lhs, "c")
optimized expr : false
evaluation:
+--------+-----------------+---------+
| | lhs | Output |
+--------+-----------------+---------+
| Type | String | Boolean |
| Domain | {"abc"..="abf"} | {FALSE} |
| Row 0 | 'abc' | false |
| Row 1 | 'abd' | false |
| Row 2 | 'abe' | false |
| Row 3 | 'abf' | false |
+--------+-----------------+---------+
evaluation (internal):
+--------+------------------------------------------------------------------------------+
| Column | Data |
+--------+------------------------------------------------------------------------------+
| lhs | StringColumn { data: 0x616263616264616265616266, offsets: [0, 3, 6, 9, 12] } |
| Output | Boolean([0b____0000]) |
+--------+------------------------------------------------------------------------------+


ast : lhs like rhs
raw expr : like(lhs::String, rhs::String)
checked expr : like<String, String>(lhs, rhs)
Expand Down

1 comment on commit ed9bdba

@vercel
Copy link

@vercel vercel bot commented on ed9bdba Apr 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend.vercel.app
databend-databend.vercel.app
databend-git-main-databend.vercel.app
databend.rs

Please sign in to comment.