You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/08/28 21:39:57 UTC

[GitHub] [airflow] mariotaddeucci opened a new pull request #17887: New google operator: SQLToGoogleSheetsOperator

mariotaddeucci opened a new pull request #17887:
URL: https://github.com/apache/airflow/pull/17887


   Adds an operator to send sql results to google spreadsheet.
   
   ---
   **^ Add meaningful description above**
   
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/main/UPDATING.md).
   


-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] ashb commented on pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
ashb commented on pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#issuecomment-912039388


   Hmmm, curious, this seems to be causing the docs build to fail on mster, but it passed on this PR branch.


-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] github-actions[bot] commented on pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#issuecomment-910220791


   The PR is likely OK to be merged with just subset of tests for default Python and Database versions without running the full matrix of tests, because it does not modify the core of Airflow. If the committers decide that the full tests matrix is needed, they will add the label 'full tests needed'. Then you should rebase to the latest main or amend the last commit of the PR, and push it with --force-with-lease.


-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] uranusjr commented on a change in pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
uranusjr commented on a change in pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#discussion_r698339206



##########
File path: airflow/providers/google/suite/transfers/sql_to_sheets.py
##########
@@ -0,0 +1,140 @@
+# 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.
+
+
+import datetime
+import decimal
+from contextlib import closing
+from typing import Any, Iterable, Mapping, Optional, Sequence, Union
+
+from airflow.operators.sql import BaseSQLOperator
+from airflow.providers.google.suite.hooks.sheets import GSheetsHook
+
+
+class SQLToGoogleSheetsOperator(BaseSQLOperator):
+    """
+    Copy data from SQL results to provided Google Spreadsheet.
+
+    :param sql: The SQL to execute.
+    :type sql: str
+    :param spreadsheet_id: The Google Sheet ID to interact with.
+    :type spreadsheet_id: str
+    :param conn_id: the connection ID used to connect to the database.
+    :type sql_conn_id: str
+    :param parameters: The parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param database: name of database which overwrite the defined one in connection
+    :type database: str
+    :param spreadsheet_range: The A1 notation of the values to retrieve.
+    :type spreadsheet_range: str
+    :param gcp_conn_id: The connection ID to use when fetching connection info.
+    :type gcp_conn_id: str
+    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
+        if any. For this to work, the service account making the request must have
+        domain-wide delegation enabled.
+    :type delegate_to: str
+    :param impersonation_chain: Optional service account to impersonate using short-term
+        credentials, or chained list of accounts required to get the access_token
+        of the last account in the list, which will be impersonated in the request.
+        If set as a string, the account must grant the originating account
+        the Service Account Token Creator IAM role.
+        If set as a sequence, the identities from the list must grant
+        Service Account Token Creator IAM role to the directly preceding identity, with first
+        account from the list granting this role to the originating account (templated).
+    :type impersonation_chain: Union[str, Sequence[str]]
+    """
+
+    template_fields = [
+        "sql",
+        "spreadsheet_id",
+        "spreadsheet_range",
+        "impersonation_chain",
+    ]
+
+    template_ext = (".sql",)
+    ui_color = "#a0e08c"
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        spreadsheet_id: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        database: str = None,
+        spreadsheet_range: str = "Sheet1",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+
+        self.sql = sql
+        self.conn_id = sql_conn_id
+        self.database = database
+        self.parameters = parameters
+        self.gcp_conn_id = gcp_conn_id
+        self.spreadsheet_id = spreadsheet_id
+        self.spreadsheet_range = spreadsheet_range
+        self.delegate_to = delegate_to
+        self.impersonation_chain = impersonation_chain
+
+    def _data_prep(self, data):
+        for row in data:
+            item_list = []
+            for item in row:
+                if type(item) is datetime.date:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is datetime.datetime:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is decimal.Decimal:
+                    item = float(item)
+                item_list.append(item)
+            yield item_list
+
+    def _get_data(self):
+        hook = self.get_db_hook()
+        with closing(hook.get_conn()) as conn:
+            with closing(conn.cursor()) as cur:
+                self.log.info("Executing query")
+                if self.parameters is not None:
+                    cur.execute(self.sql, self.parameters)
+                else:
+                    cur.execute(self.sql)
+
+                yield [field[0] for field in cur.description]
+                yield from self._data_prep(cur.fetchall())

