{"record":{"id":"62677af351df200d","repo":"apache/beam","slug":"invalid-pcollection-to-flatten-index-v","errorCode":null,"errorMessage":"invalid pcollection to flatten: index %v","messagePattern":"invalid pcollection to flatten: index (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/flatten.go","lineNumber":43,"sourceCode":"// all the input PCollections. The name \"Flatten\" suggests taking a list of lists\n// and flattening them into a single list.\n//\n// By default, the Coder of the output PCollection is the same as the Coder\n// of the first PCollection.\nfunc Flatten(s Scope, cols ...PCollection) PCollection {\n\treturn Must(TryFlatten(s, cols...))\n}\n\n// TryFlatten merges incoming PCollections of type 'A' to a single PCollection\n// of type 'A'. Returns an error indicating the set of PCollections that could\n// not be flattened.\nfunc TryFlatten(s Scope, cols ...PCollection) (PCollection, error) {\n\tif !s.IsValid() {\n\t\treturn PCollection{}, errors.New(\"invalid scope\")\n\t}\n\tfor i, in := range cols {\n\t\tif !in.IsValid() {\n\t\t\treturn PCollection{}, errors.Errorf(\"invalid pcollection to flatten: index %v\", i)\n\t\t}\n\t}\n\tif len(cols) == 0 {\n\t\treturn PCollection{}, errors.New(\"no input pcollections\")\n\t}\n\tif len(cols) == 1 {\n\t\treturn cols[0], nil // no-op\n\t}\n\n\tvar in []*graph.Node\n\tfor _, s := range cols {\n\t\tin = append(in, s.n)\n\t}\n\tedge, err := graph.NewFlatten(s.real, s.scope, in)\n\tif err != nil {\n\t\treturn PCollection{}, err\n\t}\n\tret := PCollection{edge.Output[0].To}","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/flatten.go#L25-L61","documentation":"TryFlatten (sdks/go/pkg/beam/flatten.go) merges several PCollections; before building the flatten node it verifies each input is a valid PCollection. An invalid (zero-value or wrongly-scoped) input at position i triggers \"invalid pcollection to flatten: index i\". It prevents silently flattening uninitialized collections.","triggerScenarios":"Calling beam.Flatten (or TryFlatten) with a zero-value beam.PCollection{} among the cols arguments, e.g. an uninitialized variable in a variadic call.","commonSituations":"Building inputs in a loop with a declared-outside var that never gets assigned; conditionally assigning a PCollection but passing it regardless of the branch taken.","solutions":["Inspect the PCollection at the reported index and assign it the output of a real transform.","Guard each input with in.IsValid() before calling Flatten.","Use beam.TryFlatten to get an error instead of a panic from Flatten.","Collect PCollections into a slice only from successful transform results."],"exampleFix":"// before\nvar cols []beam.PCollection\nvar maybe beam.PCollection // never assigned on some path\ncols = append(cols, maybe)\nbeam.Flatten(s, cols...)\n// after\nif !maybe.IsValid() { return errors.New(\"missing branch output\") }\ncols = append(cols, maybe)\nout, err := beam.TryFlatten(s, cols...)","handlingStrategy":"validation","validationCode":"for i, c := range cols {\n    if !c.IsValid() {\n        return fmt.Errorf(\"flatten input %d is invalid\", i)\n    }\n}","typeGuard":null,"tryCatchPattern":"out, err := beam.TryFlatten(s, cols...)\nif err != nil {\n    return fmt.Errorf(\"flatten failed: %w\", err)\n}","preventionTips":["Check every conditional branch assigns the PCollection before flattening","Avoid declaring PCollections with var and appending unconditionally","Build input slices only from transform outputs"],"tags":["beam","pipeline-construction","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}