jrmccluskey commented on code in PR #41434:
URL: https://github.com/apache/beam/pull/41434#discussion_r4670370296


##########
sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql_test.py:
##########
@@ -614,6 +615,160 @@ def test_unsupported_query_config_type_error(self):
     self.assertIn(
         "Unsupported query configuration type", str(context.exception))
 
+  def test_column_names_auto_includes_where_clause_fields(self):
+    """Test that column_names automatically includes where_clause_fields."""
+    connection_config = ExternalSQLDBConnectionConfig(
+        db_adapter=DatabaseTypeAdapter.POSTGRESQL,
+        host='localhost',
+        port=5432,
+        user='user',
+        password='password',
+        db_id='db')
+
+    config = TableFieldsQueryConfig(
+        table_id="users",
+        where_clause_template="id = :id",
+        where_clause_fields=["id"])
+
+    # where_clause_fields ('id') omitted from column_names.
+    handler = CloudSQLEnrichmentHandler(
+        connection_config=connection_config,
+        query_config=config,
+        column_names=["name", "age"])
+    self.assertEqual(handler._column_names, "name,age,id")
+    self.assertEqual(
+        handler.query_template, "SELECT name,age,id FROM users WHERE id = :id")
+
+    # where_clause_fields ('id') already in column_names.
+    handler2 = CloudSQLEnrichmentHandler(
+        connection_config=connection_config,
+        query_config=config,
+        column_names=["id", "name"])
+    self.assertEqual(handler2._column_names, "id,name")
+
+  def test_process_batch_request_handles_unmatched_and_preserves_order(self):
+    """Test batch processing returns beam.Row() for unmatched requests and 
preserves order."""
+    connection_config = ExternalSQLDBConnectionConfig(
+        db_adapter=DatabaseTypeAdapter.POSTGRESQL,
+        host='localhost',
+        port=5432,
+        user='user',
+        password='password',
+        db_id='db')
+
+    config = TableFieldsQueryConfig(
+        table_id="users",
+        where_clause_template="id = :id",
+        where_clause_fields=["id"])
+
+    handler = CloudSQLEnrichmentHandler(
+        connection_config=connection_config, query_config=config)
+
+    import apache_beam as beam

Review Comment:
   +1 to deduplicating the import



##########
sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql.py:
##########
@@ -600,16 +608,38 @@ def _extract_parameter_names(self, template: str) -> 
list[str]:
 
   def create_row_key(self, row: beam.Row):
     if isinstance(self._query_config, TableFunctionQueryConfig):
-      return tuple(self._query_config.where_clause_value_fn(row))
+      try:
+        return tuple(self._query_config.where_clause_value_fn(row))
+      except (KeyError, AttributeError, TypeError):

Review Comment:
   +1, I do not particularly like handwaving away a wide swath of errors here. 
Ideally we should consider more fine-grained handling to surface real errors.



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