Review comment:
       ```suggestion
           with closing(hook.get_conn()) as conn, closing(conn.cursor()) as cur:
               self.log.info("Executing query")
               cur.execute(self.sql, self.parameters or ())
   
               yield [field[0] for field in cur.description]
               yield from self._data_prep(cur.fetchall())
   ```




-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] mariotaddeucci commented on a change in pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
mariotaddeucci commented on a change in pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#discussion_r698676864



##########
File path: airflow/providers/google/suite/transfers/sql_to_sheets.py
##########
@@ -0,0 +1,140 @@
+# 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.
+
+
+import datetime
+import decimal
+from contextlib import closing
+from typing import Any, Iterable, Mapping, Optional, Sequence, Union
+
+from airflow.operators.sql import BaseSQLOperator
+from airflow.providers.google.suite.hooks.sheets import GSheetsHook
+
+
+class SQLToGoogleSheetsOperator(BaseSQLOperator):
+    """
+    Copy data from SQL results to provided Google Spreadsheet.
+
+    :param sql: The SQL to execute.
+    :type sql: str
+    :param spreadsheet_id: The Google Sheet ID to interact with.
+    :type spreadsheet_id: str
+    :param conn_id: the connection ID used to connect to the database.
+    :type sql_conn_id: str
+    :param parameters: The parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param database: name of database which overwrite the defined one in connection
+    :type database: str
+    :param spreadsheet_range: The A1 notation of the values to retrieve.
+    :type spreadsheet_range: str
+    :param gcp_conn_id: The connection ID to use when fetching connection info.
+    :type gcp_conn_id: str
+    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
+        if any. For this to work, the service account making the request must have
+        domain-wide delegation enabled.
+    :type delegate_to: str
+    :param impersonation_chain: Optional service account to impersonate using short-term
+        credentials, or chained list of accounts required to get the access_token
+        of the last account in the list, which will be impersonated in the request.
+        If set as a string, the account must grant the originating account
+        the Service Account Token Creator IAM role.
+        If set as a sequence, the identities from the list must grant
+        Service Account Token Creator IAM role to the directly preceding identity, with first
+        account from the list granting this role to the originating account (templated).
+    :type impersonation_chain: Union[str, Sequence[str]]
+    """
+
+    template_fields = [
+        "sql",
+        "spreadsheet_id",
+        "spreadsheet_range",
+        "impersonation_chain",
+    ]
+
+    template_ext = (".sql",)
+    ui_color = "#a0e08c"
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        spreadsheet_id: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        database: str = None,
+        spreadsheet_range: str = "Sheet1",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+
+        self.sql = sql
+        self.conn_id = sql_conn_id
+        self.database = database
+        self.parameters = parameters
+        self.gcp_conn_id = gcp_conn_id
+        self.spreadsheet_id = spreadsheet_id
+        self.spreadsheet_range = spreadsheet_range
+        self.delegate_to = delegate_to
+        self.impersonation_chain = impersonation_chain
+
+    def _data_prep(self, data):
+        for row in data:
+            item_list = []
+            for item in row:
+                if type(item) is datetime.date:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is datetime.datetime:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is decimal.Decimal:
+                    item = float(item)
+                item_list.append(item)
+            yield item_list
+
+    def _get_data(self):
+        hook = self.get_db_hook()
+        with closing(hook.get_conn()) as conn:
+            with closing(conn.cursor()) as cur:
+                self.log.info("Executing query")
+                if self.parameters is not None:
+                    cur.execute(self.sql, self.parameters)
+                else:
+                    cur.execute(self.sql)
+
+                yield [field[0] for field in cur.description]
+                yield from self._data_prep(cur.fetchall())

Review comment:
       Awesome




-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] mariotaddeucci commented on a change in pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
mariotaddeucci commented on a change in pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#discussion_r698676249



