jk-shah opened a new issue, #41077:
URL: https://github.com/apache/beam/issues/41077
### What happened?
### What happened?
In Beam YAML, `Filter` and `MapToFields` with `language: python` and
`expression:` fail with `SyntaxError: invalid syntax` whenever the input schema
contains a column named after a Python keyword (such as `in`, `from`, `is`,
`not`, `and`, `or`, `class`, `def`)—**even when the expression does not
reference that column at all**:
```yaml
pipeline:
type: chain
transforms:
- type: Create
config:
elements:
- {id: 1, status: ACTIVE, in: "warehouse_a"}
- type: Filter
config:
language: python
keep:
expression: "status in ('ACTIVE', 'PENDING')"
- type: LogForTesting
```
Running this pipeline fails during expansion with SyntaxError because Beam
YAML generates in = __row__.in inside def fn(__row__):.
(Note: even JavaRowUdf.createFunctionFromExpression in
sdks/java/core/.../JavaRowUdf.java:206 has the analogous
expression.indexOf(field.getName()) != -1 substring check for Java keywords.)
Cause
In sdks/python/apache_beam/yaml/yaml_mapping.py (lines 318–321),
_expand_python_mapping_func checks raw substring membership (if name in
expression) without checking name.isidentifier() / not keyword.iskeyword(name):
```python
source = '\n'.join(['def fn(__row__):'] + [
f' {name} = __row__.{name}'
for name in original_fields if name in expression
] + [' return (' + expression + ')'])
```
Skipping keyword.iskeyword(name) / non-identifiers in local variable
bindings (so unreferenced keyword columns do not break valid Python operators
like in, not, is, and, or, while getattr(__row__, 'in') still works when
explicitly accessed) fixes the bug.
### 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
- [ ] Component: IO connector
- [ ] Component: Beam YAML
- [ ] 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
--
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]