vincbeck commented on code in PR #76721:
URL: https://github.com/apache/airflow/pull/76721#discussion_r4643760993


##########
providers/amazon/src/airflow/providers/amazon/aws/hooks/redshift_data.py:
##########
@@ -241,23 +241,24 @@ def get_table_primary_key(
 
         :return: Primary key columns list
         """
-        sql = f"""
+        sql = """
             select kcu.column_name
             from information_schema.table_constraints tco
                     join information_schema.key_column_usage kcu
                         on kcu.constraint_name = tco.constraint_name
                             and kcu.constraint_schema = tco.constraint_schema
                             and kcu.constraint_name = tco.constraint_name
             where tco.constraint_type = 'PRIMARY KEY'
-            and kcu.table_schema = {schema}
-            and kcu.table_name = {table}
+            and kcu.table_schema = :schema

Review Comment:
   When `schema` is `None`, `S3ToRedshiftOperator` writes to the bare table 
name, which is resolved through the `search_path`. Should we filter on 
`current_schema()` instead of hard coding `public`? Otherwise the keys could 
come from a different table than the one we upsert into.



##########
providers/amazon/src/airflow/providers/amazon/aws/hooks/redshift_data.py:
##########
@@ -241,23 +241,24 @@ def get_table_primary_key(
 
         :return: Primary key columns list
         """
-        sql = f"""
+        sql = """
             select kcu.column_name
             from information_schema.table_constraints tco
                     join information_schema.key_column_usage kcu
                         on kcu.constraint_name = tco.constraint_name
                             and kcu.constraint_schema = tco.constraint_schema
                             and kcu.constraint_name = tco.constraint_name
             where tco.constraint_type = 'PRIMARY KEY'
-            and kcu.table_schema = {schema}
-            and kcu.table_name = {table}
+            and kcu.table_schema = :schema
+            and kcu.table_name = :table
         """
         stmt_id = self.execute_query(
             sql=sql,
             database=database,
             cluster_identifier=cluster_identifier,
             workgroup_name=workgroup_name,
             db_user=db_user,
+            parameters=[{"name": "schema", "value": schema or "public"}, 
{"name": "table", "value": table}],

Review Comment:
   `RedshiftSQLHook.get_table_primary_key` has the same problem. With 
`schema=None` it binds `NULL`, so `kcu.table_schema = NULL` never matches and 
`S3ToRedshiftOperator` fails with "No primary key on None.<table>". Should we 
fix it there as well so both paths behave the same?



##########
providers/amazon/tests/unit/amazon/aws/hooks/test_redshift_data.py:
##########
@@ -355,8 +355,40 @@ def test_get_table_primary_key_no_token(self, mock_conn):
             statement_name=statement_name,
         )
 
+        mock_conn.execute_statement.assert_called_once_with(
+            Database=DATABASE,
+            Sql=mock.ANY,
+            ClusterIdentifier=cluster_identifier,
+            DbUser=db_user,
+            Parameters=[{"name": "schema", "value": schema}, {"name": "table", 
"value": table}],
+            SecretArn=secret_arn,
+            StatementName=statement_name,
+            WithEvent=False,
+        )
+        sql = mock_conn.execute_statement.call_args.kwargs["Sql"]
+        assert "kcu.table_schema = :schema" in sql
+        assert "kcu.table_name = :table" in sql
         mock_conn.get_statement_result.assert_called_once_with(Id=STATEMENT_ID)
 
+    
@mock.patch("airflow.providers.amazon.aws.hooks.redshift_data.RedshiftDataHook.conn")
+    def test_get_table_primary_key_schema_none_defaults_to_public(self, 
mock_conn):

Review Comment:
   nit: this test only differs from `test_get_table_primary_key_no_token` by 
the schema input and the expected value. Can you merge them with 
`@pytest.mark.parametrize`?



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