##########
File path: airflow/providers/google/suite/transfers/sql_to_sheets.py
##########
@@ -0,0 +1,140 @@
+# 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.
+
+
+import datetime
+import decimal
+from contextlib import closing
+from typing import Any, Iterable, Mapping, Optional, Sequence, Union
+
+from airflow.operators.sql import BaseSQLOperator
+from airflow.providers.google.suite.hooks.sheets import GSheetsHook
+
+
+class SQLToGoogleSheetsOperator(BaseSQLOperator):
+    """
+    Copy data from SQL results to provided Google Spreadsheet.
+
+    :param sql: The SQL to execute.
+    :type sql: str
+    :param spreadsheet_id: The Google Sheet ID to interact with.
+    :type spreadsheet_id: str
+    :param conn_id: the connection ID used to connect to the database.
+    :type sql_conn_id: str
+    :param parameters: The parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param database: name of database which overwrite the defined one in connection
+    :type database: str
+    :param spreadsheet_range: The A1 notation of the values to retrieve.
+    :type spreadsheet_range: str
+    :param gcp_conn_id: The connection ID to use when fetching connection info.
+    :type gcp_conn_id: str
+    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
+        if any. For this to work, the service account making the request must have
+        domain-wide delegation enabled.
+    :type delegate_to: str
+    :param impersonation_chain: Optional service account to impersonate using short-term
+        credentials, or chained list of accounts required to get the access_token
+        of the last account in the list, which will be impersonated in the request.
+        If set as a string, the account must grant the originating account
+        the Service Account Token Creator IAM role.
+        If set as a sequence, the identities from the list must grant
+        Service Account Token Creator IAM role to the directly preceding identity, with first
+        account from the list granting this role to the originating account (templated).
+    :type impersonation_chain: Union[str, Sequence[str]]
+    """
+
+    template_fields = [
+        "sql",
+        "spreadsheet_id",
+        "spreadsheet_range",
+        "impersonation_chain",
+    ]
+
+    template_ext = (".sql",)
+    ui_color = "#a0e08c"
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        spreadsheet_id: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        database: str = None,
+        spreadsheet_range: str = "Sheet1",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+
+        self.sql = sql
+        self.conn_id = sql_conn_id
+        self.database = database
+        self.parameters = parameters
+        self.gcp_conn_id = gcp_conn_id
+        self.spreadsheet_id = spreadsheet_id
+        self.spreadsheet_range = spreadsheet_range
+        self.delegate_to = delegate_to
+        self.impersonation_chain = impersonation_chain
+
+    def _data_prep(self, data):
+        for row in data:
+            item_list = []
+            for item in row:
+                if type(item) is datetime.date:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is datetime.datetime:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is decimal.Decimal:
+                    item = float(item)

Review comment:
       The dropped time was a mistake, fixed it now with your suggestion :)

##########
File path: airflow/providers/google/suite/transfers/sql_to_sheets.py
##########
@@ -0,0 +1,140 @@
+# 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.
+
+
+import datetime
+import decimal
+from contextlib import closing
+from typing import Any, Iterable, Mapping, Optional, Sequence, Union
+
+from airflow.operators.sql import BaseSQLOperator
+from airflow.providers.google.suite.hooks.sheets import GSheetsHook
+
+
+class SQLToGoogleSheetsOperator(BaseSQLOperator):
+    """
+    Copy data from SQL results to provided Google Spreadsheet.
+
+    :param sql: The SQL to execute.
+    :type sql: str
+    :param spreadsheet_id: The Google Sheet ID to interact with.
+    :type spreadsheet_id: str
+    :param conn_id: the connection ID used to connect to the database.
+    :type sql_conn_id: str
+    :param parameters: The parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param database: name of database which overwrite the defined one in connection
+    :type database: str
+    :param spreadsheet_range: The A1 notation of the values to retrieve.
+    :type spreadsheet_range: str
+    :param gcp_conn_id: The connection ID to use when fetching connection info.
+    :type gcp_conn_id: str
+    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
+        if any. For this to work, the service account making the request must have
+        domain-wide delegation enabled.
+    :type delegate_to: str
+    :param impersonation_chain: Optional service account to impersonate using short-term
+        credentials, or chained list of accounts required to get the access_token
+        of the last account in the list, which will be impersonated in the request.
+        If set as a string, the account must grant the originating account
+        the Service Account Token Creator IAM role.
+        If set as a sequence, the identities from the list must grant
+        Service Account Token Creator IAM role to the directly preceding identity, with first
+        account from the list granting this role to the originating account (templated).
+    :type impersonation_chain: Union[str, Sequence[str]]
+    """
+
+    template_fields = [
+        "sql",
+        "spreadsheet_id",
+        "spreadsheet_range",
+        "impersonation_chain",
+    ]
+
+    template_ext = (".sql",)
+    ui_color = "#a0e08c"
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        spreadsheet_id: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        database: str = None,
+        spreadsheet_range: str = "Sheet1",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+
+        self.sql = sql
+        self.conn_id = sql_conn_id
+        self.database = database
+        self.parameters = parameters
+        self.gcp_conn_id = gcp_conn_id
+        self.spreadsheet_id = spreadsheet_id
+        self.spreadsheet_range = spreadsheet_range
+        self.delegate_to = delegate_to
+        self.impersonation_chain = impersonation_chain
+
+    def _data_prep(self, data):
+        for row in data:
+            item_list = []
+            for item in row:
+                if type(item) is datetime.date:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is datetime.datetime:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is decimal.Decimal:
+                    item = float(item)
+                item_list.append(item)
+            yield item_list
+
+    def _get_data(self):
+        hook = self.get_db_hook()
+        with closing(hook.get_conn()) as conn:
+            with closing(conn.cursor()) as cur:
+                self.log.info("Executing query")
+                if self.parameters is not None:
+                    cur.execute(self.sql, self.parameters)
+                else:
+                    cur.execute(self.sql)
+
+                yield [field[0] for field in cur.description]
+                yield from self._data_prep(cur.fetchall())

