You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/11/14 20:35:11 UTC

[PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

alamb opened a new pull request, #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177

   ## Which issue does this PR close?
   
   Part of https://github.com/apache/arrow-datafusion/issues/8078
   
   ## Rationale for this change
   
   I am trying to consolidate uses of `Precision` into methods on the Precision enum so that 
   1. how it is used is clearer
   2. Its usage is consistent
   3. So we can more easily change how it is implemented in https://github.com/apache/arrow-datafusion/issues/8078
   
   
   ## What changes are included in this PR?
   1. Move the code that applies selectivity from `Filter::statistics` into `Precision::apply_filter`
   
   ## Are these changes tested?
   Existing tests 
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   4. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1395584158


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,15 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter with estimated selectivity
+    /// `selectivity` to this Precision. A selectivity of `1.0` means that all
+    /// rows are selected. A selectivity of `0.5` means half the rows are
+    /// selected. Will always return inexact statistics.
+    pub fn apply_filter(self, selectivity: f64) -> Self {
+        self.map(|v| ((v as f64 * selectivity).ceil()) as usize)

Review Comment:
   Maybe Inexact(0) triggers some decisions at somewhere?



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1395909884


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,13 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter of selectivity `selectivity` to
+    /// this Precision. A selectivity of `1.0` means that all rows are selected.
+    /// A selectivity of `0.5` means half the rows are selected.
+    pub fn apply_filter(self, selectivity: f64) -> Self {

Review Comment:
   renamed



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1395219495


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,13 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter of selectivity `selectivity` to
+    /// this Precision. A selectivity of `1.0` means that all rows are selected.
+    /// A selectivity of `0.5` means half the rows are selected.
+    pub fn apply_filter(self, selectivity: f64) -> Self {

Review Comment:
   I think `Precision::with_estimated_selectivity` sounds more descriptive.



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1395748851


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,15 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter with estimated selectivity
+    /// `selectivity` to this Precision. A selectivity of `1.0` means that all
+    /// rows are selected. A selectivity of `0.5` means half the rows are
+    /// selected. Will always return inexact statistics.
+    pub fn apply_filter(self, selectivity: f64) -> Self {
+        self.map(|v| ((v as f64 * selectivity).ceil()) as usize)

Review Comment:
   Yeah, I think there were several cases where DataFusion was (incorrectly) optimizing away scans based on `Inexact(0)` -- I tried to capture some of what is going on in the  https://github.com/apache/arrow-datafusion/issues/8227 ticket if you are interested



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `with_estimated_selectivity ` to Precision [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177


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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1395749728


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,15 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter with estimated selectivity
+    /// `selectivity` to this Precision. A selectivity of `1.0` means that all
+    /// rows are selected. A selectivity of `0.5` means half the rows are
+    /// selected. Will always return inexact statistics.
+    pub fn apply_filter(self, selectivity: f64) -> Self {
+        self.map(|v| ((v as f64 * selectivity).ceil()) as usize)

Review Comment:
   > Why use ceil in this case rather than round?
   
   Also, specifically in this PR I used `ceil()` because that is what the existing code did in the older codepath. 



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1395367928


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,15 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter with estimated selectivity
+    /// `selectivity` to this Precision. A selectivity of `1.0` means that all
+    /// rows are selected. A selectivity of `0.5` means half the rows are
+    /// selected. Will always return inexact statistics.
+    pub fn apply_filter(self, selectivity: f64) -> Self {
+        self.map(|v| ((v as f64 * selectivity).ceil()) as usize)

Review Comment:
   Why use ceil in this case rather than `round`?



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1394565763


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,13 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter of selectivity `selectivity` to
+    /// this Precision. A selectivity of `1.0` means that all rows are selected.
+    /// A selectivity of `0.5` means half the rows are selected.
+    pub fn apply_filter(self, selectivity: f64) -> Self {

Review Comment:
   This function both multiplies the values **and** turns the statistics into inexact (which I actually missed my first time through this code).
   
   I think the intent is much clearer and consistent in this PR where the logic is commented in a function (rather than replicated 4 times, inconsistently).
   
   That being said, I agree the notion of 'filter' is probably more specific to the physical plan rather than a "precision"
   
   Perhaps we can come up with a different name for the function ? what about `Precision::estimate_filtered` or `Precision::with_estimated_selectivity`?



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1393249713


##########
datafusion/physical-plan/src/filter.rs:
##########
@@ -200,15 +200,10 @@ impl ExecutionPlan for FilterExec {
             // assume filter selects 20% of rows if we cannot do anything smarter
             // tracking issue for making this configurable:
             // https://github.com/apache/arrow-datafusion/issues/8133
-            let selectivity = 0.2_f32;
+            let selectivity = 0.2_f64;

Review Comment:
   Interestingly this code from @andygrove  in https://github.com/apache/arrow-datafusion/pull/8126 is different to the way the selectivity is implemented below -- it uses `f32` and doesn't apply `ceil()` 
   
   This PR makes sure these two paths are consistent which I think is an improvement



##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,13 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter of selectivity `selectivity` to
+    /// this Precision. A selectivity of `1.0` means that all rows are selected.
+    /// A selectivity of `0.5` means half the rows are selected.
+    pub fn apply_filter(self, selectivity: f64) -> Self {

Review Comment:
   This is one of the key APIs I expect to change as part of https://github.com/apache/arrow-datafusion/issues/8078 (the output will retain information about the min/max).



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Minor: add `apply_filter` to Precision [arrow-datafusion]

Posted by "berkaysynnada (via GitHub)" <gi...@apache.org>.
berkaysynnada commented on code in PR #8177:
URL: https://github.com/apache/arrow-datafusion/pull/8177#discussion_r1393772792


##########
datafusion/common/src/stats.rs:
##########
@@ -151,6 +151,13 @@ impl Precision<usize> {
             (_, _) => Precision::Absent,
         }
     }
+
+    /// Return the estimate of applying a filter of selectivity `selectivity` to
+    /// this Precision. A selectivity of `1.0` means that all rows are selected.
+    /// A selectivity of `0.5` means half the rows are selected.
+    pub fn apply_filter(self, selectivity: f64) -> Self {

Review Comment:
   I am wondering if it might be more suitable to locate this function in physical_plan's? There's already a multiplication function here, and it seems to me that this function could potentially be more relevant with filter context.



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

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org