github-actions[bot] commented on code in PR #41434:
URL: https://github.com/apache/beam/pull/41434#discussion_r4603582979


##########
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:
   **🟡 Medium**: Catching `(KeyError, AttributeError, TypeError)` (and 
`KeyError` on line 621) and silently returning `None` in `create_row_key` can 
mask genuine bugs or configuration issues during batch processing.
   
   For example, if a user configures `TableFunctionQueryConfig` alongside 
`column_names` that omits the column accessed by `where_clause_value_fn` (since 
auto-inclusion in `__init__` only applies to `TableFieldsQueryConfig`), 
`create_row_key(response_row)` will silently return `None` for every database 
result, causing `_process_batch_request` to return empty `beam.Row()` 
enrichments for all matched records without raising an error or logging a 
warning. Consider either logging a warning when key extraction fails or 
allowing errors on malformed `request` rows to surface.



##########
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:
   **🟢 Low**: Nit: `import apache_beam as beam` is repeated inside each of the 
new test methods. Consider moving it to the top-level module imports to keep 
the test methods cleaner.



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