|
| 1 | +import subprocess |
| 2 | +import time |
| 3 | +from datafusion import SessionContext |
| 4 | +from datafusion_table_providers import clickhouse # hypothetical provider |
| 5 | + |
| 6 | +def run_docker_container(): |
| 7 | + subprocess.run( |
| 8 | + ["docker", "run", "--name", "clickhouse", |
| 9 | + "-e", "CLICKHOUSE_USER=user", |
| 10 | + "-e", "CLICKHOUSE_PASSWORD=secret", |
| 11 | + "-p", "8123:8123", |
| 12 | + "-d", "clickhouse/clickhouse-server:latest"], |
| 13 | + check=True, |
| 14 | + ) |
| 15 | + time.sleep(20) |
| 16 | + |
| 17 | +def create_schema(): |
| 18 | + sql = r""" |
| 19 | + CREATE TABLE companies ( |
| 20 | + id UInt32, |
| 21 | + name String, |
| 22 | + founded Date, |
| 23 | + revenue Decimal(18,2), |
| 24 | + is_active Bool, |
| 25 | + tags Array(String) |
| 26 | + ) ENGINE = MergeTree() |
| 27 | + ORDER BY id; |
| 28 | +
|
| 29 | + INSERT INTO companies VALUES |
| 30 | + (1, 'Acme Corporation', '1999-03-12', 12500000.50, 1, ['manufacturing', 'global']), |
| 31 | + (2, 'Widget Inc.', '2005-07-01', 4500000.00, 1, ['gadgets', 'innovation']), |
| 32 | + (3, 'Gizmo Corp.', '2010-11-23', 780000.75, 0, ['hardware']), |
| 33 | + (4, 'Tech Solutions', '2018-01-15', 220000.00, 1, ['consulting','it']), |
| 34 | + (5, 'Data Innovations', '2021-05-10', 98000.99, 1, ['analytics','startup']); |
| 35 | +
|
| 36 | + CREATE VIEW companies_param_view AS |
| 37 | + SELECT id, name, founded, revenue, is_active, tags |
| 38 | + FROM companies |
| 39 | + WHERE name = {name:String}; |
| 40 | + """ |
| 41 | + subprocess.run( |
| 42 | + ["docker", "exec", "-i", "clickhouse", "clickhouse-client", "--multiquery", "--query", sql], |
| 43 | + check=True |
| 44 | + ) |
| 45 | + |
| 46 | +def stop_container(): |
| 47 | + subprocess.run(["docker", "stop", "clickhouse"], check=True) |
| 48 | + subprocess.run(["docker", "rm", "clickhouse"], check=True) |
| 49 | + |
| 50 | +class TestClickHouseParameterized: |
| 51 | + @classmethod |
| 52 | + def setup_class(cls): |
| 53 | + run_docker_container() |
| 54 | + create_schema() |
| 55 | + cls.ctx = SessionContext() |
| 56 | + connection_param = { |
| 57 | + "url": "http://localhost:8123", |
| 58 | + "database": "default", |
| 59 | + "user": "user", |
| 60 | + "password": "secret" |
| 61 | + } |
| 62 | + cls.pool = clickhouse.ClickHouseTableFactory(connection_param) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def teardown_class(cls): |
| 66 | + stop_container() |
| 67 | + |
| 68 | + def test_get_tables(self): |
| 69 | + tables = self.pool.tables() |
| 70 | + assert "companies" in tables |
| 71 | + assert "companies_param_view" in tables |
| 72 | + |
| 73 | + def test_parameterized_view(self): |
| 74 | + # Register provider so DF can access the view\ |
| 75 | + |
| 76 | + self.ctx.register_table_provider( |
| 77 | + "companies_param_view", |
| 78 | + self.pool.get_table("companies_param_view", [("name", "Gizmo Corp.")]) |
| 79 | + ) |
| 80 | + |
| 81 | + # Query using parameter |
| 82 | + df = self.ctx.sql( |
| 83 | + "SELECT id, name, revenue FROM companies_param_view" |
| 84 | + ) |
| 85 | + rows = df.collect()[0] |
| 86 | + |
| 87 | + assert len(rows["name"]) == 1 |
| 88 | + assert rows["name"][0].as_py() == "Gizmo Corp." |
| 89 | + assert rows["revenue"][0].as_py() == 780000.75 |
0 commit comments