Skip to content

Commit 7ed5143

Browse files
chfanboycpcloud
chfanboy
authored andcommitted
feat(clickhouse): support asof_join
1 parent aa3c31c commit 7ed5143

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ci/schema/clickhouse.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ INSERT INTO array_types VALUES
5252
([2, NULL, 3], ['b', NULL, 'c'], NULL, 'b', 5.0, []),
5353
([4, NULL, NULL, 5], ['d', NULL, NULL, 'e'], [4.0, NULL, NULL, 5.0], 'c', 6.0, [[1, 2, 3]]);
5454

55+
CREATE OR REPLACE TABLE time_df1 (
56+
time Int64,
57+
value Nullable(Float64),
58+
key Nullable(String)
59+
) ENGINE = Memory;
60+
INSERT INTO time_df1 VALUES
61+
(1, 1.0, 'x'),
62+
(20, 20.0, 'x'),
63+
(30, 30.0, 'x'),
64+
(40, 40.0, 'x'),
65+
(50, 50.0, 'x');
66+
67+
CREATE OR REPLACE TABLE time_df2 (
68+
time Int64,
69+
value Nullable(Float64),
70+
key Nullable(String)
71+
) ENGINE = Memory;
72+
INSERT INTO time_df2 VALUES
73+
(19, 19.0, 'x'),
74+
(21, 21.0, 'x'),
75+
(39, 39.0, 'x'),
76+
(49, 49.0, 'x'),
77+
(1000, 1000.0, 'x');
5578

5679
CREATE OR REPLACE TABLE struct (
5780
abc Tuple(

ibis/backends/clickhouse/compiler/relations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def _aggregation(op: ops.Aggregation, *, table, **kw):
103103
ops.CrossJoin: "CROSS",
104104
ops.LeftSemiJoin: "LEFT SEMI",
105105
ops.LeftAntiJoin: "LEFT ANTI",
106+
ops.AsOfJoin: "LEFT ASOF",
106107
}
107108

108109

ibis/backends/clickhouse/tests/test_select.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def awards_players(con):
2323
return con.table('awards_players')
2424

2525

26+
@pytest.fixture(scope='module')
27+
def time_left(con):
28+
return con.table('time_df1')
29+
30+
31+
@pytest.fixture(scope='module')
32+
def time_right(con):
33+
return con.table('time_df2')
34+
35+
2636
def test_timestamp_extract_field(alltypes, snapshot):
2737
t = alltypes.timestamp_col
2838
expr = alltypes[
@@ -354,3 +364,18 @@ def test_join_with_external_table(alltypes, df):
354364
expected = expected.sort_values('id').reset_index(drop=True)
355365

356366
tm.assert_frame_equal(result, expected, check_column_type=False)
367+
368+
369+
def test_asof_join(time_left, time_right):
370+
expr = time_left.asof_join(
371+
time_right,
372+
predicates=[
373+
time_left['key'] == time_right['key'],
374+
time_left['time'] >= time_right['time'],
375+
],
376+
)
377+
result = expr.execute()
378+
result['time'] = result['time_x']
379+
result.drop(['time_x', 'time_y'], axis=1, inplace=True)
380+
expected = pd.merge_asof(time_left.execute(), time_right.execute(), on='time')
381+
tm.assert_frame_equal(result[expected.columns], expected)

0 commit comments

Comments
 (0)