blcksrx commented on code in PR #25541:
URL: https://github.com/apache/airflow/pull/25541#discussion_r944344190


##########
airflow/providers/snowflake/transfers/copy_into_snowflake.py:
##########
@@ -0,0 +1,149 @@
+#
+# 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 abstract operator that child classes implements
+COPY INTO <TABLE> SQL in Snowflake
+"""
+
+
+from typing import List, Optional, Sequence
+
+from airflow.models import BaseOperator
+from airflow.providers.snowflake.hooks.snowflake import SnowflakeHook
+from airflow.providers.snowflake.utils.common import enclose_param
+from airflow.utils.context import Context
+
+
+class CopyFromExternalStageToSnowflakeOperator(BaseOperator):
+    """
+    Executes an COPY INTO command to load files from an external stage from 
clouds to Snowflake
+    .. seealso::
+       `COPY INTO Syntax: 
<https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html>`_
+       `CREATE STAGE Syntax: 
<https://docs.snowflake.com/en/sql-reference/sql/create-stage.html>`_
+
+    This operator requires the snowflake_conn_id connection. The snowflake 
host, login,
+    and, password field must be setup in the connection. Other inputs can be 
defined
+    in the connection or hook instantiation.
+
+    :param namespace: snowflake namespace
+    :param table: snowflake table
+    :param file_format: file format name i.e. CSV, AVRO, etc
+    :param stage: reference to a specific snowflake stage. If the stage's 
schema is not the same as the
+        table one, it must be specified
+    :param prefix: cloud storage location specified to limit the set of files 
to load
+    :param files: files to load into table
+    :param pattern: pattern to load files from external location to table
+    :param copy_into_postifx: optional sql postfix for INSERT INTO query
+           such as formatTypeOptions and copyOptions
+    :param snowflake_conn_id:  Reference to :ref:`Snowflake connection 
id<howto/connection:snowflake>`
+    :param account: snowflake account name
+    :param warehouse: name of snowflake warehouse
+    :param database: name of snowflake database
+    :param region: name of snowflake region
+    :param role: name of snowflake role
+    :param schema: name of snowflake schema
+    :param authenticator: authenticator for Snowflake.
+        'snowflake' (default) to use the internal Snowflake authenticator
+        'externalbrowser' to authenticate using your web browser and
+        Okta, ADFS or any other SAML 2.0-compliant identify provider
+        (IdP) that has been defined for your account
+        ``https://<your_okta_account_name>.okta.com`` to authenticate
+        through native Okta.
+    :param session_parameters: You can set session-level parameters at
+        the time you connect to Snowflake
+
+    """
+
+    template_fields: Sequence[str] = ("files",)
+    template_fields_renderers = {"files": "json"}
+
+    def __init__(
+        self,
+        table: str,
+        file_format: str,
+        stage: str,
+        stage_postfix: Optional[str] = None,
+        columns_array: Optional[List[str]] = None,
+        files: Optional[List[str]] = None,
+        pattern: Optional[str] = None,
+        copy_into_postfix: Optional[str] = None,
+        snowflake_conn_id: str = "snowflake_default",
+        account: Optional[str] = None,
+        warehouse: Optional[str] = None,
+        database: Optional[str] = None,
+        region: Optional[str] = None,
+        role: Optional[str] = None,
+        schema: Optional[str] = None,
+        authenticator: Optional[str] = None,
+        session_parameters: Optional[dict] = None,
+        autocommit: bool = True,
+        *args,
+        **kwargs,
+    ):
+        super().__init__(*args, **kwargs)
+        self.table = table
+        self.file_format = file_format
+        self.stage = stage
+        self.stage_postfix = stage_postfix
+        self.columns_array = columns_array
+        self.files = files
+        self.pattern = pattern
+        self.copy_into_postfix = copy_into_postfix
+        self.snowflake_conn_id = snowflake_conn_id
+        self.account = account
+        self.warehouse = warehouse
+        self.database = database
+        self.region = region
+        self.role = role
+        self.schema = schema
+        self.authenticator = authenticator
+        self.session_parameters = session_parameters
+        self.autocommit = autocommit
+
+    def execute(self, context: Context) -> None:
+        snowflake_hook = SnowflakeHook(
+            snowflake_conn_id=self.snowflake_conn_id,
+            account=self.account,
+            warehouse=self.warehouse,
+            database=self.database,
+            region=self.region,
+            role=self.role,
+            schema=self.schema,
+            authenticator=self.authenticator,
+            session_parameters=self.session_parameters,
+        )
+
+        if self.schema:
+            into = f"{self.schema}.{self.table}"
+        else:
+            into = self.table
+
+        if self.columns_array:
+            into = f"{into}({', '.join(self.columns_array)})"
+
+        sql = f"""
+        COPY INTO {into}
+             FROM  @{self.stage}/{self.stage_postfix or ""}
+        {"FILES=" + ",".join(map(enclose_param ,self.files)) if self.files 
else ""}
+        {"PATTERN=" + enclose_param(self.pattern) if self.pattern else ""}
+        FILE_FORMAT={self.file_format}
+        {self.copy_into_postfix or ""}
+        """

Review Comment:
   I rather to use multiline f-string, it's more clear of string block 
construction



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