| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
|
| 3 |
from petl import ( |
| 4 |
antijoin, |
| 5 |
crossjoin, |
| 6 |
cut, |
| 7 |
hashantijoin, |
| 8 |
hashjoin, |
| 9 |
hashleftjoin, |
| 10 |
hashlookupjoin, |
| 11 |
hashrightjoin, |
| 12 |
join, |
| 13 |
leftjoin, |
| 14 |
lookupjoin, |
| 15 |
outerjoin, |
| 16 |
rightjoin, |
| 17 |
sort, |
| 18 |
unjoin, |
| 19 |
) |
| 20 |
from petl.test.helpers import ieq |
| 21 |
|
| 22 |
|
| 23 |
def _test_join_basic(join_impl): |
| 24 |
|
| 25 |
table1 = (('id', 'colour'), |
| 26 |
(1, 'blue'), |
| 27 |
(2, 'red'), |
| 28 |
(3, 'purple')) |
| 29 |
table2 = (('id', 'shape'), |
| 30 |
(1, 'circle'), |
| 31 |
(3, 'square'), |
| 32 |
(4, 'ellipse')) |
| 33 |
|
| 34 |
# normal inner join |
| 35 |
table3 = join_impl(table1, table2, key='id') |
| 36 |
expect3 = (('id', 'colour', 'shape'), |
| 37 |
(1, 'blue', 'circle'), |
| 38 |
(3, 'purple', 'square')) |
| 39 |
ieq(expect3, table3) |
| 40 |
ieq(expect3, table3) # check twice |
| 41 |
|
| 42 |
# natural join |
| 43 |
table4 = join_impl(table1, table2) |
| 44 |
expect4 = expect3 |
| 45 |
ieq(expect4, table4) |
| 46 |
ieq(expect4, table4) # check twice |
| 47 |
|
| 48 |
# multiple rows for each key |
| 49 |
table5 = (('id', 'colour'), |
| 50 |
(1, 'blue'), |
| 51 |
(1, 'red'), |
| 52 |
(2, 'purple')) |
| 53 |
table6 = (('id', 'shape'), |
| 54 |
(1, 'circle'), |
| 55 |
(1, 'square'), |
| 56 |
(2, 'ellipse')) |
| 57 |
table7 = join_impl(table5, table6, key='id') |
| 58 |
expect7 = (('id', 'colour', 'shape'), |
| 59 |
(1, 'blue', 'circle'), |
| 60 |
(1, 'blue', 'square'), |
| 61 |
(1, 'red', 'circle'), |
| 62 |
(1, 'red', 'square'), |
| 63 |
(2, 'purple', 'ellipse')) |
| 64 |
ieq(expect7, table7) |
| 65 |
|
| 66 |
|
| 67 |
def _test_join_compound_keys(join_impl): |
| 68 |
|
| 69 |
# compound keys |
| 70 |
table8 = (('id', 'time', 'height'), |
| 71 |
(1, 1, 12.3), |
| 72 |
(1, 2, 34.5), |
| 73 |
(2, 1, 56.7)) |
| 74 |
table9 = (('id', 'time', 'weight'), |
| 75 |
(1, 2, 4.5), |
| 76 |
(2, 1, 6.7), |
| 77 |
(2, 2, 8.9)) |
| 78 |
table10 = join_impl(table8, table9, key=['id', 'time']) |
| 79 |
expect10 = (('id', 'time', 'height', 'weight'), |
| 80 |
(1, 2, 34.5, 4.5), |
| 81 |
(2, 1, 56.7, 6.7)) |
| 82 |
ieq(expect10, table10) |
| 83 |
|
| 84 |
# natural join on compound key |
| 85 |
table11 = join_impl(table8, table9) |
| 86 |
expect11 = expect10 |
| 87 |
ieq(expect11, table11) |
| 88 |
|
| 89 |
|
| 90 |
def _test_join_string_key(join_impl): |
| 91 |
|
| 92 |
table1 = (('id', 'colour'), |
| 93 |
('aa', 'blue'), |
| 94 |
('bb', 'red'), |
| 95 |
('cc', 'purple')) |
| 96 |
table2 = (('id', 'shape'), |
| 97 |
('aa', 'circle'), |
| 98 |
('cc', 'square'), |
| 99 |
('dd', 'ellipse')) |
| 100 |
|
| 101 |
# normal inner join |
| 102 |
table3 = join_impl(table1, table2, key='id') |
| 103 |
expect3 = (('id', 'colour', 'shape'), |
| 104 |
('aa', 'blue', 'circle'), |
| 105 |
('cc', 'purple', 'square')) |
| 106 |
ieq(expect3, table3) |
| 107 |
ieq(expect3, table3) # check twice |
| 108 |
|
| 109 |
|
| 110 |
def _test_join_empty(join_impl): |
| 111 |
|
| 112 |
table1 = (('id', 'colour'), |
| 113 |
(1, 'blue'), |
| 114 |
(2, 'red'), |
| 115 |
(3, 'purple')) |
| 116 |
table2 = (('id', 'shape'),) |
| 117 |
table3 = join_impl(table1, table2, key='id') |
| 118 |
expect3 = (('id', 'colour', 'shape'),) |
| 119 |
ieq(expect3, table3) |
| 120 |
|
| 121 |
table1 = (('id', 'colour'),) |
| 122 |
table2 = (('id', 'shape'), |
| 123 |
(1, 'circle'), |
| 124 |
(3, 'square'), |
| 125 |
(4, 'ellipse')) |
| 126 |
table3 = join_impl(table1, table2, key='id') |
| 127 |
expect3 = (('id', 'colour', 'shape'),) |
| 128 |
ieq(expect3, table3) |
| 129 |
|
| 130 |
|
| 131 |
def _test_join_novaluefield(join_impl): |
| 132 |
|
| 133 |
table1 = (('id', 'colour'), |
| 134 |
(1, 'blue'), |
| 135 |
(2, 'red'), |
| 136 |
(3, 'purple')) |
| 137 |
table2 = (('id', 'shape'), |
| 138 |
(1, 'circle'), |
| 139 |
(3, 'square'), |
| 140 |
(4, 'ellipse')) |
| 141 |
|
| 142 |
expect = (('id', 'colour', 'shape'), |
| 143 |
(1, 'blue', 'circle'), |
| 144 |
(3, 'purple', 'square')) |
| 145 |
|
| 146 |
actual = join_impl(table1, table2, key='id') |
| 147 |
ieq(expect, actual) |
| 148 |
actual = join_impl(cut(table1, 'id'), table2, key='id') |
| 149 |
ieq(cut(expect, 'id', 'shape'), actual) |
| 150 |
actual = join_impl(table1, cut(table2, 'id'), key='id') |
| 151 |
ieq(cut(expect, 'id', 'colour'), actual) |
| 152 |
actual = join_impl(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 153 |
ieq(cut(expect, 'id'), actual) |
| 154 |
|
| 155 |
|
| 156 |
def _test_join_prefix(join_impl): |
| 157 |
|
| 158 |
table1 = (('id', 'colour'), |
| 159 |
('aa', 'blue'), |
| 160 |
('bb', 'red'), |
| 161 |
('cc', 'purple')) |
| 162 |
table2 = (('id', 'shape'), |
| 163 |
('aa', 'circle'), |
| 164 |
('cc', 'square'), |
| 165 |
('dd', 'ellipse')) |
| 166 |
|
| 167 |
table3 = join_impl(table1, table2, key='id', lprefix='l_', rprefix='r_') |
| 168 |
expect3 = (('l_id', 'l_colour', 'r_shape'), |
| 169 |
('aa', 'blue', 'circle'), |
| 170 |
('cc', 'purple', 'square')) |
| 171 |
ieq(expect3, table3) |
| 172 |
|
| 173 |
|
| 174 |
def _test_join_lrkey(join_impl): |
| 175 |
|
| 176 |
table1 = (('id', 'colour'), |
| 177 |
('aa', 'blue'), |
| 178 |
('bb', 'red'), |
| 179 |
('cc', 'purple')) |
| 180 |
table2 = (('identifier', 'shape'), |
| 181 |
('aa', 'circle'), |
| 182 |
('cc', 'square'), |
| 183 |
('dd', 'ellipse')) |
| 184 |
|
| 185 |
table3 = join_impl(table1, table2, lkey='id', rkey='identifier') |
| 186 |
expect3 = (('id', 'colour', 'shape'), |
| 187 |
('aa', 'blue', 'circle'), |
| 188 |
('cc', 'purple', 'square')) |
| 189 |
ieq(expect3, table3) |
| 190 |
|
| 191 |
|
| 192 |
def _test_join_multiple(join_impl): |
| 193 |
|
| 194 |
table1 = (('id', 'color', 'cost'), |
| 195 |
(1, 'blue', 12), |
| 196 |
(1, 'red', 8), |
| 197 |
(2, 'yellow', 15), |
| 198 |
(2, 'orange', 5), |
| 199 |
(3, 'purple', 4), |
| 200 |
(4, 'chartreuse', 42)) |
| 201 |
|
| 202 |
table2 = (('id', 'shape', 'size'), |
| 203 |
(1, 'circle', 'big'), |
| 204 |
(2, 'square', 'tiny'), |
| 205 |
(2, 'square', 'big'), |
| 206 |
(3, 'ellipse', 'small'), |
| 207 |
(3, 'ellipse', 'tiny'), |
| 208 |
(5, 'didodecahedron', 3.14159265)) |
| 209 |
|
| 210 |
actual = join_impl(table1, table2, key='id') |
| 211 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 212 |
(1, 'blue', 12, 'circle', 'big'), |
| 213 |
(1, 'red', 8, 'circle', 'big'), |
| 214 |
(2, 'yellow', 15, 'square', 'tiny'), |
| 215 |
(2, 'yellow', 15, 'square', 'big'), |
| 216 |
(2, 'orange', 5, 'square', 'tiny'), |
| 217 |
(2, 'orange', 5, 'square', 'big'), |
| 218 |
(3, 'purple', 4, 'ellipse', 'small'), |
| 219 |
(3, 'purple', 4, 'ellipse', 'tiny')) |
| 220 |
ieq(expect, actual) |
| 221 |
|
| 222 |
|
| 223 |
def _test_join_uneven_rows(join_impl): |
| 224 |
table1 = (('id', 'colour'), |
| 225 |
('aa',), |
| 226 |
('bb', 'red'), |
| 227 |
('cc', 'purple')) |
| 228 |
table2 = (('identifier', 'shape'), |
| 229 |
('aa', 'circle'), |
| 230 |
('cc',), |
| 231 |
('dd', 'ellipse')) |
| 232 |
|
| 233 |
table3 = join_impl(table1, table2, lkey='id', rkey='identifier') |
| 234 |
expect3 = (('id', 'colour', 'shape'), |
| 235 |
('aa', None, 'circle'), |
| 236 |
('cc', 'purple', None)) |
| 237 |
ieq(expect3, table3) |
| 238 |
|
| 239 |
|
| 240 |
def _test_join(join_impl): |
| 241 |
_test_join_basic(join_impl) |
| 242 |
_test_join_compound_keys(join_impl) |
| 243 |
_test_join_string_key(join_impl) |
| 244 |
_test_join_empty(join_impl) |
| 245 |
_test_join_novaluefield(join_impl) |
| 246 |
_test_join_prefix(join_impl) |
| 247 |
_test_join_lrkey(join_impl) |
| 248 |
_test_join_multiple(join_impl) |
| 249 |
_test_join_uneven_rows(join_impl) |
| 250 |
|
| 251 |
|
| 252 |
def test_join(): |
| 253 |
_test_join(join) |
| 254 |
|
| 255 |
|
| 256 |
def _test_leftjoin_1(leftjoin_impl): |
| 257 |
|
| 258 |
table1 = (('id', 'colour'), |
| 259 |
(1, 'blue'), |
| 260 |
(2, 'red'), |
| 261 |
(3, 'purple'), |
| 262 |
(5, 'yellow'), |
| 263 |
(7, 'orange')) |
| 264 |
table2 = (('id', 'shape'), |
| 265 |
(1, 'circle'), |
| 266 |
(3, 'square'), |
| 267 |
(4, 'ellipse')) |
| 268 |
table3 = leftjoin_impl(table1, table2, key='id') |
| 269 |
expect3 = (('id', 'colour', 'shape'), |
| 270 |
(1, 'blue', 'circle'), |
| 271 |
(2, 'red', None), |
| 272 |
(3, 'purple', 'square'), |
| 273 |
(5, 'yellow', None,), |
| 274 |
(7, 'orange', None)) |
| 275 |
ieq(expect3, table3) |
| 276 |
ieq(expect3, table3) # check twice |
| 277 |
|
| 278 |
# natural join |
| 279 |
table4 = leftjoin_impl(table1, table2) |
| 280 |
expect4 = expect3 |
| 281 |
ieq(expect4, table4) |
| 282 |
|
| 283 |
|
| 284 |
def _test_leftjoin_2(leftjoin_impl): |
| 285 |
|
| 286 |
table1 = (('id', 'colour'), |
| 287 |
(1, 'blue'), |
| 288 |
(2, 'red'), |
| 289 |
(3, 'purple'), |
| 290 |
(5, 'yellow'), |
| 291 |
(7, 'orange')) |
| 292 |
table2 = (('id', 'shape'), |
| 293 |
(1, 'circle'), |
| 294 |
(3, 'square')) |
| 295 |
table3 = leftjoin_impl(table1, table2, key='id') |
| 296 |
expect3 = (('id', 'colour', 'shape'), |
| 297 |
(1, 'blue', 'circle'), |
| 298 |
(2, 'red', None), |
| 299 |
(3, 'purple', 'square'), |
| 300 |
(5, 'yellow', None,), |
| 301 |
(7, 'orange', None)) |
| 302 |
ieq(expect3, table3) |
| 303 |
ieq(expect3, table3) # check twice |
| 304 |
|
| 305 |
# natural join |
| 306 |
table4 = leftjoin_impl(table1, table2) |
| 307 |
expect4 = expect3 |
| 308 |
ieq(expect4, table4) |
| 309 |
|
| 310 |
|
| 311 |
def _test_leftjoin_3(leftjoin_impl): |
| 312 |
|
| 313 |
table1 = (('id', 'colour'), |
| 314 |
(1, 'blue'), |
| 315 |
(2, 'red'), |
| 316 |
(3, 'purple')) |
| 317 |
table2 = (('id', 'shape'), |
| 318 |
(1, 'circle'), |
| 319 |
(3, 'square'), |
| 320 |
(4, 'ellipse'), |
| 321 |
(5, 'triangle')) |
| 322 |
table3 = leftjoin_impl(table1, table2, key='id') |
| 323 |
expect3 = (('id', 'colour', 'shape'), |
| 324 |
(1, 'blue', 'circle'), |
| 325 |
(2, 'red', None), |
| 326 |
(3, 'purple', 'square')) |
| 327 |
ieq(expect3, table3) |
| 328 |
ieq(expect3, table3) # check twice |
| 329 |
|
| 330 |
# natural join |
| 331 |
table4 = leftjoin_impl(table1, table2) |
| 332 |
expect4 = expect3 |
| 333 |
ieq(expect4, table4) |
| 334 |
|
| 335 |
|
| 336 |
def _test_leftjoin_compound_keys(leftjoin_impl): |
| 337 |
|
| 338 |
# compound keys |
| 339 |
table5 = (('id', 'time', 'height'), |
| 340 |
(1, 1, 12.3), |
| 341 |
(1, 2, 34.5), |
| 342 |
(2, 1, 56.7)) |
| 343 |
table6 = (('id', 'time', 'weight', 'bp'), |
| 344 |
(1, 2, 4.5, 120), |
| 345 |
(2, 1, 6.7, 110), |
| 346 |
(2, 2, 8.9, 100)) |
| 347 |
table7 = leftjoin_impl(table5, table6, key=['id', 'time']) |
| 348 |
expect7 = (('id', 'time', 'height', 'weight', 'bp'), |
| 349 |
(1, 1, 12.3, None, None), |
| 350 |
(1, 2, 34.5, 4.5, 120), |
| 351 |
(2, 1, 56.7, 6.7, 110)) |
| 352 |
ieq(expect7, table7) |
| 353 |
|
| 354 |
|
| 355 |
def _test_leftjoin_empty(leftjoin_impl): |
| 356 |
|
| 357 |
table1 = (('id', 'colour'), |
| 358 |
(1, 'blue'), |
| 359 |
(2, 'red'), |
| 360 |
(3, 'purple'), |
| 361 |
(5, 'yellow'), |
| 362 |
(7, 'orange')) |
| 363 |
table2 = (('id', 'shape'),) |
| 364 |
table3 = leftjoin_impl(table1, table2, key='id') |
| 365 |
expect3 = (('id', 'colour', 'shape'), |
| 366 |
(1, 'blue', None), |
| 367 |
(2, 'red', None), |
| 368 |
(3, 'purple', None), |
| 369 |
(5, 'yellow', None,), |
| 370 |
(7, 'orange', None)) |
| 371 |
ieq(expect3, table3) |
| 372 |
|
| 373 |
|
| 374 |
def _test_leftjoin_novaluefield(leftjoin_impl): |
| 375 |
|
| 376 |
table1 = (('id', 'colour'), |
| 377 |
(1, 'blue'), |
| 378 |
(2, 'red'), |
| 379 |
(3, 'purple'), |
| 380 |
(5, 'yellow'), |
| 381 |
(7, 'orange')) |
| 382 |
table2 = (('id', 'shape'), |
| 383 |
(1, 'circle'), |
| 384 |
(3, 'square'), |
| 385 |
(4, 'ellipse')) |
| 386 |
expect = (('id', 'colour', 'shape'), |
| 387 |
(1, 'blue', 'circle'), |
| 388 |
(2, 'red', None), |
| 389 |
(3, 'purple', 'square'), |
| 390 |
(5, 'yellow', None,), |
| 391 |
(7, 'orange', None)) |
| 392 |
|
| 393 |
actual = leftjoin_impl(table1, table2, key='id') |
| 394 |
ieq(expect, actual) |
| 395 |
actual = leftjoin_impl(cut(table1, 'id'), table2, key='id') |
| 396 |
ieq(cut(expect, 'id', 'shape'), actual) |
| 397 |
actual = leftjoin_impl(table1, cut(table2, 'id'), key='id') |
| 398 |
ieq(cut(expect, 'id', 'colour'), actual) |
| 399 |
actual = leftjoin_impl(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 400 |
ieq(cut(expect, 'id'), actual) |
| 401 |
|
| 402 |
|
| 403 |
def _test_leftjoin_multiple(leftjoin_impl): |
| 404 |
|
| 405 |
table1 = (('id', 'color', 'cost'), |
| 406 |
(1, 'blue', 12), |
| 407 |
(1, 'red', 8), |
| 408 |
(2, 'yellow', 15), |
| 409 |
(2, 'orange', 5), |
| 410 |
(3, 'purple', 4), |
| 411 |
(4, 'chartreuse', 42)) |
| 412 |
|
| 413 |
table2 = (('id', 'shape', 'size'), |
| 414 |
(1, 'circle', 'big'), |
| 415 |
(2, 'square', 'tiny'), |
| 416 |
(2, 'square', 'big'), |
| 417 |
(3, 'ellipse', 'small'), |
| 418 |
(3, 'ellipse', 'tiny'), |
| 419 |
(5, 'didodecahedron', 3.14159265)) |
| 420 |
|
| 421 |
actual = leftjoin_impl(table1, table2, key='id') |
| 422 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 423 |
(1, 'blue', 12, 'circle', 'big'), |
| 424 |
(1, 'red', 8, 'circle', 'big'), |
| 425 |
(2, 'yellow', 15, 'square', 'tiny'), |
| 426 |
(2, 'yellow', 15, 'square', 'big'), |
| 427 |
(2, 'orange', 5, 'square', 'tiny'), |
| 428 |
(2, 'orange', 5, 'square', 'big'), |
| 429 |
(3, 'purple', 4, 'ellipse', 'small'), |
| 430 |
(3, 'purple', 4, 'ellipse', 'tiny'), |
| 431 |
(4, 'chartreuse', 42, None, None)) |
| 432 |
ieq(expect, actual) |
| 433 |
|
| 434 |
|
| 435 |
def _test_leftjoin_prefix(leftjoin_impl): |
| 436 |
table1 = (('id', 'colour'), |
| 437 |
(1, 'blue'), |
| 438 |
(2, 'red'), |
| 439 |
(3, 'purple'), |
| 440 |
(5, 'yellow'), |
| 441 |
(7, 'orange')) |
| 442 |
table2 = (('id', 'shape'), |
| 443 |
(1, 'circle'), |
| 444 |
(3, 'square'), |
| 445 |
(4, 'ellipse')) |
| 446 |
table3 = leftjoin_impl(table1, table2, key='id', lprefix='l_', rprefix='r_') |
| 447 |
expect3 = (('l_id', 'l_colour', 'r_shape'), |
| 448 |
(1, 'blue', 'circle'), |
| 449 |
(2, 'red', None), |
| 450 |
(3, 'purple', 'square'), |
| 451 |
(5, 'yellow', None,), |
| 452 |
(7, 'orange', None)) |
| 453 |
ieq(expect3, table3) |
| 454 |
|
| 455 |
|
| 456 |
def _test_leftjoin_lrkey(leftjoin_impl): |
| 457 |
table1 = (('id', 'colour'), |
| 458 |
(1, 'blue'), |
| 459 |
(2, 'red'), |
| 460 |
(3, 'purple'), |
| 461 |
(5, 'yellow'), |
| 462 |
(7, 'orange')) |
| 463 |
table2 = (('identifier', 'shape'), |
| 464 |
(1, 'circle'), |
| 465 |
(3, 'square'), |
| 466 |
(4, 'ellipse')) |
| 467 |
table3 = leftjoin_impl(table1, table2, lkey='id', rkey='identifier') |
| 468 |
expect3 = (('id', 'colour', 'shape'), |
| 469 |
(1, 'blue', 'circle'), |
| 470 |
(2, 'red', None), |
| 471 |
(3, 'purple', 'square'), |
| 472 |
(5, 'yellow', None,), |
| 473 |
(7, 'orange', None)) |
| 474 |
ieq(expect3, table3) |
| 475 |
|
| 476 |
|
| 477 |
def _test_leftjoin(leftjoin_impl): |
| 478 |
_test_leftjoin_1(leftjoin_impl) |
| 479 |
_test_leftjoin_2(leftjoin_impl) |
| 480 |
_test_leftjoin_3(leftjoin_impl) |
| 481 |
_test_leftjoin_compound_keys(leftjoin_impl) |
| 482 |
_test_leftjoin_empty(leftjoin_impl) |
| 483 |
_test_leftjoin_novaluefield(leftjoin_impl) |
| 484 |
_test_leftjoin_multiple(leftjoin_impl) |
| 485 |
_test_leftjoin_prefix(leftjoin_impl) |
| 486 |
_test_leftjoin_lrkey(leftjoin_impl) |
| 487 |
|
| 488 |
|
| 489 |
def test_leftjoin(): |
| 490 |
_test_leftjoin(leftjoin) |
| 491 |
|
| 492 |
|
| 493 |
def _test_rightjoin_1(rightjoin_impl): |
| 494 |
|
| 495 |
table1 = (('id', 'colour'), |
| 496 |
(1, 'blue'), |
| 497 |
(2, 'red'), |
| 498 |
(3, 'purple')) |
| 499 |
table2 = (('id', 'shape'), |
| 500 |
(0, 'triangle'), |
| 501 |
(1, 'circle'), |
| 502 |
(3, 'square'), |
| 503 |
(4, 'ellipse'), |
| 504 |
(5, 'pentagon')) |
| 505 |
table3 = rightjoin_impl(table1, table2, key='id') |
| 506 |
expect3 = (('id', 'colour', 'shape'), |
| 507 |
(0, None, 'triangle'), |
| 508 |
(1, 'blue', 'circle'), |
| 509 |
(3, 'purple', 'square'), |
| 510 |
(4, None, 'ellipse'), |
| 511 |
(5, None, 'pentagon')) |
| 512 |
ieq(expect3, table3) |
| 513 |
ieq(expect3, table3) # check twice |
| 514 |
|
| 515 |
# natural join |
| 516 |
table4 = rightjoin_impl(table1, table2) |
| 517 |
expect4 = expect3 |
| 518 |
ieq(expect4, table4) |
| 519 |
|
| 520 |
|
| 521 |
def _test_rightjoin_2(rightjoin_impl): |
| 522 |
|
| 523 |
table1 = (('id', 'colour'), |
| 524 |
(0, 'black'), |
| 525 |
(1, 'blue'), |
| 526 |
(2, 'red'), |
| 527 |
(3, 'purple'), |
| 528 |
(5, 'yellow'), |
| 529 |
(7, 'white')) |
| 530 |
table2 = (('id', 'shape'), |
| 531 |
(1, 'circle'), |
| 532 |
(3, 'square'), |
| 533 |
(4, 'ellipse')) |
| 534 |
table3 = rightjoin_impl(table1, table2, key='id') |
| 535 |
expect3 = (('id', 'colour', 'shape'), |
| 536 |
(1, 'blue', 'circle'), |
| 537 |
(3, 'purple', 'square'), |
| 538 |
(4, None, 'ellipse')) |
| 539 |
ieq(expect3, table3) |
| 540 |
ieq(expect3, table3) # check twice |
| 541 |
|
| 542 |
# natural join |
| 543 |
table4 = rightjoin_impl(table1, table2) |
| 544 |
expect4 = expect3 |
| 545 |
ieq(expect4, table4) |
| 546 |
|
| 547 |
|
| 548 |
def _test_rightjoin_3(rightjoin_impl): |
| 549 |
|
| 550 |
table1 = (('id', 'colour'), |
| 551 |
(1, 'blue'), |
| 552 |
(2, 'red'), |
| 553 |
(3, 'purple'), |
| 554 |
(4, 'orange')) |
| 555 |
table2 = (('id', 'shape'), |
| 556 |
(0, 'triangle'), |
| 557 |
(1, 'circle'), |
| 558 |
(3, 'square'), |
| 559 |
(5, 'ellipse'), |
| 560 |
(7, 'pentagon')) |
| 561 |
table3 = rightjoin_impl(table1, table2, key='id') |
| 562 |
expect3 = (('id', 'colour', 'shape'), |
| 563 |
(0, None, 'triangle'), |
| 564 |
(1, 'blue', 'circle'), |
| 565 |
(3, 'purple', 'square'), |
| 566 |
(5, None, 'ellipse'), |
| 567 |
(7, None, 'pentagon')) |
| 568 |
ieq(expect3, table3) |
| 569 |
ieq(expect3, table3) # check twice |
| 570 |
|
| 571 |
# natural join |
| 572 |
table4 = rightjoin_impl(table1, table2) |
| 573 |
expect4 = expect3 |
| 574 |
ieq(expect4, table4) |
| 575 |
|
| 576 |
|
| 577 |
def _test_rightjoin_empty(rightjoin_impl): |
| 578 |
|
| 579 |
table1 = (('id', 'colour'),) |
| 580 |
table2 = (('id', 'shape'), |
| 581 |
(0, 'triangle'), |
| 582 |
(1, 'circle'), |
| 583 |
(3, 'square'), |
| 584 |
(4, 'ellipse'), |
| 585 |
(5, 'pentagon')) |
| 586 |
table3 = rightjoin_impl(table1, table2, key='id') |
| 587 |
expect3 = (('id', 'colour', 'shape'), |
| 588 |
(0, None, 'triangle'), |
| 589 |
(1, None, 'circle'), |
| 590 |
(3, None, 'square'), |
| 591 |
(4, None, 'ellipse'), |
| 592 |
(5, None, 'pentagon')) |
| 593 |
ieq(expect3, table3) |
| 594 |
|
| 595 |
|
| 596 |
def _test_rightjoin_novaluefield(rightjoin_impl): |
| 597 |
table1 = (('id', 'colour'), |
| 598 |
(1, 'blue'), |
| 599 |
(2, 'red'), |
| 600 |
(3, 'purple')) |
| 601 |
table2 = (('id', 'shape'), |
| 602 |
(0, 'triangle'), |
| 603 |
(1, 'circle'), |
| 604 |
(3, 'square'), |
| 605 |
(4, 'ellipse'), |
| 606 |
(5, 'pentagon')) |
| 607 |
expect = (('id', 'colour', 'shape'), |
| 608 |
(0, None, 'triangle'), |
| 609 |
(1, 'blue', 'circle'), |
| 610 |
(3, 'purple', 'square'), |
| 611 |
(4, None, 'ellipse'), |
| 612 |
(5, None, 'pentagon')) |
| 613 |
|
| 614 |
actual = rightjoin_impl(table1, table2, key='id') |
| 615 |
ieq(expect, actual) |
| 616 |
actual = rightjoin_impl(cut(table1, 'id'), table2, key='id') |
| 617 |
ieq(cut(expect, 'id', 'shape'), actual) |
| 618 |
actual = rightjoin_impl(table1, cut(table2, 'id'), key='id') |
| 619 |
ieq(cut(expect, 'id', 'colour'), actual) |
| 620 |
actual = rightjoin_impl(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 621 |
ieq(cut(expect, 'id'), actual) |
| 622 |
|
| 623 |
|
| 624 |
def _test_rightjoin_prefix(rightjoin_impl): |
| 625 |
table1 = (('id', 'colour'), |
| 626 |
(1, 'blue'), |
| 627 |
(2, 'red'), |
| 628 |
(3, 'purple')) |
| 629 |
table2 = (('id', 'shape'), |
| 630 |
(0, 'triangle'), |
| 631 |
(1, 'circle'), |
| 632 |
(3, 'square'), |
| 633 |
(4, 'ellipse'), |
| 634 |
(5, 'pentagon')) |
| 635 |
table3 = rightjoin_impl(table1, table2, key='id', lprefix='l_', |
| 636 |
rprefix='r_') |
| 637 |
expect3 = (('l_id', 'l_colour', 'r_shape'), |
| 638 |
(0, None, 'triangle'), |
| 639 |
(1, 'blue', 'circle'), |
| 640 |
(3, 'purple', 'square'), |
| 641 |
(4, None, 'ellipse'), |
| 642 |
(5, None, 'pentagon')) |
| 643 |
ieq(expect3, table3) |
| 644 |
|
| 645 |
|
| 646 |
def _test_rightjoin_lrkey(rightjoin_impl): |
| 647 |
table1 = (('id', 'colour'), |
| 648 |
(1, 'blue'), |
| 649 |
(2, 'red'), |
| 650 |
(3, 'purple')) |
| 651 |
table2 = (('identifier', 'shape'), |
| 652 |
(0, 'triangle'), |
| 653 |
(1, 'circle'), |
| 654 |
(3, 'square'), |
| 655 |
(4, 'ellipse'), |
| 656 |
(5, 'pentagon')) |
| 657 |
table3 = rightjoin_impl(table1, table2, lkey='id', rkey='identifier') |
| 658 |
expect3 = (('id', 'colour', 'shape'), |
| 659 |
(0, None, 'triangle'), |
| 660 |
(1, 'blue', 'circle'), |
| 661 |
(3, 'purple', 'square'), |
| 662 |
(4, None, 'ellipse'), |
| 663 |
(5, None, 'pentagon')) |
| 664 |
ieq(expect3, table3) |
| 665 |
|
| 666 |
|
| 667 |
def _test_rightjoin_multiple(rightjoin_impl): |
| 668 |
|
| 669 |
table1 = (('id', 'color', 'cost'), |
| 670 |
(1, 'blue', 12), |
| 671 |
(1, 'red', 8), |
| 672 |
(2, 'yellow', 15), |
| 673 |
(2, 'orange', 5), |
| 674 |
(3, 'purple', 4), |
| 675 |
(4, 'chartreuse', 42)) |
| 676 |
|
| 677 |
table2 = (('id', 'shape', 'size'), |
| 678 |
(1, 'circle', 'big'), |
| 679 |
(2, 'square', 'tiny'), |
| 680 |
(2, 'square', 'big'), |
| 681 |
(3, 'ellipse', 'small'), |
| 682 |
(3, 'ellipse', 'tiny'), |
| 683 |
(5, 'didodecahedron', 3.14159265)) |
| 684 |
|
| 685 |
actual = rightjoin_impl(table1, table2, key='id') |
| 686 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 687 |
(1, 'blue', 12, 'circle', 'big'), |
| 688 |
(1, 'red', 8, 'circle', 'big'), |
| 689 |
(2, 'yellow', 15, 'square', 'tiny'), |
| 690 |
(2, 'yellow', 15, 'square', 'big'), |
| 691 |
(2, 'orange', 5, 'square', 'tiny'), |
| 692 |
(2, 'orange', 5, 'square', 'big'), |
| 693 |
(3, 'purple', 4, 'ellipse', 'small'), |
| 694 |
(3, 'purple', 4, 'ellipse', 'tiny'), |
| 695 |
(5, None, None, 'didodecahedron', 3.14159265)) |
| 696 |
|
| 697 |
# N.B., need to sort because hash and sort implementations will return |
| 698 |
# rows in a different order |
| 699 |
ieq(sort(expect), sort(actual)) |
| 700 |
|
| 701 |
|
| 702 |
def _test_rightjoin(rightjoin_impl): |
| 703 |
_test_rightjoin_1(rightjoin_impl) |
| 704 |
_test_rightjoin_2(rightjoin_impl) |
| 705 |
_test_rightjoin_3(rightjoin_impl) |
| 706 |
_test_rightjoin_empty(rightjoin_impl) |
| 707 |
_test_rightjoin_novaluefield(rightjoin_impl) |
| 708 |
_test_rightjoin_prefix(rightjoin_impl) |
| 709 |
_test_rightjoin_lrkey(rightjoin_impl) |
| 710 |
_test_rightjoin_multiple(rightjoin_impl) |
| 711 |
|
| 712 |
|
| 713 |
def test_rightjoin(): |
| 714 |
_test_rightjoin(rightjoin) |
| 715 |
|
| 716 |
|
| 717 |
def test_outerjoin(): |
| 718 |
|
| 719 |
table1 = (('id', 'colour'), |
| 720 |
(0, 'black'), |
| 721 |
(1, 'blue'), |
| 722 |
(2, 'red'), |
| 723 |
(3, 'purple'), |
| 724 |
(5, 'yellow'), |
| 725 |
(7, 'white')) |
| 726 |
table2 = (('id', 'shape'), |
| 727 |
(1, 'circle'), |
| 728 |
(3, 'square'), |
| 729 |
(4, 'ellipse')) |
| 730 |
table3 = outerjoin(table1, table2, key='id') |
| 731 |
expect3 = (('id', 'colour', 'shape'), |
| 732 |
(0, 'black', None), |
| 733 |
(1, 'blue', 'circle'), |
| 734 |
(2, 'red', None), |
| 735 |
(3, 'purple', 'square'), |
| 736 |
(4, None, 'ellipse'), |
| 737 |
(5, 'yellow', None), |
| 738 |
(7, 'white', None)) |
| 739 |
ieq(expect3, table3) |
| 740 |
ieq(expect3, table3) # check twice |
| 741 |
|
| 742 |
# natural join |
| 743 |
table4 = outerjoin(table1, table2) |
| 744 |
expect4 = expect3 |
| 745 |
ieq(expect4, table4) |
| 746 |
|
| 747 |
|
| 748 |
def test_outerjoin_2(): |
| 749 |
|
| 750 |
table1 = (('id', 'colour'), |
| 751 |
(1, 'blue'), |
| 752 |
(2, 'red'), |
| 753 |
(3, 'purple')) |
| 754 |
table2 = (('id', 'shape'), |
| 755 |
(0, 'pentagon'), |
| 756 |
(1, 'circle'), |
| 757 |
(3, 'square'), |
| 758 |
(4, 'ellipse'), |
| 759 |
(5, 'triangle')) |
| 760 |
table3 = outerjoin(table1, table2, key='id') |
| 761 |
expect3 = (('id', 'colour', 'shape'), |
| 762 |
(0, None, 'pentagon'), |
| 763 |
(1, 'blue', 'circle'), |
| 764 |
(2, 'red', None), |
| 765 |
(3, 'purple', 'square'), |
| 766 |
(4, None, 'ellipse'), |
| 767 |
(5, None, 'triangle')) |
| 768 |
ieq(expect3, table3) |
| 769 |
ieq(expect3, table3) # check twice |
| 770 |
|
| 771 |
# natural join |
| 772 |
table4 = outerjoin(table1, table2) |
| 773 |
expect4 = expect3 |
| 774 |
ieq(expect4, table4) |
| 775 |
|
| 776 |
|
| 777 |
def test_outerjoin_fieldorder(): |
| 778 |
|
| 779 |
table1 = (('colour', 'id'), |
| 780 |
('blue', 1), |
| 781 |
('red', 2), |
| 782 |
('purple', 3)) |
| 783 |
table2 = (('id', 'shape'), |
| 784 |
(1, 'circle'), |
| 785 |
(3, 'square'), |
| 786 |
(4, 'ellipse')) |
| 787 |
table3 = outerjoin(table1, table2, key='id') |
| 788 |
expect3 = (('colour', 'id', 'shape'), |
| 789 |
('blue', 1, 'circle'), |
| 790 |
('red', 2, None), |
| 791 |
('purple', 3, 'square'), |
| 792 |
(None, 4, 'ellipse')) |
| 793 |
ieq(expect3, table3) |
| 794 |
ieq(expect3, table3) # check twice |
| 795 |
|
| 796 |
|
| 797 |
def test_outerjoin_empty(): |
| 798 |
|
| 799 |
table1 = (('id', 'colour'), |
| 800 |
(0, 'black'), |
| 801 |
(1, 'blue'), |
| 802 |
(2, 'red'), |
| 803 |
(3, 'purple'), |
| 804 |
(5, 'yellow'), |
| 805 |
(7, 'white')) |
| 806 |
table2 = (('id', 'shape'),) |
| 807 |
table3 = outerjoin(table1, table2, key='id') |
| 808 |
expect3 = (('id', 'colour', 'shape'), |
| 809 |
(0, 'black', None), |
| 810 |
(1, 'blue', None), |
| 811 |
(2, 'red', None), |
| 812 |
(3, 'purple', None), |
| 813 |
(5, 'yellow', None), |
| 814 |
(7, 'white', None)) |
| 815 |
ieq(expect3, table3) |
| 816 |
|
| 817 |
|
| 818 |
def test_outerjoin_novaluefield(): |
| 819 |
table1 = (('id', 'colour'), |
| 820 |
(0, 'black'), |
| 821 |
(1, 'blue'), |
| 822 |
(2, 'red'), |
| 823 |
(3, 'purple'), |
| 824 |
(5, 'yellow'), |
| 825 |
(7, 'white')) |
| 826 |
table2 = (('id', 'shape'), |
| 827 |
(1, 'circle'), |
| 828 |
(3, 'square'), |
| 829 |
(4, 'ellipse')) |
| 830 |
expect = (('id', 'colour', 'shape'), |
| 831 |
(0, 'black', None), |
| 832 |
(1, 'blue', 'circle'), |
| 833 |
(2, 'red', None), |
| 834 |
(3, 'purple', 'square'), |
| 835 |
(4, None, 'ellipse'), |
| 836 |
(5, 'yellow', None), |
| 837 |
(7, 'white', None)) |
| 838 |
actual = outerjoin(table1, table2, key='id') |
| 839 |
ieq(expect, actual) |
| 840 |
actual = outerjoin(cut(table1, 'id'), table2, key='id') |
| 841 |
ieq(cut(expect, 'id', 'shape'), actual) |
| 842 |
actual = outerjoin(table1, cut(table2, 'id'), key='id') |
| 843 |
ieq(cut(expect, 'id', 'colour'), actual) |
| 844 |
actual = outerjoin(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 845 |
ieq(cut(expect, 'id'), actual) |
| 846 |
|
| 847 |
|
| 848 |
def test_outerjoin_prefix(): |
| 849 |
|
| 850 |
table1 = (('id', 'colour'), |
| 851 |
(0, 'black'), |
| 852 |
(1, 'blue'), |
| 853 |
(2, 'red'), |
| 854 |
(3, 'purple'), |
| 855 |
(5, 'yellow'), |
| 856 |
(7, 'white')) |
| 857 |
table2 = (('id', 'shape'), |
| 858 |
(1, 'circle'), |
| 859 |
(3, 'square'), |
| 860 |
(4, 'ellipse')) |
| 861 |
table3 = outerjoin(table1, table2, key='id', lprefix='l_', rprefix='r_') |
| 862 |
expect3 = (('l_id', 'l_colour', 'r_shape'), |
| 863 |
(0, 'black', None), |
| 864 |
(1, 'blue', 'circle'), |
| 865 |
(2, 'red', None), |
| 866 |
(3, 'purple', 'square'), |
| 867 |
(4, None, 'ellipse'), |
| 868 |
(5, 'yellow', None), |
| 869 |
(7, 'white', None)) |
| 870 |
ieq(expect3, table3) |
| 871 |
ieq(expect3, table3) # check twice |
| 872 |
|
| 873 |
|
| 874 |
def test_outerjoin_lrkey(): |
| 875 |
|
| 876 |
table1 = (('id', 'colour'), |
| 877 |
(0, 'black'), |
| 878 |
(1, 'blue'), |
| 879 |
(2, 'red'), |
| 880 |
(3, 'purple'), |
| 881 |
(5, 'yellow'), |
| 882 |
(7, 'white')) |
| 883 |
table2 = (('identifier', 'shape'), |
| 884 |
(1, 'circle'), |
| 885 |
(3, 'square'), |
| 886 |
(4, 'ellipse')) |
| 887 |
table3 = outerjoin(table1, table2, lkey='id', rkey='identifier') |
| 888 |
expect3 = (('id', 'colour', 'shape'), |
| 889 |
(0, 'black', None), |
| 890 |
(1, 'blue', 'circle'), |
| 891 |
(2, 'red', None), |
| 892 |
(3, 'purple', 'square'), |
| 893 |
(4, None, 'ellipse'), |
| 894 |
(5, 'yellow', None), |
| 895 |
(7, 'white', None)) |
| 896 |
ieq(expect3, table3) |
| 897 |
ieq(expect3, table3) # check twice |
| 898 |
|
| 899 |
|
| 900 |
def test_outerjoin_multiple(): |
| 901 |
|
| 902 |
table1 = (('id', 'color', 'cost'), |
| 903 |
(1, 'blue', 12), |
| 904 |
(1, 'red', 8), |
| 905 |
(2, 'yellow', 15), |
| 906 |
(2, 'orange', 5), |
| 907 |
(3, 'purple', 4), |
| 908 |
(4, 'chartreuse', 42)) |
| 909 |
|
| 910 |
table2 = (('id', 'shape', 'size'), |
| 911 |
(1, 'circle', 'big'), |
| 912 |
(2, 'square', 'tiny'), |
| 913 |
(2, 'square', 'big'), |
| 914 |
(3, 'ellipse', 'small'), |
| 915 |
(3, 'ellipse', 'tiny'), |
| 916 |
(5, 'didodecahedron', 3.14159265)) |
| 917 |
|
| 918 |
actual = outerjoin(table1, table2, key='id') |
| 919 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 920 |
(1, 'blue', 12, 'circle', 'big'), |
| 921 |
(1, 'red', 8, 'circle', 'big'), |
| 922 |
(2, 'yellow', 15, 'square', 'tiny'), |
| 923 |
(2, 'yellow', 15, 'square', 'big'), |
| 924 |
(2, 'orange', 5, 'square', 'tiny'), |
| 925 |
(2, 'orange', 5, 'square', 'big'), |
| 926 |
(3, 'purple', 4, 'ellipse', 'small'), |
| 927 |
(3, 'purple', 4, 'ellipse', 'tiny'), |
| 928 |
(4, 'chartreuse', 42, None, None), |
| 929 |
(5, None, None, 'didodecahedron', 3.14159265)) |
| 930 |
|
| 931 |
ieq(expect, actual) |
| 932 |
|
| 933 |
|
| 934 |
def test_crossjoin(): |
| 935 |
|
| 936 |
table1 = (('id', 'colour'), |
| 937 |
(1, 'blue'), |
| 938 |
(2, 'red')) |
| 939 |
table2 = (('id', 'shape'), |
| 940 |
(1, 'circle'), |
| 941 |
(3, 'square')) |
| 942 |
table3 = crossjoin(table1, table2) |
| 943 |
expect3 = (('id', 'colour', 'id', 'shape'), |
| 944 |
(1, 'blue', 1, 'circle'), |
| 945 |
(1, 'blue', 3, 'square'), |
| 946 |
(2, 'red', 1, 'circle'), |
| 947 |
(2, 'red', 3, 'square')) |
| 948 |
ieq(expect3, table3) |
| 949 |
|
| 950 |
|
| 951 |
def test_crossjoin_empty(): |
| 952 |
|
| 953 |
table1 = (('id', 'colour'), |
| 954 |
(1, 'blue'), |
| 955 |
(2, 'red')) |
| 956 |
table2 = (('id', 'shape'),) |
| 957 |
table3 = crossjoin(table1, table2) |
| 958 |
expect3 = (('id', 'colour', 'id', 'shape'),) |
| 959 |
ieq(expect3, table3) |
| 960 |
|
| 961 |
|
| 962 |
def test_crossjoin_novaluefield(): |
| 963 |
table1 = (('id', 'colour'), |
| 964 |
(1, 'blue'), |
| 965 |
(2, 'red')) |
| 966 |
table2 = (('id', 'shape'), |
| 967 |
(1, 'circle'), |
| 968 |
(3, 'square')) |
| 969 |
expect = (('id', 'colour', 'id', 'shape'), |
| 970 |
(1, 'blue', 1, 'circle'), |
| 971 |
(1, 'blue', 3, 'square'), |
| 972 |
(2, 'red', 1, 'circle'), |
| 973 |
(2, 'red', 3, 'square')) |
| 974 |
actual = crossjoin(table1, table2, key='id') |
| 975 |
ieq(expect, actual) |
| 976 |
actual = crossjoin(cut(table1, 'id'), table2, key='id') |
| 977 |
ieq(cut(expect, 0, 2, 'shape'), actual) |
| 978 |
actual = crossjoin(table1, cut(table2, 'id'), key='id') |
| 979 |
ieq(cut(expect, 0, 'colour', 2), actual) |
| 980 |
actual = crossjoin(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 981 |
ieq(cut(expect, 0, 2), actual) |
| 982 |
|
| 983 |
|
| 984 |
def test_crossjoin_prefix(): |
| 985 |
|
| 986 |
table1 = (('id', 'colour'), |
| 987 |
(1, 'blue'), |
| 988 |
(2, 'red')) |
| 989 |
table2 = (('id', 'shape'), |
| 990 |
(1, 'circle'), |
| 991 |
(3, 'square')) |
| 992 |
table3 = crossjoin(table1, table2, prefix=True) |
| 993 |
expect3 = (('1_id', '1_colour', '2_id', '2_shape'), |
| 994 |
(1, 'blue', 1, 'circle'), |
| 995 |
(1, 'blue', 3, 'square'), |
| 996 |
(2, 'red', 1, 'circle'), |
| 997 |
(2, 'red', 3, 'square')) |
| 998 |
ieq(expect3, table3) |
| 999 |
|
| 1000 |
def test_crossjoin_uneven_rows(): |
| 1001 |
table1 = (('id', 'colour'), |
| 1002 |
(1,), |
| 1003 |
(2, 'red')) |
| 1004 |
table2 = (('id', 'shape'), |
| 1005 |
(1, 'circle'), |
| 1006 |
(3,)) |
| 1007 |
table3 = crossjoin(table1, table2, prefix=True) |
| 1008 |
expect3 = (('1_id', '1_colour', '2_id', '2_shape'), |
| 1009 |
(1, None, 1, 'circle'), |
| 1010 |
(1, None, 3, None), |
| 1011 |
(2, 'red', 1, 'circle'), |
| 1012 |
(2, 'red', 3, None)) |
| 1013 |
ieq(expect3, table3) |
| 1014 |
|
| 1015 |
def _test_antijoin_basics(antijoin_impl): |
| 1016 |
|
| 1017 |
table1 = (('id', 'colour'), |
| 1018 |
(0, 'black'), |
| 1019 |
(1, 'blue'), |
| 1020 |
(2, 'red'), |
| 1021 |
(4, 'yellow'), |
| 1022 |
(5, 'white')) |
| 1023 |
table2 = (('id', 'shape'), |
| 1024 |
(1, 'circle'), |
| 1025 |
(3, 'square')) |
| 1026 |
table3 = antijoin_impl(table1, table2, key='id') |
| 1027 |
expect3 = (('id', 'colour'), |
| 1028 |
(0, 'black'), |
| 1029 |
(2, 'red'), |
| 1030 |
(4, 'yellow'), |
| 1031 |
(5, 'white')) |
| 1032 |
ieq(expect3, table3) |
| 1033 |
|
| 1034 |
table4 = antijoin_impl(table1, table2) |
| 1035 |
expect4 = expect3 |
| 1036 |
ieq(expect4, table4) |
| 1037 |
|
| 1038 |
|
| 1039 |
def _test_antijoin_empty(antijoin_impl): |
| 1040 |
|
| 1041 |
table1 = (('id', 'colour'), |
| 1042 |
(0, 'black'), |
| 1043 |
(1, 'blue'), |
| 1044 |
(2, 'red'), |
| 1045 |
(4, 'yellow'), |
| 1046 |
(5, 'white')) |
| 1047 |
table2 = (('id', 'shape'),) |
| 1048 |
actual = antijoin_impl(table1, table2, key='id') |
| 1049 |
expect = table1 |
| 1050 |
ieq(expect, actual) |
| 1051 |
|
| 1052 |
|
| 1053 |
def _test_antijoin_novaluefield(antijoin_impl): |
| 1054 |
table1 = (('id', 'colour'), |
| 1055 |
(0, 'black'), |
| 1056 |
(1, 'blue'), |
| 1057 |
(2, 'red'), |
| 1058 |
(4, 'yellow'), |
| 1059 |
(5, 'white')) |
| 1060 |
table2 = (('id', 'shape'), |
| 1061 |
(1, 'circle'), |
| 1062 |
(3, 'square')) |
| 1063 |
expect = (('id', 'colour'), |
| 1064 |
(0, 'black'), |
| 1065 |
(2, 'red'), |
| 1066 |
(4, 'yellow'), |
| 1067 |
(5, 'white')) |
| 1068 |
actual = antijoin_impl(table1, table2, key='id') |
| 1069 |
ieq(expect, actual) |
| 1070 |
actual = antijoin_impl(cut(table1, 'id'), table2, key='id') |
| 1071 |
ieq(cut(expect, 'id'), actual) |
| 1072 |
actual = antijoin_impl(table1, cut(table2, 'id'), key='id') |
| 1073 |
ieq(expect, actual) |
| 1074 |
actual = antijoin_impl(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 1075 |
ieq(cut(expect, 'id'), actual) |
| 1076 |
|
| 1077 |
|
| 1078 |
def _test_antijoin_lrkey(antijoin_impl): |
| 1079 |
|
| 1080 |
table1 = (('id', 'colour'), |
| 1081 |
(0, 'black'), |
| 1082 |
(1, 'blue'), |
| 1083 |
(2, 'red'), |
| 1084 |
(4, 'yellow'), |
| 1085 |
(5, 'white')) |
| 1086 |
table2 = (('identifier', 'shape'), |
| 1087 |
(1, 'circle'), |
| 1088 |
(3, 'square')) |
| 1089 |
table3 = antijoin_impl(table1, table2, lkey='id', rkey='identifier') |
| 1090 |
expect3 = (('id', 'colour'), |
| 1091 |
(0, 'black'), |
| 1092 |
(2, 'red'), |
| 1093 |
(4, 'yellow'), |
| 1094 |
(5, 'white')) |
| 1095 |
ieq(expect3, table3) |
| 1096 |
|
| 1097 |
def _test_antijoin_uneven_rows(antijoin_impl): |
| 1098 |
table1 = (('id', 'colour'), |
| 1099 |
(0,), |
| 1100 |
(1,), |
| 1101 |
(2, 'red'), |
| 1102 |
(4, 'yellow'), |
| 1103 |
(5, 'white')) |
| 1104 |
table2 = (('identifier', 'shape'), |
| 1105 |
(1,), |
| 1106 |
(3, 'square')) |
| 1107 |
table3 = antijoin_impl(table1, table2, lkey='id', rkey='identifier') |
| 1108 |
expect3 = (('id', 'colour'), |
| 1109 |
(0, None), |
| 1110 |
(2, 'red'), |
| 1111 |
(4, 'yellow'), |
| 1112 |
(5, 'white')) |
| 1113 |
ieq(expect3, table3) |
| 1114 |
|
| 1115 |
def _test_antijoin(antijoin_impl): |
| 1116 |
_test_antijoin_basics(antijoin_impl) |
| 1117 |
_test_antijoin_empty(antijoin_impl) |
| 1118 |
_test_antijoin_novaluefield(antijoin_impl) |
| 1119 |
_test_antijoin_lrkey(antijoin_impl) |
| 1120 |
_test_antijoin_uneven_rows(antijoin_impl) |
| 1121 |
|
| 1122 |
|
| 1123 |
def test_antijoin(): |
| 1124 |
_test_antijoin(antijoin) |
| 1125 |
|
| 1126 |
|
| 1127 |
def _test_lookupjoin_1(lookupjoin_impl): |
| 1128 |
|
| 1129 |
table1 = (('id', 'color', 'cost'), |
| 1130 |
(1, 'blue', 12), |
| 1131 |
(2, 'red', 8), |
| 1132 |
(3, 'purple', 4)) |
| 1133 |
|
| 1134 |
table2 = (('id', 'shape', 'size'), |
| 1135 |
(1, 'circle', 'big'), |
| 1136 |
(2, 'square', 'tiny'), |
| 1137 |
(3, 'ellipse', 'small')) |
| 1138 |
|
| 1139 |
actual = lookupjoin_impl(table1, table2, key='id') |
| 1140 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 1141 |
(1, 'blue', 12, 'circle', 'big'), |
| 1142 |
(2, 'red', 8, 'square', 'tiny'), |
| 1143 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1144 |
ieq(expect, actual) |
| 1145 |
ieq(expect, actual) |
| 1146 |
|
| 1147 |
# natural join |
| 1148 |
actual = lookupjoin_impl(table1, table2) |
| 1149 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 1150 |
(1, 'blue', 12, 'circle', 'big'), |
| 1151 |
(2, 'red', 8, 'square', 'tiny'), |
| 1152 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1153 |
ieq(expect, actual) |
| 1154 |
ieq(expect, actual) |
| 1155 |
|
| 1156 |
|
| 1157 |
def _test_lookupjoin_2(lookupjoin_impl): |
| 1158 |
|
| 1159 |
table1 = (('id', 'color', 'cost'), |
| 1160 |
(1, 'blue', 12), |
| 1161 |
(2, 'red', 8), |
| 1162 |
(3, 'purple', 4)) |
| 1163 |
|
| 1164 |
table2 = (('id', 'shape', 'size'), |
| 1165 |
(1, 'circle', 'big'), |
| 1166 |
(1, 'circle', 'small'), |
| 1167 |
(2, 'square', 'tiny'), |
| 1168 |
(2, 'square', 'big'), |
| 1169 |
(3, 'ellipse', 'small'), |
| 1170 |
(3, 'ellipse', 'tiny')) |
| 1171 |
|
| 1172 |
actual = lookupjoin_impl(table1, table2, key='id') |
| 1173 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 1174 |
(1, 'blue', 12, 'circle', 'big'), |
| 1175 |
(2, 'red', 8, 'square', 'tiny'), |
| 1176 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1177 |
ieq(expect, actual) |
| 1178 |
ieq(expect, actual) |
| 1179 |
|
| 1180 |
|
| 1181 |
def _test_lookupjoin_prefix(lookupjoin_impl): |
| 1182 |
|
| 1183 |
table1 = (('id', 'color', 'cost'), |
| 1184 |
(1, 'blue', 12), |
| 1185 |
(2, 'red', 8), |
| 1186 |
(3, 'purple', 4)) |
| 1187 |
|
| 1188 |
table2 = (('id', 'shape', 'size'), |
| 1189 |
(1, 'circle', 'big'), |
| 1190 |
(2, 'square', 'tiny'), |
| 1191 |
(3, 'ellipse', 'small')) |
| 1192 |
|
| 1193 |
actual = lookupjoin_impl(table1, table2, key='id', lprefix='l_', |
| 1194 |
rprefix='r_') |
| 1195 |
expect = (('l_id', 'l_color', 'l_cost', 'r_shape', 'r_size'), |
| 1196 |
(1, 'blue', 12, 'circle', 'big'), |
| 1197 |
(2, 'red', 8, 'square', 'tiny'), |
| 1198 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1199 |
ieq(expect, actual) |
| 1200 |
|
| 1201 |
|
| 1202 |
def _test_lookupjoin_lrkey(lookupjoin_impl): |
| 1203 |
|
| 1204 |
table1 = (('id', 'color', 'cost'), |
| 1205 |
(1, 'blue', 12), |
| 1206 |
(2, 'red', 8), |
| 1207 |
(3, 'purple', 4)) |
| 1208 |
|
| 1209 |
table2 = (('identifier', 'shape', 'size'), |
| 1210 |
(1, 'circle', 'big'), |
| 1211 |
(2, 'square', 'tiny'), |
| 1212 |
(3, 'ellipse', 'small')) |
| 1213 |
|
| 1214 |
actual = lookupjoin_impl(table1, table2, lkey='id', rkey='identifier') |
| 1215 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 1216 |
(1, 'blue', 12, 'circle', 'big'), |
| 1217 |
(2, 'red', 8, 'square', 'tiny'), |
| 1218 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1219 |
ieq(expect, actual) |
| 1220 |
|
| 1221 |
|
| 1222 |
def _test_lookupjoin_novaluefield(lookupjoin_impl): |
| 1223 |
table1 = (('id', 'color', 'cost'), |
| 1224 |
(1, 'blue', 12), |
| 1225 |
(2, 'red', 8), |
| 1226 |
(3, 'purple', 4)) |
| 1227 |
table2 = (('id', 'shape', 'size'), |
| 1228 |
(1, 'circle', 'big'), |
| 1229 |
(2, 'square', 'tiny'), |
| 1230 |
(3, 'ellipse', 'small')) |
| 1231 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 1232 |
(1, 'blue', 12, 'circle', 'big'), |
| 1233 |
(2, 'red', 8, 'square', 'tiny'), |
| 1234 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1235 |
actual = lookupjoin_impl(table1, table2, key='id') |
| 1236 |
ieq(expect, actual) |
| 1237 |
actual = lookupjoin_impl(cut(table1, 'id'), table2, key='id') |
| 1238 |
ieq(cut(expect, 'id', 'shape', 'size'), actual) |
| 1239 |
actual = lookupjoin_impl(table1, cut(table2, 'id'), key='id') |
| 1240 |
ieq(cut(expect, 'id', 'color', 'cost'), actual) |
| 1241 |
actual = lookupjoin_impl(cut(table1, 'id'), cut(table2, 'id'), key='id') |
| 1242 |
ieq(cut(expect, 'id'), actual) |
| 1243 |
|
| 1244 |
|
| 1245 |
def _test_lookupjoin_uneven_rows(lookupjoin_impl): |
| 1246 |
table1 = (('id', 'color', 'cost'), |
| 1247 |
(1, 'blue',), |
| 1248 |
(2, 'red', 8), |
| 1249 |
(3, 'purple', 4)) |
| 1250 |
|
| 1251 |
table2 = (('identifier', 'shape', 'size'), |
| 1252 |
(1, 'circle', 'big'), |
| 1253 |
(2, 'square',), |
| 1254 |
(3, 'ellipse', 'small')) |
| 1255 |
|
| 1256 |
actual = lookupjoin_impl(table1, table2, lkey='id', rkey='identifier') |
| 1257 |
expect = (('id', 'color', 'cost', 'shape', 'size'), |
| 1258 |
(1, 'blue', None, 'circle', 'big'), |
| 1259 |
(2, 'red', 8, 'square', None), |
| 1260 |
(3, 'purple', 4, 'ellipse', 'small')) |
| 1261 |
ieq(expect, actual) |
| 1262 |
|
| 1263 |
|
| 1264 |
def _test_lookupjoin(lookupjoin_impl): |
| 1265 |
_test_lookupjoin_1(lookupjoin_impl) |
| 1266 |
_test_lookupjoin_2(lookupjoin_impl) |
| 1267 |
_test_lookupjoin_prefix(lookupjoin_impl) |
| 1268 |
_test_lookupjoin_lrkey(lookupjoin_impl) |
| 1269 |
_test_lookupjoin_novaluefield(lookupjoin_impl) |
| 1270 |
_test_lookupjoin_uneven_rows(lookupjoin_impl) |
| 1271 |
|
| 1272 |
|
| 1273 |
def test_lookupjoin(): |
| 1274 |
_test_lookupjoin(lookupjoin) |
| 1275 |
|
| 1276 |
|
| 1277 |
def test_hashjoin(): |
| 1278 |
_test_join(hashjoin) |
| 1279 |
|
| 1280 |
|
| 1281 |
def test_hashleftjoin(): |
| 1282 |
_test_leftjoin(hashleftjoin) |
| 1283 |
|
| 1284 |
|
| 1285 |
def test_hashrightjoin(): |
| 1286 |
_test_rightjoin(hashrightjoin) |
| 1287 |
|
| 1288 |
|
| 1289 |
def test_hashantijoin(): |
| 1290 |
_test_antijoin(hashantijoin) |
| 1291 |
|
| 1292 |
|
| 1293 |
def test_hashlookupjoin(): |
| 1294 |
_test_lookupjoin(hashlookupjoin) |
| 1295 |
|
| 1296 |
|
| 1297 |
def test_unjoin_implicit_key(): |
| 1298 |
|
| 1299 |
# test the case where the join key needs to be reconstructed |
| 1300 |
|
| 1301 |
table1 = (('foo', 'bar'), |
| 1302 |
(1, 'apple'), |
| 1303 |
(2, 'apple'), |
| 1304 |
(3, 'orange')) |
| 1305 |
|
| 1306 |
expect_left = (('foo', 'bar_id'), |
| 1307 |
(1, 1), |
| 1308 |
(2, 1), |
| 1309 |
(3, 2)) |
| 1310 |
expect_right = (('id', 'bar'), |
| 1311 |
(1, 'apple'), |
| 1312 |
(2, 'orange')) |
| 1313 |
|
| 1314 |
left, right = unjoin(table1, 'bar') |
| 1315 |
ieq(expect_left, left) |
| 1316 |
ieq(expect_left, left) |
| 1317 |
ieq(expect_right, right) |
| 1318 |
ieq(expect_right, right) |
| 1319 |
|
| 1320 |
|
| 1321 |
def test_unjoin_explicit_key(): |
| 1322 |
|
| 1323 |
# test the case where the join key is still present |
| 1324 |
|
| 1325 |
table2 = (('Customer ID', 'First Name', 'Surname', 'Telephone Number'), |
| 1326 |
(123, 'Robert', 'Ingram', '555-861-2025'), |
| 1327 |
(456, 'Jane', 'Wright', '555-403-1659'), |
| 1328 |
(456, 'Jane', 'Wright', '555-776-4100'), |
| 1329 |
(789, 'Maria', 'Fernandez', '555-808-9633')) |
| 1330 |
|
| 1331 |
expect_left = (('Customer ID', 'First Name', 'Surname'), |
| 1332 |
(123, 'Robert', 'Ingram'), |
| 1333 |
(456, 'Jane', 'Wright'), |
| 1334 |
(789, 'Maria', 'Fernandez')) |
| 1335 |
expect_right = (('Customer ID', 'Telephone Number'), |
| 1336 |
(123, '555-861-2025'), |
| 1337 |
(456, '555-403-1659'), |
| 1338 |
(456, '555-776-4100'), |
| 1339 |
(789, '555-808-9633')) |
| 1340 |
left, right = unjoin(table2, 'Telephone Number', key='Customer ID') |
| 1341 |
ieq(expect_left, left) |
| 1342 |
ieq(expect_left, left) |
| 1343 |
ieq(expect_right, right) |
| 1344 |
ieq(expect_right, right) |
| 1345 |
|
| 1346 |
|
| 1347 |
def test_unjoin_explicit_key_2(): |
| 1348 |
|
| 1349 |
table3 = (('Employee', 'Skill', 'Current Work Location'), |
| 1350 |
('Jones', 'Typing', '114 Main Street'), |
| 1351 |
('Jones', 'Shorthand', '114 Main Street'), |
| 1352 |
('Jones', 'Whittling', '114 Main Street'), |
| 1353 |
('Bravo', 'Light Cleaning', '73 Industrial Way'), |
| 1354 |
('Ellis', 'Alchemy', '73 Industrial Way'), |
| 1355 |
('Ellis', 'Flying', '73 Industrial Way'), |
| 1356 |
('Harrison', 'Light Cleaning', '73 Industrial Way')) |
| 1357 |
# N.B., we do expect rows will get sorted |
| 1358 |
expect_left = (('Employee', 'Current Work Location'), |
| 1359 |
('Bravo', '73 Industrial Way'), |
| 1360 |
('Ellis', '73 Industrial Way'), |
| 1361 |
('Harrison', '73 Industrial Way'), |
| 1362 |
('Jones', '114 Main Street')) |
| 1363 |
expect_right = (('Employee', 'Skill'), |
| 1364 |
('Bravo', 'Light Cleaning'), |
| 1365 |
('Ellis', 'Alchemy'), |
| 1366 |
('Ellis', 'Flying'), |
| 1367 |
('Harrison', 'Light Cleaning'), |
| 1368 |
('Jones', 'Shorthand'), |
| 1369 |
('Jones', 'Typing'), |
| 1370 |
('Jones', 'Whittling')) |
| 1371 |
left, right = unjoin(table3, 'Skill', key='Employee') |
| 1372 |
ieq(expect_left, left) |
| 1373 |
ieq(expect_left, left) |
| 1374 |
ieq(expect_right, right) |
| 1375 |
ieq(expect_right, right) |
| 1376 |
|
| 1377 |
|
| 1378 |
def test_unjoin_explicit_key_3(): |
| 1379 |
|
| 1380 |
table4 = (('Tournament', 'Year', 'Winner', 'Date of Birth'), |
| 1381 |
('Indiana Invitational', 1998, 'Al Fredrickson', '21 July 1975'), |
| 1382 |
('Cleveland Open', 1999, 'Bob Albertson', '28 September 1968'), |
| 1383 |
('Des Moines Masters', 1999, 'Al Fredrickson', '21 July 1975'), |
| 1384 |
('Indiana Invitational', 1999, 'Chip Masterson', '14 March 1977')) |
| 1385 |
|
| 1386 |
# N.B., we do expect rows will get sorted |
| 1387 |
expect_left = (('Tournament', 'Year', 'Winner'), |
| 1388 |
('Cleveland Open', 1999, 'Bob Albertson'), |
| 1389 |
('Des Moines Masters', 1999, 'Al Fredrickson'), |
| 1390 |
('Indiana Invitational', 1998, 'Al Fredrickson'), |
| 1391 |
('Indiana Invitational', 1999, 'Chip Masterson')) |
| 1392 |
expect_right = (('Winner', 'Date of Birth'), |
| 1393 |
('Al Fredrickson', '21 July 1975'), |
| 1394 |
('Bob Albertson', '28 September 1968'), |
| 1395 |
('Chip Masterson', '14 March 1977')) |
| 1396 |
left, right = unjoin(table4, 'Date of Birth', key='Winner') |
| 1397 |
ieq(expect_left, left) |
| 1398 |
ieq(expect_left, left) |
| 1399 |
ieq(expect_right, right) |
| 1400 |
ieq(expect_right, right) |
| 1401 |
|
| 1402 |
|
| 1403 |
def test_unjoin_explicit_key_4(): |
| 1404 |
|
| 1405 |
table5 = (('Restaurant', 'Pizza Variety', 'Delivery Area'), |
| 1406 |
('A1 Pizza', 'Thick Crust', 'Springfield'), |
| 1407 |
('A1 Pizza', 'Thick Crust', 'Shelbyville'), |
| 1408 |
('A1 Pizza', 'Thick Crust', 'Capital City'), |
| 1409 |
('A1 Pizza', 'Stuffed Crust', 'Springfield'), |
| 1410 |
('A1 Pizza', 'Stuffed Crust', 'Shelbyville'), |
| 1411 |
('A1 Pizza', 'Stuffed Crust', 'Capital City'), |
| 1412 |
('Elite Pizza', 'Thin Crust', 'Capital City'), |
| 1413 |
('Elite Pizza', 'Stuffed Crust', 'Capital City'), |
| 1414 |
("Vincenzo's Pizza", "Thick Crust", "Springfield"), |
| 1415 |
("Vincenzo's Pizza", "Thick Crust", "Shelbyville"), |
| 1416 |
("Vincenzo's Pizza", "Thin Crust", "Springfield"), |
| 1417 |
("Vincenzo's Pizza", "Thin Crust", "Shelbyville")) |
| 1418 |
|
| 1419 |
# N.B., we do expect rows will get sorted |
| 1420 |
expect_left = (('Restaurant', 'Pizza Variety'), |
| 1421 |
('A1 Pizza', 'Stuffed Crust'), |
| 1422 |
('A1 Pizza', 'Thick Crust'), |
| 1423 |
('Elite Pizza', 'Stuffed Crust'), |
| 1424 |
('Elite Pizza', 'Thin Crust'), |
| 1425 |
("Vincenzo's Pizza", "Thick Crust"), |
| 1426 |
("Vincenzo's Pizza", "Thin Crust")) |
| 1427 |
expect_right = (('Restaurant', 'Delivery Area'), |
| 1428 |
('A1 Pizza', 'Capital City'), |
| 1429 |
('A1 Pizza', 'Shelbyville'), |
| 1430 |
('A1 Pizza', 'Springfield'), |
| 1431 |
('Elite Pizza', 'Capital City'), |
| 1432 |
("Vincenzo's Pizza", "Shelbyville"), |
| 1433 |
("Vincenzo's Pizza", "Springfield")) |
| 1434 |
left, right = unjoin(table5, 'Delivery Area', key='Restaurant') |
| 1435 |
ieq(expect_left, left) |
| 1436 |
ieq(expect_left, left) |
| 1437 |
ieq(expect_right, right) |
| 1438 |
ieq(expect_right, right) |
| 1439 |
|
| 1440 |
|
| 1441 |
def test_unjoin_explicit_key_5(): |
| 1442 |
|
| 1443 |
table6 = (('ColA', 'ColB', 'ColC'), |
| 1444 |
('A', 1, 'apple'), |
| 1445 |
('B', 1, 'apple'), |
| 1446 |
('C', 2, 'orange'), |
| 1447 |
('D', 3, 'lemon'), |
| 1448 |
('E', 3, 'lemon')) |
| 1449 |
|
| 1450 |
# N.B., we do expect rows will get sorted |
| 1451 |
expect_left = (('ColA', 'ColB'), |
| 1452 |
('A', 1), |
| 1453 |
('B', 1), |
| 1454 |
('C', 2), |
| 1455 |
('D', 3), |
| 1456 |
('E', 3)) |
| 1457 |
expect_right = (('ColB', 'ColC'), |
| 1458 |
(1, 'apple'), |
| 1459 |
(2, 'orange'), |
| 1460 |
(3, 'lemon')) |
| 1461 |
left, right = unjoin(table6, 'ColC', key='ColB') |
| 1462 |
ieq(expect_left, left) |
| 1463 |
ieq(expect_left, left) |
| 1464 |
ieq(expect_right, right) |
| 1465 |
ieq(expect_right, right) |