You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2024/08/05 18:55:33 UTC

[PR] Minor: make it clearer that clone() is not slow [datafusion]

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

   ## Which issue does this PR close?
   
   Builds on https://github.com/apache/datafusion/pull/11802
   
   
   ## Rationale for this change
   
   While reviewing https://github.com/apache/datafusion/pull/11802 from @Rachelint I had to double check that a call to `clone()` was not copying anything large (specifically a `ScalarValue`) and it turns it that it is not
   
   ## What changes are included in this PR?
   
   Move the `clone()` (of a `usize`) into a function where the type is explict
   
   ## Are these changes tested?
   
   By existing CI
   
   ## Are there any user-facing changes?
   No
   


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Make `Precision` copy to make it clear clones are not expensive [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -25,7 +25,7 @@ use arrow_schema::Schema;
 
 /// Represents a value with a degree of certainty. `Precision` is used to
 /// propagate information the precision of statistical values.
-#[derive(Clone, PartialEq, Eq, Default)]
+#[derive(Clone, PartialEq, Eq, Default, Copy)]

Review Comment:
   👍  -- I think that is basically what `#[derive(Debug)]` does in this case, which is kind of cook



##########
datafusion/common/src/stats.rs:
##########
@@ -25,7 +25,7 @@ use arrow_schema::Schema;
 
 /// Represents a value with a degree of certainty. `Precision` is used to
 /// propagate information the precision of statistical values.
-#[derive(Clone, PartialEq, Eq, Default)]
+#[derive(Clone, PartialEq, Eq, Default, Copy)]

Review Comment:
   👍  -- I think that is basically what `#[derive(Debug)]` does in this case, which is kind of cool



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Make `Precision` copy to make it clear clones are not expensive [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -25,7 +25,7 @@ use arrow_schema::Schema;
 
 /// Represents a value with a degree of certainty. `Precision` is used to
 /// propagate information the precision of statistical values.
-#[derive(Clone, PartialEq, Eq, Default)]
+#[derive(Clone, PartialEq, Eq, Default, Copy)]

Review Comment:
   This change only affects the type when the templated type is `Copy`
   
   So in other words this doesn't make it easy to accidentally copy `Precision<ScalarValue>` only copies when the underlying `T` also supports copy 
   
   I found https://users.rust-lang.org/t/copy-bound-on-type-parameters/58928 helpful



##########
datafusion/common/src/stats.rs:
##########
@@ -417,9 +417,9 @@ mod tests {
         let inexact_precision = Precision::Inexact(42);
         let absent_precision = Precision::<i32>::Absent;
 
-        assert_eq!(exact_precision.clone().to_inexact(), inexact_precision);
-        assert_eq!(inexact_precision.clone().to_inexact(), inexact_precision);
-        assert_eq!(absent_precision.clone().to_inexact(), absent_precision);
+        assert_eq!(exact_precision.to_inexact(), inexact_precision);

Review Comment:
   Once I derived `Copy`, clippy lead me to all the various other changes in this PR



##########
datafusion/common/src/stats.rs:
##########
@@ -459,4 +459,19 @@ mod tests {
         assert_eq!(precision2.multiply(&precision3), Precision::Inexact(15));
         assert_eq!(precision1.multiply(&absent_precision), Precision::Absent);
     }
+
+    #[test]
+    fn test_precision_cloning() {
+        // Precision<usize> is copy
+        let precision: Precision<usize> = Precision::Exact(42);
+        let p2 = precision;
+        assert_eq!(precision, p2);
+
+        // Precision<ScalarValue> is not copy (requires .clone())
+        let precision: Precision<ScalarValue> =
+            Precision::Exact(ScalarValue::Int64(Some(42)));
+        // Clippy would complain about this if it were Copy
+        let p2 = precision.clone();

Review Comment:
   here is my test to ensure `Precision<ScalarValue>` is not copy



##########
datafusion/core/src/datasource/statistics.rs:
##########
@@ -91,10 +91,10 @@ pub async fn get_statistics_with_limit(
                 // counts across all the files in question. If any file does not
                 // provide any information or provides an inexact value, we demote
                 // the statistic precision to inexact.
-                num_rows = add_row_stats(file_stats.num_rows.clone(), num_rows);
+                num_rows = add_row_stats(&file_stats.num_rows, &num_rows);

Review Comment:
   THank you @Dandandan  -- that was a great idea -- I did it in in 7b71ea5af and it worked great



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Minor: make it clearer that clone() is not slow [datafusion]

Posted by "Rachelint (via GitHub)" <gi...@apache.org>.
Rachelint commented on PR #11828:
URL: https://github.com/apache/datafusion/pull/11828#issuecomment-2270189202

   Yes, I check the clone cost of `Precision` many times too... It is really good to make it clear


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Make `Precision` copy to make it clear clones are not expensive [datafusion]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum commented on code in PR #11828:
URL: https://github.com/apache/datafusion/pull/11828#discussion_r1713654802


##########
datafusion/common/src/stats.rs:
##########
@@ -25,7 +25,7 @@ use arrow_schema::Schema;
 
 /// Represents a value with a degree of certainty. `Precision` is used to
 /// propagate information the precision of statistical values.
-#[derive(Clone, PartialEq, Eq, Default)]
+#[derive(Clone, PartialEq, Eq, Default, Copy)]

Review Comment:
   BTW: You can always manually implement the `Copy` trait if you want to have fine grained control about the bounds:
   
   ```rust
   impl<T> Copy for Precision<T> where: TDebug + Clone + PartialEq + Eq + PartialOrd + Clone {}
   ```



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Minor: make it clearer that clone() is not slow [datafusion]

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


##########
datafusion/core/src/datasource/statistics.rs:
##########
@@ -91,10 +91,10 @@ pub async fn get_statistics_with_limit(
                 // counts across all the files in question. If any file does not
                 // provide any information or provides an inexact value, we demote
                 // the statistic precision to inexact.
-                num_rows = add_row_stats(file_stats.num_rows.clone(), num_rows);
+                num_rows = add_row_stats(&file_stats.num_rows, &num_rows);

Review Comment:
   Are we able to have `Precision` to be `Copy` when `T` is `Copy`? That would simplify things. 



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Make `Precision` copy to make it clear clones are not expensive [datafusion]

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


-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Make `Precision` copy to make it clear clones are not expensive [datafusion]

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


##########
datafusion/core/src/datasource/statistics.rs:
##########
@@ -91,10 +91,10 @@ pub async fn get_statistics_with_limit(
                 // counts across all the files in question. If any file does not
                 // provide any information or provides an inexact value, we demote
                 // the statistic precision to inexact.
-                num_rows = add_row_stats(file_stats.num_rows.clone(), num_rows);
+                num_rows = add_row_stats(&file_stats.num_rows, &num_rows);

Review Comment:
   Nice 👌 



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Make `Precision` copy to make it clear clones are not expensive [datafusion]

Posted by "crepererum (via GitHub)" <gi...@apache.org>.
crepererum commented on code in PR #11828:
URL: https://github.com/apache/datafusion/pull/11828#discussion_r1713654802


##########
datafusion/common/src/stats.rs:
##########
@@ -25,7 +25,7 @@ use arrow_schema::Schema;
 
 /// Represents a value with a degree of certainty. `Precision` is used to
 /// propagate information the precision of statistical values.
-#[derive(Clone, PartialEq, Eq, Default)]
+#[derive(Clone, PartialEq, Eq, Default, Copy)]

Review Comment:
   BTW: You can always manually implement the `Copy` trait if you want to have fine grained control about the bounds:
   
   ```rust
   impl<T> Copy for Precision<T> where T: Debug + Clone + PartialEq + Eq + PartialOrd + Clone {}
   ```



-- 
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@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org