You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "SameerMesiah97 (via GitHub)" <gi...@apache.org> on 2026/07/03 19:56:33 UTC

[PR] Add SQLBulkLoadOperator to Common SQL Provider [airflow]

SameerMesiah97 opened a new pull request, #69362:
URL: https://github.com/apache/airflow/pull/69362

   **Description**
   
   This change introduces a new operator, `SQLBulkLoadOperator`, to bulk load tab-delimited files into SQL database tables.
   
   The operator delegates to the underlying hook's `bulk_load` implementation, allowing each SQL provider to use its native bulk loading mechanism through a common interface. It also supports optional `preoperator` and `postoperator` SQL statements to perform common setup and cleanup tasks as part of the bulk load workflow.
   
   **Rationale**
   
   `DbApiHook` already exposes a common `bulk_load` API, which is implemented by multiple SQL providers. However, unlike `run()` and `insert_rows()`, there is currently no corresponding operator that exposes this functionality to DAG authors.
   
   While the operator provides a convenient wrapper around the existing hook API, it also simplifies common bulk loading workflows by allowing setup and cleanup SQL to be executed as part of the same task. This avoids requiring users to implement custom `PythonOperator`s solely to orchestrate bulk loading operations.
   
   **Tests**
   
   Added unit tests verifying that:
   
   * `SQLBulkLoadOperator` correctly delegates to the underlying hook's `bulk_load` method.
   * Optional `preoperator` and `postoperator` SQL statements are executed before and after the bulk load operation, respectively.
   * Template fields are correctly rendered before invoking the underlying hook.
   
   **Example DAG**
   
   Added a new example DAG, `example_sql_bulk_load`, demonstrating how to bulk load a tab-delimited file into a SQL table using `SQLBulkLoadOperator`. The example also shows how to use `preoperator` and `postoperator` to prepare and clean up the destination table as part of the bulk load workflow.
   
   **Documentation**
   
   Added documentation for `SQLBulkLoadOperator`, including its supported parameters and a usage example.
   
   **Backwards Compatibility**
   
   This is a new operator and does not modify existing functionality. No breaking changes are introduced.


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


Re: [PR] Add SQLBulkLoadOperator to Common SQL Provider [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 commented on code in PR #69362:
URL: https://github.com/apache/airflow/pull/69362#discussion_r3650231948


##########
providers/common/sql/tests/unit/common/sql/operators/test_sql.py:
##########
@@ -2957,3 +2958,92 @@ def test_insert_rows_operator_with_rows_processor(self, mock_get_db_hook):
             (4, "Lundgren", "Dolph", 66),
             (5, "Norris", "Chuck", 84),
         ]
+
+
+class TestSQLBulkLoadOperator:
+    def _construct_operator(self, table, tmp_file, **kwargs):
+        dag = DAG("test_dag", schedule=None, start_date=datetime.datetime(2017, 1, 1))
+        return SQLBulkLoadOperator(
+            task_id="test_task",
+            conn_id="default_conn",
+            table=table,
+            tmp_file=tmp_file,
+            dag=dag,
+            **kwargs,
+        )
+
+    @mock.patch.object(SQLBulkLoadOperator, "get_db_hook")
+    def test_execute(self, mock_get_db_hook):
+        operator = self._construct_operator(
+            table="users",
+            tmp_file="/tmp/users.tsv",
+            preoperator="CREATE TABLE users (id INT);",
+            postoperator="DROP TABLE users;",
+        )
+
+        operator.execute(context=MagicMock())
+
+        hook = mock_get_db_hook.return_value
+
+        hook.run.assert_has_calls(
+            [
+                mock.call("CREATE TABLE users (id INT);"),
+                mock.call("DROP TABLE users;"),
+            ]
+        )
+
+        hook.bulk_load.assert_called_once_with(
+            table="users",
+            tmp_file="/tmp/users.tsv",
+        )

Review Comment:
   It might be better to assert on the exact order of the calls: Create table ->>bulk load->Drop table



##########
providers/common/sql/src/airflow/providers/common/sql/operators/sql.py:
##########
@@ -1916,3 +1916,75 @@ def _initialize_partition_clause(clause: str | None) -> str | None:
         raise ValueError("Invalid partition_clause: semicolons (;) not allowed.")
 
     return clause
+
+
+class SQLBulkLoadOperator(BaseSQLOperator):
+    """
+    Bulk load a tab-delimited file into a database table.
+
+    This operator delegates to the underlying hook's ``bulk_load`` implementation,
+    allowing each provider to use its native bulk loading mechanism.
+
+    :param table: Name of the target table.

Review Comment:
   nit: `table` is also templated



##########
providers/common/sql/src/airflow/providers/common/sql/operators/sql.py:
##########
@@ -1916,3 +1916,75 @@ def _initialize_partition_clause(clause: str | None) -> str | None:
         raise ValueError("Invalid partition_clause: semicolons (;) not allowed.")
 
     return clause
+
+
+class SQLBulkLoadOperator(BaseSQLOperator):
+    """
+    Bulk load a tab-delimited file into a database table.
+
+    This operator delegates to the underlying hook's ``bulk_load`` implementation,
+    allowing each provider to use its native bulk loading mechanism.
+
+    :param table: Name of the target table.
+    :param tmp_file: Path to the tab-delimited file to load (templated).
+    :param conn_id: The connection ID used to connect to the database. Defaults to ``None``.
+    :param database: Name of the database which overrides the one defined in the connection.
+        Defaults to ``None``.
+    :param preoperator: SQL statement or list of statements to execute before bulk loading
+        (templated). Defaults to ``None``.
+    :param postoperator: SQL statement or list of statements to execute after bulk loading
+        (templated). Defaults to ``None``.
+    """

Review Comment:
   Maybe worth adding a refernce to the docs:
   
   ```rst
    .. seealso::
        For more information on how to use this operator, take a look at the guide:
        :ref:`howto/operator:SQLBulkLoadOperator`
   ```



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


Re: [PR] Add SQLBulkLoadOperator to Common SQL Provider [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 merged PR #69362:
URL: https://github.com/apache/airflow/pull/69362


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


Re: [PR] Add SQLBulkLoadOperator to Common SQL Provider [airflow]

Posted by "SameerMesiah97 (via GitHub)" <gi...@apache.org>.
SameerMesiah97 commented on PR #69362:
URL: https://github.com/apache/airflow/pull/69362#issuecomment-5007126817

   Requesting review for this.


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