Skip to content

Commit ca7e09f

Browse files
committed
sdks/python: fix issue regards generic binding parameteres in CloudSQL
1 parent 5727fd9 commit ca7e09f

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -478,16 +478,14 @@ def _build_parameters_dict(
478478

479479
# For batched queries, use unique parameter names per batch item.
480480
if batch_size > 1:
481-
# Extract parameter names from the template using regex.
482-
# Batching is only used with table-based query configs
481+
# Batching is only used with table-based query configs.
483482
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
484483
assert isinstance(self._query_config, table_query_configs)
485-
param_names = self._extract_parameter_names(
486-
self._query_config.where_clause_template)
487-
for param_name, val in zip(param_names, current_values):
484+
batch_param_dict = self._build_single_param_dict(current_values)
485+
# Prefix batch parameters to make them globally unique.
486+
for param_name, val in batch_param_dict.items():
488487
param_dict[f'batch_{i}_{param_name}'] = val
489488
else:
490-
# For single request, use the helper function.
491489
single_param_dict = self._build_single_param_dict(current_values)
492490
param_dict.update(single_param_dict)
493491

@@ -502,17 +500,15 @@ def _build_single_param_dict(self, values: list[Any]) -> dict[str, Any]:
502500
Returns:
503501
Dictionary mapping parameter names to values
504502
"""
505-
if isinstance(self._query_config, TableFieldsQueryConfig):
506-
return {
507-
field_name: val
508-
for field_name, val in zip(
509-
self._query_config.where_clause_fields, values)
510-
}
511-
else: # TableFunctionQueryConfig.
512-
assert isinstance(self._query_config, TableFunctionQueryConfig)
513-
_, param_dict = self._get_unique_template_and_params(
514-
self._query_config.where_clause_template, values)
515-
return param_dict
503+
table_query_configs = (TableFieldsQueryConfig, TableFunctionQueryConfig)
504+
if not isinstance(self._query_config, table_query_configs):
505+
raise ValueError(
506+
f"Parameter binding not supported for "
507+
f"{type(self._query_config).__name__}")
508+
509+
_, param_dict = self._get_unique_template_and_params(
510+
self._query_config.where_clause_template, values)
511+
return param_dict
516512

517513
def _get_unique_template_and_params(
518514
self, template: str, values: list[Any]) -> tuple[str, dict[str, Any]]:

sdks/python/apache_beam/transforms/enrichment_handlers/cloudsql_it_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def test_sql_enrichment(self):
320320

321321
query_config = TableFieldsQueryConfig(
322322
table_id=self._table_id,
323-
where_clause_template="id = :id",
323+
where_clause_template="id = :id_param",
324324
where_clause_fields=fields)
325325

326326
handler = CloudSQLEnrichmentHandler(

0 commit comments

Comments
 (0)