()
| 404 | |
| 405 | |
| 406 | def test_hash_join(): |
| 407 | left = pa.table({'key': [1, 2, 3], 'a': [4, 5, 6]}) |
| 408 | left_source = Declaration("table_source", options=TableSourceNodeOptions(left)) |
| 409 | right = pa.table({'key': [2, 3, 4], 'b': [4, 5, 6]}) |
| 410 | right_source = Declaration("table_source", options=TableSourceNodeOptions(right)) |
| 411 | |
| 412 | # inner join |
| 413 | join_opts = HashJoinNodeOptions("inner", left_keys="key", right_keys="key") |
| 414 | joined = Declaration( |
| 415 | "hashjoin", options=join_opts, inputs=[left_source, right_source]) |
| 416 | result = joined.to_table() |
| 417 | expected = pa.table( |
| 418 | [[2, 3], [5, 6], [2, 3], [4, 5]], |
| 419 | names=["key", "a", "key", "b"]) |
| 420 | assert result.equals(expected) |
| 421 | |
| 422 | for keys in [field("key"), ["key"], [field("key")]]: |
| 423 | join_opts = HashJoinNodeOptions("inner", left_keys=keys, right_keys=keys) |
| 424 | joined = Declaration( |
| 425 | "hashjoin", options=join_opts, inputs=[left_source, right_source]) |
| 426 | result = joined.to_table() |
| 427 | assert result.equals(expected) |
| 428 | |
| 429 | # left join |
| 430 | join_opts = HashJoinNodeOptions( |
| 431 | "left outer", left_keys="key", right_keys="key") |
| 432 | joined = Declaration( |
| 433 | "hashjoin", options=join_opts, inputs=[left_source, right_source]) |
| 434 | result = joined.to_table() |
| 435 | expected = pa.table( |
| 436 | [[1, 2, 3], [4, 5, 6], [None, 2, 3], [None, 4, 5]], |
| 437 | names=["key", "a", "key", "b"] |
| 438 | ) |
| 439 | assert result.sort_by("a").equals(expected) |
| 440 | |
| 441 | # suffixes |
| 442 | join_opts = HashJoinNodeOptions( |
| 443 | "left outer", left_keys="key", right_keys="key", |
| 444 | output_suffix_for_left="_left", output_suffix_for_right="_right") |
| 445 | joined = Declaration( |
| 446 | "hashjoin", options=join_opts, inputs=[left_source, right_source]) |
| 447 | result = joined.to_table() |
| 448 | expected = pa.table( |
| 449 | [[1, 2, 3], [4, 5, 6], [None, 2, 3], [None, 4, 5]], |
| 450 | names=["key_left", "a", "key_right", "b"] |
| 451 | ) |
| 452 | assert result.sort_by("a").equals(expected) |
| 453 | |
| 454 | # manually specifying output columns |
| 455 | join_opts = HashJoinNodeOptions( |
| 456 | "left outer", left_keys="key", right_keys="key", |
| 457 | left_output=["key", "a"], right_output=[field("b")]) |
| 458 | joined = Declaration( |
| 459 | "hashjoin", options=join_opts, inputs=[left_source, right_source]) |
| 460 | result = joined.to_table() |
| 461 | expected = pa.table( |
| 462 | [[1, 2, 3], [4, 5, 6], [None, 4, 5]], |
| 463 | names=["key", "a", "b"] |
nothing calls this directly
no test coverage detected