[Bug]: Beam YAML `Explode` transform crashes on `None`/null collections and fails on non-standard field names
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
What happened?
In Beam YAML, the Explode transform (apache_beam.yaml.yaml_mapping._Explode in the Python SDK and org.apache.beam.sdk.schemas.transforms.providers.JavaExplodeTransformProvider in the Java SDK) is designed to unnest/flatten iterable fields into individual rows.
However, both the Python and Java implementations contain critical edge-case and schema flaws when processing real-world data:
1. Unhandled TypeError: 'NoneType' object is not iterable on NULL/missing array fields in Python (yaml_mapping.py)
In sdks/python/apache_beam/yaml/yaml_mapping.py (lines 612–628), explode_cross_product and explode_zip directly iterate over the target field values without checking if the field value is None (standard for nullable array/list columns where no elements exist):
# sdks/python/apache_beam/yaml/yaml_mapping.py:612-628
def explode_cross_product(base, fields):
if fields:
copy = dict(base)
for value in base[fields[0]]: # <--- CRASH if base[fields[0]] is None
copy[fields[0]] = value
yield from explode_cross_product(copy, fields[1:])
else:
yield beam.Row(**base)
def explode_zip(base, fields):
to_zip = [base[field] for field in fields]
copy = dict(base)
for values in itertools.zip_longest(*to_zip, fillvalue=None): # <--- CRASH if to_zip contains None
for ix, field in enumerate(fields):
copy[field] = values[ix]
yield beam.Row(**copy)
- When
cross_product: true: If a record hastags: None(or missing nullable field),for value in base[fields[0]]:evaluates asfor value in None:, throwing an unhandledTypeError: 'NoneType' object is not iterable. - When
cross_product: false: If any field infieldsisNone,to_zipcontainsNone. PassingNoneas a positional argument toitertools.zip_longest(*to_zip)throws an unhandledTypeError: 'NoneType' object is not iterable.
In standard relational/ETL semantics (such as SQL UNNEST, BigQuery UNNEST, and PySpark explode), an inner explode on an empty list ([]) or a NULL list yields 0 rows. In Beam YAML, while an empty list [] yields 0 rows, a None/null value crashes the entire pipeline.
2. Unhandled NullPointerException on null array fields in Java (JavaExplodeTransformProvider.java)
The cross-language Java provider (sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/providers/JavaExplodeTransformProvider.java, lines 182–203) suffers from the exact same failure mode:
- For cross-product (
crossProduct == true, line 183):
Iffor (Object value : inputRow.getIterable(index)) { // <--- Throws NullPointerException if field is null current[index] = value; emitCrossProduct(inputRow, index + 1, current, out); }inputRow.getValue(index)isnull,inputRow.getIterable(index)returnsnull, causing an immediateNullPointerException. - For zip (
crossProduct == false, line 201):
Callingiterators[i] = inputRow.getIterable(i).iterator(); // <--- Throws NullPointerException if field is null.iterator()on a null iterable throws an immediateNullPointerException.
Furthermore, in JavaExplodeTransformProvider.expand (lines 128–131):
outputSchemaBuilder =
outputSchemaBuilder.addField(
field.getName(), field.getType().getCollectionElementType());
The newly created output field does not mark the field as nullable. When crossProduct == false and input arrays have unequal lengths, line 212 attempts builder.addValue(null). If field.getType().getCollectionElementType() is not nullable, Row.Builder.build() throws IllegalArgumentException: Field ... is not nullable.
3. Field Access Failure via getattr(row, name) in Python (yaml_mapping.py)
In sdks/python/apache_beam/yaml/yaml_mapping.py (lines 634–637):
(explode_cross_product if cross_product else explode_zip)({
name: getattr(row, name)
for name in all_fields
}, to_explode)
- If an input record has field names that are not valid Python identifiers (such as kebab-case
user-tagsororder-id, common in JSON, YAML, and BigQuery data),getattr(row, name)raisesAttributeError: 'Row' object has no attribute 'user-tags', becausebeam.Rowdoes not set non-identifier names as Python object attributes. - If a schema field collides with built-in
Rowmethods (such asvalues,items,get,copy, or_asdict),getattr(row, name)retrieves the built-in method rather than the field value.
Record fields should be accessed via mapping syntax (row[name]orrow._asdict()[name]).
4. Type Inference Degradation to Any for Nullable Collections
In sdks/python/apache_beam/yaml/yaml_mapping.py (lines 639–644):
def infer_output_type(self, input_type):
return row_type.RowTypeConstraint.from_fields([(
name,
trivial_inference.element_type(typ) if name in self._fields else
typ) for (name, typ) in named_fields_from_element_type(input_type)])
When an array column is nullable in Beam (Optional[Sequence[T]] / Union[Sequence[T], None]), trivial_inference.element_type does not unwrap UnionConstraint and falls through to return typing.Any (trivial_inference.py:238). As a result, the exploded output schema degrades to Any, losing type safety for downstream transforms.
Code Pointers / Steps to Reproduce
Python Minimal Reproduction
import apache_beam as beam
from apache_beam.yaml.yaml_transform import YamlTransform
# Scenario 1: Null collection crashes Explode with TypeError
with beam.Pipeline() as p:
_ = (
p
| beam.Create([
beam.Row(id=1, tags=['beam', 'yaml']),
beam.Row(id=2, tags=None), # Null array
])
| YamlTransform('''
type: Explode
config:
fields: [tags]
cross_product: true
''')
)
# Raises: TypeError: 'NoneType' object is not iterable
# (In explode_cross_product: for value in base[fields[0]])
# Scenario 2: Zip mode with unequal / null collections
with beam.Pipeline() as p:
_ = (
p
| beam.Create([
beam.Row(id=1, tags=['a', 'b'], meta=None),
])
| YamlTransform('''
type: Explode
config:
fields: [tags, meta]
cross_product: false
''')
)
# Raises: TypeError: 'NoneType' object is not iterable
# (In explode_zip: itertools.zip_longest(*to_zip))
# Scenario 3: Non-identifier field name (e.g. kebab-case)
with beam.Pipeline() as p:
_ = (
p
| beam.Create([
beam.Row(**{'item-id': 1, 'user-tags': ['x', 'y']}),
])
| YamlTransform('''
type: Explode
config:
fields: ['user-tags']
''')
)
# Raises: AttributeError: 'Row' object has no attribute 'user-tags'
Impact
- Pipeline Crashes on Production Data: Any real-world dataset containing optional or missing array fields (e.g., ingested from JSON, Kafka, Pub/Sub, or database tables where list columns can be
NULL) immediately crashes the pipeline with unhandledTypeError(Python) orNullPointerException(Java), rather than cleanly omitting or handling rows with empty/null arrays. - Inability to Explode Hyphenated / Non-Identifier Fields: Real-world schemas often feature kebab-case (
event-tags,item-id), causingAttributeErrorduring field extraction. - Loss of Schema Type Safety: Downstream transforms receive degraded
Anytypes rather than the underlying scalar element type for nullable collection schemas.
Proposed Solution
-
In
sdks/python/apache_beam/yaml/yaml_mapping.py(_Explode.expand):- Safely extract row fields using
row._asdict()orrow[name]instead ofgetattr(row, name). - In
explode_cross_product: treatNonevalues identically to empty collections (base[fields[0]] or ()), yielding 0 rows for that record. - In
explode_zip: guardto_zipinputs withbase[field] or ()soitertools.zip_longestreceives valid iterables. - In
infer_output_type: strip optionality /NoneTypebefore delegating totrivial_inference.element_type, and preserve nullability on the resulting element type.
- Safely extract row fields using
-
In
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/providers/JavaExplodeTransformProvider.java:- Check if
inputRow.getIterable(index) == nullbefore iterating in both cross-product and zip execution paths. - Ensure the generated output field preserves nullable status or allows
nullvalues produced during zip alignment.
- Check if
Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
Issue Components
- Component: Python SDK
- Component: Java SDK
- Component: Beam YAML
- Ngôn ngữ chính
- Java
- Star
- 8.7k
- Fork
- 4.8k
- Merge trung bình
- 3 ngày 16 giờ
- Pull request đã merge (30 ngày)
- 445
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/beam
-
awaiting triage java new feature P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
[Bug]: [Python] BigTableEnrichmentHandler coerces None lookup keys to literal b'None' and formats blank key name on KeyErrorCó thể đã có người làm @shikhabhadoria đã nhận 6 ngày trước. Đang mởio P2 python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
apache/beam#41346 · 4 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Release notes links wrongCó thể đã có người làm @schizophrenicmaniac đã nhận 20 ngày trước. Đang mởbug P3
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 67/100
apache/beam#41197 · 3 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
apache/beam#39624 · 2 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[Bug]: PubsubIO used in batch incorrect batch cutoff sizeCó thể đã có người làm @1fanwang đã nhận 98 ngày trước. Đang mởbug io P3 pinned pubsub
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/beam#28011 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Issue tương tự
-
effort/l priority/p1 risk/high triage/core
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
iflytek/skillhub#1188 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
testng-team/testng#3675 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Improve readability of build file paths in the Run/Debug configuration "Build file" dropdownĐang mởdesign
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
OpenLiberty/liberty-tools-intellij#1869 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`ovsx verify` fails for a version older than the 100 newest of an extensionCó thể đã có người làm @trivikr đã nhận hôm nay. Đang mởcli
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 81/100
eclipse-openvsx/openvsx#2482 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
Maintainer thường phản hồi trong vòng 1 ngày