Skip to content
Closed
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
14 changes: 13 additions & 1 deletion cpp/src/arrow/compute/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,19 @@ struct Inequality {

std::string func_name = Comparison::GetName(guarantee.cmp);
DCHECK_NE(func_name, "na");
std::vector<Datum> args{options->value_set, guarantee.bound};
// Set lookup first casts the value set to the input type. Comparing the
// original values to the bound can discard matches (e.g. double to float).
const auto* input_type = is_in_call->arguments[0].type();
Datum comparable = options->value_set;
if (!comparable.type()->Equals(*input_type)) {
auto casted = Cast(comparable, input_type);
if (!casted.ok()) {
// Set lookup can cast the input to the set type instead.
return std::nullopt;
}
comparable = *std::move(casted);
}
std::vector<Datum> args{std::move(comparable), guarantee.bound};
ARROW_ASSIGN_OR_RAISE(Datum filter_mask, CallFunction(func_name, args));
FilterOptions filter_options(null_selection);
ARROW_ASSIGN_OR_RAISE(Datum simplified_value_set,
Expand Down
22 changes: 22 additions & 0 deletions cpp/src/arrow/compute/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,28 @@ TEST(Expression, SimplifyThenExecute) {
AssertDatumsEqual(evaluated, simplified_evaluated, /*verbose=*/true);
}

TEST(Expression, SimplifyIsInWithDifferentValueSetType) {
auto is_in = [](std::shared_ptr<DataType> set_type, std::string values) {
return call("is_in", {field_ref("f32")},
SetLookupOptions{ArrayFromJSON(set_type, values)});
};
auto lower_bound = greater_equal(field_ref("f32"), literal(0.1f));

// Set lookup rounds the double and parses the string to the float column type.
Simplify{is_in(float64(), "[0.0, 0.1]")}
.WithGuarantee(lower_bound)
.Expect(is_in(float64(), "[0.1]"));
Simplify{is_in(utf8(), R"(["0.0", "0.1"])")}
.WithGuarantee(lower_bound)
.Expect(is_in(utf8(), R"(["0.1"])"));

// This integer cannot be safely cast to float32. Set lookup casts the input
// to the set type instead.
Simplify{is_in(int64(), "[16777217]")}
.WithGuarantee(greater_equal(field_ref("f32"), literal(16777216.0f)))
.ExpectUnchanged();
}

TEST(Expression, SimplifyIsInThenExecute) {
auto input = RecordBatchFromJSON(kBoringSchema, R"([
{"i64": 2, "i32": 5},
Expand Down