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

feat: support inlist in LiteralGurantee for pruning #8654

Merged
merged 7 commits into from
Dec 28, 2023
26 changes: 20 additions & 6 deletions datafusion/physical-expr/src/utils/guarantee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ impl LiteralGuarantee {
.downcast_ref::<crate::expressions::Column>();
let Some(col) = col else {
return builder;
}
};

let literals = inlist
.list()
.iter()
.map(|e| e.as_any().downcast_ref::<crate::expressions::Literal>())
.collect::<Option<Vec<_>>>();
if literals.is_none() {
let Some(literals) = literals else {
return builder;
}
};

let guarantee = if inlist.negated() {
Guarantee::NotIn
Expand All @@ -153,9 +153,9 @@ impl LiteralGuarantee {
};

builder.aggregate_multi_conjunct(
col.unwrap(),
col,
guarantee,
literals.unwrap().iter().map(|e| e.value()),
literals.iter().map(|e| e.value()),
)
} else {
// split disjunction: <expr> OR <expr> OR ...
Expand Down Expand Up @@ -378,7 +378,6 @@ impl<'a> ColOpLit<'a> {
left.downcast_ref::<crate::expressions::Literal>(),
right.downcast_ref::<crate::expressions::Column>(),
) {
// Used swapped operator operator, if possible
Some(Self {
col,
guarantee,
Expand Down Expand Up @@ -780,6 +779,21 @@ mod test {
.and(col("b").eq(lit(3)).or(col("b").eq(lit(4)))),
vec![not_in_guarantee("b", [1, 2, 3]), in_guarantee("b", [3, 4])],
);
// b IN (1, 2, 3) OR b = 2
// TODO this should be in_guarantee("b", [1, 2, 3]) but currently we don't support to anylize this kind of disjunction. Only `ColOpLit OR ColOpLit` is supported.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

test_analyze(
col("b")
.in_list(vec![lit(1), lit(2), lit(3)], false)
.or(col("b").eq(lit(2))),
vec![],
);
// b IN (1, 2, 3) OR b != 3
test_analyze(
col("b")
.in_list(vec![lit(1), lit(2), lit(3)], false)
.or(col("b").not_eq(lit(3))),
vec![],
);
}

/// Tests that analyzing expr results in the expected guarantees
Expand Down