bkietz commented on a change in pull request #9670: URL: https://github.com/apache/arrow/pull/9670#discussion_r592357682
########## File path: cpp/src/arrow/dataset/file_base.cc ########## @@ -137,16 +130,69 @@ std::string FileSystemDataset::ToString() const { return repr; } -Result<FragmentIterator> FileSystemDataset::GetFragmentsImpl(Expression predicate) { - FragmentVector fragments; +void FileSystemDataset::SetupSubtreePruning() { + SubtreeImpl impl; - for (const auto& fragment : fragments_) { - ARROW_ASSIGN_OR_RAISE( - auto simplified, - SimplifyWithGuarantee(predicate, fragment->partition_expression())); - if (simplified.IsSatisfiable()) { - fragments.push_back(fragment); + auto encoded = impl.EncodeFragments(fragments_); + + std::sort(encoded.begin(), encoded.end(), + [](const SubtreeImpl::Encoded& l, const SubtreeImpl::Encoded& r) { + return l.partition_expression < r.partition_expression; + }); + + for (const auto& e : encoded) { + if (e.fragment_index) { + fragments_and_subtrees_.emplace_back(*e.fragment_index); + } else { + fragments_and_subtrees_.emplace_back(impl.GetSubtreeExpression(e)); + } + } + + forest_ = Forest(static_cast<int>(encoded.size()), [&](int l, int r) { + if (encoded[l].fragment_index) { + // Fragment: not an ancestor. + return false; + } + + const auto& ancestor = encoded[l].partition_expression; + const auto& descendant = encoded[r].partition_expression; + + if (descendant.size() >= ancestor.size()) { + return std::equal(ancestor.begin(), ancestor.end(), descendant.begin()); } + return false; + }); +} + +Result<FragmentIterator> FileSystemDataset::GetFragmentsImpl(Expression predicate) { + std::vector<int> fragment_indices; + + std::vector<Expression> predicates{predicate}; + RETURN_NOT_OK(forest_.Visit( + [&](Forest::Ref ref) -> Result<bool> { + if (auto fragment_index = util::get_if<int>(&fragments_and_subtrees_[ref.i])) { + fragment_indices.push_back(*fragment_index); + return false; + } + + const auto& subtree_expr = util::get<Expression>(fragments_and_subtrees_[ref.i]); + ARROW_ASSIGN_OR_RAISE(auto simplified, + SimplifyWithGuarantee(predicates.back(), subtree_expr)); + + if (!simplified.IsSatisfiable()) { + return false; + } + + predicates.push_back(std::move(simplified)); + return true; + }, + [&](Forest::Ref ref) { predicates.pop_back(); })); + + std::sort(fragment_indices.begin(), fragment_indices.end()); + + FragmentVector fragments; + for (int i : fragment_indices) { + fragments.push_back(fragments_[i]); } Review comment: ```suggestion FragmentVector fragments(fragment_indices.size()); std::transform(fragment_indices.begin(), fragment_indices.end(), fragments.begin(), [](int i) { return fragments_[i]; }); ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org