Review comment:
       Awesome




-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] mariotaddeucci commented on a change in pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
mariotaddeucci commented on a change in pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#discussion_r698676249



##########
File path: airflow/providers/google/suite/transfers/sql_to_sheets.py
##########
@@ -0,0 +1,140 @@
+# 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.
+
+
+import datetime
+import decimal
+from contextlib import closing
+from typing import Any, Iterable, Mapping, Optional, Sequence, Union
+
+from airflow.operators.sql import BaseSQLOperator
+from airflow.providers.google.suite.hooks.sheets import GSheetsHook
+
+
+class SQLToGoogleSheetsOperator(BaseSQLOperator):
+    """
+    Copy data from SQL results to provided Google Spreadsheet.
+
+    :param sql: The SQL to execute.
+    :type sql: str
+    :param spreadsheet_id: The Google Sheet ID to interact with.
+    :type spreadsheet_id: str
+    :param conn_id: the connection ID used to connect to the database.
+    :type sql_conn_id: str
+    :param parameters: The parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param database: name of database which overwrite the defined one in connection
+    :type database: str
+    :param spreadsheet_range: The A1 notation of the values to retrieve.
+    :type spreadsheet_range: str
+    :param gcp_conn_id: The connection ID to use when fetching connection info.
+    :type gcp_conn_id: str
+    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
+        if any. For this to work, the service account making the request must have
+        domain-wide delegation enabled.
+    :type delegate_to: str
+    :param impersonation_chain: Optional service account to impersonate using short-term
+        credentials, or chained list of accounts required to get the access_token
+        of the last account in the list, which will be impersonated in the request.
+        If set as a string, the account must grant the originating account
+        the Service Account Token Creator IAM role.
+        If set as a sequence, the identities from the list must grant
+        Service Account Token Creator IAM role to the directly preceding identity, with first
+        account from the list granting this role to the originating account (templated).
+    :type impersonation_chain: Union[str, Sequence[str]]
+    """
+
+    template_fields = [
+        "sql",
+        "spreadsheet_id",
+        "spreadsheet_range",
+        "impersonation_chain",
+    ]
+
+    template_ext = (".sql",)
+    ui_color = "#a0e08c"
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        spreadsheet_id: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        database: str = None,
+        spreadsheet_range: str = "Sheet1",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+
+        self.sql = sql
+        self.conn_id = sql_conn_id
+        self.database = database
+        self.parameters = parameters
+        self.gcp_conn_id = gcp_conn_id
+        self.spreadsheet_id = spreadsheet_id
+        self.spreadsheet_range = spreadsheet_range
+        self.delegate_to = delegate_to
+        self.impersonation_chain = impersonation_chain
+
+    def _data_prep(self, data):
+        for row in data:
+            item_list = []
+            for item in row:
+                if type(item) is datetime.date:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is datetime.datetime:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is decimal.Decimal:
+                    item = float(item)

Review comment:
       The dropped time was a mistake, fixed it now with your suggestion :)




-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] potiuk commented on pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
potiuk commented on pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#issuecomment-913030172


   I think the failing builds are something else entirely - looks like `eager-upgrade` in main is causing the errors by some new dependencies (will take a look tomorrow)


