larry98 commented on code in PR #43256:
URL: https://github.com/apache/arrow/pull/43256#discussion_r1722156744
##########
cpp/src/arrow/compute/expression.cc:
##########
@@ -1242,8 +1309,168 @@ struct Inequality {
/*insert_implicit_casts=*/false, &exec_context);
}
+ /// Simplify an `is_in` value set against an inequality guarantee.
+ ///
+ /// Simplifying an `is_in` predicate involves filtering out any values from
+ /// the value set that cannot possibly be found given the guarantee. For
+ /// example, if we have the predicate 'x is_in [1, 2, 3, 4]' and the
guarantee
+ /// 'x > 2', then the simplified predicate 'x is_in [3, 4]' is equivalent.
+ /// This can be done efficiently if the value set is sorted and unique by
+ /// binary searching the inequality gound and slicing the value set
+ /// accordingly.
+ ///
+ /// \pre `guarantee` is non-nullable
+ /// \pre `guarantee.bound` is a scalar
+ /// \pre `guarantee.bound.type()->id() == value_set->type_id()`
+ /// \return a simplified value set, or a bool if the simplification of the
value set
+ /// means the whole is_in expr can become a boolean literal.
+ template <typename ArrowType>
+ static Result<std::variant<std::shared_ptr<Array>, bool>>
SimplifyIsInValueSet(
+ const Inequality& guarantee, std::shared_ptr<Array> value_set) {
+ using ArrayType = typename TypeTraits<ArrowType>::ArrayType;
+
+ DCHECK(guarantee.bound.is_scalar());
+
+ if (value_set->length() == 0) return false;
+
+ ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Scalar> scalar_bound,
+ guarantee.bound.scalar()->CastTo(value_set->type()));
+ auto bound = internal::UnboxScalar<ArrowType>::Unbox(*scalar_bound);
+ auto compare = [&bound, &value_set](size_t i) -> Comparison::type {
+ DCHECK(value_set->IsValid(i));
+ auto value = checked_pointer_cast<ArrayType>(value_set)->GetView(i);
+ return value == bound ? Comparison::EQUAL
+ : value < bound ? Comparison::LESS
+ : Comparison::GREATER;
+ };
+
+ size_t lo = 0;
+ size_t hi = value_set->length();
+ while (lo + 1 < hi) {
+ size_t mid = (lo + hi) / 2;
+ Comparison::type cmp = compare(mid);
+ if (cmp & Comparison::LESS_EQUAL) {
+ lo = mid;
+ } else {
+ hi = mid;
+ }
+ }
+
+ Comparison::type cmp = compare(lo);
+ size_t pivot = lo + (cmp == Comparison::LESS ? 1 : 0);
+ bool found = cmp == Comparison::EQUAL;
+
+ switch (guarantee.cmp) {
+ case Comparison::EQUAL:
+ return found;
+ case Comparison::LESS:
+ value_set = value_set->Slice(0, pivot);
+ break;
+ case Comparison::LESS_EQUAL:
+ value_set = value_set->Slice(0, pivot + (found ? 1 : 0));
Review Comment:
Nulls are already removed in `PrepareIsInValueSet`. But as you pointed out
above this is incorrect if the null matching behavior is INCONCLUSIVE. I'll
handle this by skipping simplification in this case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]