Taragolis commented on code in PR #36171:
URL: https://github.com/apache/airflow/pull/36171#discussion_r1423802816


##########
airflow/providers/amazon/aws/hooks/athena_sql.py:
##########
@@ -0,0 +1,107 @@
+# 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 functools import cached_property
+from typing import TYPE_CHECKING
+
+import pyathena
+from sqlalchemy import create_engine
+
+from airflow.exceptions import AirflowException
+from airflow.providers.common.sql.hooks.sql import DbApiHook
+
+if TYPE_CHECKING:
+    from pyathena.connection import Connection as AthenaConnection
+
+
+class AthenaSQLHook(DbApiHook):
+    """Execute statements against Amazon Athena.
+
+    .. note::
+    get_sqlalchemy_engine() and get_uri() depends on SQLAlchemy and PyAthena.
+    """
+
+    conn_name_attr = "athena_conn_id"
+    default_conn_name = "athena_default"
+    conn_type = "athena"
+    hook_name = "Amazon Athena"
+    supports_autocommit = True
+
+    def __init__(self, *args, athena_conn_id: str = "athena_default", 
**kwargs) -> None:
+        super().__init__(*args, **kwargs)
+        self.athena_conn_id = athena_conn_id
+
+    @cached_property
+    def conn(self):
+        return self.get_connection(self.athena_conn_id)  # type: 
ignore[attr-defined]
+
+    def _get_conn_params(self) -> dict[str, str | int | None]:
+        """Retrieve connection parameters."""
+        conn = self.conn
+
+        s3_staging_dir = conn.extra_dejson.get("s3_staging_dir", None)
+        if not s3_staging_dir:
+            raise AirflowException(
+                "S3 Staging directory (`s3_staging_dir`) must be specified in 
the extra field"
+            )
+
+        conn_params: dict[str, str | int | None] = {
+            "aws_access_key_id": conn.login if conn.login else None,
+            "aws_secret_access_key": conn.password if conn.password else None,
+            "schema_name": conn.schema if conn.schema else "default",
+            "region_name": conn.extra_dejson.get("region_name", "us-east-1"),

Review Comment:
   ```suggestion
               "region_name": conn.extra_dejson.get("region_name"),
   ```
   
   Better to get an error (`boto3` / `botocore`) if region not defined rather 
than send request thought "default" region. I'm not sure is it leads to 
additional costs or not but better to prevent this situation, as it done in 
regular AWS Hooks 



##########
airflow/providers/amazon/aws/hooks/athena_sql.py:
##########
@@ -0,0 +1,107 @@
+# 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 functools import cached_property
+from typing import TYPE_CHECKING
+
+import pyathena
+from sqlalchemy import create_engine
+
+from airflow.exceptions import AirflowException
+from airflow.providers.common.sql.hooks.sql import DbApiHook
+
+if TYPE_CHECKING:
+    from pyathena.connection import Connection as AthenaConnection
+
+
+class AthenaSQLHook(DbApiHook):
+    """Execute statements against Amazon Athena.
+
+    .. note::
+    get_sqlalchemy_engine() and get_uri() depends on SQLAlchemy and PyAthena.
+    """
+
+    conn_name_attr = "athena_conn_id"
+    default_conn_name = "athena_default"
+    conn_type = "athena"
+    hook_name = "Amazon Athena"
+    supports_autocommit = True
+
+    def __init__(self, *args, athena_conn_id: str = "athena_default", 
**kwargs) -> None:
+        super().__init__(*args, **kwargs)
+        self.athena_conn_id = athena_conn_id
+
+    @cached_property
+    def conn(self):
+        return self.get_connection(self.athena_conn_id)  # type: 
ignore[attr-defined]
+
+    def _get_conn_params(self) -> dict[str, str | int | None]:
+        """Retrieve connection parameters."""
+        conn = self.conn
+
+        s3_staging_dir = conn.extra_dejson.get("s3_staging_dir", None)
+        if not s3_staging_dir:
+            raise AirflowException(
+                "S3 Staging directory (`s3_staging_dir`) must be specified in 
the extra field"
+            )

Review Comment:
   According to the [Athena AWS API 
](https://docs.aws.amazon.com/athena/latest/APIReference/API_ResultConfiguration.html)OutputLocation
 is optional and could be skipped if Workgroup is provided and configured to 
output location.



##########
airflow/providers/amazon/aws/hooks/athena_sql.py:
##########
@@ -0,0 +1,107 @@
+# 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 functools import cached_property
+from typing import TYPE_CHECKING
+
+import pyathena
+from sqlalchemy import create_engine
+
+from airflow.exceptions import AirflowException
+from airflow.providers.common.sql.hooks.sql import DbApiHook
+
+if TYPE_CHECKING:
+    from pyathena.connection import Connection as AthenaConnection
+
+
+class AthenaSQLHook(DbApiHook):
+    """Execute statements against Amazon Athena.
+
+    .. note::
+    get_sqlalchemy_engine() and get_uri() depends on SQLAlchemy and PyAthena.
+    """
+
+    conn_name_attr = "athena_conn_id"
+    default_conn_name = "athena_default"
+    conn_type = "athena"
+    hook_name = "Amazon Athena"
+    supports_autocommit = True
+
+    def __init__(self, *args, athena_conn_id: str = "athena_default", 
**kwargs) -> None:
+        super().__init__(*args, **kwargs)
+        self.athena_conn_id = athena_conn_id
+
+    @cached_property
+    def conn(self):
+        return self.get_connection(self.athena_conn_id)  # type: 
ignore[attr-defined]
+
+    def _get_conn_params(self) -> dict[str, str | int | None]:
+        """Retrieve connection parameters."""
+        conn = self.conn
+
+        s3_staging_dir = conn.extra_dejson.get("s3_staging_dir", None)
+        if not s3_staging_dir:
+            raise AirflowException(
+                "S3 Staging directory (`s3_staging_dir`) must be specified in 
the extra field"
+            )
+
+        conn_params: dict[str, str | int | None] = {
+            "aws_access_key_id": conn.login if conn.login else None,
+            "aws_secret_access_key": conn.password if conn.password else None,

Review Comment:
   This implementation is limited by permanent credentials, and might not works 
with AssumeRole.
   Instead of provide: 
   - aws_access_key_id
   - aws_secret_access_key
   - region_name
   
   Better to create `boto3.session.Session` and provide it as `session` 
argument. In addition AwsGenericHook and 
[BaseSessionFactory](https://github.com/apache/airflow/blob/47a9c8a4a4ecc4da34bd210d56331e97f9fe8e7e/airflow/providers/amazon/aws/hooks/base_aws.py#L73)
 could handle different cases for retrieve credentials



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to