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

perf: simplify negated predicates to improve row groups skipping #14370

Merged
merged 1 commit into from
Feb 8, 2024

Conversation

taki-mekhalfa
Copy link
Contributor

This simplifies usual boolean predicates to allow for more row groups skippings when reading parquets or pruning hive partitions.

- not(a = b) => a != b
- not(a != b) => a == b
- not(a < b) => a >= b (likewise for a <= b)
- not(a > b) => a <= b (likewise for a >= b)
- not(a and b) => not(a) or not(b)
- not(a or b) => not(a) and not(b)
- not(col(x) IS BETWEEN [a, b]) => col(x) < a or col(x) > b

Here are some examples:

>>> pl.scan_parquet('examples/datasets/foods1.parquet').filter(~(pl.col('calories')<300)).collect()
parquet file can be skipped, the statistics were sufficient to apply the predicate.
shape: (0, 4)
┌──────────┬──────────┬────────┬──────────┐
│ categorycaloriesfats_gsugars_g │
│ ------------      │
│ stri64f64i64      │
╞══════════╪══════════╪════════╪══════════╡
└──────────┴──────────┴────────┴──────────┘

>>> pl.scan_parquet('examples/datasets/foods1.parquet').filter(~(pl.col('calories')>=15)).collect()
parquet file can be skipped, the statistics were sufficient to apply the predicate.
shape: (0, 4)
┌──────────┬──────────┬────────┬──────────┐
│ categorycaloriesfats_gsugars_g │
│ ------------      │
│ stri64f64i64      │
╞══════════╪══════════╪════════╪══════════╡
└──────────┴──────────┴────────┴──────────┘

>>> pl.scan_parquet('examples/datasets/foods1.parquet').filter(~(pl.col('calories').gt(200) | pl.col('sugars_g').le(30))).collect()
parquet file must be read, statistics not sufficient for predicate.
parquet file can be skipped, the statistics were sufficient to apply the predicate.
shape: (0, 4)
┌──────────┬──────────┬────────┬──────────┐
│ categorycaloriesfats_gsugars_g │
│ ------------      │
│ stri64f64i64      │
╞══════════╪══════════╪════════╪══════════╡
└──────────┴──────────┴────────┴──────────┘

@github-actions github-actions bot added performance Performance issues or improvements python Related to Python Polars rust Related to Rust Polars labels Feb 8, 2024
function: FunctionExpr::Boolean(BooleanFunction::IsBetween { closed }),
..
} => {
if !matches!(expr_arena.get(input[0]), AExpr::Column(_)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this because I am not sure if otherwise it would hurt performance if we duplicate a more complex input into two predicates

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @taki-mekhalfa

@ritchie46 ritchie46 merged commit 96600d3 into pola-rs:main Feb 8, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance issues or improvements 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