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
3 changes: 2 additions & 1 deletion cpp/src/arrow/compute/expression_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ struct Comparison {
auto to_id = expr.type()->id();

if (is_floating(to_id)) {
if (is_integer(from_id) || is_floating(from_id)) {
if (is_integer(from_id) ||
(is_floating(from_id) && bit_width(to_id) >= bit_width(from_id))) {
return StripOrderPreservingCasts(from);
}
return expr;
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/arrow/compute/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ TEST(ExpressionUtils, StripOrderPreservingCasts) {
// int(a) == 3, int(b) == 3, assert(!(int(a) > int(b)))
Expect(cast(field_ref("f32"), int32()), no_change);

// casting any float type to another preserves ordering
// Widening preserves ordering, but narrowing can round distinct values together.
Expect(cast(field_ref("f32"), float64()), field_ref("f32"));
Expect(cast(field_ref("f64"), float32()), field_ref("f64"));
auto narrowing_cast = cast(field_ref("f64"), float32());
Expect(narrowing_cast, no_change);
Expect(cast(narrowing_cast, float64()), narrowing_cast);

// casting signed integer to unsigned can alter ordering
Expect(cast(field_ref("i32"), uint32()), no_change);
Expand Down
26 changes: 26 additions & 0 deletions cpp/src/arrow/dataset/file_parquet_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <vector>

#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"
Expand Down Expand Up @@ -375,6 +376,31 @@ TEST_F(TestParquetFileFormat, CountRowsPredicatePushdown) {
}
}

// GH-55886
TEST_F(TestParquetFileFormat, PredicatePushdownWithNarrowingFloatCast) {
auto test_schema = schema({field("x", float64())});
auto cast_x = call("cast", {field_ref("x")}, compute::CastOptions::Safe(float32()));
auto Check = [&](const char* input, compute::Expression filter, const char* expected) {
SCOPED_TRACE(filter.ToString());
auto batch = RecordBatchFromJSON(test_schema, input);
ASSERT_OK(batch->ValidateFull());
ASSERT_OK_AND_ASSIGN(auto reader, RecordBatchReader::Make({batch}, test_schema));
auto source = GetFileSource(reader.get());
auto fragment = MakeFragment(*source);
ScannerBuilder builder(test_schema, fragment, std::make_shared<ScanOptions>());
ASSERT_OK(builder.Filter(filter));
ASSERT_OK_AND_ASSIGN(auto scanner, builder.Finish());
ASSERT_OK_AND_ASSIGN(auto actual, scanner->ToTable());
AssertTablesEqual(*TableFromJSON(test_schema, {expected}), *actual,
/*same_chunk_layout=*/false);
};

Check("[[17.799999999999997], [23.0]]",
less_equal(cast_x, literal(17.799999237060547f)), "[[17.799999999999997]]");
Check("[[1.0], [17.8000008]]", greater_equal(cast_x, literal(17.80000114440918f)),
"[[17.8000008]]");
}

TEST_F(TestParquetFileFormat, CachedMetadata) {
// Create a test file
auto mock_fs = std::make_shared<fs::internal::MockFileSystem>(fs::kNoTime);
Expand Down