Skip to content
Open
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
31 changes: 27 additions & 4 deletions cpp/src/arrow/compute/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -959,15 +959,34 @@ std::optional<std::pair<FieldRef, Datum>> ExtractOneFieldValue(
return std::nullopt;
}

bool IsFloatingPointZero(const Datum& value) {
if (!value.is_scalar() || !value.scalar()->is_valid) return false;

switch (value.type()->id()) {
case Type::HALF_FLOAT:
return util::Float16::FromBits(value.scalar_as<HalfFloatScalar>().value).is_zero();
case Type::FLOAT:
return value.scalar_as<FloatScalar>().value == 0.0F;
case Type::DOUBLE:
return value.scalar_as<DoubleScalar>().value == 0.0;
default:
return false;
}
}

// Conjunction members which are represented in known_values are erased from
// conjunction_members
Status ExtractKnownFieldValues(std::vector<Expression>* conjunction_members,
KnownFieldValues* known_values) {
KnownFieldValues* known_values,
bool allow_floating_point_zero) {
// filter out consumed conjunction members, leaving only unconsumed
*conjunction_members = arrow::internal::FilterVector(
std::move(*conjunction_members),
[known_values](const Expression& guarantee) -> bool {
[known_values, allow_floating_point_zero](const Expression& guarantee) -> bool {
if (auto known_value = ExtractOneFieldValue(guarantee)) {
if (!allow_floating_point_zero && IsFloatingPointZero(known_value->second)) {
return true;
}
known_values->map.insert(std::move(*known_value));
return false;
}
Expand All @@ -983,7 +1002,8 @@ Result<KnownFieldValues> ExtractKnownFieldValues(
const Expression& guaranteed_true_predicate) {
KnownFieldValues known_values;
auto conjunction_members = GuaranteeConjunctionMembers(guaranteed_true_predicate);
RETURN_NOT_OK(ExtractKnownFieldValues(&conjunction_members, &known_values));
RETURN_NOT_OK(ExtractKnownFieldValues(&conjunction_members, &known_values,
/*allow_floating_point_zero=*/true));
return known_values;
}

Expand Down Expand Up @@ -1443,7 +1463,10 @@ Result<Expression> SimplifyWithGuarantee(Expression expr,
KnownFieldValues known_values;
auto conjunction_members = GuaranteeConjunctionMembers(guaranteed_true_predicate);

RETURN_NOT_OK(ExtractKnownFieldValues(&conjunction_members, &known_values));
// Equality with a floating-point zero constrains comparisons but cannot establish
// which sign to substitute into arithmetic or casts.
RETURN_NOT_OK(ExtractKnownFieldValues(&conjunction_members, &known_values,
/*allow_floating_point_zero=*/false));

ARROW_ASSIGN_OR_RAISE(expr,
ReplaceFieldsWithKnownValues(known_values, std::move(expr)));
Expand Down
29 changes: 29 additions & 0 deletions cpp/src/arrow/compute/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,35 @@ struct Simplify {
Expectable WithGuarantee(Expression guarantee) { return {expr, guarantee}; }
};

TEST(Expression, FloatingPointZeroGuarantees) {
auto x = field_ref("f32");
auto reciprocal = call("divide", {literal(1.0F), x});
auto nonzero_values = ArrayFromJSON(float32(), "[1, 2]");
ASSERT_OK(nonzero_values->ValidateFull());
auto in_nonzero = call("is_in", {x}, SetLookupOptions{nonzero_values});

for (float zero : {-0.0F, 0.0F}) {
auto guarantee = equal(x, literal(zero));
Simplify{greater(reciprocal, literal(0.0F))}
.WithGuarantee(guarantee)
.ExpectUnchanged();
Simplify{equal(cast(x, utf8()), literal("0"))}
.WithGuarantee(guarantee)
.ExpectUnchanged();
Simplify{equal(x, literal(0.0F))}.WithGuarantee(guarantee).Expect(true);
Simplify{not_equal(x, literal(0.0F))}.WithGuarantee(guarantee).Expect(false);
Simplify{greater(x, literal(0.0F))}.WithGuarantee(guarantee).Expect(false);
Simplify{is_null(x)}.WithGuarantee(guarantee).Expect(false);
Simplify{in_nonzero}.WithGuarantee(guarantee).Expect(false);

ASSERT_OK_AND_ASSIGN(auto known, ExtractKnownFieldValues(guarantee));
EXPECT_THAT(
known.map,
UnorderedElementsAreArray(
std::unordered_map<FieldRef, Datum, FieldRef::Hash>{{"f32", Datum(zero)}}));
}
}

TEST(Expression, SingleComparisonGuarantees) {
auto i32 = field_ref("i32");

Expand Down
64 changes: 64 additions & 0 deletions cpp/src/arrow/dataset/file_parquet_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,44 @@ TEST_P(TestParquetFileFormatScan, ExplicitRowGroupSelection) {
scan_status);
}

TEST_P(TestParquetFileFormatScan, PredicatePushdownPreservesSignedZero) {
auto table = TableFromJSON(schema({field("x", float64())}), {R"([{"x": 0.0}])"});
ASSERT_OK(table->ValidateFull());
TableBatchReader reader(*table);
auto source = GetFileSource(&reader);

SetSchema(reader.schema()->fields());
ASSERT_OK_AND_ASSIGN(auto fragment, format_->MakeFragment(*source));

auto x = field_ref("x");
auto reciprocal = compute::call("divide", {literal(1.0), x});
CountRowGroupsInFragment(fragment, {0}, greater(reciprocal, literal(0.0)));

SetFilter(less(reciprocal, literal(0.0)));
ASSERT_FINISHES_OK_AND_EQ(std::nullopt, fragment->CountRows(opts_->filter, opts_));
ASSERT_OK_AND_ASSIGN(auto scanner,
ScannerBuilder(reader.schema(), fragment, opts_).Finish());
ASSERT_OK_AND_EQ(0, scanner->CountRows());

SetFilter(equal(x, literal(0.0)));
ASSERT_FINISHES_OK_AND_EQ(1, fragment->CountRows(opts_->filter, opts_));
SetFilter(not_equal(x, literal(0.0)));
ASSERT_FINISHES_OK_AND_EQ(0, fragment->CountRows(opts_->filter, opts_));
}

TEST_P(TestParquetFileFormatScan, PruneNullableZeroRowGroup) {
auto table =
TableFromJSON(schema({field("x", float64())}), {R"([{"x": 0.0}, {"x": null}])"});
ASSERT_OK(table->ValidateFull());
TableBatchReader reader(*table);
auto source = GetFileSource(&reader);

SetSchema(reader.schema()->fields());
ASSERT_OK_AND_ASSIGN(auto fragment, format_->MakeFragment(*source));
SetFilter(greater(field_ref("x"), literal(1.0)));
ASSERT_FINISHES_OK_AND_EQ(0, fragment->CountRows(opts_->filter, opts_));
}
Comment on lines +810 to +846

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 To verify both correctness across zero-bound representations and that pruning/row counting don't regress, consider adding coverage for:

  • SimplifyWithGuarantee directly (in expression_test.cc) with equal(x, ±0.0): sign-sensitive expressions (divide(1.0, x) > 0, cast(x, utf8()) == "0") stay unsimplified, while comparisons (x == 0.0, x != 0.0, x > 0.0) still simplify.
  • EncodedStatistics with min = +0.0, max = +0.0 (the TYPE_ORDER compatibility case) and TryCountRows with a sign-sensitive filter (e.g., 1.0 / x < 0.0 returning 0 on [+0.0] and x == 0.0 returning 1).
  • A nullable [0.0, null] row group still pruning x > 1.0.


TEST_P(TestParquetFileFormatScan, PredicatePushdownRowGroupFragmentsUsingStringColumn) {
auto table = TableFromJSON(schema({field("x", utf8())}),
{
Expand Down Expand Up @@ -924,6 +962,32 @@ TEST(TestParquetStatistics, NullMax) {
EXPECT_EQ(stat_expression->ToString(), "(x >= 1)");
}

TEST(TestParquetStatistics, SameSignZeroBounds) {
auto field = ::arrow::field("x", float64());
auto node = parquet::schema::Double("x", parquet::Repetition::REQUIRED);
parquet::ColumnDescriptor descr(node, /*max_definition_level=*/0,
/*max_repetition_level=*/0);
auto x = field_ref("x");
auto reciprocal = compute::call("divide", {literal(1.0), x});
ASSERT_OK_AND_ASSIGN(auto filter,
greater(reciprocal, literal(0.0)).Bind(*schema({field})));

// TYPE_ORDER bounds may have the same zero sign as each other without
// establishing the sign of the values in the row group.
for (bool negative : {false, true}) {
std::string encoded_zero(sizeof(double), '\0');
if (negative) encoded_zero.back() = static_cast<char>(0x80);
parquet::EncodedStatistics encoded;
encoded.set_min(encoded_zero).set_max(encoded_zero).set_null_count(0);
auto stats = parquet::Statistics::Make(&descr, &encoded, /*num_values=*/1);
auto guarantee = ParquetFileFragment::EvaluateStatisticsAsExpression(*field, *stats);
ASSERT_TRUE(guarantee.has_value());
ASSERT_OK_AND_ASSIGN(auto simplified,
compute::SimplifyWithGuarantee(filter, *guarantee));
EXPECT_EQ(filter, simplified);
}
}

TEST(TestParquetStatistics, NoNullCount) {
auto field = ::arrow::field("x", int32());
auto parquet_node_ptr = ::parquet::schema::Int32("x", ::parquet::Repetition::REQUIRED);
Expand Down