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
9 changes: 7 additions & 2 deletions cpp/src/arrow/compute/expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1422,15 +1422,20 @@ Result<Expression> SimplifyIsValidGuarantee(Expression expr,
std::move(expr), [](Expression expr) { return expr; },
[&](Expression expr, ...) -> Result<Expression> {
auto call = expr.call();
if (!call) return expr;
if (!call || call->arguments.empty()) return expr;

if (call->arguments[0] != guarantee.arguments[0]) return expr;

if (call->function_name == "is_valid") return literal(true);

if (call->function_name == "true_unless_null") return literal(true);

if (call->function_name == "is_null") return literal(false);
if (call->function_name == "is_null" &&
(!call->options ||
!checked_cast<const NullOptions&>(*call->options).nan_is_null ||
!is_floating(call->arguments[0].type()->id()))) {
return literal(false);
}

return expr;
});
Expand Down
11 changes: 11 additions & 0 deletions cpp/src/arrow/compute/expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,17 @@ TEST(Expression, SimplifyWithGuarantee) {
}

TEST(Expression, SimplifyWithValidityGuarantee) {
auto nan_is_null = is_null(field_ref("f64"), /*nan_is_null=*/true);
Simplify{nan_is_null}.WithGuarantee(is_valid(field_ref("f64"))).Expect(nan_is_null);
Simplify{is_null(field_ref("i32"), /*nan_is_null=*/true)}
.WithGuarantee(is_valid(field_ref("i32")))
.Expect(literal(false));
Simplify{call("is_null", {field_ref("f64")})}
.WithGuarantee(is_valid(field_ref("f64")))
.Expect(literal(false));
auto random = call("random", {}, RandomOptions::FromSeed(7));
Simplify{random}.WithGuarantee(is_valid(field_ref("f64"))).Expect(random);

Simplify{is_null(field_ref("i32"))}
.WithGuarantee(is_null(field_ref("i32")))
.Expect(literal(true));
Expand Down
146 changes: 125 additions & 21 deletions cpp/src/arrow/dataset/file_parquet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "arrow/dataset/scanner.h"
#include "arrow/filesystem/path_util.h"
#include "arrow/table.h"
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/future.h"
#include "arrow/util/iterator.h"
Expand Down Expand Up @@ -829,9 +830,6 @@ Status ParquetFileFragment::SetMetadata(
physical_schema_ = given_physical_schema_;
}

statistics_expressions_.resize(row_groups_->size(), compute::literal(true));
statistics_expressions_complete_.resize(manifest_->descr->num_columns(), false);

for (int row_group : *row_groups_) {
// Ensure RowGroups are indexing valid RowGroups before augmenting.
if (row_group < metadata_->num_row_groups()) continue;
Expand All @@ -845,10 +843,13 @@ Status ParquetFileFragment::SetMetadata(
}

Status ParquetFileFragment::ClearCachedMetadata() {
auto lock = physical_schema_mutex_.Lock();
metadata_.reset();
manifest_.reset();
original_metadata_.reset();
return FileFragment::ClearCachedMetadata();
statistics_expressions_.clear();
physical_schema_.reset();
return Status::OK();
}

Result<FragmentVector> ParquetFileFragment::SplitByRowGroup(
Expand Down Expand Up @@ -897,6 +898,73 @@ inline void FoldingAnd(compute::Expression* l, compute::Expression r) {
}
}

namespace {

// Bounds remain valid when the schema cast preserves ordering and every value.
bool CanUseStatistics(const DataType& physical, const DataType& logical) {
if (physical.Equals(logical)) return true;
if (is_integer(physical.id()) && is_integer(logical.id())) {
if (is_signed_integer(physical.id()) == is_signed_integer(logical.id())) {
return logical.bit_width() > physical.bit_width();
}
return is_unsigned_integer(physical.id()) &&
logical.bit_width() > physical.bit_width();
}
if (is_floating(physical.id()) && is_floating(logical.id())) {
return logical.bit_width() > physical.bit_width();
}
if (is_integer(physical.id()) &&
(logical.id() == Type::FLOAT || logical.id() == Type::DOUBLE)) {
int precision = logical.id() == Type::DOUBLE ? 53 : 24;
return physical.bit_width() - is_signed_integer(physical.id()) <= precision;
}
if ((is_string(physical.id()) && is_string(logical.id())) ||
(is_binary(physical.id()) && is_binary(logical.id()))) {
return true;
}
if (physical.id() == Type::FIXED_SIZE_BINARY && logical.id() == Type::LARGE_BINARY) {
return true;
}
if (is_string(physical.id()) &&
(logical.id() == Type::LARGE_BINARY ||
(physical.id() == Type::STRING && logical.id() == Type::BINARY))) {
return true;
}
if (is_decimal(physical.id()) && is_decimal(logical.id())) {
const auto& from = checked_cast<const DecimalType&>(physical);
const auto& to = checked_cast<const DecimalType&>(logical);
return from.scale() == to.scale() && from.precision() <= to.precision();
}
if (physical.id() == Type::DATE32 && logical.id() == Type::DATE64) return true;
if (physical.id() == Type::DATE32 && logical.id() == Type::TIMESTAMP) {
const auto& to = checked_cast<const TimestampType&>(logical);
return to.timezone().empty() && to.unit() <= TimeUnit::MILLI;
}
if (physical.id() == Type::TIMESTAMP && logical.id() == Type::TIMESTAMP) {
const auto& from = checked_cast<const TimestampType&>(physical);
const auto& to = checked_cast<const TimestampType&>(logical);
return from.timezone() == to.timezone() && from.unit() <= to.unit();
}
// Parquet stores time32 seconds as milliseconds. Keep time-of-day comparisons.
return is_time(physical.id()) &&
(physical.id() == logical.id() ||
(physical.id() == Type::TIME32 && logical.id() == Type::TIME64));
}

void CollectFieldParameters(
const compute::Expression& expression,
std::vector<const compute::Expression::Parameter*>* parameters) {
if (const auto* parameter = expression.parameter()) {
parameters->push_back(parameter);
} else if (const auto* call = expression.call()) {
for (const auto& argument : call->arguments) {
CollectFieldParameters(argument, parameters);
}
}
}

} // namespace

Result<std::vector<int>> ParquetFileFragment::FilterRowGroups(
compute::Expression predicate) {
std::vector<int> row_groups;
Expand Down Expand Up @@ -924,9 +992,14 @@ Result<std::vector<compute::Expression>> ParquetFileFragment::TestRowGroups(
return std::vector<compute::Expression>{};
}

for (const FieldRef& ref : FieldsInExpression(predicate)) {
std::vector<const compute::Expression::Parameter*> parameters;
CollectFieldParameters(predicate, &parameters);
std::unordered_set<int> seen_columns;
std::vector<std::pair<const std::vector<compute::Expression>*, compute::Expression>>
applicable_statistics;
for (const auto* parameter : parameters) {
const auto& ref = parameter->ref;
ARROW_ASSIGN_OR_RAISE(auto match, ref.FindOneOrNone(*physical_schema_));

if (match.empty()) continue;
const SchemaField* schema_field = &manifest_->schema_fields[match[0]];

Expand All @@ -937,29 +1010,60 @@ Result<std::vector<compute::Expression>> ParquetFileFragment::TestRowGroups(
schema_field = &schema_field->children[match[i]];
}

// Statistics are bound to the physical Arrow schema, before any dataset cast.
ARROW_ASSIGN_OR_RAISE(auto physical_field, match.Get(*physical_schema_));
if (!schema_field->is_leaf()) continue;
if (statistics_expressions_complete_[schema_field->column_index]) continue;
statistics_expressions_complete_[schema_field->column_index] = true;

int i = 0;
for (int row_group : *row_groups_) {
auto row_group_metadata = metadata_->RowGroup(row_group);

if (auto minmax = ColumnChunkStatisticsAsExpression(ref, *schema_field,
*row_group_metadata)) {
FoldingAnd(&statistics_expressions_[i], std::move(*minmax));
ARROW_ASSIGN_OR_RAISE(statistics_expressions_[i],
statistics_expressions_[i].Bind(*physical_schema_));
}
auto null_test = compute::literal(true);
if (!CanUseStatistics(*physical_field->type(), *parameter->type)) {
ARROW_ASSIGN_OR_RAISE(null_test,
is_null(compute::field_ref(ref)).Bind(*physical_schema_));
}
const int column_index = schema_field->column_index;
if (!seen_columns.insert(column_index).second) continue;

++i;
if (statistics_expressions_.empty()) {
statistics_expressions_.resize(manifest_->descr->num_columns());
}
auto& column_statistics = statistics_expressions_[column_index];
if (!column_statistics) {
std::vector<compute::Expression> statistics;
for (size_t i = 0; i < row_groups_->size(); ++i) {
auto row_group_metadata = metadata_->RowGroup((*row_groups_)[i]);
auto minmax =
ColumnChunkStatisticsAsExpression(ref, *schema_field, *row_group_metadata);
if (minmax) {
ARROW_ASSIGN_OR_RAISE(auto bound, minmax->Bind(*physical_schema_));
if (statistics.empty()) {
statistics.resize(row_groups_->size(), compute::literal(true));
}
statistics[i] = std::move(bound);
}
}
column_statistics = std::move(statistics);
}
if (!column_statistics->empty()) {
applicable_statistics.emplace_back(&*column_statistics, std::move(null_test));
}
}

std::vector<compute::Expression> row_groups(row_groups_->size());
for (size_t i = 0; i < row_groups_->size(); ++i) {
auto guarantee = compute::literal(true);
for (const auto& [column, null_test] : applicable_statistics) {
auto statistics = (*column)[i];
if (null_test != compute::literal(true)) {
ARROW_ASSIGN_OR_RAISE(auto nullness,
SimplifyWithGuarantee(null_test, statistics));
if (!nullness.literal()) continue;
statistics = nullness.IsSatisfiable() ? null_test
: is_valid(null_test.call()->arguments[0]);
}
if (statistics != compute::literal(true)) {
FoldingAnd(&guarantee, statistics);
}
}
ARROW_ASSIGN_OR_RAISE(auto row_group_predicate,
SimplifyWithGuarantee(predicate, statistics_expressions_[i]));
SimplifyWithGuarantee(predicate, guarantee));
row_groups[i] = std::move(row_group_predicate);
}
return row_groups;
Expand Down
10 changes: 4 additions & 6 deletions cpp/src/arrow/dataset/file_parquet.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,10 @@ class ARROW_DS_EXPORT ParquetFileFragment : public FileFragment {
/// or std::nullopt if all row groups are selected.
std::optional<std::vector<int>> row_groups_;

// the expressions (combined for all columns for which statistics have been
// processed) are stored per column group
std::vector<compute::Expression> statistics_expressions_;
// statistics status are kept track of by Parquet Schema column indices
// (i.e. not Arrow schema field index)
std::vector<bool> statistics_expressions_complete_;
// Lazily allocated statistics indexed by Parquet column, then by row group
// position in this fragment. A disengaged entry has not been read yet;
// an empty vector means that the column has no usable statistics.
std::vector<std::optional<std::vector<compute::Expression>>> statistics_expressions_;
std::shared_ptr<parquet::FileMetaData> metadata_;
std::shared_ptr<parquet::arrow::SchemaManifest> manifest_;
// The FileMetaData that owns the SchemaDescriptor pointed by SchemaManifest.
Expand Down
Loading