Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LIKE performance for Dictionary arrays #11058

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ impl<'a> TreeNodeRewriter for TypeCoercionRewriter<'a> {
"There isn't a common type to coerce {left_type} and {right_type} in {op_name} expression"
)
})?;
let expr = Box::new(expr.cast_to(&coerced_type, self.schema)?);
let expr = match left_type {
DataType::Dictionary(_, inner) if *inner == DataType::Utf8 => expr,
_ => Box::new(expr.cast_to(&coerced_type, self.schema)?),
};
let pattern = Box::new(pattern.cast_to(&coerced_type, self.schema)?);
Ok(Transformed::yes(Expr::Like(Like::new(
negated,
Expand Down
10 changes: 9 additions & 1 deletion datafusion/physical-expr/src/expressions/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ impl PartialEq<dyn Any> for LikeExpr {
}
}

/// used for optimize Dictionary like
fn can_like_type(from_type: &DataType) -> bool {
match from_type {
DataType::Dictionary(_, inner_type_from) => **inner_type_from == DataType::Utf8,
_ => false,
}
}

/// Create a like expression, erroring if the argument types are not compatible.
pub fn like(
negated: bool,
Expand All @@ -158,7 +166,7 @@ pub fn like(
) -> Result<Arc<dyn PhysicalExpr>> {
let expr_type = &expr.data_type(input_schema)?;
let pattern_type = &pattern.data_type(input_schema)?;
if !expr_type.eq(pattern_type) {
if !expr_type.eq(pattern_type) && !can_like_type(expr_type) {
return internal_err!(
"The type of {expr_type} AND {pattern_type} of like physical should be same"
);
Expand Down
81 changes: 81 additions & 0 deletions datafusion/sqllogictest/test_files/regexp.slt
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,84 @@ true

statement ok
drop table t;

statement ok
create or replace table strings as values
('FooBar'),
('Foo'),
('Foo'),
('Bar'),
('FooBar'),
('Bar'),
('Baz');

statement ok
create or replace table dict_table as
select arrow_cast(column1, 'Dictionary(Int32, Utf8)') as column1
from strings;

query ?
select column1 from dict_table where column1 LIKE '%oo%';
----
FooBar
Foo
Foo
FooBar

query ?
select column1 from dict_table where column1 NOT LIKE '%oo%';
----
Bar
Bar
Baz

query ?
select column1 from dict_table where column1 ILIKE '%oO%';
----
FooBar
Foo
Foo
FooBar

query ?
select column1 from dict_table where column1 NOT ILIKE '%oO%';
----
Bar
Bar
Baz


# plan should not cast the column, instead it should use the dictionary directly
query TT
explain select column1 from dict_table where column1 LIKE '%oo%';
----
logical_plan
01)Filter: dict_table.column1 LIKE Utf8("%oo%")
02)--TableScan: dict_table projection=[column1]
physical_plan
01)CoalesceBatchesExec: target_batch_size=8192
02)--FilterExec: column1@0 LIKE %oo%
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no cast! Yay!

03)----MemoryExec: partitions=1, partition_sizes=[1]

# Ensure casting / coercion works for all operators
# (there should be no casts to Utf8)
query TT
explain select
column1 LIKE '%oo%',
column1 NOT LIKE '%oo%',
column1 ILIKE '%oo%',
column1 NOT ILIKE '%oo%'
from dict_table;
----
logical_plan
01)Projection: dict_table.column1 LIKE Utf8("%oo%"), dict_table.column1 NOT LIKE Utf8("%oo%"), dict_table.column1 ILIKE Utf8("%oo%"), dict_table.column1 NOT ILIKE Utf8("%oo%")
02)--TableScan: dict_table projection=[column1]
physical_plan
01)ProjectionExec: expr=[column1@0 LIKE %oo% as dict_table.column1 LIKE Utf8("%oo%"), column1@0 NOT LIKE %oo% as dict_table.column1 NOT LIKE Utf8("%oo%"), column1@0 ILIKE %oo% as dict_table.column1 ILIKE Utf8("%oo%"), column1@0 NOT ILIKE %oo% as dict_table.column1 NOT ILIKE Utf8("%oo%")]
02)--MemoryExec: partitions=1, partition_sizes=[1]

statement ok
drop table strings

statement ok
drop table dict_table
Loading