[Bug]: [Python] BigTableEnrichmentHandler coerces None lookup keys to literal b'None' and formats blank key name on KeyError
What happened?
BigTableEnrichmentHandler.__call__ (sdks/python/apache_beam/transforms/enrichment_handlers/bigtable.py, lines 118-148) has two bugs when extracting row_key:
- Nullable
Nonejoin keys are queried as literalb'None'(lines123-126):
When an inputrequest_dict = request._asdict() row_key_str = str(request_dict[self._row_key]) row_key = row_key_str.encode(self._encoding) row = self._table.read_row(row_key, filter_=self._row_filter)beam.RowhasNoneforself._row_key,str(None)silently coerces it to'None', issuing a live Bigtableread_row(b'None')RPC (and joining unrelated data if a'None'row exists). - Blank field name in
KeyErrormessage whenrow_keycolumn is missing (lines118, 124, 146-147):row_key_stris initialized to""on line118. Whenself._row_keyis absent fromrequest_dict,request_dict[self._row_key]on line124raisesKeyErrorbeforerow_key_stris assigned, so lines146-147raise:KeyError: 'row_key not found in input PCollection.'(printing an empty string instead ofself._row_key).
Minimal reproducer:
from unittest import mock
import apache_beam as beam
from apache_beam.transforms.enrichment_handlers.bigtable import BigTableEnrichmentHandler
h = BigTableEnrichmentHandler(
project_id='p', instance_id='i', table_id='t', row_key='user_id')
h._table = mock.MagicMock()
h._table.read_row.return_value = None
h(beam.Row(user_id=None))
print('Queried Bigtable key:', h._table.read_row.call_args[0][0]) # b'None'
try:
h(beam.Row(other_col='x'))
except KeyError as e:
print('KeyError:', e) # "KeyError: 'row_key not found in input PCollection.'"
Cause
request_dict[self._row_key] is passed unconditionally to str(...) without checking is None, and except KeyError: interpolates row_key_str ("") instead of self._row_key.
Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
Issue Components
- [x] Component: Python SDK
- [ ] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [x] Component: IO connector
- [ ] Component: Beam YAML
- [ ] Component: Beam ML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner
Hi @jk-shah, I'd like to work on this one.
I reproduced both problems with the snippet in the description. For the KeyError message, I'll interpolate self._row_key instead of row_key_str.
For the None key, I'd check for None before the str() call and skip the Bigtable RPC. I have one question about the behavior. I'm planning to follow the existing missing-row handling, so:
ExceptionLevel.WARNlogs a warning and returnsrequest, beam.Row()ExceptionLevel.RAISEraises aValueErrorwith a clear message
Or would you prefer that a None key always raises, regardless of exception_level? I'll go with the first option unless you say otherwise.
I'll add unit tests for a None key at each exception level and for the missing-column error message, using the mocked table as in the reproducer.
Thanks for picking this up @Abhi2939!
Yes — going with your first option (following self._exception_level and skipping self._table.read_row(...)) is definitely the right behavior. Nullable join keys (e.g. anonymous events with user_id=None) are common in left-join enrichment pipelines, so always raising would break pipelines configured with ExceptionLevel.WARN (the default) or ExceptionLevel.QUIET:
Two small related details in bigtable.py to keep in mind while you're in there:
- For
KeyError: whenrow_key_fnis passed instead ofrow_key,self._row_keydefaults to""— so scoping theKeyErrorcatch specifically aroundrequest_dict[self._row_key](rather than theself._row_key_fn(request)call) avoids turning aKeyErrorinside a customrow_key_fninto"row_key not found...". - In
get_cache_key(line 168),"%s: %s" % (self._row_key, val)maps bothNoneand the literal string"None"to the same Redis cache key ("user_id: None"), in case you want to keepNonedistinct there too.
Your test plan sounds great!