bkietz commented on a change in pull request #9670:
URL: https://github.com/apache/arrow/pull/9670#discussion_r592351160



##########
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) {

Review comment:
       Probably a worthwhile special case:
   ```suggestion
   Result<FragmentIterator> FileSystemDataset::GetFragmentsImpl(Expression 
predicate) {
     if (predicate == literal(true)) {
       // trivial predicate; skip subtree pruning
       return MakeVectorIterator(FragmentVector(fragments_.begin(), 
fragments_.end()));
     }
   ```




----------------------------------------------------------------
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


Reply via email to