|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pandas as pd |
| 16 | + |
| 17 | +from vanna.capabilities.sql_runner import SqlRunner, RunSqlToolArgs |
| 18 | +from vanna.core.tool import ToolContext |
| 19 | + |
| 20 | + |
| 21 | +class ClickHouseRunner(SqlRunner): |
| 22 | + """ClickHouse implementation of the SqlRunner interface.""" |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + host: str, |
| 27 | + database: str, |
| 28 | + user: str, |
| 29 | + password: str, |
| 30 | + port: int = 9000, |
| 31 | + **kwargs, |
| 32 | + ): |
| 33 | + """Initialize with ClickHouse connection parameters. |
| 34 | +
|
| 35 | + Args: |
| 36 | + host: Database host address |
| 37 | + database: Database name |
| 38 | + user: Database user |
| 39 | + password: Database password |
| 40 | + port: Database port (default: 9000 for native protocol) |
| 41 | + **kwargs: Additional clickhouse_driver connection parameters |
| 42 | + """ |
| 43 | + try: |
| 44 | + from clickhouse_driver import Client |
| 45 | + |
| 46 | + self.Client = Client |
| 47 | + except ImportError as e: |
| 48 | + raise ImportError( |
| 49 | + "clickhouse-driver package is required. " |
| 50 | + "Install with: pip install clickhouse-driver" |
| 51 | + ) from e |
| 52 | + |
| 53 | + self.host = host |
| 54 | + self.port = port |
| 55 | + self.user = user |
| 56 | + self.password = password |
| 57 | + self.database = database |
| 58 | + self.kwargs = kwargs |
| 59 | + |
| 60 | + async def run_sql(self, args: RunSqlToolArgs, context: ToolContext) -> pd.DataFrame: |
| 61 | + """Execute SQL query against ClickHouse database and return results as DataFrame. |
| 62 | +
|
| 63 | + Args: |
| 64 | + args: SQL query arguments |
| 65 | + context: Tool execution context |
| 66 | +
|
| 67 | + Returns: |
| 68 | + DataFrame with query results |
| 69 | +
|
| 70 | + Raises: |
| 71 | + Exception: If query execution fails |
| 72 | + """ |
| 73 | + # Connect to the database |
| 74 | + client = self.Client( |
| 75 | + host=self.host, |
| 76 | + port=self.port, |
| 77 | + user=self.user, |
| 78 | + password=self.password, |
| 79 | + database=self.database, |
| 80 | + **self.kwargs, |
| 81 | + ) |
| 82 | + |
| 83 | + try: |
| 84 | + # Execute the query |
| 85 | + result = client.execute(args.sql, with_column_types=True) |
| 86 | + |
| 87 | + # result is a tuple: (data, [(column_name, column_type), ...]) |
| 88 | + data = result[0] |
| 89 | + columns = [col[0] for col in result[1]] |
| 90 | + |
| 91 | + # Create a pandas dataframe from the results |
| 92 | + df = pd.DataFrame(data, columns=columns) |
| 93 | + return df |
| 94 | + |
| 95 | + finally: |
| 96 | + client.disconnect() |
0 commit comments