bilalaslamseattle commented on code in PR #30204: URL: https://github.com/apache/airflow/pull/30204#discussion_r1150505379
########## airflow/providers/databricks/sensors/sql.py: ########## @@ -0,0 +1,124 @@ +# +# 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 contains Databricks sensors.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence + +from airflow.compat.functools import cached_property +from airflow.providers.common.sql.hooks.sql import fetch_all_handler +from airflow.providers.databricks.hooks.databricks_sql import DatabricksSqlHook +from airflow.sensors.base import BaseSensorOperator + +if TYPE_CHECKING: + from airflow.utils.context import Context + + +class DatabricksSqlSensor(BaseSensorOperator): + """ + Sensor to execute SQL statements on a Delta table via Databricks. Review Comment: This reads wrong because SQL statements aren't really run on Delta tables ...? Rephrase as: "Sensor that runs a SQL query on Databricks" ########## airflow/providers/databricks/sensors/sql.py: ########## @@ -0,0 +1,124 @@ +# +# 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 contains Databricks sensors.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence + +from airflow.compat.functools import cached_property +from airflow.providers.common.sql.hooks.sql import fetch_all_handler +from airflow.providers.databricks.hooks.databricks_sql import DatabricksSqlHook +from airflow.sensors.base import BaseSensorOperator + +if TYPE_CHECKING: + from airflow.utils.context import Context + + +class DatabricksSqlSensor(BaseSensorOperator): + """ + Sensor to execute SQL statements on a Delta table via Databricks. + + :param databricks_conn_id: Reference to :ref:`Databricks + connection id<howto/connection:databricks>` (templated), defaults to + DatabricksSqlHook.default_conn_name + :param http_path: Optional string specifying HTTP path of Databricks SQL Endpoint or cluster. + If not specified, it should be either specified in the Databricks connection's + extra parameters, or ``sql_endpoint_name`` must be specified. Review Comment: Please check `sql_endpoint_name` parameter ... can this be better named as `sql_warehouse_name`? ########## airflow/providers/databricks/sensors/sql.py: ########## @@ -0,0 +1,124 @@ +# +# 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 contains Databricks sensors.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence + +from airflow.compat.functools import cached_property +from airflow.providers.common.sql.hooks.sql import fetch_all_handler +from airflow.providers.databricks.hooks.databricks_sql import DatabricksSqlHook +from airflow.sensors.base import BaseSensorOperator + +if TYPE_CHECKING: + from airflow.utils.context import Context + + +class DatabricksSqlSensor(BaseSensorOperator): + """ + Sensor to execute SQL statements on a Delta table via Databricks. + + :param databricks_conn_id: Reference to :ref:`Databricks + connection id<howto/connection:databricks>` (templated), defaults to + DatabricksSqlHook.default_conn_name + :param http_path: Optional string specifying HTTP path of Databricks SQL Endpoint or cluster. Review Comment: - SQL Warehouse, not endpoint. - All Purpose cluster not cluster. ########## airflow/providers/databricks/sensors/sql.py: ########## @@ -0,0 +1,124 @@ +# +# 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 contains Databricks sensors.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence + +from airflow.compat.functools import cached_property +from airflow.providers.common.sql.hooks.sql import fetch_all_handler +from airflow.providers.databricks.hooks.databricks_sql import DatabricksSqlHook +from airflow.sensors.base import BaseSensorOperator + +if TYPE_CHECKING: + from airflow.utils.context import Context + + +class DatabricksSqlSensor(BaseSensorOperator): + """ + Sensor to execute SQL statements on a Delta table via Databricks. + + :param databricks_conn_id: Reference to :ref:`Databricks + connection id<howto/connection:databricks>` (templated), defaults to + DatabricksSqlHook.default_conn_name + :param http_path: Optional string specifying HTTP path of Databricks SQL Endpoint or cluster. + If not specified, it should be either specified in the Databricks connection's + extra parameters, or ``sql_endpoint_name`` must be specified. + :param sql_endpoint_name: Optional name of Databricks SQL Endpoint. If not specified, ``http_path`` + must be provided as described above, defaults to None + :param session_configuration: An optional dictionary of Spark session parameters. If not specified, + it could be specified in the Databricks connection's extra parameters., defaults to None + :param http_headers: An optional list of (k, v) pairs + that will be set as HTTP headers on every request. (templated). + :param catalog: An optional initial catalog to use. + Requires DBR version 9.0+ (templated), defaults to "" Review Comment: I would spell out Databricks Runtime ########## airflow/providers/databricks/sensors/sql.py: ########## @@ -0,0 +1,124 @@ +# +# 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 contains Databricks sensors.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence + +from airflow.compat.functools import cached_property +from airflow.providers.common.sql.hooks.sql import fetch_all_handler +from airflow.providers.databricks.hooks.databricks_sql import DatabricksSqlHook +from airflow.sensors.base import BaseSensorOperator + +if TYPE_CHECKING: + from airflow.utils.context import Context + + +class DatabricksSqlSensor(BaseSensorOperator): + """ + Sensor to execute SQL statements on a Delta table via Databricks. + + :param databricks_conn_id: Reference to :ref:`Databricks + connection id<howto/connection:databricks>` (templated), defaults to + DatabricksSqlHook.default_conn_name + :param http_path: Optional string specifying HTTP path of Databricks SQL Endpoint or cluster. + If not specified, it should be either specified in the Databricks connection's + extra parameters, or ``sql_endpoint_name`` must be specified. + :param sql_endpoint_name: Optional name of Databricks SQL Endpoint. If not specified, ``http_path`` + must be provided as described above, defaults to None + :param session_configuration: An optional dictionary of Spark session parameters. If not specified, Review Comment: What happens when you use a SQL Warehouse and specify Spark session params? ########## tests/providers/databricks/sensors/test_sql.py: ########## @@ -0,0 +1,81 @@ +# +# 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 datetime import datetime, timedelta +from unittest.mock import patch + +import pytest + +from airflow.exceptions import AirflowException +from airflow.models import DAG +from airflow.providers.databricks.sensors.sql import DatabricksSqlSensor +from airflow.utils import timezone + +TASK_ID = "db-sensor" +DEFAULT_CONN_ID = "databricks_default" +HOST = "xx.cloud.databricks.com" +HOST_WITH_SCHEME = "https://xx.cloud.databricks.com" +TOKEN = "token" + +DEFAULT_SCHEMA = "schema1" +DEFAULT_CATALOG = "catalog1" +DEFAULT_TABLE = "table1" +DEFAULT_SQL_ENDPOINT = "sql_warehouse_default" Review Comment: Warehouse ########## tests/providers/databricks/sensors/test_sql.py: ########## @@ -0,0 +1,81 @@ +# +# 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 datetime import datetime, timedelta +from unittest.mock import patch + +import pytest + +from airflow.exceptions import AirflowException +from airflow.models import DAG +from airflow.providers.databricks.sensors.sql import DatabricksSqlSensor +from airflow.utils import timezone + +TASK_ID = "db-sensor" +DEFAULT_CONN_ID = "databricks_default" +HOST = "xx.cloud.databricks.com" +HOST_WITH_SCHEME = "https://xx.cloud.databricks.com" +TOKEN = "token" Review Comment: Maybe explicitly make this `PERSONAL_ACCESS_TOKEN`? ########## docs/apache-airflow-providers-databricks/operators/sql.rst: ########## @@ -84,3 +84,32 @@ An example usage of the DatabricksSqlOperator to perform statements from a file :language: python :start-after: [START howto_operator_databricks_sql_multiple_file] :end-before: [END howto_operator_databricks_sql_multiple_file] + + +DatabricksSqlSensor +=================== + +Use the :class:`~airflow.providers.databricks.sensors.sql.DatabricksSqlSensor` to run the sensor +for a specific Delta table on a Databricks SQL workspace. + +Using the Sensor +---------------- + +The sensor executes the SQL statement supplied by the user. The only required parameters are: + +* ``sql`` - SQL query to execute for the sensor. + +* One of ``sql_endpoint_name`` (name of Databricks SQL endpoint to use) or ``http_path`` (HTTP path for Databricks SQL endpoint or Databricks cluster). Review Comment: Warehouse -- 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]
