MCPcopy Create free account
hub / github.com/apache/arrow / test_hash_join

Function test_hash_join

python/pyarrow/tests/test_acero.py:303–362  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

301
302
303def test_hash_join():
304 left = pa.table({'key': [1, 2, 3], 'a': [4, 5, 6]})
305 left_source = Declaration("table_source", options=TableSourceNodeOptions(left))
306 right = pa.table({'key': [2, 3, 4], 'b': [4, 5, 6]})
307 right_source = Declaration("table_source", options=TableSourceNodeOptions(right))
308
309 # inner join
310 join_opts = HashJoinNodeOptions("inner", left_keys="key", right_keys="key")
311 joined = Declaration(
312 "hashjoin", options=join_opts, inputs=[left_source, right_source])
313 result = joined.to_table()
314 expected = pa.table(
315 [[2, 3], [5, 6], [2, 3], [4, 5]],
316 names=["key", "a", "key", "b"])
317 assert result.equals(expected)
318
319 for keys in [field("key"), ["key"], [field("key")]]:
320 join_opts = HashJoinNodeOptions("inner", left_keys=keys, right_keys=keys)
321 joined = Declaration(
322 "hashjoin", options=join_opts, inputs=[left_source, right_source])
323 result = joined.to_table()
324 assert result.equals(expected)
325
326 # left join
327 join_opts = HashJoinNodeOptions(
328 "left outer", left_keys="key", right_keys="key")
329 joined = Declaration(
330 "hashjoin", options=join_opts, inputs=[left_source, right_source])
331 result = joined.to_table()
332 expected = pa.table(
333 [[1, 2, 3], [4, 5, 6], [None, 2, 3], [None, 4, 5]],
334 names=["key", "a", "key", "b"]
335 )
336 assert result.sort_by("a").equals(expected)
337
338 # suffixes
339 join_opts = HashJoinNodeOptions(
340 "left outer", left_keys="key", right_keys="key",
341 output_suffix_for_left="_left", output_suffix_for_right="_right")
342 joined = Declaration(
343 "hashjoin", options=join_opts, inputs=[left_source, right_source])
344 result = joined.to_table()
345 expected = pa.table(
346 [[1, 2, 3], [4, 5, 6], [None, 2, 3], [None, 4, 5]],
347 names=["key_left", "a", "key_right", "b"]
348 )
349 assert result.sort_by("a").equals(expected)
350
351 # manually specifying output columns
352 join_opts = HashJoinNodeOptions(
353 "left outer", left_keys="key", right_keys="key",
354 left_output=["key", "a"], right_output=[field("b")])
355 joined = Declaration(
356 "hashjoin", options=join_opts, inputs=[left_source, right_source])
357 result = joined.to_table()
358 expected = pa.table(
359 [[1, 2, 3], [4, 5, 6], [None, 4, 5]],
360 names=["key", "a", "b"]

Callers

nothing calls this directly

Calls 6

DeclarationClass · 0.90
TableSourceNodeOptionsFunction · 0.90
HashJoinNodeOptionsFunction · 0.90
fieldFunction · 0.90
equalsMethod · 0.80
to_tableMethod · 0.45

Tested by

no test coverage detected