| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
np = pytest.importorskip("numpy") |
| 17 |
pd = pytest.importorskip("pandas") |
| 18 |
|
| 19 |
import dask |
| 20 |
import dask.dataframe as dd |
| 21 |
from dask.dataframe.utils import assert_eq |
| 22 |
|
| 23 |
from distributed.utils_test import gen_cluster |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def df_left(): |
| 30 |
|
| 31 |
|
| 32 |
partition_sizes = np.array([3, 4, 2, 5, 3, 2, 5, 9, 4, 7, 4]) |
| 33 |
idx = [i for i, s in enumerate(partition_sizes) for _ in range(s)] |
| 34 |
k = [i for s in partition_sizes for i in range(s)] |
| 35 |
vi = range(len(k)) |
| 36 |
|
| 37 |
return pd.DataFrame(dict(idx=idx, k=k, v1=vi)).set_index(["idx"]) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def df_right(): |
| 42 |
|
| 43 |
|
| 44 |
partition_sizes = np.array([4, 2, 5, 3, 2, 5, 9, 4, 7, 4, 8]) |
| 45 |
idx = [i for i, s in enumerate(partition_sizes) for _ in range(s)] |
| 46 |
k = [i for s in partition_sizes for i in range(s)] |
| 47 |
vi = range(len(k)) |
| 48 |
|
| 49 |
return pd.DataFrame(dict(idx=idx, k=k, v1=vi)).set_index(["idx"]) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture |
| 53 |
def ddf_left(df_left): |
| 54 |
|
| 55 |
|
| 56 |
return dd.repartition(df_left, [0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11]) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.fixture |
| 60 |
def ddf_left_unknown(ddf_left): |
| 61 |
return ddf_left.clear_divisions() |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture |
| 65 |
def ddf_left_single(df_left): |
| 66 |
return dd.from_pandas(df_left, npartitions=1, sort=False) |
| 67 |
|
| 68 |
|
| 69 |
@pytest.fixture |
| 70 |
def ddf_right(df_right): |
| 71 |
|
| 72 |
|
| 73 |
return dd.repartition(df_right, [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11]) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.fixture |
| 77 |
def ddf_right_unknown(ddf_right): |
| 78 |
return ddf_right.clear_divisions() |
| 79 |
|
| 80 |
|
| 81 |
@pytest.fixture |
| 82 |
def ddf_right_single(df_right): |
| 83 |
return dd.from_pandas(df_right, npartitions=1, sort=False) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.fixture |
| 87 |
def ddf_right_double(df_right): |
| 88 |
return dd.from_pandas(df_right, npartitions=2, sort=False) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.fixture |
| 92 |
def ddf_left_double(df_left): |
| 93 |
return dd.from_pandas(df_left, npartitions=2, sort=False) |
| 94 |
|
| 95 |
|
| 96 |
@pytest.fixture(params=["inner", "left", "right", "outer"]) |
| 97 |
def how(request): |
| 98 |
return request.param |
| 99 |
|
| 100 |
|
| 101 |
@pytest.fixture(params=["idx", ["idx"], ["idx", "k"], ["k", "idx"]]) |
| 102 |
def on(request): |
| 103 |
return request.param |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| •110 |
@pytest.importorskip("pyarrow") |
| 111 |
@gen_cluster(client=True) |
| 112 |
async def test_merge_known_to_unknown( |
| 113 |
c, |
| 114 |
s, |
| 115 |
a, |
| 116 |
b, |
| 117 |
df_left, |
| 118 |
df_right, |
| 119 |
ddf_left, |
| 120 |
ddf_right_unknown, |
| 121 |
on, |
| 122 |
how, |
| 123 |
): |
| 124 |
|
| 125 |
expected = df_left.merge(df_right, on=on, how=how) |
| 126 |
|
| 127 |
|
| 128 |
with dask.config.set({"dataframe.shuffle.method": "p2p"}): |
| 129 |
result_graph = ddf_left.merge(ddf_right_unknown, on=on, how=how) |
| 130 |
result = await c.compute(result_graph) |
| 131 |
|
| 132 |
assert_eq(result, expected) |
| 133 |
assert_eq(result_graph.divisions, tuple(None for _ in range(11))) |
| 134 |
|
| 135 |
|
| •136 |
@pytest.importorskip("pyarrow") |
| 137 |
@gen_cluster(client=True) |
| 138 |
async def test_merge_unknown_to_known( |
| 139 |
c, |
| 140 |
s, |
| 141 |
a, |
| 142 |
b, |
| 143 |
df_left, |
| 144 |
df_right, |
| 145 |
ddf_left_unknown, |
| 146 |
ddf_right, |
| 147 |
on, |
| 148 |
how, |
| 149 |
): |
| 150 |
|
| 151 |
expected = df_left.merge(df_right, on=on, how=how) |
| 152 |
|
| 153 |
|
| 154 |
with dask.config.set({"dataframe.shuffle.method": "p2p"}): |
| 155 |
result_graph = ddf_left_unknown.merge(ddf_right, on=on, how=how) |
| 156 |
result = await c.compute(result_graph) |
| 157 |
|
| 158 |
|
| 159 |
assert_eq(result, expected) |
| 160 |
assert_eq(result_graph.divisions, tuple(None for _ in range(11))) |
| 161 |
|
| 162 |
|
| •163 |
@pytest.importorskip("pyarrow") |
| 164 |
@gen_cluster(client=True) |
| 165 |
async def test_merge_unknown_to_unknown( |
| 166 |
c, |
| 167 |
s, |
| 168 |
a, |
| 169 |
b, |
| 170 |
df_left, |
| 171 |
df_right, |
| 172 |
ddf_left_unknown, |
| 173 |
ddf_right_unknown, |
| 174 |
on, |
| 175 |
how, |
| 176 |
): |
| 177 |
|
| 178 |
expected = df_left.merge(df_right, on=on, how=how) |
| 179 |
|
| 180 |
|
| 181 |
with dask.config.set({"dataframe.shuffle.method": "p2p"}): |
| 182 |
result_graph = ddf_left_unknown.merge(ddf_right_unknown, on=on, how=how) |
| 183 |
result = await c.compute(result_graph) |
| 184 |
|
| 185 |
assert_eq(result, expected) |
| 186 |
assert_eq(result_graph.divisions, tuple(None for _ in range(11))) |