alexott commented on code in PR #49516: URL: https://github.com/apache/airflow/pull/49516#discussion_r2070168781
########## providers/databricks/src/airflow/providers/databricks/sensors/databricks.py: ########## @@ -0,0 +1,152 @@ +# +# 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 __future__ import annotations + +from collections.abc import Sequence +from functools import cached_property +from typing import TYPE_CHECKING, Any + +from airflow.configuration import conf +from airflow.exceptions import AirflowException +from airflow.providers.databricks.hooks.databricks import DatabricksHook, SQLStatementState +from airflow.providers.databricks.operators.databricks import DEFER_METHOD_NAME +from airflow.providers.databricks.utils.mixins import DatabricksSQLStatementsMixin +from airflow.providers.databricks.version_compat import AIRFLOW_V_3_0_PLUS + +if AIRFLOW_V_3_0_PLUS: + from airflow.sdk import BaseSensorOperator +else: + from airflow.sensors.base import BaseSensorOperator + +if TYPE_CHECKING: + from airflow.utils.context import Context + +XCOM_STATEMENT_ID_KEY = "statement_id" + + +class DatabricksSQLStatementsSensor(DatabricksSQLStatementsMixin, BaseSensorOperator): + """DatabricksSQLStatementsSensor.""" + + template_fields: Sequence[str] = ( + "databricks_conn_id", + "statement", + "statement_id", + ) + template_ext: Sequence[str] = (".json-tpl",) + ui_color = "#1CB1C2" + ui_fgcolor = "#fff" + + def __init__( + self, + warehouse_id: str, + *, + statement: str | None = None, + statement_id: str | None = None, + catalog: str | None = None, + schema: str | None = None, + parameters: list[dict[str, Any]] | None = None, + databricks_conn_id: str = "databricks_default", + polling_period_seconds: int = 30, + databricks_retry_limit: int = 3, + databricks_retry_delay: int = 1, + databricks_retry_args: dict[Any, Any] | None = None, + do_xcom_push: bool = True, + wait_for_termination: bool = True, + timeout: float = 3600, + deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False), + **kwargs, + ): + super().__init__(**kwargs) + self.statement = statement + self.warehouse_id = warehouse_id + self.catalog = catalog + self.schema = schema + self.statement_id = statement_id # Optional Review Comment: We need to fail if both statement and statement id are None, or both are defined ########## providers/databricks/tests/system/databricks/example_databricks_sensors.py: ########## @@ -67,6 +70,18 @@ ) # [END howto_sensor_databricks_sql] + # [START howto_sensor_databricks_sql_statement] + # Example of using the DatabricksSQLStatementSensor to wait for a query + # to successfully run. + sql_statement_sensor = DatabricksSQLStatementsSensor( + task_id="sql_statement_sensor_task", + databricks_conn_id=connection_id, + warehouse_id=WAREHOUSE_ID, + statement_id="statement_id", Review Comment: Statement ID exists only when it's created by a corresponding api. SQL could be more appropriate here ########## providers/databricks/tests/system/databricks/example_databricks_sensors.py: ########## @@ -30,6 +31,8 @@ # [DAG name to be shown on Airflow UI] DAG_ID = "example_databricks_sensor" +WAREHOUSE_ID = os.environ.get("WAREHOUSE_ID", "cf414a2206dfb397") Review Comment: I would prefer to avoid having a default value, or at least put it into a separate constant and add a note to change it. -- 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]
