Use parameterized queries in HiveStatsCollectionOperator#66751
Open
Har1sh-k wants to merge 1 commit into
Open
Conversation
|
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
|
HiveStatsCollectionOperator builds its bookkeeping SQL (the SELECT and DELETE against hive_stats in the MySQL metastore, and the SELECT ... FROM <table> WHERE <partition_key> = '<value>' against Presto) by f-string-interpolating template-rendered fields (table, partition, dttm) directly into raw SQL strings. Per the security model in airflow-core/docs/security/security_model.rst and airflow-core/docs/security/sql.rst, Dag authors are trusted users responsible for sanitizing input before passing it to operators. The change here is defense-in-depth so that the operator does not rely on each Dag author to sanitize. The MySQL bookkeeping SELECT and DELETE now use %s placeholders with the parameters= kwarg of MySqlHook.get_records / .run, so the values are bound by the driver instead of interpolated. For the Presto SELECT, partition values are passed as bound parameters using the hook's declared placeholder (PrestoHook overrides the DbApiHook default to "?"). Identifiers (table name and partition column names) cannot be parameterized in standard SQL — they are validated against ^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?$ for the table and ^[A-Za-z_][A-Za-z0-9_]*$ for partition keys, raising AirflowException on mismatch.
7ed5db6 to
4bb0946
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
HiveStatsCollectionOperatorbuilds its bookkeeping SQL (theSELECTandDELETEagainsthive_statsin the MySQL metastore, and theSELECT ... FROM <table> WHERE <partition_key> = '<value>'against Presto) by f-string-interpolating template-rendered fields (table,partition,dttm) directly into raw SQL strings.This PR is intended as defense-in-depth and aligns the operator with the documented guidance in
security_model.rstandsql.rstthat Dag authors should avoid passing unsanitized input into operators and hooks — the built-in bookkeeping and partition-predicate paths no longer rely on each Dag author to sanitize these values.extra_exprs/assignment_funcstill accept DAG-authored SQL expressions by design and are intentionally unchanged.SELECTandDELETEuse%splaceholders with theparameters=kwarg ofMySqlHook.get_records/.run, so values are bound by the driver instead of interpolated.SELECT, partition values are passed as bound parameters using the hook's declared placeholder (PrestoHookoverridesDbApiHook's default%sto?).^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?$and^[A-Za-z_][A-Za-z0-9_]*$respectively, raisingAirflowExceptionon mismatch.Test plan
test_execute_rejects_invalid_table_identifierandtest_execute_rejects_invalid_partition_columnexercise the allowlist regexes.test_execute_parameterizes_mysql_bookkeeping_queriesandtest_execute_parameterizes_presto_partition_valuesverify the SQL is parameterized (no f-string interpolation oftable/partitionvalues into the SQL body) and that the Presto query uses the hook's?placeholder.test_execute_delete_previous_runs_rowsandtest_runs_for_hive_statsto assert the parameterized form.test_hive_stats.pytests continue to pass.Was generative AI tooling used to co-author this PR?
I used Claude Opus 4.7 to assist with this PR, following the Gen-AI Assisted contributions guidelines:
presto.placeholder, the MySQL parameterization, and the tests were iterated againstairflow-core/docs/security/security_model.rstandairflow-core/docs/security/sql.rstrather than blindly accepted.python -m py_compileandast.parsesucceed.providers/apache/hive/with no unrelated changes.