eladkal commented on code in PR #25714: URL: https://github.com/apache/airflow/pull/25714#discussion_r951136511
########## airflow/providers/clickhouse/operators/clickhouse.py: ########## @@ -0,0 +1,72 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Sequence + +from airflow.models import BaseOperator +from airflow.providers.clickhouse.hooks.clickhouse import ClickHouseHook + +if TYPE_CHECKING: + from airflow.utils.context import Context + + +class ClickHouseOperator(BaseOperator): + """ + Executes SQL query in a ClickHouse database + + .. seealso:: + For more information on how to use this operator, take a look at the guide: + :ref:`howto/operator:ClickHouseOperator` + + :param sql: the SQL query to be executed. Can receive a str representing a + SQL statement, or you can provide .sql file having the query + :param params: substitution parameters for SELECT queries and data for INSERT queries. + :param database: database to query, if not provided schema from Connection will be used (optional) + :param result_processor: function to further process the Result from ClickHouse + :param clickhouse_conn_id: Reference to + :ref:`ClickHouse connection id<howto/connection:clickhouse>`. + """ + + template_fields: Sequence[str] = ('sql',) + + template_ext: Sequence[str] = (".sql",) + template_fields_renderers = {"sql": "sql"} + ui_color = '#ebcc34' + + def __init__( + self, + *, + sql: str, + clickhouse_conn_id: str = 'clickhouse_default', + params: Optional[Dict[str, Any]] = None, + database: Optional[str] = None, + result_processor: Optional[Callable] = None, + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.clickhouse_conn_id = clickhouse_conn_id + self.sql = sql + self.params = params + self.database = database + self.result_processor = result_processor + + def execute(self, context: 'Context'): + hook = ClickHouseHook(clickhouse_conn_id=self.clickhouse_conn_id, database=self.database) + + result = hook.query(sql=self.sql, params=self.params) + if self.result_processor: + self.result_processor(result) Review Comment: Where is `result_processor` implemented? ########## airflow/providers/clickhouse/sensors/clickhouse.py: ########## @@ -0,0 +1,71 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from typing import Any, Callable, Dict, Optional + +from airflow.providers.clickhouse.hooks.clickhouse import ClickHouseHook +from airflow.sensors.sql import SqlSensor + + +class ClickHouseSensor(SqlSensor): Review Comment: I asked it elsewhere but if ClickHouseHook will inhert from DBApiHook then we won't need this custom sensor because SqlSensor will support ClickHouse natively https://github.com/pateash/airflow/blob/f7e2ffe42bb7e957e4e16d0cb65e9541c87bd72b/airflow/providers/common/sql/sensors/sql.py#L80 ########## airflow/providers/clickhouse/hooks/clickhouse.py: ########## @@ -0,0 +1,139 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""This module allows connecting to a ClickHouse.""" +from typing import Any, Dict, Optional, Tuple + +from clickhouse_driver import Client as ClickHouseClient + +from airflow import AirflowException +from airflow.hooks.base import BaseHook + + +class ClickHouseHook(BaseHook): + """ + Interact with ClickHouse. + Performs a connection to ClickHouse and retrieves client. + + :param clickhouse_conn_id: Reference to + :ref:`ClickHouse connection id<howto/connection:clickhouse>`. + :param database: database for the hook, if not provided schema from Connection will be used (optional). + """ + + conn_name_attr = 'clickhouse_conn_id' + default_conn_name = 'clickhouse_default' + conn_type = 'clickhouse' + hook_name = 'ClickHouse' + + def __init__( + self, clickhouse_conn_id: str = default_conn_name, database: Optional[str] = None, *args, **kwargs + ) -> None: + super().__init__(*args, **kwargs) + self.clickhouse_conn_id = clickhouse_conn_id + self.database = database + + self.client: Optional[ClickHouseClient] = None + self.get_conn() + + def get_conn(self) -> ClickHouseClient: + """Function that initiates a new ClickHouse connection""" + if self.client is not None: + return self.client + + conn = self.get_connection(self.clickhouse_conn_id) + connection_kwargs = conn.extra_dejson.copy() + + if conn.port: + connection_kwargs.update(port=int(conn.port)) + if conn.login: + connection_kwargs.update(user=conn.login) + if conn.password: + connection_kwargs.update(password=conn.password) + + # if database is provided use it or use from schema + if self.database: + connection_kwargs.update(database=self.database) + elif conn.schema: + connection_kwargs.update(database=conn.schema) Review Comment: I think this is a source for confusion? Maybe we should customize the connection in the like we do with other providers? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
