You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Weijun-H (via GitHub)" <gi...@apache.org> on 2024/04/27 01:36:25 UTC

[PR] fix: Correct null_count in describe() [datafusion]

Weijun-H opened a new pull request, #10260:
URL: https://github.com/apache/datafusion/pull/10260

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #10231
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. 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@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] fix: Correct null_count in describe() [datafusion]

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


##########
datafusion/expr/src/expr_fn.rs:
##########
@@ -215,6 +215,18 @@ pub fn count(expr: Expr) -> Expr {
     ))
 }
 
+/// Create an expression to represent the COUNT(IS NULL x) aggregate function

Review Comment:
   I found this confusing as first because it isn't `COUNT(x IS NULL)` -- I think a more accurate description is `COUNT(x) FILTER (WHERE x IS NULL)` or something like that 
   
   ```suggestion
   /// Create an aggregate that returns the number `NULL` values in a column
   ```



##########
datafusion/core/src/dataframe/mod.rs:
##########
@@ -534,7 +534,7 @@ impl DataFrame {
                 vec![],
                 original_schema_fields
                     .clone()
-                    .map(|f| count(is_null(col(f.name()))).alias(f.name()))
+                    .map(|f| count_null(col(f.name())).alias(f.name()))

Review Comment:
   Got it. Make sense to me. Another classic trick is to use a CASE expression, something like
   
   ```sql
   SELECT  SUM(CASE WHEN x IS NULL THEN 1 ELSE 0 END) as null_count ...
   ```



-- 
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] fix: Correct null_count in describe() [datafusion]

Posted by "Weijun-H (via GitHub)" <gi...@apache.org>.
Weijun-H commented on code in PR #10260:
URL: https://github.com/apache/datafusion/pull/10260#discussion_r1581828836


##########
datafusion/core/src/dataframe/mod.rs:
##########
@@ -534,7 +534,7 @@ impl DataFrame {
                 vec![],
                 original_schema_fields
                     .clone()
-                    .map(|f| count(is_null(col(f.name()))).alias(f.name()))
+                    .map(|f| count_null(col(f.name())).alias(f.name()))

Review Comment:
   When using `count`, it sums all `non-null` Expr while `is_null` converts `Expr` to a boolean expression, which is why it doesn't work.



-- 
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] fix: Correct null_count in describe() [datafusion]

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


##########
datafusion/core/tests/dataframe/describe.rs:
##########
@@ -81,6 +81,36 @@ async fn describe_boolean_binary() -> Result<()> {
     Ok(())
 }
 
+#[tokio::test]
+async fn describe_null() -> Result<()> {
+    let ctx = parquet_context().await;
+
+    //add test case for only boolean boolean/binary column
+    let result = ctx
+        .sql("select 'a' as a, null as b")
+        .await?
+        .describe()
+        .await?
+        .collect()
+        .await?;
+    #[rustfmt::skip]
+    let expected = [
+        "+------------+------+------+",
+        "| describe   | a    | b    |",
+        "+------------+------+------+",
+        "| count      | 1    | 0    |",
+        "| null_count | 0    | 1    |",

Review Comment:
   ❤️  -- thank you @Weijun-H 



-- 
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] fix: Correct null_count in describe() [datafusion]

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


-- 
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] fix: Correct null_count in describe() [datafusion]

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


##########
datafusion/expr/src/expr_fn.rs:
##########
@@ -215,6 +215,18 @@ pub fn count(expr: Expr) -> Expr {
     ))
 }
 
+/// Create an expression to represent the count_null() aggregate function

Review Comment:
   I think this function is creating a function like `COUNT(IS NULL x)` rather than a `count_null` function
   
   



##########
datafusion/core/tests/dataframe/describe.rs:
##########
@@ -39,7 +39,7 @@ async fn describe() -> Result<()> {
         "| describe   | id                | bool_col | tinyint_col        | smallint_col       | int_col            | bigint_col         | float_col          | double_col         | date_string_col | string_col | timestamp_col           | year               | month             |",
         "+------------+-------------------+----------+--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+-----------------+------------+-------------------------+--------------------+-------------------+",
         "| count      | 7300.0            | 7300     | 7300.0             | 7300.0             | 7300.0             | 7300.0             | 7300.0             | 7300.0             | 7300            | 7300       | 7300                    | 7300.0             | 7300.0            |",
-        "| null_count | 7300.0            | 7300     | 7300.0             | 7300.0             | 7300.0             | 7300.0             | 7300.0             | 7300.0             | 7300            | 7300       | 7300                    | 7300.0             | 7300.0            |",
+        "| null_count | 0.0               | 0        | 0.0                | 0.0                | 0.0                | 0.0                | 0.0                | 0.0                | 0               | 0          | 0                       | 0.0                | 0.0               |",

Review Comment:
   that certainly looks better



##########
datafusion/core/src/dataframe/mod.rs:
##########
@@ -534,7 +534,7 @@ impl DataFrame {
                 vec![],
                 original_schema_fields
                     .clone()
-                    .map(|f| count(is_null(col(f.name()))).alias(f.name()))
+                    .map(|f| count_null(col(f.name())).alias(f.name()))

Review Comment:
   Instead of a `count_null()` new function, what do you think about using the existing [`count`](https://docs.rs/datafusion/latest/datafusion/logical_expr/fn.count.html) function?
   
   Something like `count(expr.is_null())`
   
   ```suggestion
                       .map(|f| count(col(f.name()).is_null()).alias(f.name())))
   ```



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