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

[PR] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

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

   Fixes incorrect SQL keyword parameterization in `MySqlHook.bulk_load_custom()` that causes MySQL drivers to quote `IGNORE`/`REPLACE` keywords as string literals, producing invalid SQL.
   
   ## Problem
   
   `bulk_load_custom()` passes `duplicate_key_handling` (e.g., `IGNORE`, `REPLACE`) and `extra_options` as parameterized query values via `cursor.execute(sql, parameters)`. The MySQL driver treats parameterized values as data and quotes them as string literals, producing invalid SQL like:
   
   ```sql
   LOAD DATA LOCAL INFILE '/tmp/file' 'IGNORE' INTO TABLE `my_table` 'FIELDS TERMINATED BY ...'
   ```
   
   This was introduced in PR #33328 which changed from string concatenation to parameterization for these keywords.
   
   ## Root Cause
   
   In `providers/mysql/src/airflow/providers/mysql/hooks/mysql.py`, the `bulk_load_custom` method builds the SQL as:
   
   ```python
   sql_statement = f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s"
   parameters = (tmp_file, duplicate_key_handling, extra_options)
   ```
   
   Both `duplicate_key_handling` and `extra_options` are SQL syntax keywords, not data values. Only `tmp_file` is actual data that should be parameterized.
   
   ## Fix
   
   Changed `bulk_load_custom` to interpolate `duplicate_key_handling` and `extra_options` directly into the SQL statement via f-string, while keeping `tmp_file` as the sole parameterized value:
   
   ```python
   sql_statement = f"LOAD DATA LOCAL INFILE %s {duplicate_key_handling} INTO TABLE `{table}` {extra_options}"
   parameters = (tmp_file,)
   ```
   
   Updated existing tests (`test_bulk_load_custom`, `test_bulk_load_custom_hook_lineage`) to assert the new SQL shape and parameter tuple. Added a new parametrized test `test_bulk_load_custom_duplicate_key_not_parameterized` that validates both `IGNORE` and `REPLACE` appear literally in the executed SQL and only `tmp_file` is parameterized.
   
   closes: #62506
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Opus 4.6
   
   Generated-by: Claude Opus 4.6 following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)


-- 
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] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

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


##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -342,8 +342,8 @@ def bulk_load_custom(
         conn = self.get_conn()
         cursor = conn.cursor()
 
-        sql_statement = f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s"
-        parameters = (tmp_file, duplicate_key_handling, extra_options)
+        sql_statement = f"LOAD DATA LOCAL INFILE %s {duplicate_key_handling} INTO TABLE `{table}` {extra_options}"
+        parameters = (tmp_file,)

Review Comment:
   I think `duplicate_key_handling` here could benefit from validation (must be `IGNORE` or `REPLACE`) to prevent the injection of destructive SQL statements i.e. DROP TABLE. But this depends on whether the MySQL driver being used here can accept multiple statements. No need to add this to the current PR but this would be a solid rationale for a follow-up PR. 



##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -506,14 +506,8 @@ def test_bulk_load_custom(self, table):
             IGNORE 1 LINES""",
         )
         self.cur.execute.assert_called_once_with(
-            f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s",
-            (
-                "/tmp/file",
-                "IGNORE",
-                """FIELDS TERMINATED BY ';'
-            OPTIONALLY ENCLOSED BY '"'
-            IGNORE 1 LINES""",
-            ),
+            f"LOAD DATA LOCAL INFILE %s IGNORE INTO TABLE `{table}` FIELDS TERMINATED BY ';'\n            OPTIONALLY ENCLOSED BY '\"'\n            IGNORE 1 LINES",
+            ("/tmp/file",),

Review Comment:
   I would fix the spacing here if you can. It looks a bit odd.



-- 
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] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #63530:
URL: https://github.com/apache/airflow/pull/63530#issuecomment-4056068703

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [prek-hooks]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-prek-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
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] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

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


##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -506,14 +506,8 @@ def test_bulk_load_custom(self, table):
             IGNORE 1 LINES""",
         )
         self.cur.execute.assert_called_once_with(
-            f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s",
-            (
-                "/tmp/file",
-                "IGNORE",
-                """FIELDS TERMINATED BY ';'
-            OPTIONALLY ENCLOSED BY '"'
-            IGNORE 1 LINES""",
-            ),
+            f"LOAD DATA LOCAL INFILE %s IGNORE INTO TABLE `{table}` FIELDS TERMINATED BY ';'\n            OPTIONALLY ENCLOSED BY '\"'\n            IGNORE 1 LINES",
+            ("/tmp/file",),

Review Comment:
   Fixed in a previous commit — removed the unnecessary wrapping parentheses to clean up the assertion formatting.



##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -342,8 +342,8 @@ def bulk_load_custom(
         conn = self.get_conn()
         cursor = conn.cursor()
 
-        sql_statement = f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s"
-        parameters = (tmp_file, duplicate_key_handling, extra_options)
+        sql_statement = f"LOAD DATA LOCAL INFILE %s {duplicate_key_handling} INTO TABLE `{table}` {extra_options}"
+        parameters = (tmp_file,)

Review Comment:
   Good point — agreed that validating `duplicate_key_handling` to only accept `IGNORE` or `REPLACE` would be a solid follow-up. I'll open a separate PR for that.



-- 
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] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #63530:
URL: https://github.com/apache/airflow/pull/63530#issuecomment-4195034234

   Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
   


-- 
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] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

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


##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -506,14 +506,8 @@ def test_bulk_load_custom(self, table):
             IGNORE 1 LINES""",
         )
         self.cur.execute.assert_called_once_with(
-            f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s",
-            (
-                "/tmp/file",
-                "IGNORE",
-                """FIELDS TERMINATED BY ';'
-            OPTIONALLY ENCLOSED BY '"'
-            IGNORE 1 LINES""",
-            ),
+            f"LOAD DATA LOCAL INFILE %s IGNORE INTO TABLE `{table}` FIELDS TERMINATED BY ';'\n            OPTIONALLY ENCLOSED BY '\"'\n            IGNORE 1 LINES",
+            ("/tmp/file",),

Review Comment:
   Fixed — removed the unnecessary wrapping parentheses to clean up the assertion formatting.



##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -342,8 +342,8 @@ def bulk_load_custom(
         conn = self.get_conn()
         cursor = conn.cursor()
 
-        sql_statement = f"LOAD DATA LOCAL INFILE %s %s INTO TABLE `{table}` %s"
-        parameters = (tmp_file, duplicate_key_handling, extra_options)
+        sql_statement = f"LOAD DATA LOCAL INFILE %s {duplicate_key_handling} INTO TABLE `{table}` {extra_options}"
+        parameters = (tmp_file,)

Review Comment:
   Good point — agreed that validating `duplicate_key_handling` to only accept `IGNORE` or `REPLACE` would be a solid follow-up. I'll open a separate PR for that.



-- 
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] fix: prevent SQL keyword parameterization in MySQL bulk_load_custom [airflow]

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


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