|
1 | | -import pytest |
2 | | -import pyarrow as pa |
3 | | - |
4 | | -from clickhouse_connect.driver import Client |
5 | | -from clickhouse_connect.driver.ddl import ( |
6 | | - arrow_schema_to_column_defs, |
7 | | - create_table, |
8 | | - create_table_from_arrow_schema, |
9 | | -) |
10 | | - |
11 | | -pytest.importorskip("pyarrow") |
12 | | - |
13 | | - |
14 | | -def test_arrow_create_table_and_insert(test_client: Client): |
15 | | - if not test_client.min_version("20"): |
16 | | - pytest.skip( |
17 | | - f"Not supported server version {test_client.server_version}" |
18 | | - ) |
19 | | - |
20 | | - table_name = "test_arrow_basic_integration" |
21 | | - |
22 | | - test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
23 | | - |
24 | | - schema = pa.schema( |
25 | | - [ |
26 | | - ("id", pa.int64()), |
27 | | - ("name", pa.string()), |
28 | | - ("score", pa.float32()), |
29 | | - ("flag", pa.bool_()), |
30 | | - ] |
31 | | - ) |
32 | | - |
33 | | - ddl = create_table_from_arrow_schema( |
34 | | - table_name=table_name, |
35 | | - schema=schema, |
36 | | - engine="MergeTree", |
37 | | - engine_params={"ORDER BY": "id"}, |
38 | | - ) |
39 | | - test_client.command(ddl) |
40 | | - |
41 | | - arrow_table = pa.table( |
42 | | - { |
43 | | - "id": [1, 2], |
44 | | - "name": ["a", "b"], |
45 | | - "score": [1.5, 2.5], |
46 | | - "flag": [True, False], |
47 | | - }, |
48 | | - schema=schema, |
49 | | - ) |
50 | | - |
51 | | - test_client.insert_arrow(table=table_name, arrow_table=arrow_table) |
52 | | - |
53 | | - result = test_client.query( |
54 | | - f"SELECT id, name, score, flag FROM {table_name} ORDER BY id" |
55 | | - ) |
56 | | - assert result.result_rows == [ |
57 | | - (1, "a", 1.5, True), |
58 | | - (2, "b", 2.5, False), |
59 | | - ] |
60 | | - |
61 | | - test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
62 | | - |
63 | | - |
64 | | -def test_arrow_schema_to_column_defs(test_client: Client): |
65 | | - table_name = "test_arrow_manual_integration" |
66 | | - |
67 | | - test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
68 | | - |
69 | | - schema = pa.schema( |
70 | | - [ |
71 | | - ("id", pa.int64()), |
72 | | - ("name", pa.string()), |
73 | | - ] |
74 | | - ) |
75 | | - |
76 | | - # check using the explicit helper path. |
77 | | - col_defs = arrow_schema_to_column_defs(schema) |
78 | | - |
79 | | - ddl = create_table( |
80 | | - table_name=table_name, |
81 | | - columns=col_defs, |
82 | | - engine="MergeTree", |
83 | | - engine_params={"ORDER BY": "id"}, |
84 | | - ) |
85 | | - test_client.command(ddl) |
86 | | - |
87 | | - arrow_table = pa.table( |
88 | | - { |
89 | | - "id": [10, 20], |
90 | | - "name": ["x", "y"], |
91 | | - }, |
92 | | - schema=schema, |
93 | | - ) |
94 | | - |
95 | | - test_client.insert_arrow(table=table_name, arrow_table=arrow_table) |
96 | | - |
97 | | - result = test_client.query(f"SELECT id, name FROM {table_name} ORDER BY id") |
98 | | - assert result.result_rows == [ |
99 | | - (10, "x"), |
100 | | - (20, "y"), |
101 | | - ] |
102 | | - |
103 | | - test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
| 1 | +import pytest |
| 2 | +import pyarrow as pa |
| 3 | + |
| 4 | +from clickhouse_connect.driver import Client |
| 5 | +from clickhouse_connect.driver.ddl import ( |
| 6 | + arrow_schema_to_column_defs, |
| 7 | + create_table, |
| 8 | + create_table_from_arrow_schema, |
| 9 | +) |
| 10 | + |
| 11 | +pytest.importorskip("pyarrow") |
| 12 | + |
| 13 | + |
| 14 | +def test_arrow_create_table_and_insert(test_client: Client): |
| 15 | + if not test_client.min_version("20"): |
| 16 | + pytest.skip( |
| 17 | + f"Not supported server version {test_client.server_version}" |
| 18 | + ) |
| 19 | + |
| 20 | + table_name = "test_arrow_basic_integration" |
| 21 | + |
| 22 | + test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
| 23 | + |
| 24 | + schema = pa.schema( |
| 25 | + [ |
| 26 | + ("id", pa.int64()), |
| 27 | + ("name", pa.string()), |
| 28 | + ("score", pa.float32()), |
| 29 | + ("flag", pa.bool_()), |
| 30 | + ] |
| 31 | + ) |
| 32 | + |
| 33 | + ddl = create_table_from_arrow_schema( |
| 34 | + table_name=table_name, |
| 35 | + schema=schema, |
| 36 | + engine="MergeTree", |
| 37 | + engine_params={"ORDER BY": "id"}, |
| 38 | + ) |
| 39 | + test_client.command(ddl) |
| 40 | + |
| 41 | + arrow_table = pa.table( |
| 42 | + { |
| 43 | + "id": [1, 2], |
| 44 | + "name": ["a", "b"], |
| 45 | + "score": [1.5, 2.5], |
| 46 | + "flag": [True, False], |
| 47 | + }, |
| 48 | + schema=schema, |
| 49 | + ) |
| 50 | + |
| 51 | + test_client.insert_arrow(table=table_name, arrow_table=arrow_table) |
| 52 | + |
| 53 | + result = test_client.query( |
| 54 | + f"SELECT id, name, score, flag FROM {table_name} ORDER BY id" |
| 55 | + ) |
| 56 | + assert result.result_rows == [ |
| 57 | + (1, "a", 1.5, True), |
| 58 | + (2, "b", 2.5, False), |
| 59 | + ] |
| 60 | + |
| 61 | + test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
| 62 | + |
| 63 | + |
| 64 | +def test_arrow_schema_to_column_defs(test_client: Client): |
| 65 | + table_name = "test_arrow_manual_integration" |
| 66 | + |
| 67 | + test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
| 68 | + |
| 69 | + schema = pa.schema( |
| 70 | + [ |
| 71 | + ("id", pa.int64()), |
| 72 | + ("name", pa.string()), |
| 73 | + ] |
| 74 | + ) |
| 75 | + |
| 76 | + # check using the explicit helper path. |
| 77 | + col_defs = arrow_schema_to_column_defs(schema) |
| 78 | + |
| 79 | + ddl = create_table( |
| 80 | + table_name=table_name, |
| 81 | + columns=col_defs, |
| 82 | + engine="MergeTree", |
| 83 | + engine_params={"ORDER BY": "id"}, |
| 84 | + ) |
| 85 | + test_client.command(ddl) |
| 86 | + |
| 87 | + arrow_table = pa.table( |
| 88 | + { |
| 89 | + "id": [10, 20], |
| 90 | + "name": ["x", "y"], |
| 91 | + }, |
| 92 | + schema=schema, |
| 93 | + ) |
| 94 | + |
| 95 | + test_client.insert_arrow(table=table_name, arrow_table=arrow_table) |
| 96 | + |
| 97 | + result = test_client.query(f"SELECT id, name FROM {table_name} ORDER BY id") |
| 98 | + assert result.result_rows == [ |
| 99 | + (10, "x"), |
| 100 | + (20, "y"), |
| 101 | + ] |
| 102 | + |
| 103 | + test_client.command(f"DROP TABLE IF EXISTS {table_name}") |
0 commit comments