-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for impure function correlation behavior
Need to fix the UDF test case. Related to #8921, trying to write down exactly what the expected behavior is.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
import pandas.testing as tm | ||
import pytest | ||
|
||
import ibis | ||
from ibis import _ | ||
|
||
_counter = 0 | ||
|
||
|
||
@ibis.udf.scalar.python(side_effects=True) | ||
def get_id() -> int: | ||
global _counter # noqa: PLW0603 | ||
_counter += 1 | ||
return _counter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"impure", | ||
[ | ||
pytest.param( | ||
lambda: ibis.random(), | ||
id="random", | ||
), | ||
pytest.param( | ||
lambda: ibis.uuid(), | ||
id="uuid", | ||
), | ||
pytest.param( | ||
get_id, | ||
id="udf", | ||
), | ||
], | ||
) | ||
def test_impure_uncorrelated(alltypes, impure): | ||
df = alltypes.select(x=impure(), y=impure()).execute() | ||
assert (df.x != df.y).mean() >= 0.999 | ||
# Even if the two expressions have the exact same ID, they should still be | ||
# uncorrelated | ||
common = impure() | ||
df = alltypes.select(x=common, y=common).execute() | ||
assert (df.x != df.y).mean() >= 0.999 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"impure", | ||
[ | ||
pytest.param( | ||
lambda: ibis.random(), | ||
id="random", | ||
), | ||
pytest.param( | ||
lambda: ibis.uuid(), | ||
id="uuid", | ||
), | ||
pytest.param( | ||
get_id, | ||
id="udf", | ||
# once this is fixed, can we unify these params with the above params? | ||
marks=pytest.mark.xfail(reason="executed multiple times"), | ||
), | ||
], | ||
) | ||
def test_impure_correlated(alltypes, impure): | ||
df = alltypes.select(common=impure()).select(x=_.common, y=_.common).execute() | ||
tm.assert_series_equal(df.x, df.y, check_names=False) |