-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtest_join.py
332 lines (285 loc) · 9.1 KB
/
test_join.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import numpy as np
import pandas as pd
import pytest
from pandas.testing import assert_frame_equal
def test_join(c):
df = c.sql(
"SELECT lhs.user_id, lhs.b, rhs.c FROM user_table_1 AS lhs JOIN user_table_2 AS rhs ON lhs.user_id = rhs.user_id"
)
df = df.compute()
expected_df = pd.DataFrame(
{"user_id": [1, 1, 2, 2], "b": [3, 3, 1, 3], "c": [1, 2, 3, 3]}
)
assert_frame_equal(
df.sort_values(["user_id", "b", "c"]).reset_index(drop=True), expected_df,
)
def test_join_inner(c):
df = c.sql(
"SELECT lhs.user_id, lhs.b, rhs.c FROM user_table_1 AS lhs INNER JOIN user_table_2 AS rhs ON lhs.user_id = rhs.user_id"
)
df = df.compute()
expected_df = pd.DataFrame(
{"user_id": [1, 1, 2, 2], "b": [3, 3, 1, 3], "c": [1, 2, 3, 3]}
)
assert_frame_equal(
df.sort_values(["user_id", "b", "c"]).reset_index(drop=True), expected_df,
)
def test_join_outer(c):
df = c.sql(
"SELECT lhs.user_id, lhs.b, rhs.c FROM user_table_1 AS lhs FULL JOIN user_table_2 AS rhs ON lhs.user_id = rhs.user_id"
)
df = df.compute()
expected_df = pd.DataFrame(
{
# That is strange. Unfortunately, it seems dask fills in the
# missing rows with NaN, not with NA...
"user_id": [1, 1, 2, 2, 3, np.NaN],
"b": [3, 3, 1, 3, 3, np.NaN],
"c": [1, 2, 3, 3, np.NaN, 4],
}
)
assert_frame_equal(
df.sort_values(["user_id", "b", "c"]).reset_index(drop=True), expected_df
)
def test_join_left(c):
df = c.sql(
"SELECT lhs.user_id, lhs.b, rhs.c FROM user_table_1 AS lhs LEFT JOIN user_table_2 AS rhs ON lhs.user_id = rhs.user_id"
)
df = df.compute()
expected_df = pd.DataFrame(
{
# That is strange. Unfortunately, it seems dask fills in the
# missing rows with NaN, not with NA...
"user_id": [1, 1, 2, 2, 3],
"b": [3, 3, 1, 3, 3],
"c": [1, 2, 3, 3, np.NaN],
}
)
assert_frame_equal(
df.sort_values(["user_id", "b", "c"]).reset_index(drop=True), expected_df,
)
def test_join_right(c):
df = c.sql(
"SELECT lhs.user_id, lhs.b, rhs.c FROM user_table_1 AS lhs RIGHT JOIN user_table_2 AS rhs ON lhs.user_id = rhs.user_id"
)
df = df.compute()
expected_df = pd.DataFrame(
{
# That is strange. Unfortunately, it seems dask fills in the
# missing rows with NaN, not with NA...
"user_id": [1, 1, 2, 2, np.NaN],
"b": [3, 3, 1, 3, np.NaN],
"c": [1, 2, 3, 3, 4],
}
)
assert_frame_equal(
df.sort_values(["user_id", "b", "c"]).reset_index(drop=True), expected_df,
)
def test_join_complex(c):
df = c.sql(
"SELECT lhs.a, rhs.b FROM df_simple AS lhs JOIN df_simple AS rhs ON lhs.a < rhs.b",
)
df = df.compute()
df_expected = pd.DataFrame(
{"a": [1, 1, 1, 2, 2, 3], "b": [1.1, 2.2, 3.3, 2.2, 3.3, 3.3]}
)
assert_frame_equal(df.sort_values(["a", "b"]).reset_index(drop=True), df_expected)
df = c.sql(
"""
SELECT lhs.a, lhs.b, rhs.a, rhs.b
FROM
df_simple AS lhs
JOIN df_simple AS rhs
ON lhs.a < rhs.b AND lhs.b < rhs.a
"""
)
df = df.compute()
df_expected = pd.DataFrame(
{"a": [1, 1, 2], "b": [1.1, 1.1, 2.2], "a0": [2, 3, 3], "b0": [2.2, 3.3, 3.3],}
)
assert_frame_equal(df.sort_values(["a", "b0"]).reset_index(drop=True), df_expected)
def test_join_complex_2(c):
df = c.sql(
"""
SELECT
lhs.user_id, lhs.b, rhs.user_id, rhs.c
FROM user_table_1 AS lhs
JOIN user_table_2 AS rhs
ON rhs.user_id = lhs.user_id AND rhs.c - lhs.b >= 0
"""
)
df = df.compute()
df_expected = pd.DataFrame(
{"user_id": [2, 2], "b": [1, 3], "user_id0": [2, 2], "c": [3, 3]}
)
assert_frame_equal(df.sort_values("b").reset_index(drop=True), df_expected)
def test_join_literal(c):
df = c.sql(
"""
SELECT
lhs.user_id, lhs.b, rhs.user_id, rhs.c
FROM user_table_1 AS lhs
JOIN user_table_2 AS rhs
ON True
"""
)
df = df.compute()
df_expected = pd.DataFrame(
{
"user_id": [2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
"b": [1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
"user_id0": [1, 1, 2, 4, 1, 1, 2, 4, 1, 1, 2, 4, 1, 1, 2, 4],
"c": [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4],
}
)
assert_frame_equal(
df.sort_values(["b", "user_id", "user_id0"]).reset_index(drop=True),
df_expected,
)
df = c.sql(
"""
SELECT
lhs.user_id, lhs.b, rhs.user_id, rhs.c
FROM user_table_1 AS lhs
JOIN user_table_2 AS rhs
ON False
"""
)
df = df.compute()
df_expected = pd.DataFrame({"user_id": [], "b": [], "user_id0": [], "c": []})
assert_frame_equal(df.reset_index(), df_expected.reset_index(), check_dtype=False)
def test_join_lricomplex(c):
# ---------- Panel data (equality and inequality conditions)
# Correct answer
dfcorrpn = pd.DataFrame(
[
[0, 1, pd.NA, pd.NA, pd.NA, pd.NA],
[1, 5, 32, 2, pd.NA, 112],
[1, 5, 32, 4, 13, 113],
[2, 1, 33, pd.NA, pd.NA, pd.NA],
],
columns=["ids", "dates", "pn_nullint", "startdate", "lk_nullint", "lk_int",],
)
change_types = {
"pn_nullint": "Int32",
"lk_nullint": "Int32",
"startdate": "Int64",
"lk_int": "Int64",
}
for k, v in change_types.items():
dfcorrpn[k] = dfcorrpn[k].astype(v)
# Left Join
querypnl = """
select a.*, b.startdate, b.lk_nullint, b.lk_int
from user_table_pn a left join user_table_lk b
on a.ids=b.id and b.startdate<=a.dates
"""
dftestpnl = (
c.sql(querypnl)
.compute()
.sort_values(["ids", "dates", "startdate"])
.reset_index(drop=True)
)
assert_frame_equal(dftestpnl, dfcorrpn, check_dtype=False)
# Right Join
querypnr = """
select b.*, a.startdate, a.lk_nullint, a.lk_int
from user_table_lk a right join user_table_pn b
on b.ids=a.id and a.startdate<=b.dates
"""
dftestpnr = (
c.sql(querypnr)
.compute()
.sort_values(["ids", "dates", "startdate"])
.reset_index(drop=True)
)
assert_frame_equal(dftestpnr, dfcorrpn, check_dtype=False)
# Inner Join
querypni = """
select a.*, b.startdate, b.lk_nullint, b.lk_int
from user_table_pn a inner join user_table_lk b
on a.ids=b.id and b.startdate<=a.dates
"""
dftestpni = (
c.sql(querypni)
.compute()
.sort_values(["ids", "dates", "startdate"])
.reset_index(drop=True)
)
assert_frame_equal(
dftestpni,
dfcorrpn.dropna(subset=["startdate"])
.assign(
startdate=lambda x: x["startdate"].astype("int64"),
lk_int=lambda x: x["lk_int"].astype("int64"),
)
.reset_index(drop=True),
check_dtype=False,
)
# ---------- Time-series data (inequality condition only)
# # Correct answer
dfcorrts = pd.DataFrame(
[
[1, 21, pd.NA, pd.NA, pd.NA],
[3, pd.NA, 2, pd.NA, 112],
[7, 23, 2, pd.NA, 112],
[7, 23, 4, 13, 113],
],
columns=["dates", "ts_nullint", "startdate", "lk_nullint", "lk_int",],
)
change_types = {
"ts_nullint": "Int32",
"lk_nullint": "Int32",
"startdate": "Int64",
"lk_int": "Int64",
}
for k, v in change_types.items():
dfcorrts[k] = dfcorrts[k].astype(v)
# Left Join
querytsl = """
select a.*, b.startdate, b.lk_nullint, b.lk_int
from user_table_ts a left join user_table_lk2 b
on b.startdate<=a.dates
"""
dftesttsl = (
c.sql(querytsl)
.compute()
.sort_values(["dates", "startdate"])
.reset_index(drop=True)
)
assert_frame_equal(dftesttsl, dfcorrts, check_dtype=False)
# Right Join
querytsr = """
select b.*, a.startdate, a.lk_nullint, a.lk_int
from user_table_lk2 a right join user_table_ts b
on a.startdate<=b.dates
"""
dftesttsr = (
c.sql(querytsr)
.compute()
.sort_values(["dates", "startdate"])
.reset_index(drop=True)
)
assert_frame_equal(dftesttsr, dfcorrts, check_dtype=False)
# Inner Join
querytsi = """
select a.*, b.startdate, b.lk_nullint, b.lk_int
from user_table_ts a inner join user_table_lk2 b
on b.startdate<=a.dates
"""
dftesttsi = (
c.sql(querytsi)
.compute()
.sort_values(["dates", "startdate"])
.reset_index(drop=True)
)
assert_frame_equal(
dftesttsi,
dfcorrts.dropna(subset=["startdate"])
.assign(
startdate=lambda x: x["startdate"].astype("int64"),
lk_int=lambda x: x["lk_int"].astype("int64"),
)
.reset_index(drop=True),
check_dtype=False,
)