Skip to content

Commit

Permalink
fix: SQL COUNT should not include null values
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jul 29, 2024
1 parent 9c29683 commit 2f4e9d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/polars-sql/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,13 +1567,13 @@ impl SQLFunctionVisitor<'_> {
(false, [FunctionArgExpr::Expr(sql_expr)]) => {
let expr = parse_sql_expr(sql_expr, self.ctx, self.active_schema)?;
let expr = self.apply_window_spec(expr, &self.func.over)?;
Ok(expr.count())
Ok(expr.drop_nulls().count())
},
// count(distinct column_name)
(true, [FunctionArgExpr::Expr(sql_expr)]) => {
let expr = parse_sql_expr(sql_expr, self.ctx, self.active_schema)?;
let expr = self.apply_window_spec(expr, &self.func.over)?;
Ok(expr.n_unique())
Ok(expr.drop_nulls().n_unique())
},
_ => self.not_supported_error(),
}
Expand Down
39 changes: 39 additions & 0 deletions py-polars/tests/unit/sql/test_miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,45 @@ def test_any_all() -> None:
}


def test_count() -> None:
df = pl.DataFrame(
{
"a": [1, 2, 3, 4, 5],
"b": [1, 1, 22, 22, 333],
"c": [1, 1, None, None, 2],
}
)
res = df.sql(
"""
SELECT
-- count
COUNT(a) AS count_a,
COUNT(b) AS count_b,
COUNT(c) AS count_c,
COUNT(*) AS count_star,
COUNT(NULL) AS count_null,
-- count distinct
COUNT(DISTINCT a) AS count_unique_a,
COUNT(DISTINCT b) AS count_unique_b,
COUNT(DISTINCT(c)) AS count_unique_c,
COUNT(DISTINCT NULL) AS count_unique_null,
FROM self
""",
)

assert res.to_dict(as_series=False) == {
"count_a": [5],
"count_b": [5],
"count_c": [3],
"count_star": [5],
"count_null": [0],
"count_unique_a": [5],
"count_unique_b": [3],
"count_unique_c": [2],
"count_unique_null": [0],
}


def test_distinct() -> None:
df = pl.DataFrame(
{
Expand Down

0 comments on commit 2f4e9d6

Please sign in to comment.