diff --git a/cpp/src/arrow/acero/expression_benchmark.cc b/cpp/src/arrow/acero/expression_benchmark.cc index a57dd6b9e3f2..fd83409009cd 100644 --- a/cpp/src/arrow/acero/expression_benchmark.cc +++ b/cpp/src/arrow/acero/expression_benchmark.cc @@ -20,6 +20,8 @@ #include #include "arrow/acero/test_util_internal.h" +#include "arrow/array/builder_primitive.h" +#include "arrow/compute/api_scalar.h" #include "arrow/compute/cast.h" #include "arrow/compute/expression.h" #include "arrow/dataset/partition.h" @@ -151,6 +153,30 @@ auto guarantee = and_(equal(field_ref("a"), literal(int64_t(99))), auto guarantee_dictionary = and_(equal(field_ref("a"), literal(ninety_nine_dict)), equal(field_ref("b"), literal(ninety_nine_dict))); +// An is_in filter whose value set is too large to be narrowed for each guarantee: +// 100000 multiples of 1000. +Expression MakeLargeIsInFilter() { + Int64Builder builder; + for (int64_t i = 0; i < 100000; ++i) { + ABORT_NOT_OK(builder.Append(i * 1000)); + } + ASSIGN_OR_ABORT(auto value_set, builder.Finish()); + return call("is_in", {field_ref("a")}, compute::SetLookupOptions{value_set}); +} +auto filter_large_is_in = MakeLargeIsInFilter(); + +// The guarantee which the statistics of a Parquet row group give: its smallest and +// largest value, here between two values of the set or including one. +auto guarantee_range_negative = + and_(greater_equal(field_ref("a"), literal(int64_t(5001))), + less_equal(field_ref("a"), literal(int64_t(5999)))); +auto guarantee_range_positive = + and_(greater_equal(field_ref("a"), literal(int64_t(5000))), + less_equal(field_ref("a"), literal(int64_t(5999)))); +// ... and of a row group which may contain nulls. +auto guarantee_nullable_range_negative = + or_(guarantee_range_negative, is_null(field_ref("a"))); + auto complex_expression = and_(less(field_ref("x"), literal(20)), greater(field_ref("x"), literal(0))); auto complex_integer_expression = @@ -171,6 +197,12 @@ BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, guarantee_dictionary); BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, negative_filter_cast_guarantee_dictionary, filter_cast_negative, guarantee_dictionary); +BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, + negative_filter_large_is_in_guarantee_range, filter_large_is_in, + guarantee_range_negative); +BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, + negative_filter_large_is_in_guarantee_nullable_range, + filter_large_is_in, guarantee_nullable_range_negative); // Positive queries (partition expressions that pass the filter) BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, positive_filter_simple_guarantee_simple, filter_simple_positive, guarantee); @@ -181,6 +213,9 @@ BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, guarantee_dictionary); BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, positive_filter_cast_guarantee_dictionary, filter_cast_positive, guarantee_dictionary); +BENCHMARK_CAPTURE(SimplifyFilterWithGuarantee, + positive_filter_large_is_in_guarantee_range, filter_large_is_in, + guarantee_range_positive); BENCHMARK_CAPTURE(BindAndEvaluate, simple_array, field_ref("int_arr")); BENCHMARK_CAPTURE(BindAndEvaluate, simple_scalar, field_ref("int_scalar")); diff --git a/cpp/src/arrow/compute/expression.cc b/cpp/src/arrow/compute/expression.cc index cd952055666c..271c6778cae3 100644 --- a/cpp/src/arrow/compute/expression.cc +++ b/cpp/src/arrow/compute/expression.cc @@ -30,6 +30,7 @@ #include "arrow/compute/exec_internal.h" #include "arrow/compute/expression_internal.h" #include "arrow/compute/function_internal.h" +#include "arrow/compute/range_evaluation_internal.h" #include "arrow/compute/util.h" #include "arrow/io/memory.h" #ifdef ARROW_IPC @@ -1160,8 +1161,42 @@ Result Canonicalize(Expression expr, compute::ExecContext* exec_cont namespace { +// Value sets up to this length are simplified against a guarantee by filtering the +// value set and rebinding the `is_in` call (see Inequality::SimplifyIsIn). This is +// too expensive for longer value sets (https://github.com/apache/arrow/issues/46777), +// for which the kernel is instead asked what the call evaluates to over the range of +// the guarantee (see internal::RangeEvaluator), which never rebinds. +constexpr int64_t kIsInSimplificationMaxValueSet = 50; + +// An Expression which evaluates to `value` where `target` is valid and to null where +// it is null: true_unless_null(target) or invert(true_unless_null(target)). +// +// true_unless_null is cheap; it purely reuses the validity bitmap for the values +// buffer. Inversion is less cheap but we expect that term never to be evaluated +// since invert(true_unless_null(x)) is not satisfiable. +Result ValueUnlessNull(const Expression& target, bool value) { + ExecContext exec_context; + + Expression::Call call; + call.function_name = "true_unless_null"; + call.arguments = {target}; + ARROW_ASSIGN_OR_RAISE(auto true_unless_null, + BindNonRecursive(std::move(call), + /*insert_implicit_casts=*/false, &exec_context)); + if (value) return true_unless_null; + + Expression::Call invert; + invert.function_name = "invert"; + invert.arguments = {std::move(true_unless_null)}; + return BindNonRecursive(std::move(invert), + /*insert_implicit_casts=*/false, &exec_context); +} + // An inequality comparison which a target Expression is known to satisfy. If nullable, // the target may evaluate to null in addition to values satisfying the comparison. +// +// `target` and `bound` refer into the guarantee the inequality was extracted from, +// which must outlive it. struct Inequality { // The inequality type Comparison::type cmp; @@ -1171,37 +1206,45 @@ struct Inequality { const Datum& bound; // Whether target can be null bool nullable; + // Whether the inequality was extracted from a range which may be null, see Extract + bool from_nullable_range = false; + + // Extract the inequalities implied by a guarantee, derived from "less", + // "greater", "less_equal", "greater_equal" and "equal" expressions, possibly + // disjuncted with an "is_null" Expression: + // cmp(a, 2) + // cmp(a, 2) or is_null(a) + // (cmp(a, 2) and cmp(a, 5)) or is_null(a) + // The last is the shape of a Parquet row group's statistics when it may contain + // nulls. In a disjunction each bound only holds for non-null values of the field. + // + // Inequalities of the last shape are used with more care than the others, whose + // handling is left as it is: + // - only where no types need to be reconciled, see AppliesTo; + // - for floating point fields, only where NaN evaluates alike, see Simplify. + static std::vector Extract(const Expression& guarantee) { + if (auto inequality = ExtractOneFromComparison(guarantee)) return {*inequality}; - // Extract an Inequality if possible, derived from "less", - // "greater", "less_equal", and "greater_equal" expressions, - // possibly disjuncted with an "is_null" Expression. - // cmp(a, 2) - // cmp(a, 2) or is_null(a) - static std::optional ExtractOne(const Expression& guarantee) { auto call = guarantee.call(); - if (!call) return std::nullopt; - - if (call->function_name == "or_kleene") { - // expect the LHS to be a usable field inequality - auto out = ExtractOneFromComparison(call->arguments[0]); - if (!out) return std::nullopt; - - // expect the RHS to be an is_null expression - auto call_rhs = call->arguments[1].call(); - if (!call_rhs) return std::nullopt; - if (call_rhs->function_name != "is_null") return std::nullopt; - - // ... and that it references the same target - auto target = call_rhs->arguments[0].field_ref(); - if (!target) return std::nullopt; - if (*target != out->target) return std::nullopt; - - out->nullable = true; - return out; + if (!call || call->function_name != "or_kleene") return {}; + + // expect the RHS to be an is_null expression ... + auto null_call = call->arguments[1].call(); + if (!null_call || null_call->function_name != "is_null") return {}; + auto target = null_call->arguments[0].field_ref(); + if (!target) return {}; + + // ... and the LHS to be a conjunction of usable inequalities on the same field + std::vector inequalities; + const auto members = GuaranteeConjunctionMembers(call->arguments[0]); + for (const auto& member : members) { + auto inequality = ExtractOneFromComparison(member); + if (!inequality || inequality->target != *target) continue; + inequality->nullable = true; + inequality->from_nullable_range = members.size() > 1; + inequalities.push_back(*inequality); } - - // fall back to a simple comparison with no "is_null" - return ExtractOneFromComparison(guarantee); + return inequalities; } static std::optional ExtractOneFromComparison(const Expression& guarantee) { @@ -1217,7 +1260,8 @@ struct Inequality { auto bound = call->arguments[1].literal(); if (!bound) return std::nullopt; - if (!bound->is_scalar()) return std::nullopt; + // A comparison with null is null, which says nothing about the target + if (!bound->is_scalar() || !bound->scalar()->is_valid) return std::nullopt; return Inequality{*cmp, /*target=*/*target, *bound, /*nullable=*/false}; } @@ -1225,33 +1269,26 @@ struct Inequality { return std::nullopt; } + /// Whether this inequality is used to simplify a call on `argument`, which is the + /// target, possibly wrapped in order preserving casts. + /// + /// A range which may be null is only used for calls on the field itself, not on a + /// cast of it, where the field has the type of the bounds. (A column may be read as + /// another type than it is stored as, in which case its statistics bound it with + /// values of another type.) + bool AppliesTo(const Expression& argument) const { + if (!from_nullable_range) return true; + return argument.field_ref() != nullptr && bound.type()->Equals(*argument.type()); + } + /// The given expression simplifies to `value` if the inequality /// target is not nullable. Otherwise, it simplifies to either a /// call to true_unless_null or !true_unless_null. Result simplified_to(const Expression& bound_target, bool value) const { if (!nullable) return literal(value); - ExecContext exec_context; - // Data may be null, so comparison will yield `value` - or null IFF the data was null - // - // true_unless_null is cheap; it purely reuses the validity bitmap for the values - // buffer. Inversion is less cheap but we expect that term never to be evaluated - // since invert(true_unless_null(x)) is not satisfiable. - Expression::Call call; - call.function_name = "true_unless_null"; - call.arguments = {bound_target}; - ARROW_ASSIGN_OR_RAISE( - auto true_unless_null, - BindNonRecursive(std::move(call), - /*insert_implicit_casts=*/false, &exec_context)); - if (value) return true_unless_null; - - Expression::Call invert; - invert.function_name = "invert"; - invert.arguments = {std::move(true_unless_null)}; - return BindNonRecursive(std::move(invert), - /*insert_implicit_casts=*/false, &exec_context); + return ValueUnlessNull(bound_target, value); } /// Simplify an `is_in` call against an inequality guarantee. @@ -1270,11 +1307,7 @@ struct Inequality { auto options = checked_pointer_cast(is_in_call->options); - // The maximum number of values in the is_in expression set of values - // in order to use the simplification. - // If the set is large there are performance implications, see: - // https://github.com/apache/arrow/issues/46777 - constexpr int16_t kIsInSimplificationMaxValueSet = 50; + // Large value sets are handled by the kernel instead, see SimplifyOverRange. if (options->value_set.length() > kIsInSimplificationMaxValueSet) { return std::nullopt; } @@ -1282,9 +1315,25 @@ struct Inequality { const auto& lhs = Comparison::StripOrderPreservingCasts(is_in_call->arguments[0]); if (!lhs.field_ref()) return std::nullopt; if (*lhs.field_ref() != guarantee.target) return std::nullopt; + if (!guarantee.AppliesTo(is_in_call->arguments[0])) return std::nullopt; + // ... which includes the value set, which the kernel matches as the field's type + if (guarantee.from_nullable_range && + !options->value_set.type()->Equals(*lhs.type())) { + return std::nullopt; + } + + // Statistics based guarantees cannot bound NaN (a row group's minimum and + // maximum ignore it) whereas `is_in` matches NaN with NaN, so a NaN in the + // value set must not be filtered out if the field can be NaN. Leave such value + // sets alone, as the kernel does for large ones. + if (is_floating(lhs.type()->id()) && is_floating(options->value_set.type()->id())) { + ARROW_ASSIGN_OR_RAISE(Datum is_nan, IsNan(options->value_set)); + ARROW_ASSIGN_OR_RAISE(Datum any_nan, Any(is_nan)); + if (any_nan.scalar_as().value) return std::nullopt; + } FilterOptions::NullSelectionBehavior null_selection{}; - switch (options->null_matching_behavior) { + switch (options->GetNullMatchingBehavior()) { case SetLookupOptions::MATCH: null_selection = guarantee.nullable ? FilterOptions::EMIT_NULL : FilterOptions::DROP; @@ -1321,7 +1370,7 @@ struct Inequality { simplified_call.function_name = "is_in"; simplified_call.arguments = is_in_call->arguments; simplified_call.options = std::make_shared( - simplified_value_set, options->null_matching_behavior); + simplified_value_set, options->GetNullMatchingBehavior()); ARROW_ASSIGN_OR_RAISE( Expression simplified_expr, BindNonRecursive(std::move(simplified_call), @@ -1330,7 +1379,7 @@ struct Inequality { } /// \brief Simplify the given expression given this inequality as a guarantee. - Result Simplify(Expression expr) { + Result Simplify(Expression expr) const { const auto& guarantee = *this; auto call = expr.call(); @@ -1361,6 +1410,17 @@ struct Inequality { const auto& lhs = Comparison::StripOrderPreservingCasts(call->arguments[0]); if (!lhs.field_ref()) return expr; if (*lhs.field_ref() != guarantee.target) return expr; + if (!guarantee.AppliesTo(call->arguments[0])) return expr; + // The statistics of a row group ignore NaN, so a range which may be null bounds + // the values of a floating point field other than NaN. A comparison is then only + // decided the way it evaluates for NaN: true for not_equal, false for the others. + const bool may_be_nan = + guarantee.from_nullable_range && is_floating(lhs.type()->id()); + auto simplified_to = [&](const Expression& bound_target, + bool value) -> Result { + if (may_be_nan && value != (*cmp == Comparison::NOT_EQUAL)) return expr; + return guarantee.simplified_to(bound_target, value); + }; // Whether the RHS of the expression is EQUAL, LESS, or GREATER than the // RHS of the guarantee. N.B. Comparison::type is a bitmask @@ -1412,6 +1472,96 @@ struct Inequality { } }; +/// \brief The range which the inequalities of a guarantee leave to a field: the +/// tightest lower and upper bound on it, and whether it may be null. +/// +/// Only bounds which have the type of the field are used; others are ignored, which +/// only loses precision. +internal::ArgumentRange ResolveRange(const std::vector& inequalities, + const FieldRef& field, const DataType& field_type) { + internal::ArgumentRange range; + + // Whether a bound is tighter than the current one on that side. Bounds which + // cannot be compared are ignored. + auto is_tighter = [](const std::shared_ptr& bound, bool inclusive, + const std::shared_ptr& current, bool current_inclusive, + Comparison::type tighter) { + if (!current) return true; + auto cmp = Comparison::Execute(bound, current); + if (!cmp.ok()) return false; + if (*cmp == Comparison::EQUAL) return current_inclusive && !inclusive; + return *cmp == tighter; + }; + + for (const Inequality& inequality : inequalities) { + if (inequality.target != field) continue; + // The guarantee holds for every row, and an inequality which does not admit + // nulls can only hold for non-null data. + range.nullable = range.nullable && inequality.nullable; + + const std::shared_ptr& bound = inequality.bound.scalar(); + if (!bound->type->Equals(field_type)) continue; + const bool inclusive = inequality.cmp & Comparison::EQUAL; + // Unless the comparison admits values below the bound, it is a lower bound; + // likewise for values above the bound. Equality is both. + if (!(inequality.cmp & Comparison::LESS) && + is_tighter(bound, inclusive, range.lower, range.lower_inclusive, + Comparison::GREATER)) { + range.lower = bound; + range.lower_inclusive = inclusive; + } + if (!(inequality.cmp & Comparison::GREATER) && + is_tighter(bound, inclusive, range.upper, range.upper_inclusive, + Comparison::LESS)) { + range.upper = bound; + range.upper_inclusive = inclusive; + } + } + return range; +} + +/// \brief Simplify a call whose kernel can tell what it evaluates to over a range +/// (currently `is_in`, which searches its sorted value set) against all the +/// inequalities on its argument at once. +/// +/// Applying the inequalities together matters: a row group's statistics yield +/// `x >= min and x <= max`, and neither bound on its own can rule out a value set +/// whose hull spans the row group. This costs O(log N) per guarantee, with no +/// rebinding. `is_in` calls with small value sets are left to +/// Inequality::SimplifyIsIn. +Result SimplifyOverRange(const std::vector& inequalities, + Expression expr) { + auto call = expr.call(); + if (!call || call->arguments.size() != 1) return expr; + + const auto* evaluator = + dynamic_cast(call->kernel_state.get()); + // The range is that of a field: the call must be on the field itself, not on a + // cast of it. + const FieldRef* field = call->arguments[0].field_ref(); + if (!evaluator || !field) return expr; + if (call->function_name == "is_in" && + checked_cast(*call->options).value_set.length() <= + kIsInSimplificationMaxValueSet) { + return expr; + } + + ARROW_ASSIGN_OR_RAISE(auto evaluation, + evaluator->EvaluateOverRange(ResolveRange( + inequalities, *field, *call->arguments[0].type()))); + switch (evaluation) { + case internal::RangeEvaluation::kUnknown: + break; + case internal::RangeEvaluation::kFalse: + return literal(false); + case internal::RangeEvaluation::kNull: + return literal(MakeNullScalar(call->type.GetSharedPtr())); + case internal::RangeEvaluation::kFalseUnlessNull: + return ValueUnlessNull(call->arguments[0], false); + } + return expr; +} + /// \brief Simplify an expression given a guarantee, if the guarantee /// is is_valid(). Result SimplifyIsValidGuarantee(Expression expr, @@ -1445,6 +1595,16 @@ Result SimplifyWithGuarantee(Expression expr, RETURN_NOT_OK(ExtractKnownFieldValues(&conjunction_members, &known_values)); + // Ranges which may be null are applied after the other members of the guarantee, so + // that what they simplify is not offered to the comparisons against those members, + // whose handling is left as it is (see Inequality::Extract). + std::stable_partition(conjunction_members.begin(), conjunction_members.end(), + [](const Expression& guarantee) { + auto inequalities = Inequality::Extract(guarantee); + return inequalities.empty() || + !inequalities.front().from_nullable_range; + }); + ARROW_ASSIGN_OR_RAISE(expr, ReplaceFieldsWithKnownValues(known_values, std::move(expr))); @@ -1458,12 +1618,12 @@ Result SimplifyWithGuarantee(Expression expr, for (const auto& guarantee : conjunction_members) { if (!guarantee.call()) continue; - if (auto inequality = Inequality::ExtractOne(guarantee)) { + for (const Inequality& inequality : Inequality::Extract(guarantee)) { ARROW_ASSIGN_OR_RAISE(auto simplified, ModifyExpression( std::move(expr), [](Expression expr) { return expr; }, [&](Expression expr, ...) -> Result { - return inequality->Simplify(std::move(expr)); + return inequality.Simplify(std::move(expr)); })); if (Expression::Identical(simplified, expr)) continue; @@ -1484,6 +1644,32 @@ Result SimplifyWithGuarantee(Expression expr, } } + // Calls which can be evaluated over a range (`is_in` with a large value set) are + // simplified against all the inequalities of the guarantee at once, see + // SimplifyOverRange. The inequalities refer into conjunction_members. + // + // This comes last, so that the comparisons simplified above are those which would + // be simplified without it: they are not offered a literal which a call folded to. + std::vector inequalities; + for (const auto& guarantee : conjunction_members) { + for (const Inequality& inequality : Inequality::Extract(guarantee)) { + inequalities.push_back(inequality); + } + } + if (!inequalities.empty()) { + ARROW_ASSIGN_OR_RAISE(auto simplified, + ModifyExpression( + expr, [](Expression expr) { return expr; }, + [&](Expression expr, ...) -> Result { + return SimplifyOverRange(inequalities, std::move(expr)); + })); + + if (!Expression::Identical(simplified, expr)) { + expr = std::move(simplified); + RETURN_NOT_OK(CanonicalizeAndFoldConstants()); + } + } + return expr; } diff --git a/cpp/src/arrow/compute/expression_test.cc b/cpp/src/arrow/compute/expression_test.cc index 10c1b348e62d..e40059005153 100644 --- a/cpp/src/arrow/compute/expression_test.cc +++ b/cpp/src/arrow/compute/expression_test.cc @@ -31,6 +31,7 @@ #include "arrow/compute/expression_internal.h" #include "arrow/compute/function_internal.h" #include "arrow/compute/registry.h" +#include "arrow/memory_pool.h" #include "arrow/testing/gtest_util.h" #include "arrow/testing/matchers.h" @@ -90,6 +91,14 @@ std::string make_range_json(int start, int end) { return result; } +// Concatenate JSON arrays: "[1,2]" + "[3]" -> "[1,2,3]" +std::string concat_json(const std::string& left, const std::string& right) { + std::string result = left.substr(0, left.size() - 1); + if (result.size() > 1 && right.size() > 2) result += ","; + result += right.substr(1); + return result; +} + const auto no_change = std::nullopt; TEST(ExpressionUtils, Comparison) { @@ -1699,6 +1708,24 @@ struct Simplify { Expectable WithGuarantee(Expression guarantee) { return {expr, guarantee}; } }; +// Rows satisfying `guarantee` (as JSON) must evaluate identically under `filter` and +// under `filter` simplified against the guarantee. +void ExpectSimplifiedEvaluatesEqually(Expression filter, const Expression& guarantee, + const std::string& rows) { + ASSERT_OK_AND_ASSIGN(filter, filter.Bind(*kBoringSchema)); + ASSERT_OK_AND_ASSIGN(auto simplified, SimplifyWithGuarantee(filter, guarantee)); + auto input = RecordBatchFromJSON(kBoringSchema, rows); + Datum evaluated, simplified_evaluated; + ExpectExecute(filter, input, &evaluated); + ExpectExecute(simplified, input, &simplified_evaluated); + if (simplified_evaluated.is_scalar()) { + ASSERT_OK_AND_ASSIGN( + simplified_evaluated, + MakeArrayFromScalar(*simplified_evaluated.scalar(), input->num_rows())); + } + AssertDatumsEqual(evaluated, simplified_evaluated, /*verbose=*/true); +} + TEST(Expression, SingleComparisonGuarantees) { auto i32 = field_ref("i32"); @@ -2003,6 +2030,97 @@ TEST(Expression, SimplifyWithComparisonAndNullableCaveat) { true_unless_null(field_ref("i32")))); // not satisfiable, will drop row group } +TEST(Expression, SimplifyWithNullableRangeGuarantee) { + // The shape of Parquet statistics for a row group which may contain nulls: both + // bounds hold for every non-null value. + auto i32 = field_ref("i32"); + auto u32 = field_ref("u32"); + auto range = and_(greater_equal(i32, literal(1)), less_equal(i32, literal(5))); + auto nullable_range = or_(range, is_null(i32)); + + Simplify{greater(i32, literal(10))} + .WithGuarantee(nullable_range) + .Expect(not_(true_unless_null(i32))); + Simplify{less(i32, literal(0))} + .WithGuarantee(nullable_range) + .Expect(not_(true_unless_null(i32))); + Simplify{less(i32, literal(10))} + .WithGuarantee(nullable_range) + .Expect(true_unless_null(i32)); + Simplify{is_null(i32)}.WithGuarantee(nullable_range).ExpectUnchanged(); + Simplify{is_valid(i32)}.WithGuarantee(nullable_range).ExpectUnchanged(); + + // Small value sets are filtered by both bounds + auto is_in = [&](std::string json_array) { + return call( + "is_in", {i32}, + SetLookupOptions{ArrayFromJSON(int32(), json_array), SetLookupOptions::MATCH}); + }; + Simplify{is_in("[10, 11]")}.WithGuarantee(nullable_range).Expect(false); + Simplify{is_in("[0, 3, 11]")}.WithGuarantee(nullable_range).Expect(is_in("[3]")); + + // Statistics cannot bound NaN, which is_in matches, so a small value set + // containing NaN is not filtered either + auto f64 = field_ref("f64"); + auto f64_range = and_(greater_equal(f64, literal(0.0)), less_equal(f64, literal(10.0))); + auto is_in_f64 = [&](std::string json_array) { + return call("is_in", {f64}, SetLookupOptions{ArrayFromJSON(float64(), json_array)}); + }; + Simplify{is_in_f64("[NaN]")}.WithGuarantee(f64_range).ExpectUnchanged(); + Simplify{is_in_f64("[NaN, 5.0, 20.0]")}.WithGuarantee(f64_range).ExpectUnchanged(); + // ... of any floating point type: a half float value set is matched as float32 + auto f32 = field_ref("f32"); + Simplify{call("is_in", {f32}, SetLookupOptions{ArrayFromJSON(float16(), "[NaN, 20]")})} + .WithGuarantee(greater_equal(f32, literal(0.0F))) + .ExpectUnchanged(); + // ... unless the field cannot be NaN + Simplify{call("is_in", {i32}, SetLookupOptions{ArrayFromJSON(float64(), "[NaN, 20]")})} + .WithGuarantee(range) + .Expect(false); + + // A null bound says nothing + Simplify{greater(i32, literal(10))} + .WithGuarantee(or_(and_(greater_equal(i32, literal(MakeNullScalar(int32()))), + less_equal(i32, literal(5))), + is_null(i32))) + .Expect(not_(true_unless_null(i32))); + + // A range which may be null is applied after the other members of the guarantee, + // so that a comparison against those is not offered what a call folds to + Simplify{greater(f64, cast(is_in("[10, 11]"), float64()))} + .WithGuarantee(and_(nullable_range, greater_equal(f64, literal(1.0)))) + .Expect(greater(f64, literal(0.0))); + + // Bounds are not inferred for a field other than the one which may be null + Simplify{greater(i32, literal(10))} + .WithGuarantee(or_(range, is_null(u32))) + .ExpectUnchanged(); + + // The bounds of a nullable range are only used where no types need to be + // reconciled: the field has the type of the bounds (as the statistics of a column + // have unless it is read as another type than it is stored as), the call is on the + // field itself and a small value set has the field's type. + auto i64 = field_ref("i64"); + auto int64_10 = literal(10); + auto int32_range = and_(greater_equal(i64, literal(1)), less_equal(i64, literal(5))); + auto is_in_int64_set = + call("is_in", {i32}, SetLookupOptions{ArrayFromJSON(int64(), "[10]")}); + // ... int32 bounds on an int64 field + Simplify{greater(i64, int64_10)} + .WithGuarantee(or_(int32_range, is_null(i64))) + .ExpectUnchanged(); + // ... an int32 field which is cast to compare it + Simplify{greater(cast(i32, int64()), int64_10)} + .WithGuarantee(nullable_range) + .ExpectUnchanged(); + // ... an int64 value set for an int32 field + Simplify{is_in_int64_set}.WithGuarantee(nullable_range).ExpectUnchanged(); + // A guarantee of a shape which was recognized before is used as before + Simplify{greater(i64, int64_10)} + .WithGuarantee(or_(less_equal(i64, literal(5)), is_null(i64))) + .Expect(not_(true_unless_null(i64))); +} + TEST(Expression, SimplifyIsIn) { auto is_in = [](Expression field, std::shared_ptr value_set_type, std::string json_array, @@ -2063,8 +2181,8 @@ TEST(Expression, SimplifyIsIn) { .WithGuarantee(greater(field_ref("u32"), literal(10))) .Expect(is_in(field_ref("u32"), int64(), make_range_json(11, 40), null_matching)); - // For large ranges we don't do any simplification, see - // `kIsInSimplificationMaxValueSet` in expression.cc. + // Large value sets (longer than `kIsInSimplificationMaxValueSet`) are never + // narrowed, only simplified to false. See SimplifyIsInLargeValueSet. Simplify{is_in(field_ref("u32"), int64(), make_range_json(1, 100), null_matching)} .WithGuarantee(greater(field_ref("u32"), literal(3))) .ExpectUnchanged(); @@ -2151,6 +2269,262 @@ TEST(Expression, SimplifyIsIn) { .ExpectUnchanged(); } +TEST(Expression, SimplifyIsInLargeValueSet) { + // Value sets longer than kIsInSimplificationMaxValueSet are searched for the range + // admitted by the inequalities of the guarantee. The call is simplified to false if + // the range is empty and is left unchanged otherwise. Only bounds which have the + // type of the field are used, as the statistics of a column have. + auto u32 = field_ref("u32"); + auto i32 = field_ref("i32"); + auto i64 = field_ref("i64"); + auto f64 = field_ref("f64"); + auto str = field_ref("str"); + + // 100 contiguous values, and 101 values with a gap: [1, 50] and [100, 150] + auto range = make_range_json(1, 100); + auto gapped = concat_json(make_range_json(1, 50), make_range_json(100, 150)); + auto in_gap = and_(greater_equal(i32, literal(51)), less_equal(i32, literal(99))); + // 100 to 1, each twice + std::string unsorted = "["; + for (int i = 100; i >= 1; --i) { + unsorted += std::to_string(i) + "," + std::to_string(i) + ","; + } + unsorted.back() = ']'; + // "k100" to "k159" + std::string strings = "["; + for (int i = 0; i < 60; ++i) { + strings += (i > 0 ? ",\"k" : "\"k") + std::to_string(100 + i) + "\""; + } + strings += "]"; + auto u32_literal = [](uint32_t value) { return literal(value); }; + + enum Expected { kUnchanged, kFalse }; + struct Case { + Expression arg; + std::shared_ptr value_set_type; + std::string value_set; + Expression guarantee; + Expected expected; + }; + const std::vector cases = { + // Guarantees overlapping the value set, and disjoint from its hull + {u32, int64(), range, less_equal(u32, u32_literal(1)), kUnchanged}, + {u32, int64(), range, greater(u32, u32_literal(100)), kFalse}, + {u32, int64(), range, less(u32, u32_literal(1)), kFalse}, + // Guarantees inside the value set's hull but disjoint from its values. This + // requires considering both bounds of the guarantee at once. + {i32, int32(), gapped, and_(greater(i32, literal(50)), less(i32, literal(100))), + kFalse}, + {i32, int32(), gapped, in_gap, kFalse}, + // ... but a single value of the set in the range is enough to keep the call + {i32, int32(), gapped, + and_(greater_equal(i32, literal(50)), less_equal(i32, literal(99))), kUnchanged}, + {i32, int32(), gapped, + and_(greater_equal(i32, literal(51)), less_equal(i32, literal(100))), kUnchanged}, + {i32, int32(), gapped, or_(equal(i32, literal(50)), is_null(i32)), kUnchanged}, + // Redundant bounds on the same field are all applied, a strict one winning + // over an inclusive one at the same value + {i32, int32(), gapped, + and_(and_(greater(i32, literal(10)), greater(i32, literal(50))), + less(i32, literal(100))), + kFalse}, + {i32, int32(), gapped, + and_(and_(greater_equal(i32, literal(50)), greater(i32, literal(50))), + less(i32, literal(100))), + kFalse}, + // Bounds on other fields are irrelevant, including those of a range which may + // be null for another field + {i32, int32(), gapped, or_(in_gap, is_null(u32)), kUnchanged}, + {i32, int32(), gapped, + or_(and_(in_gap, greater_equal(u32, u32_literal(51))), is_null(u32)), kUnchanged}, + // The kernel casts the value set to the field's type when the call is bound + {i64, int32(), range, greater(i64, literal(100)), kFalse}, + // Bounds of another type than the field's are ignored, the others are used + {i64, int32(), range, greater(i64, literal(100)), kUnchanged}, + {i64, int64(), range, + and_(greater_equal(i64, literal("10")), less_equal(i64, literal("9"))), + kUnchanged}, + {i64, int64(), range, + and_(greater(i64, literal(100)), less(i64, literal(200))), kFalse}, + // The call must be applied to the field itself, not to a cast of it + {cast(i32, int32()), int32(), range, greater(i32, literal(100)), kUnchanged}, + // Unsorted value sets with duplicates are sorted + {u32, int64(), unsorted, + and_(greater(u32, u32_literal(50)), less(u32, u32_literal(51))), kFalse}, + {u32, int64(), unsorted, + and_(greater_equal(u32, u32_literal(50)), less(u32, u32_literal(51))), kUnchanged}, + // Strings + {str, utf8(), strings, greater(str, literal("k159")), kFalse}, + {str, utf8(), strings, + and_(greater(str, literal("k110")), less(str, literal("k111"))), kFalse}, + {str, utf8(), strings, + and_(greater_equal(str, literal("k110")), less(str, literal("k111"))), kUnchanged}, + // Floats + {f64, float64(), range, and_(greater(f64, literal(50.0)), less(f64, literal(51.0))), + kFalse}, + {f64, float64(), range, + and_(greater(f64, literal(50.0)), less_equal(f64, literal(51.0))), kUnchanged}, + // Statistics cannot bound NaN, so a value set containing NaN is never simplified + {f64, float64(), concat_json("[NaN]", range), less(f64, literal(0.5)), kUnchanged}, + }; + // The guarantees above rule out nulls or the call is left unchanged, so the null + // matching behavior does not matter (see SimplifyIsInLargeValueSetNullMatching). + for (auto null_matching : + {SetLookupOptions::MATCH, SetLookupOptions::SKIP, SetLookupOptions::EMIT_NULL, + SetLookupOptions::INCONCLUSIVE}) { + SCOPED_TRACE(null_matching); + for (const auto& c : cases) { + SetLookupOptions options{ArrayFromJSON(c.value_set_type, c.value_set), + null_matching}; + auto expect = Simplify{call("is_in", {c.arg}, options)}.WithGuarantee(c.guarantee); + c.expected == kFalse ? expect.Expect(false) : expect.ExpectUnchanged(); + } + } + + // index_in shares the kernel state of is_in but is never simplified + Simplify{call("index_in", {i32}, SetLookupOptions{ArrayFromJSON(int32(), gapped)})} + .WithGuarantee(in_gap) + .ExpectUnchanged(); + + // Value sets longer than kIsInMaxSortedValueSet are only bounded by their smallest + // and largest value, so a guarantee inside a gap of the value set does not prune + auto chunks = ChunkedArrayFromJSON( + int32(), {make_range_json(1, 70000), make_range_json(100001, 170000)}); + auto huge = call("is_in", {i32}, SetLookupOptions{chunks}); + // Computing the bounds must not allocate a contiguous copy of the chunks. + ProxyMemoryPool pool(default_memory_pool()); + ExecContext ctx(&pool); + ASSERT_OK_AND_ASSIGN(auto bound_huge, huge.Bind(*kBoringSchema, &ctx)); + auto before = pool.total_bytes_allocated(); + ASSERT_OK_AND_ASSIGN(auto simplified, + SimplifyWithGuarantee(bound_huge, greater(i32, literal(170000)))); + EXPECT_EQ(simplified, literal(false)); + EXPECT_LT(pool.total_bytes_allocated() - before, + chunks->length() * int32()->byte_width()); + Simplify{huge} + .WithGuarantee(and_(greater(i32, literal(70000)), less(i32, literal(100001)))) + .ExpectUnchanged(); + Simplify{huge}.WithGuarantee(greater_equal(i32, literal(170000))).ExpectUnchanged(); + // ... unless they contain NaN, which the smallest and largest value ignore + auto huge_nan = call("is_in", {f64}, + SetLookupOptions{ArrayFromJSON( + float64(), concat_json("[NaN]", make_range_json(1, 140000)))}); + Simplify{huge_nan}.WithGuarantee(less(f64, literal(0.5))).ExpectUnchanged(); + + // Chunked value sets are concatenated before they are sorted + SetLookupOptions chunked_options{ + ChunkedArrayFromJSON(int32(), {make_range_json(100, 150), make_range_json(1, 50)})}; + Simplify{call("is_in", {i32}, chunked_options)}.WithGuarantee(in_gap).Expect(false); + Simplify{call("is_in", {i32}, chunked_options)} + .WithGuarantee(and_(greater_equal(i32, literal(50)), less(i32, literal(100)))) + .ExpectUnchanged(); + + // The large value set is simplified in combination with other simplifications + auto is_in_gapped = + call("is_in", {i32}, SetLookupOptions{ArrayFromJSON(int32(), gapped)}); + Simplify{and_(is_in_gapped, greater(u32, u32_literal(5)))}.WithGuarantee(in_gap).Expect( + false); + Simplify{or_(is_in_gapped, greater(u32, u32_literal(5)))}.WithGuarantee(in_gap).Expect( + greater(u32, u32_literal(5))); + // ... after them, so that a comparison is not offered what the call folds to + Simplify{greater(f64, cast(is_in_gapped, float64()))} + .WithGuarantee(and_(in_gap, greater_equal(f64, literal(1.0)))) + .Expect(greater(f64, literal(0.0))); +} + +TEST(Expression, SimplifyIsInLargeValueSetNullMatching) { + // Without possible matches the call is simplified to what it evaluates to: false, + // or null for the rows which the null matching behavior makes null. The field may + // be null if every inequality on it in the guarantee admits nulls. + auto i32 = field_ref("i32"); + auto gapped = concat_json(make_range_json(1, 50), make_range_json(100, 150)); + auto in_gap = and_(greater_equal(i32, literal(51)), less_equal(i32, literal(99))); + // The shape of Parquet statistics when a row group may contain nulls + auto nullable_in_gap = or_(in_gap, is_null(i32)); + auto nullable_above = or_(greater(i32, literal(150)), is_null(i32)); + + struct Case { + Expression guarantee; + bool nullable; + std::string rows; // rows satisfying the guarantee + }; + const std::vector cases = { + {nullable_in_gap, true, + R"([{"i32": null}, {"i32": 51}, {"i32": 75}, {"i32": 99}])"}, + {nullable_above, true, R"([{"i32": null}, {"i32": 151}])"}, + {or_(equal(i32, literal(75)), is_null(i32)), true, + R"([{"i32": null}, {"i32": 75}])"}, + {greater(i32, literal(150)), false, R"([{"i32": 151}])"}, + // Another inequality elsewhere in the guarantee rules out nulls + {and_(nullable_above, less_equal(i32, literal(200))), false, R"([{"i32": 151}])"}, + {and_(nullable_in_gap, greater(i32, literal(0))), false, R"([{"i32": 51}])"}, + }; + for (auto null_matching : + {SetLookupOptions::MATCH, SetLookupOptions::SKIP, SetLookupOptions::EMIT_NULL, + SetLookupOptions::INCONCLUSIVE}) { + for (bool value_set_has_null : {false, true}) { + for (const auto& c : cases) { + SCOPED_TRACE(null_matching); + SCOPED_TRACE(value_set_has_null); + auto value_set = value_set_has_null ? concat_json(gapped, "[null]") : gapped; + auto filter = + call("is_in", {i32}, + SetLookupOptions{ArrayFromJSON(int32(), value_set), null_matching}); + auto expect = Simplify{filter}.WithGuarantee(c.guarantee); + if (null_matching == SetLookupOptions::MATCH && value_set_has_null && + c.nullable) { + // A null in the value set matches the nulls in the data + expect.ExpectUnchanged(); + } else if (null_matching == SetLookupOptions::INCONCLUSIVE && + value_set_has_null) { + // Any non-match is null + expect.Expect(null_literal(boolean())); + } else if (c.nullable && (null_matching == SetLookupOptions::EMIT_NULL || + null_matching == SetLookupOptions::INCONCLUSIVE)) { + // Nulls in the data produce null + expect.Expect(not_(true_unless_null(i32))); + } else { + expect.Expect(false); + } + ExpectSimplifiedEvaluatesEqually(filter, c.guarantee, c.rows); + } + } + } +} + +TEST(Expression, SimplifyIsInDeprecatedSkipNulls) { + // Cover both the filter-and-rebind and the large-value-set paths, including + // options whose deprecated field overrides a conflicting modern enum. + auto i32 = field_ref("i32"); + auto rows = R"([{"i32": null}, {"i32": 1001}, {"i32": 1002}])"; + for (int num_values : {10, 100}) { + for (auto behavior : {SetLookupOptions::MATCH, SetLookupOptions::SKIP, + SetLookupOptions::EMIT_NULL, SetLookupOptions::INCONCLUSIVE}) { + for (bool skip_nulls : {false, true}) { + SCOPED_TRACE(num_values); + SCOPED_TRACE(behavior); + SCOPED_TRACE(skip_nulls); + SetLookupOptions options{ + ArrayFromJSON(int32(), concat_json(make_range_json(1, num_values), "[null]")), + behavior}; + options.skip_nulls = skip_nulls; + auto filter = call("is_in", {i32}, options); + for (int bound : {3, 1000}) { + ExpectSimplifiedEvaluatesEqually( + filter, or_(greater(i32, literal(bound)), is_null(i32)), rows); + } + // Without skip_nulls the null in the value set matches the null row + ASSERT_OK_AND_ASSIGN(auto bound_filter, filter.Bind(*kBoringSchema)); + ASSERT_OK_AND_ASSIGN( + auto simplified, + SimplifyWithGuarantee(bound_filter, + or_(greater(i32, literal(1000)), is_null(i32)))); + EXPECT_EQ(simplified.IsSatisfiable(), !skip_nulls); + } + } + } +} + TEST(Expression, SimplifyThenExecute) { auto filter = or_({equal(field_ref("f32"), literal(0)), diff --git a/cpp/src/arrow/compute/kernels/scalar_set_lookup.cc b/cpp/src/arrow/compute/kernels/scalar_set_lookup.cc index c6a3562233c0..b7dcb4f3ead5 100644 --- a/cpp/src/arrow/compute/kernels/scalar_set_lookup.cc +++ b/cpp/src/arrow/compute/kernels/scalar_set_lookup.cc @@ -15,15 +15,24 @@ // specific language governing permissions and limitations // under the License. +#include +#include + #include "arrow/array/array_base.h" +#include "arrow/array/builder_base.h" +#include "arrow/array/concatenate.h" +#include "arrow/compute/api_aggregate.h" #include "arrow/compute/api_scalar.h" +#include "arrow/compute/api_vector.h" #include "arrow/compute/cast.h" #include "arrow/compute/kernels/common_internal.h" #include "arrow/compute/kernels/util_internal.h" +#include "arrow/compute/range_evaluation_internal.h" #include "arrow/compute/registry_internal.h" #include "arrow/type.h" #include "arrow/util/bit_util.h" #include "arrow/util/bitmap_writer.h" +#include "arrow/util/float16.h" #include "arrow/util/hashing.h" #include "arrow/util/logging_internal.h" #include "arrow/visit_data_inline.h" @@ -36,9 +45,225 @@ using internal::HashTraits; namespace compute::internal { namespace { -// This base class enables non-templated access to the value set type -struct SetLookupStateBase : public KernelState { +/// Value sets up to this length are sorted for RangeEvaluator::EvaluateOverRange. +/// Longer ones are only bounded by their smallest and largest value. +/// +/// The sort is paid once, before it is known whether it rules anything out, so it +/// has to stay cheap next to what a range can save, which is reading a row group. +/// At this length it costs about as much as reading one row group of a million +/// integers; at ten times the length, about as much as reading ten. The smallest +/// and largest value are cheap to find at any length and rule out as much when the +/// values are clustered, but nothing which lies between them. +constexpr int64_t kIsInMaxSortedValueSet = 1 << 17; + +/// Whether a (valid) scalar of a floating point type is NaN. +bool IsNaNScalar(const Scalar& value) { + switch (value.type->id()) { + case Type::FLOAT: + return std::isnan(checked_cast(value).value); + case Type::DOUBLE: + return std::isnan(checked_cast(value).value); + case Type::HALF_FLOAT: + return util::Float16::FromBits(checked_cast(value).value) + .is_nan(); + default: + return false; + } +} + +/// Whether value sets of this type are sorted by `sort_indices` and searched by +/// `search_sorted` in the order of the comparison kernels. +bool IsSearchableValueSetType(const DataType& type) { + const auto id = type.id(); + return is_integer(id) || id == Type::FLOAT || id == Type::DOUBLE || is_temporal(id) || + id == Type::DURATION || is_base_binary_like(id); +} + +/// Common state for the is_in and index_in kernels. +/// +/// For `is_in` it also tells what the call evaluates to when the argument is known +/// to lie in a range (RangeEvaluator), which lets expression simplification prune +/// filters against statistics: if no value of the value set lies in the range, the +/// call evaluates to false, or to null for the rows which the null matching behavior +/// makes null. +struct SetLookupStateBase : public KernelState, public RangeEvaluator { std::shared_ptr value_set_type; + + SetLookupOptions::NullMatchingBehavior null_matching_behavior; + + /// Whether the value set contains a null (regardless of null matching behavior). + bool value_set_has_null = false; + + /// Keep the value set (as matched against the argument) for EvaluateOverRange(). + /// + /// Called once by the `is_in` kernel init, before the state is shared. Nothing + /// is computed until the first call of EvaluateOverRange(), so that only + /// expressions which are simplified against guarantees pay for it. + void SetSearchableValueSet(Datum value_set, MemoryPool* pool) { + searchable_value_set_ = std::move(value_set); + searchable_value_set_pool_ = pool; + } + + Result EvaluateOverRange(const ArgumentRange& range) const override { + if (!range.lower && !range.upper) return RangeEvaluation::kUnknown; + for (const auto* bound : {&range.lower, &range.upper}) { + if (*bound == nullptr) continue; + // If the value set could not be cast to the argument type when the call was + // bound, the kernel casts the argument instead and the argument's order (which + // the range refers to) need not be the value set's order. + if (!(*bound)->type->Equals(*value_set_type)) return RangeEvaluation::kUnknown; + // A range cannot be bounded by null or NaN, but be defensive. + if (!(*bound)->is_valid || IsNaNScalar(**bound)) return RangeEvaluation::kUnknown; + } + + // Only now materialize the (lazily sorted) value set. + const SortedValueSet& sorted_value_set = GetSortedValueSet(); + const std::shared_ptr& sorted = sorted_value_set.values; + if (!sorted) return RangeEvaluation::kUnknown; + + // Indices into `sorted` delimiting the values in the range: O(log N) per bound. + // A failed search only costs the simplification. + ExecContext exec_context(searchable_value_set_pool_); + auto search = [&](const std::shared_ptr& bound, + SearchSortedOptions::Side side) -> std::optional { + auto index = SearchSorted(sorted, bound, SearchSortedOptions(side), &exec_context); + if (!index.ok() || !index->scalar()->is_valid) return std::nullopt; + return static_cast(index->scalar_as().value); + }; + int64_t begin = 0; + int64_t end = sorted->length(); + if (range.lower) { + // The first value not less than (or, for an exclusive bound, greater than) it + auto index = + search(range.lower, range.lower_inclusive ? SearchSortedOptions::Left + : SearchSortedOptions::Right); + if (!index) return RangeEvaluation::kUnknown; + begin = *index; + } + if (range.upper) { + auto index = search(range.upper, range.upper_inclusive ? SearchSortedOptions::Right + : SearchSortedOptions::Left); + if (!index) return RangeEvaluation::kUnknown; + end = *index; + } + + // Of a value set known only by its smallest and largest value, a member may lie + // in the range unless the whole range is below or above both. + const bool may_match = + sorted_value_set.exact ? begin < end : begin < sorted->length() && end > 0; + if (may_match) return RangeEvaluation::kUnknown; + + // No (non-null) member of the value set can occur in the data. + switch (null_matching_behavior) { + case SetLookupOptions::MATCH: + // A null in the value set matches nulls in the data. + if (range.nullable && value_set_has_null) return RangeEvaluation::kUnknown; + return RangeEvaluation::kFalse; + case SetLookupOptions::SKIP: + // Nulls in the data produce false. + return RangeEvaluation::kFalse; + case SetLookupOptions::INCONCLUSIVE: + // Any non-match produces null if the value set contains a null, as do + // nulls in the data. + if (value_set_has_null) return RangeEvaluation::kNull; + [[fallthrough]]; + case SetLookupOptions::EMIT_NULL: + // Nulls in the data produce null. + return range.nullable ? RangeEvaluation::kFalseUnlessNull + : RangeEvaluation::kFalse; + } + return RangeEvaluation::kUnknown; + } + + private: + struct SortedValueSet { + /// Ascending values without nulls, or nullptr if the value set cannot be searched + std::shared_ptr values; + /// Whether `values` holds every value of the value set, or only its smallest and + /// largest value (for value sets longer than kIsInMaxSortedValueSet) + bool exact = true; + }; + + /// The value set sorted for searching with `search_sorted`. + /// + /// This is only available for `is_in` when the value set has a type for which + /// IsSearchableValueSetType() holds and contains no NaN. It is computed at most + /// once; concurrent calls are safe. + const SortedValueSet& GetSortedValueSet() const { + std::call_once(sorted_value_set_once_, [this] { + if (searchable_value_set_.is_arraylike()) { + // Best effort: a failure only disables the simplification of `is_in` + // against guarantees. + ExecContext exec_context(searchable_value_set_pool_); + sorted_value_set_ = + SortValueSet(searchable_value_set_, &exec_context).ValueOr(SortedValueSet{}); + searchable_value_set_ = Datum(); + } + }); + return sorted_value_set_; + } + + static Result SortValueSet(const Datum& value_set, ExecContext* ctx) { + if (value_set.length() > kIsInMaxSortedValueSet) { + ARROW_ASSIGN_OR_RAISE(auto bounds, ValueSetBounds(value_set, ctx)); + return SortedValueSet{std::move(bounds), /*exact=*/false}; + } + std::shared_ptr values; + if (value_set.is_array()) { + values = value_set.make_array(); + } else { + ARROW_ASSIGN_OR_RAISE( + values, Concatenate(value_set.chunked_array()->chunks(), ctx->memory_pool())); + } + ARROW_ASSIGN_OR_RAISE(auto sorted, SortValues(values, ctx)); + return SortedValueSet{std::move(sorted), /*exact=*/true}; + } + + /// \return `values` sorted with nulls removed, or nullptr if it contains NaN + static Result> SortValues(const std::shared_ptr& values, + ExecContext* ctx) { + ARROW_ASSIGN_OR_RAISE( + auto indices, + SortIndices(*values, ArraySortOptions(SortOrder::Ascending, NullPlacement::AtEnd), + ctx)); + ARROW_ASSIGN_OR_RAISE(Datum taken, + Take(values, indices, TakeOptions::NoBoundsCheck(), ctx)); + auto sorted = taken.make_array(); + sorted = sorted->Slice(0, sorted->length() - sorted->null_count()); + // NaN sorts after every other value and before nulls. Statistics based + // guarantees cannot bound NaN, so such a value set is not searchable. + if (sorted->length() > 0) { + ARROW_ASSIGN_OR_RAISE(auto last, sorted->GetScalar(sorted->length() - 1)); + if (IsNaNScalar(*last)) return nullptr; + } + return sorted; + } + + /// \return the smallest and largest value of `values`, or nullptr if it contains NaN + /// or only nulls + static Result> ValueSetBounds(const Datum& values, + ExecContext* ctx) { + if (is_floating(values.type()->id())) { + ARROW_ASSIGN_OR_RAISE(Datum is_nan, IsNan(values, ctx)); + ARROW_ASSIGN_OR_RAISE(Datum any_nan, + Any(is_nan, ScalarAggregateOptions::Defaults(), ctx)); + if (any_nan.scalar_as().value) return nullptr; + } + ARROW_ASSIGN_OR_RAISE(Datum min_max, + MinMax(values, ScalarAggregateOptions::Defaults(), ctx)); + const auto& bounds = min_max.scalar_as().value; + if (!bounds[0]->is_valid) return nullptr; + ARROW_ASSIGN_OR_RAISE(auto builder, MakeBuilder(values.type(), ctx->memory_pool())); + RETURN_NOT_OK(builder->AppendScalar(*bounds[0])); + RETURN_NOT_OK(builder->AppendScalar(*bounds[1])); + return builder->Finish(); + } + + mutable std::once_flag sorted_value_set_once_; + // Released once sorted_value_set_ is computed. + mutable Datum searchable_value_set_; + MemoryPool* searchable_value_set_pool_ = default_memory_pool(); + mutable SortedValueSet sorted_value_set_; }; template @@ -69,6 +294,7 @@ struct SetLookupState : public SetLookupStateBase { } else { return Status::Invalid("value_set should be an array or chunked array"); } + value_set_has_null = lookup_table->GetNull() >= 0; if (this->null_matching_behavior != SetLookupOptions::SKIP && lookup_table->GetNull() >= 0) { null_index = memo_index_to_value_index[lookup_table->GetNull()]; @@ -121,7 +347,6 @@ struct SetLookupState : public SetLookupStateBase { // be mapped back to indices in the value_set. std::vector memo_index_to_value_index; int32_t null_index = -1; - SetLookupOptions::NullMatchingBehavior null_matching_behavior; }; template <> @@ -130,14 +355,11 @@ struct SetLookupState : public SetLookupStateBase { Status Init(SetLookupOptions& options) { null_matching_behavior = options.GetNullMatchingBehavior(); - value_set_has_null = (options.value_set.length() > 0) && - this->null_matching_behavior != SetLookupOptions::SKIP; + // The kernels consult null_matching_behavior before using this. + value_set_has_null = options.value_set.length() > 0; value_set_type = null(); return Status::OK(); } - - bool value_set_has_null; - SetLookupOptions::NullMatchingBehavior null_matching_behavior; }; // TODO: Put this concept somewhere reusable @@ -171,10 +393,15 @@ struct InitStateVisitor { TypeHolder arg_type; std::unique_ptr result; - InitStateVisitor(KernelContext* ctx, const KernelInitArgs& args) + // Whether to keep the value set in the state so that the kernel can be evaluated + // over a range, see SetLookupStateBase::EvaluateOverRange (is_in only). + bool keep_value_set; + + InitStateVisitor(KernelContext* ctx, const KernelInitArgs& args, bool keep_value_set) : ctx(ctx), options(*checked_cast(args.options)), - arg_type(args.inputs[0]) {} + arg_type(args.inputs[0]), + keep_value_set(keep_value_set) {} template Status Init() { @@ -254,18 +481,34 @@ struct InitStateVisitor { } RETURN_NOT_OK(VisitTypeInline(*options.value_set.type(), this)); + + if (keep_value_set && IsSearchableValueSetType(*options.value_set.type())) { + checked_cast(result.get()) + ->SetSearchableValueSet(options.value_set, ctx->exec_context()->memory_pool()); + } return std::move(result); } }; Result> InitSetLookup(KernelContext* ctx, - const KernelInitArgs& args) { + const KernelInitArgs& args, + bool keep_value_set) { if (args.options == nullptr) { return Status::Invalid( "Attempted to call a set lookup function without SetLookupOptions"); } - return InitStateVisitor{ctx, args}.GetResult(); + return InitStateVisitor{ctx, args, keep_value_set}.GetResult(); +} + +Result> InitIsIn(KernelContext* ctx, + const KernelInitArgs& args) { + return InitSetLookup(ctx, args, /*keep_value_set=*/true); +} + +Result> InitIndexIn(KernelContext* ctx, + const KernelInitArgs& args) { + return InitSetLookup(ctx, args, /*keep_value_set=*/false); } struct IndexInVisitor { @@ -648,7 +891,7 @@ void RegisterScalarSetLookup(FunctionRegistry* registry) { // IsIn writes its boolean output into preallocated memory { ScalarKernel isin_base; - isin_base.init = InitSetLookup; + isin_base.init = InitIsIn; isin_base.exec = ExecIsIn; isin_base.null_handling = NullHandling::COMPUTED_PREALLOCATE; auto is_in = std::make_shared("is_in", Arity::Unary(), is_in_doc); @@ -665,7 +908,7 @@ void RegisterScalarSetLookup(FunctionRegistry* registry) { // IndexIn writes its int32 output into preallocated memory { ScalarKernel index_in_base; - index_in_base.init = InitSetLookup; + index_in_base.init = InitIndexIn; index_in_base.exec = ExecIndexIn; index_in_base.null_handling = NullHandling::COMPUTED_PREALLOCATE; auto index_in = diff --git a/cpp/src/arrow/compute/range_evaluation_internal.h b/cpp/src/arrow/compute/range_evaluation_internal.h new file mode 100644 index 000000000000..913f11eae0ce --- /dev/null +++ b/cpp/src/arrow/compute/range_evaluation_internal.h @@ -0,0 +1,70 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#pragma once + +#include + +#include "arrow/result.h" +#include "arrow/type_fwd.h" +#include "arrow/util/visibility.h" + +namespace arrow::compute::internal { + +/// \brief The values the argument of a unary call is known to take, for example +/// from the statistics of a Parquet row group. +struct ArgumentRange { + /// Bounds on the argument's non-null, non-NaN values, in the argument's type. + /// A null pointer leaves that side unbounded. Floating arguments may also be NaN. + std::shared_ptr lower, upper; + /// Whether the argument may be equal to the respective bound + bool lower_inclusive = true, upper_inclusive = true; + /// Whether the argument may be null, in addition to the values in the range + bool nullable = true; +}; + +/// \brief What a call evaluates to for every argument in an ArgumentRange +enum class RangeEvaluation { + /// Nothing is known: the call must be evaluated + kUnknown, + /// false for every argument, null or not + kFalse, + /// null for every argument, null or not + kNull, + /// false for arguments which are not null and null for those which are + kFalseUnlessNull, +}; + +/// \brief Interface for kernel states which can tell what their (unary) kernel +/// evaluates to when its argument lies in a range. +/// +/// A KernelState may implement this in addition to KernelState. Expression +/// simplification (SimplifyWithGuarantee) discovers it with dynamic_cast and +/// replaces calls for which the result is known, which is how filters are +/// pruned against statistics without the expression machinery knowing the +/// semantics of the function. +/// +/// Implementations must be safe to call concurrently: bound expressions and +/// their kernel states are shared between threads. +class ARROW_EXPORT RangeEvaluator { + public: + virtual ~RangeEvaluator() = default; + + virtual Result EvaluateOverRange(const ArgumentRange& range) const = 0; +}; + +} // namespace arrow::compute::internal diff --git a/cpp/src/arrow/dataset/file_parquet_test.cc b/cpp/src/arrow/dataset/file_parquet_test.cc index 0d86f5eaa0d3..e0e99ccfe45a 100644 --- a/cpp/src/arrow/dataset/file_parquet_test.cc +++ b/cpp/src/arrow/dataset/file_parquet_test.cc @@ -18,12 +18,15 @@ #include "arrow/dataset/file_parquet.h" #include +#include #include +#include #include #include #include #include "arrow/compute/api_scalar.h" +#include "arrow/compute/cast.h" #include "arrow/dataset/dataset_internal.h" #include "arrow/dataset/parquet_encryption_config.h" #include "arrow/dataset/scanner.h" @@ -34,6 +37,7 @@ #include "arrow/io/util_internal.h" #include "arrow/record_batch.h" #include "arrow/table.h" +#include "arrow/testing/builder.h" #include "arrow/testing/gtest_util.h" #include "arrow/testing/util.h" #include "arrow/type.h" @@ -575,6 +579,21 @@ class TestParquetFileFormatScan : public FileFormatScanMixinnum_rows(), expected + 1); } } + + // Check which row groups of `fragment` a scan with `filter` reads + void ExpectRowGroupsRead(const std::shared_ptr& fragment, + compute::Expression filter, + const std::vector& expected_row_groups) { + SetFilter(std::move(filter)); + auto parquet_fragment = checked_pointer_cast(fragment); + ASSERT_OK_AND_ASSIGN(auto fragments, parquet_fragment->SplitByRowGroup(opts_->filter)) + std::vector row_groups; + for (const auto& split : fragments) { + const auto& read = checked_pointer_cast(split)->row_groups(); + row_groups.insert(row_groups.end(), read.begin(), read.end()); + } + EXPECT_EQ(row_groups, expected_row_groups); + } }; TEST_P(TestParquetFileFormatScan, ScanRecordBatchReader) { TestScan(); } @@ -747,6 +766,139 @@ TEST_P(TestParquetFileFormatScan, PredicatePushdownRowGroupFragments) { equal(field_ref(FieldRef("struct", "str")), literal("2"))); } +TEST_P(TestParquetFileFormatScan, PredicatePushdownRowGroupFragmentsLargeIsIn) { + // Row group i holds i64 == 10 * i, ..., 10 * i + 9 and a null, so that its + // statistics yield (i64 >= min and i64 <= max) or is_null(i64). + constexpr int kNumRowGroups = 8; + auto dataset_schema = schema({field("i64", int64())}); + std::vector> batches; + for (int i = 0; i < kNumRowGroups; ++i) { + std::vector values(11); + std::iota(values.begin(), values.end(), 10 * i); + std::vector valid(values.size(), true); + valid.back() = false; + std::shared_ptr array; + ArrayFromVector(valid, values, &array); + batches.push_back(RecordBatch::Make(dataset_schema, array->length(), {array})); + } + ASSERT_OK_AND_ASSIGN(auto reader, + RecordBatchReader::Make(std::move(batches), dataset_schema)); + auto source = GetFileSource(reader.get()); + SetSchema(dataset_schema->fields()); + ASSERT_OK_AND_ASSIGN(auto fragment, format_->MakeFragment(*source)); + + auto expect_row_groups = [&](compute::Expression filter, + const std::vector& expected_row_groups) { + ExpectRowGroupsRead(fragment, std::move(filter), expected_row_groups); + }; + using compute::SetLookupOptions; + auto make_set = [](std::vector values, bool with_null = false) { + std::vector valid(values.size(), true); + if (with_null) { + values.push_back(0); + valid.push_back(false); + } + std::shared_ptr array; + ArrayFromVector(valid, values, &array); + return array; + }; + auto is_in = [](std::shared_ptr set, + SetLookupOptions::NullMatchingBehavior behavior = + SetLookupOptions::MATCH) { + return call("is_in", {field_ref("i64")}, SetLookupOptions{std::move(set), behavior}); + }; + + // A value set longer than the exact simplification threshold whose hull [0, 79] + // spans every row group but which contains no value in [20, 39]. Only the row + // groups holding a value of the set are read. + std::vector values(60); + std::iota(values.begin(), values.begin() + 20, 0); + std::iota(values.begin() + 20, values.end(), 40); + const std::vector matching = {0, 1, 4, 5, 6, 7}; + const auto all = ::arrow::internal::Iota(kNumRowGroups); + expect_row_groups(is_in(make_set(values)), matching); + + // Values beyond the data select nothing. + std::vector beyond(100); + std::iota(beyond.begin(), beyond.end(), 1000); + expect_row_groups(is_in(make_set(beyond)), {}); + + // Values cast from a narrower type are handled too. + ASSERT_OK_AND_ASSIGN(auto int32_set, compute::Cast(*make_set(values), int32())); + expect_row_groups(is_in(int32_set), matching); + + // Combined with other predicates. + expect_row_groups( + and_(is_in(make_set(values)), less(field_ref("i64"), literal(50))), + {0, 1, 4}); + + // Only a null in a MATCH value set matches the nulls in every row group; nulls + // which produce null rather than false are pruned too. + for (auto behavior : {SetLookupOptions::MATCH, SetLookupOptions::SKIP, + SetLookupOptions::EMIT_NULL, SetLookupOptions::INCONCLUSIVE}) { + SCOPED_TRACE(behavior); + expect_row_groups(is_in(make_set(values), behavior), matching); + expect_row_groups(is_in(make_set(values, /*with_null=*/true), behavior), + behavior == SetLookupOptions::MATCH ? all : matching); + } + + // Comparisons and small value sets are pruned against the same statistics. + expect_row_groups(greater(field_ref("i64"), literal(100)), {}); + expect_row_groups(less(field_ref("i64"), literal(10)), {0}); + expect_row_groups(is_null(field_ref("i64")), all); + auto small_set = ArrayFromJSON(int64(), "[5, 25]"); + expect_row_groups(is_in(small_set), {0, 2}); + expect_row_groups(and_(is_in(small_set), is_valid(field_ref("i64"))), {0, 2}); +} + +TEST_P(TestParquetFileFormatScan, PredicatePushdownNullableFloatRangeWithNaN) { + SetSchema({field("x", float64())}); + auto batch = RecordBatchFromJSON(opts_->dataset_schema, "[[1], [2], [NaN], [null]]"); + ASSERT_OK_AND_ASSIGN(auto reader, RecordBatchReader::Make({batch})); + ASSERT_OK_AND_ASSIGN(auto fragment, + format_->MakeFragment(*GetFileSource(reader.get()))); + + // The statistics ignore NaN, so the range only decides a comparison the way it + // evaluates for NaN. No row is greater than 100, NaN included. + auto x = field_ref("x"); + ExpectRowGroupsRead(fragment, greater(x, literal(100.0)), {}); + // The NaN row matches the first filter; every non-null row matches the second. + ExpectRowGroupsRead(fragment, not_(greater(x, literal(0.0))), {0}); + auto nan = literal(std::numeric_limits::quiet_NaN()); + ExpectRowGroupsRead(fragment, not_(less(x, nan)), {0}); +} + +TEST_P(TestParquetFileFormatScan, PredicatePushdownColumnReadAsAnotherType) { + // The statistics of a row group are in the type the column is stored as, while the + // filter is bound to the dataset schema, which may read the column as another type. + // The statistics of row groups with nulls are only used where the two types are + // the same, so that these row groups are read as they were before. + auto expect_row_groups = [&](const std::shared_ptr& stored, + const std::shared_ptr& read_as, + compute::Expression filter, + const std::vector& expected_row_groups) { + auto physical_schema = schema({field("x", stored->type())}); + ASSERT_OK_AND_ASSIGN( + auto reader, RecordBatchReader::Make( + {RecordBatch::Make(physical_schema, stored->length(), {stored})}, + physical_schema)); + auto source = GetFileSource(reader.get()); + SetSchema({field("x", read_as)}); + ASSERT_OK_AND_ASSIGN(auto fragment, format_->MakeFragment(*source)); + ExpectRowGroupsRead(fragment, std::move(filter), expected_row_groups); + }; + auto x = field_ref("x"); + + // int32 read as int64, with a null + expect_row_groups(ArrayFromJSON(int32(), "[1, 2, 3, null]"), int64(), + greater(x, literal(100)), {0}); + + // string read as int64: the statistics order the strings ("10" <= x <= "9") and + // say nothing about the integers they are read as + expect_row_groups(ArrayFromJSON(utf8(), R"(["10", "5", "9", "100", null])"), int64(), + equal(x, literal(5)), {0}); +} + TEST_P(TestParquetFileFormatScan, ExplicitRowGroupSelection) { constexpr int64_t kNumRowGroups = 16; constexpr int64_t kTotalNumRows = kNumRowGroups * (kNumRowGroups + 1) / 2;