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


##########
providers/amazon/tests/unit/amazon/aws/hooks/test_redshift_sql.py:
##########
@@ -366,6 +366,34 @@ def test_get_openlineage_redshift_authority_part(
         )
 
 
+class TestRedshiftSQLHookPrimaryKey:
+    @pytest.mark.parametrize(
+        ("schema", "expected_schema_filter", "expected_parameters"),
+        [
+            pytest.param("schema", "kcu.table_schema = %s", ("schema", 
"table"), id="schema_given"),
+            # With schema=None the bare table name resolves through the 
search_path, and a NULL
+            # bind would never match kcu.table_schema.
+            pytest.param(None, "kcu.table_schema = current_schema()", 
("table",), id="schema_none"),
+        ],
+    )
+    @mock.patch.object(RedshiftSQLHook, "get_records")
+    def test_get_table_primary_key(
+        self, mock_get_records, schema, expected_schema_filter, 
expected_parameters
+    ):
+        mock_get_records.return_value = [("id",), ("tenant",)]
+
+        assert RedshiftSQLHook().get_table_primary_key("table", schema) == 
["id", "tenant"]
+
+        sql, parameters = mock_get_records.call_args.args
+        assert expected_schema_filter in sql
+        assert "kcu.table_name = %s" in sql
+        assert parameters == expected_parameters
+
+    @mock.patch.object(RedshiftSQLHook, "get_records", return_value=[])
+    def test_get_table_primary_key_none_when_no_rows(self, mock_get_records):

Review Comment:
   nit: this tests the `pk_columns or None` return, which this PR does not 
change. No need to have a test for that



##########
providers/amazon/src/airflow/providers/amazon/aws/hooks/redshift_data.py:
##########
@@ -241,6 +242,11 @@ def get_table_primary_key(
 
         :return: Primary key columns list
         """
+        # The Data API rejects a parameter whose value is None, so an unset 
schema is expressed in the SQL.
+        schema_filter = "kcu.table_schema = :schema" if schema else 
"kcu.table_schema = current_schema()"

Review Comment:
   My `current_schema()` suggestion was not fully right. `current_schema()` is 
only the first existing schema of the `search_path`, while a bare table name is 
looked up in every schema of the `search_path`. Example: with the default 
`"$user", public`, a schema named after the user, and the table in `public`, 
the lookup finds nothing and the upsert fails with "No primary key". Maybe use 
`pg_table_is_visible` instead? At least the docstrings in both hooks should not 
say this is where an unqualified name resolves.



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