Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SQL COUNT(DISTINCT x) should not include NULL values #17930

Merged
merged 1 commit into from
Jul 30, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jul 29, 2024

SQL results should not include NULL values in COUNT(colname) or COUNT(DISTINCT colname) results -- only COUNT(*) should include them (we were doing the right thing for the first form, but not the second one with the inner 'distinct').

Ref: https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-AGGREGATES

Example

import polars as pl

df = pl.DataFrame({
    "x": [1, 1, 2, 2, 3],
    "y": [1, 1, 1, None, None],
})

df.sql("""
  SELECT
     COUNT(x) AS count_x,
     COUNT(y) AS count_y,
     COUNT(*) AS count_star,
     COUNT(DISTINCT x) AS count_unique_x,
     COUNT(DISTINCT y) AS count_unique_y,
   FROM self  
""")
# shape: (1, 5)
# ┌─────────┬─────────┬────────────┬────────────────┬────────────────┐
# │ count_x ┆ count_y ┆ count_star ┆ count_unique_x ┆ count_unique_y │
# │ ---     ┆ ---     ┆ ---        ┆ ---            ┆ ---            │
# │ u32     ┆ u32     ┆ u32        ┆ u32            ┆ u32            │
# ╞═════════╪═════════╪════════════╪════════════════╪════════════════╡
# │ 5       ┆ 3       ┆ 5          ┆ 3              ┆ 1              │
# └─────────┴─────────┴────────────┴────────────────┴────────────────┘

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jul 29, 2024
@alexander-beedie alexander-beedie added the A-sql Area: Polars SQL functionality label Jul 29, 2024
Copy link

codecov bot commented Jul 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.33%. Comparing base (5fc791c) to head (da3a410).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17930      +/-   ##
==========================================
- Coverage   80.49%   80.33%   -0.16%     
==========================================
  Files        1493     1492       -1     
  Lines      196477   196330     -147     
  Branches     2813     2813              
==========================================
- Hits       158155   157726     -429     
- Misses      37801    38083     +282     
  Partials      521      521              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alexander-beedie alexander-beedie changed the title fix: SQL COUNT should not include NULL values fix: SQL COUNT(DISTINCT x) should not include NULL values Jul 29, 2024
@alexander-beedie alexander-beedie force-pushed the sql-count-null branch 2 times, most recently from 7888c4c to 7a7f455 Compare July 29, 2024 19:50
@ritchie46 ritchie46 merged commit f58aa39 into pola-rs:main Jul 30, 2024
29 checks passed
@alexander-beedie alexander-beedie deleted the sql-count-null branch July 30, 2024 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql Area: Polars SQL functionality fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants