-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eeb1282
commit 0edf691
Showing
3 changed files
with
158 additions
and
127 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
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
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 |
---|---|---|
@@ -1,122 +1,85 @@ | ||
from __future__ import annotations | ||
|
||
import daft | ||
from daft import col | ||
|
||
|
||
def test_simple_intersect(make_df): | ||
df1 = make_df({"foo": [1, 2, 3]}) | ||
df2 = make_df({"bar": [2, 3, 4]}) | ||
result = df1.intersect(df2) | ||
assert result.to_pydict() == {"foo": [2, 3]} | ||
df1 = make_df({"foo": [1, 2, 2]}) | ||
df2 = make_df({"bar": [2, 2, 4]}) | ||
result = df1.intersect_all(df2) | ||
assert result.to_pydict() == {"foo": [2, 2]} | ||
|
||
|
||
def test_simple_except(make_df): | ||
df1 = make_df({"foo": [1, 2, 3]}) | ||
df2 = make_df({"bar": [2, 3, 4]}) | ||
result = df1.except_distinct(df2) | ||
assert result.to_pydict() == {"foo": [1]} | ||
df1 = make_df({"foo": [1, 1, 1, 2, 4, 4]}) | ||
df2 = make_df({"bar": [1, 2, 2, 4]}) | ||
result = df1.except_all(df2).sort(by="foo") | ||
assert result.to_pydict() == {"foo": [1, 1, 4]} | ||
|
||
|
||
def test_intersect_with_duplicate(make_df): | ||
df1 = make_df({"foo": [1, 2, 2, 3]}) | ||
df2 = make_df({"bar": [2, 3, 3]}) | ||
result = df1.intersect(df2) | ||
assert result.to_pydict() == {"foo": [2, 3]} | ||
result = df1.intersect_all(df2) | ||
assert result.to_pydict() == {"foo": [2, 3]} | ||
|
||
|
||
def test_except_with_duplicate(make_df): | ||
df1 = make_df({"foo": [1, 2, 2, 3]}) | ||
df2 = make_df({"bar": [2, 3, 3]}) | ||
result = df1.except_distinct(df2) | ||
assert result.to_pydict() == {"foo": [1]} | ||
result = df1.except_all(df2).sort(by="foo") | ||
assert result.to_pydict() == {"foo": [1, 2]} | ||
|
||
|
||
def test_self_intersect(make_df): | ||
df = make_df({"foo": [1, 2, 3]}) | ||
result = df.intersect(df).sort(by="foo") | ||
assert result.to_pydict() == {"foo": [1, 2, 3]} | ||
result = df.intersect_all(df).sort(by="foo") | ||
assert result.to_pydict() == {"foo": [1, 2, 3]} | ||
|
||
|
||
def test_self_except(make_df): | ||
df = make_df({"foo": [1, 2, 3]}) | ||
result = df.except_distinct(df).sort(by="foo") | ||
assert result.to_pydict() == {"foo": []} | ||
result = df.except_all(df).sort(by="foo") | ||
assert result.to_pydict() == {"foo": []} | ||
|
||
from typing import Any, Dict | ||
|
||
def test_intersect_empty(make_df): | ||
df1 = make_df({"foo": [1, 2, 3]}) | ||
df2 = make_df({"bar": []}).select(col("bar").cast(daft.DataType.int64())) | ||
result = df1.intersect(df2) | ||
assert result.to_pydict() == {"foo": []} | ||
result = df1.intersect_all(df2) | ||
assert result.to_pydict() == {"foo": []} | ||
|
||
|
||
def test_except_empty(make_df): | ||
df1 = make_df({"foo": [1, 2, 3]}) | ||
import daft | ||
from daft import col, DataFrame | ||
import pytest | ||
from tests.conftest import check_answer | ||
|
||
def helper(make_df, op: str, left: Dict[str, Any], right: Dict[str, Any], expected: Dict[str, Any]): | ||
df1 = make_df(left) | ||
df2 = make_df(right) | ||
df_helper(op, df1, df2, expected) | ||
|
||
def df_helper(op: str, df1: DataFrame, df2: DataFrame, expected: Dict[str, Any]): | ||
if op == "intersect": | ||
result = df1.intersect(df2) | ||
elif op == "except_distinct": | ||
result = df1.except_distinct(df2) | ||
elif op == "intersect_all": | ||
result = df1.intersect_all(df2) | ||
else: | ||
result = df1.except_all(df2) | ||
check_answer(result, expected) | ||
|
||
@pytest.mark.parametrize("op, left, right, expected", [ | ||
("intersect", {"foo": [1, 2, 3]}, {"bar": [2, 3, 4]}, {"foo": [2, 3]}), | ||
("intersect_all", {"foo": [1, 2, 2]}, {"bar": [2, 2, 4]}, {"foo": [2, 2]}), | ||
("except_distinct", {"foo": [1, 2, 3]}, {"bar": [2, 3, 4]}, {"foo": [1]}), | ||
("except_all", {"foo": [1, 2, 2]}, {"bar": [2, 4]}, {"foo": [1, 2]}), | ||
]) | ||
def test_simple_intersect_or_except(make_df, op, left, right, expected): | ||
helper(make_df, op, left, right, expected) | ||
|
||
@pytest.mark.parametrize("op, left, right, expected", [ | ||
("intersect", {"foo": [1, 2, 2, 3]}, {"bar": [2, 3, 3]}, {"foo": [2, 3]}), | ||
("intersect_all", {"foo": [1, 2, 2, 3]}, {"bar": [2, 3, 3]}, {"foo": [2, 3]}), | ||
("except_distinct", {"foo": [1, 2, 2, 3]}, {"bar": [2, 3, 3]}, {"foo": [1]}), | ||
("except_all", {"foo": [1, 2, 2, 3]}, {"bar": [2, 3, 3]}, {"foo": [1, 2]}), | ||
]) | ||
def test_with_duplicate(make_df, op, left, right, expected): | ||
helper(make_df, op, left, right, expected) | ||
|
||
@pytest.mark.parametrize("op, df, expected", [ | ||
("intersect", {"foo": [1, 2, 3]}, {"foo": [1, 2, 3]}), | ||
("intersect_all", {"foo": [1, 2, 3]}, {"foo": [1, 2, 3]}), | ||
("except_distinct", {"foo": [1, 2, 3]}, {"foo": []}), | ||
("except_all", {"foo": [1, 2, 2]}, {"foo": []}), | ||
]) | ||
def test_with_self(make_df, op, df, expected): | ||
df = make_df(df) | ||
df_helper(op, df, df, expected) | ||
|
||
@pytest.mark.parametrize("op, left, expected", [ | ||
("intersect", {"foo": [1, 2, 3]}, {"foo": []}), | ||
("intersect_all", {"foo": [1, 2, 3]}, {"foo": []}), | ||
("except_distinct", {"foo": [1, 2, 3]}, {"foo": [1, 2, 3]}), | ||
("except_all", {"foo": [1, 2, 2]}, {"foo": [1, 2, 2]}), | ||
]) | ||
def test_with_empty(make_df, op, left, expected): | ||
df1 = make_df(left) | ||
df2 = make_df({"bar": []}).select(col("bar").cast(daft.DataType.int64())) | ||
result = df1.except_distinct(df2).sort(by="foo") | ||
assert result.to_pydict() == {"foo": [1, 2, 3]} | ||
result = df1.except_all(df2).sort(by="foo") | ||
assert result.to_pydict() == {"foo": [1, 2, 3]} | ||
|
||
|
||
def test_intersect_with_nulls(make_df): | ||
df1 = make_df({"foo": [1, 2, None]}) | ||
df1_without_mull = make_df({"foo": [1, 2]}) | ||
df2 = make_df({"bar": [2, 3, None]}) | ||
df2_without_null = make_df({"bar": [2, 3]}) | ||
|
||
result = df1.intersect(df2) | ||
assert result.to_pydict() == {"foo": [2, None]} | ||
result = df1.intersect_all(df2) | ||
assert result.to_pydict() == {"foo": [2, None]} | ||
|
||
result = df1_without_mull.intersect(df2) | ||
assert result.to_pydict() == {"foo": [2]} | ||
result = df1_without_mull.intersect_all(df2) | ||
assert result.to_pydict() == {"foo": [2]} | ||
|
||
result = df1.intersect(df2_without_null) | ||
assert result.to_pydict() == {"foo": [2]} | ||
result = df1.intersect_all(df2_without_null) | ||
assert result.to_pydict() == {"foo": [2]} | ||
|
||
|
||
def test_except_with_nulls(make_df): | ||
df1 = make_df({"foo": [1, 2, None]}) | ||
df1_without_mull = make_df({"foo": [1, 2]}) | ||
df2 = make_df({"bar": [2, 3, None]}) | ||
df2_without_null = make_df({"bar": [2, 3]}) | ||
|
||
result = df1.except_distinct(df2) | ||
assert result.to_pydict() == {"foo": [1]} | ||
result = df1.except_all(df2) | ||
assert result.to_pydict() == {"foo": [1]} | ||
|
||
result = df1_without_mull.except_distinct(df2) | ||
assert result.to_pydict() == {"foo": [1]} | ||
result = df1_without_mull.except_all(df2) | ||
assert result.to_pydict() == {"foo": [1]} | ||
|
||
result = df1.except_distinct(df2_without_null) | ||
assert result.to_pydict() == {"foo": [1, None]} | ||
result = df1.except_all(df2_without_null) | ||
assert result.to_pydict() == {"foo": [1, None]} | ||
df_helper(op, df1, df2, expected) | ||
|
||
@pytest.mark.parametrize("op, left, right, expected", [ | ||
("intersect", {"foo": [1, 2, None]}, {"foo": [2, 3, None]}, {"foo": [2, None]}), | ||
("intersect_all", {"foo": [1, 2, None]}, {"foo": [2, 3, None]}, {"foo": [2, None]}), | ||
("intersect", {"foo": [1, 2]}, {"foo": [2, 3, None]}, {"foo": [2]}), | ||
("intersect_all", {"foo": [1, 2]}, {"foo": [2, 3, None]}, {"foo": [2]}), | ||
("intersect", {"foo": [1, 2, None]}, {"foo": [2, 3]}, {"foo": [2]}), | ||
("intersect_all", {"foo": [1, 2, None]}, {"foo": [2, 3]}, {"foo": [2]}), | ||
]) | ||
def test_intersect_with_nulls(make_df, op, left, right, expected): | ||
helper(make_df, op, left, right, expected) | ||
|
||
@pytest.mark.parametrize("op, left, right, expected", [ | ||
("except_distinct", {"foo": [1, 2, None]}, {"foo": [2, 3, None]}, {"foo": [1]}), | ||
("except_all", {"foo": [1, 2, None]}, {"foo": [2, 3, None]}, {"foo": [1]}), | ||
("except_distinct", {"foo": [1, 2]}, {"foo": [2, 3, None]}, {"foo": [1]}), | ||
("except_all", {"foo": [1, 2]}, {"foo": [2, 3, None]}, {"foo": [1]}), | ||
("except_distinct", {"foo": [1, 2, None]}, {"foo": [2, 3]}, {"foo": [1, None]}), | ||
("except_all", {"foo": [1, 2, None]}, {"foo": [2, 3]}, {"foo": [1, None]}), | ||
]) | ||
def test_except_with_nulls(make_df, op, left, right, expected): | ||
helper(make_df, op, left, right, expected) |