eladkal commented on code in PR #24415:
URL: https://github.com/apache/airflow/pull/24415#discussion_r896739291


##########
docs/apache-airflow-providers-trino/operators/trino.rst:
##########
@@ -0,0 +1,56 @@
+ .. 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.
+
+.. _howto/operator:TrinoOperator:
+
+TrinoOperator
+=============
+
+Use the :class:`TrinoOperator <airflow.providers.trino.operators.trino>` to 
execute
+SQL commands in a `Trino <https://trino.io/>`__ query engine.
+
+
+Using the Operator
+^^^^^^^^^^^^^^^^^^
+
+Use the ``trino_conn_id`` argument to connect to your Trino instance where
+the connection metadata is structured as follows:
+
+.. list-table:: Trino Airflow Connection Metadata
+   :widths: 25 25
+   :header-rows: 1
+
+   * - Parameter
+     - Input
+   * - Host: string
+     - Trino server host name (or) ip address
+   * - Login: string
+     - Trino user name
+   * - Port: string
+     - Set Port to connect to the Trino server, for example on AWS EMR, Trino 
uses port 8889
+
+An example usage of the TrinoOperator is as follows:
+
+.. exampleinclude:: /../../tests/system/providers/trino/example_trino.py
+    :language: python
+    :start-after: [START howto_operator_trino]
+    :end-before: [END howto_operator_trino]
+
+.. note::
+
+  This Operator can be used to run any syntactically correct Trino query(one 
at a time), but we cannot run
+  multiple queries in the same task.

Review Comment:
   If we can lets point to Trino documentation that states this. This is a 
limitation coming from Trino not from Airflow so it's important that users will 
understand this.



##########
docs/apache-airflow-providers-trino/operators/trino.rst:
##########
@@ -0,0 +1,56 @@
+ .. 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.
+
+.. _howto/operator:TrinoOperator:
+
+TrinoOperator
+=============
+
+Use the :class:`TrinoOperator <airflow.providers.trino.operators.trino>` to 
execute
+SQL commands in a `Trino <https://trino.io/>`__ query engine.
+
+
+Using the Operator
+^^^^^^^^^^^^^^^^^^
+
+Use the ``trino_conn_id`` argument to connect to your Trino instance where
+the connection metadata is structured as follows:
+
+.. list-table:: Trino Airflow Connection Metadata
+   :widths: 25 25
+   :header-rows: 1
+
+   * - Parameter
+     - Input
+   * - Host: string
+     - Trino server host name (or) ip address
+   * - Login: string
+     - Trino user name
+   * - Port: string
+     - Set Port to connect to the Trino server, for example on AWS EMR, Trino 
uses port 8889

Review Comment:
   This is information for connection doc not the operator doc.
   Example: 
https://github.com/apache/airflow/blob/5c0e98cc770b4f055dbd1c0b60ccbd69f3166da7/docs/apache-airflow-providers-salesforce/connections/salesforce.rst



##########
airflow/providers/trino/operators/trino.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.
+"""This module contains the Trino operator."""
+
+from typing import TYPE_CHECKING, Any, Callable, Optional, Sequence
+
+from airflow.models import BaseOperator
+from airflow.providers.trino.hooks.trino import TrinoHook
+
+if TYPE_CHECKING:
+    from airflow.utils.context import Context
+
+
+class TrinoOperator(BaseOperator):
+    """
+    Executes sql code using a specific Trino query Engine.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:TrinoOperator`
+
+    :param sql: the SQL code to be executed as a single string, or a reference
+        to a template file.Template references are recognized by str ending in 
'.sql'
+    :param trino_conn_id: id of the connection config for the target Trino
+        environment
+    :param autocommit: What to set the connection's autocommit setting to
+        before executing the query
+    :param handler: The result handler which is called with the result of each 
statement.
+    :param parameters: (optional) the parameters to render the SQL query with.
+    """
+
+    template_fields: Sequence[str] = ('sql',)
+    template_fields_renderers = {'sql': 'sql'}
+    template_ext: Sequence[str] = ('.sql',)
+    ui_color = '#ededed'
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        trino_conn_id: str = "trino_default",
+        autocommit: bool = False,
+        parameters: Optional[tuple] = None,
+        handler: Optional[Callable] = None,
+        **kwargs: Any,
+    ) -> None:
+        super().__init__(**kwargs)
+        self.sql = sql
+        self.trino_conn_id = trino_conn_id
+        self.hook: Optional[TrinoHook] = None
+        self.autocommit = autocommit
+        self.parameters = parameters
+        self.handler = handler
+
+    def get_hook(self) -> TrinoHook:
+        """Get Trino hook"""
+        return TrinoHook(
+            trino_conn_id=self.trino_conn_id,
+        )
+

Review Comment:
   We need `on_kill()` implementation to kill Trino query if Airflow task is 
killed.



##########
tests/system/providers/trino/example_trino_query.sql:
##########
@@ -0,0 +1,20 @@
+/*
+ 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.
+*/
+
+SELECT * FROM {{ params.SCHEMA }}.{{ params.TABLE }}

Review Comment:
   I think this file is redundant. You can just place this query in the sql 
parameter of the operator being used.
   There is no reason to test/show example of how to use templating from `.sql` 
file in TrinoOperator example.
   It's also hard for people who will read the example from the docs as they 
won't see what .sql file contains.



##########
airflow/providers/trino/operators/trino.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.
+"""This module contains the Trino operator."""
+
+from typing import TYPE_CHECKING, Any, Callable, Optional, Sequence
+
+from airflow.models import BaseOperator
+from airflow.providers.trino.hooks.trino import TrinoHook
+
+if TYPE_CHECKING:
+    from airflow.utils.context import Context
+
+
+class TrinoOperator(BaseOperator):
+    """
+    Executes sql code using a specific Trino query Engine.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:TrinoOperator`
+
+    :param sql: the SQL code to be executed as a single string, or a reference
+        to a template file.Template references are recognized by str ending in 
'.sql'
+    :param trino_conn_id: id of the connection config for the target Trino
+        environment
+    :param autocommit: What to set the connection's autocommit setting to
+        before executing the query
+    :param handler: The result handler which is called with the result of each 
statement.
+    :param parameters: (optional) the parameters to render the SQL query with.
+    """
+
+    template_fields: Sequence[str] = ('sql',)

Review Comment:
   I think in other operators we also template `parameters`?



##########
tests/system/providers/trino/example_trino.py:
##########
@@ -0,0 +1,89 @@
+#
+# 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.
+"""
+Example DAG using TrinoOperator.
+"""
+
+from datetime import datetime
+
+from airflow import models
+from airflow.providers.trino.operators.trino import TrinoOperator
+
+SCHEMA = "hive.cities"
+TABLE = "city"

Review Comment:
   Some of the queries use hard coded schema and table though you defined it 
globally.



-- 
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]

Reply via email to