-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] mariotaddeucci commented on pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
mariotaddeucci commented on pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#issuecomment-910329539


   Woohoo!
   Thanks @uranusjr and @potiuk o/


-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] potiuk merged pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
potiuk merged pull request #17887:
URL: https://github.com/apache/airflow/pull/17887


   


-- 
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-unsubscribe@airflow.apache.org

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



[GitHub] [airflow] uranusjr commented on a change in pull request #17887: New google operator: SQLToGoogleSheetsOperator

Posted by GitBox <gi...@apache.org>.
uranusjr commented on a change in pull request #17887:
URL: https://github.com/apache/airflow/pull/17887#discussion_r698337259



##########
File path: airflow/providers/google/suite/transfers/sql_to_sheets.py
##########
@@ -0,0 +1,140 @@
+# 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.
+
+
+import datetime
+import decimal
+from contextlib import closing
+from typing import Any, Iterable, Mapping, Optional, Sequence, Union
+
+from airflow.operators.sql import BaseSQLOperator
+from airflow.providers.google.suite.hooks.sheets import GSheetsHook
+
+
+class SQLToGoogleSheetsOperator(BaseSQLOperator):
+    """
+    Copy data from SQL results to provided Google Spreadsheet.
+
+    :param sql: The SQL to execute.
+    :type sql: str
+    :param spreadsheet_id: The Google Sheet ID to interact with.
+    :type spreadsheet_id: str
+    :param conn_id: the connection ID used to connect to the database.
+    :type sql_conn_id: str
+    :param parameters: The parameters to render the SQL query with.
+    :type parameters: dict or iterable
+    :param database: name of database which overwrite the defined one in connection
+    :type database: str
+    :param spreadsheet_range: The A1 notation of the values to retrieve.
+    :type spreadsheet_range: str
+    :param gcp_conn_id: The connection ID to use when fetching connection info.
+    :type gcp_conn_id: str
+    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
+        if any. For this to work, the service account making the request must have
+        domain-wide delegation enabled.
+    :type delegate_to: str
+    :param impersonation_chain: Optional service account to impersonate using short-term
+        credentials, or chained list of accounts required to get the access_token
+        of the last account in the list, which will be impersonated in the request.
+        If set as a string, the account must grant the originating account
+        the Service Account Token Creator IAM role.
+        If set as a sequence, the identities from the list must grant
+        Service Account Token Creator IAM role to the directly preceding identity, with first
+        account from the list granting this role to the originating account (templated).
+    :type impersonation_chain: Union[str, Sequence[str]]
+    """
+
+    template_fields = [
+        "sql",
+        "spreadsheet_id",
+        "spreadsheet_range",
+        "impersonation_chain",
+    ]
+
+    template_ext = (".sql",)
+    ui_color = "#a0e08c"
+
+    def __init__(
+        self,
+        *,
+        sql: str,
+        spreadsheet_id: str,
+        sql_conn_id: str,
+        parameters: Optional[Union[Mapping, Iterable]] = None,
+        database: str = None,
+        spreadsheet_range: str = "Sheet1",
+        gcp_conn_id: str = "google_cloud_default",
+        delegate_to: Optional[str] = None,
+        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+
+        self.sql = sql
+        self.conn_id = sql_conn_id
+        self.database = database
+        self.parameters = parameters
+        self.gcp_conn_id = gcp_conn_id
+        self.spreadsheet_id = spreadsheet_id
+        self.spreadsheet_range = spreadsheet_range
+        self.delegate_to = delegate_to
+        self.impersonation_chain = impersonation_chain
+
+    def _data_prep(self, data):
+        for row in data:
+            item_list = []
+            for item in row:
+                if type(item) is datetime.date:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is datetime.datetime:
+                    item = item.strftime("%Y-%m-%d")
+                elif type(item) is decimal.Decimal:
+                    item = float(item)

Review comment:
       This `type(item) is <type>` kind of check is really not a good idea. Also you dropped the time part in a datetime, is it intended?
   
   Something like this is likely better:
   
   ```suggestion
                   if isinstance(item, (datetime.date, datetime.datetime)):
                       item = item.isoformat()
                   elif isinstance(item, int):  # To exclude int from the number check.
                       pass
                   elif isinstance(item, number.Number):
                       item = float(item)
   ```




-- 
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-unsubscribe@airflow.apache.org

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