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

Re: [PR] Use parameterized queries in HiveStatsCollectionOperator [airflow]

Nataneljpwd commented on code in PR #66751:
URL: https://github.com/apache/airflow/pull/66751#discussion_r3342869282


##########
providers/apache/hive/src/airflow/providers/apache/hive/operators/hive_stats.py:
##########
@@ -29,6 +30,13 @@
 if TYPE_CHECKING:
     from airflow.providers.common.compat.sdk import Context
 
+# Hive table names may be qualified as `<database>.<table>`; identifiers must
+# be plain word characters so they can be safely interpolated into the Presto
+# query that selects partition stats. Identifiers cannot be bound as parameters
+# in standard SQL.
+_HIVE_TABLE_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?$")
+_HIVE_COLUMN_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")

Review Comment:
   Columns can include dashes and spaces if they are escaped in hive with backticks, yet this does not allow it



##########
providers/apache/hive/src/airflow/providers/apache/hive/operators/hive_stats.py:
##########
@@ -29,6 +30,13 @@
 if TYPE_CHECKING:
     from airflow.providers.common.compat.sdk import Context
 
+# Hive table names may be qualified as `<database>.<table>`; identifiers must
+# be plain word characters so they can be safely interpolated into the Presto
+# query that selects partition stats. Identifiers cannot be bound as parameters
+# in standard SQL.
+_HIVE_TABLE_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?$")
+_HIVE_COLUMN_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")

Review Comment:
   Or with ", if we are in MySQL 



##########
providers/apache/hive/src/airflow/providers/apache/hive/operators/hive_stats.py:
##########
@@ -128,13 +146,16 @@ def execute(self, context: Context) -> None:
         exprs.update(self.extra_exprs)
         exprs_str = ",\n        ".join(f"{v} AS {k[0]}__{k[1]}" for k, v in exprs.items())
 
-        where_clause_ = [f"{k} = '{v}'" for k, v in self.partition.items()]
+        presto = PrestoHook(presto_conn_id=self.presto_conn_id)
+        # PrestoHook overrides DbApiHook's default `%s` placeholder with `?`,
+        # so the WHERE clause is built against the hook's declared placeholder.
+        placeholder = presto.placeholder
+        where_clause_ = [f"{k} = {placeholder}" for k in self.partition.keys()]
         where_clause = " AND\n        ".join(where_clause_)
         sql = f"SELECT {exprs_str} FROM {self.table} WHERE {where_clause};"
 
-        presto = PrestoHook(presto_conn_id=self.presto_conn_id)
         self.log.info("Executing SQL check: %s", sql)
-        row = presto.get_first(sql)
+        row = presto.get_first(sql, parameters=tuple(self.partition.values()))

Review Comment:
   Does it need tuple conversion?



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