polars/polars-join-without-validate
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Polars | info | none, edit by hand | All versions |
A Polars join allows duplicate keys on either side by default. Pass validate when the keys should be unique on one or both sides.
df.join(df, on="x")df.join(df, on="x", validate="1:1")What the rule reports
Section titled “What the rule reports”The rule reports join with on=, left_on= or right_on= and no validate=, unless the lines before it prove that the join cannot repeat a row. A left join needs the right key proven unique, a right join needs the left key, and every other join needs both. group_by("x") with an aggregation and unique(subset="x") prove a key unique, and so does a check that stops on a duplicate.
The rule leaves a join with how="cross", how="semi" or how="anti" alone.
Two identical keys on each side yielded 4 rows. 1:1 validation raised ComputeError.
How to fix it
Section titled “How to fix it”Edit the code using the replacement in the example.
Polars validation is cheap. In one measurement on 500,000 to 5,000,000 rows with Polars 2.2.0, a join with validate="m:1" took 0.9 to 1.1 times as long as the join alone. The validation ignores null keys, so two rows with a null key pass validate="1:1".
Diagnostic message
Section titled “Diagnostic message”A Polars join allows duplicate keys on either side by default. Pass validate when the keys should be unique on one or both sides.See all rules in the Polars pack.