{"record":{"id":"60c66a3ff9eaae3e","repo":"apache/beam","slug":"encoding-partition-function","errorCode":null,"errorMessage":"encoding partition function","messagePattern":"encoding partition function","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/partition.go","lineNumber":87,"sourceCode":"\t\tin = []reflect.Type{EventTimeType, k, v}\n\t} else {\n\t\tt := col.Type().Type()\n\t\tfuncx.MustSatisfy(fn, funcx.Replace(sig, TType, t))\n\t\temit = reflect.FuncOf([]reflect.Type{EventTimeType, t}, nil, false)\n\t\tin = []reflect.Type{EventTimeType, t}\n\t}\n\n\t// The partitionFn is a DoFn with a signature that is dependent on the input, so\n\t// neither reflection nor type-specialization is adequate. Instead, it uses a\n\t// dynamic function.\n\tfor i := 0; i < n; i++ {\n\t\tin = append(in, emit)\n\t}\n\tfnT := reflect.FuncOf(in, []reflect.Type{reflectx.Error}, false)\n\n\tdata, err := json.Marshal(partitionData{KV: typex.IsKV(col.Type()), N: n, Fn: EncodedFunc{Fn: reflectx.MakeFunc(fn)}})\n\tif err != nil {\n\t\tpanic(errors.WithContext(err, \"encoding partition function\"))\n\t}\n\n\treturn ParDoN(s, &graph.DynFn{Name: \"beam.partitionFn\", Data: data, T: fnT, Gen: makePartitionFn}, col)\n}\n\n// partitionData contains the data needed for the partition DoFn generator.\ntype partitionData struct {\n\tKV bool        `json:\"kv\"`\n\tN  int         `json:\"n\"`\n\tFn EncodedFunc `json:\"fn\"`\n}\n\n// partitionFn is a Func with the following underlying type:\n//\n//\tfn : (EventTime, T, emit_1, emit_2, ..., emit_N) -> error\n//\n// where emit_i : (EventTime, T) -> () and N is given by the encoded\n// partitionData value. For any input element, it invokes to the","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/partition.go#L69-L105","documentation":"beam.Partition serializes its partition function (via EncodedFunc) into JSON to ship it to the runner. If json.Marshal of the partitionData (including the encoded function) fails, it panics with errors wrapped in the context \"encoding partition function\". This indicates a marshaling failure of the function's type/encoding metadata.","triggerScenarios":"Passing a partition function whose signature/type cannot be encoded by reflectx.MakeFunc/EncodedFunc — e.g. a function value that is nil, or a fn argument whose reflection-based encoding fails.","commonSituations":"Passing nil as fn; passing a value that is not a function where a func is required; exotic function types not supported by Beam's reflection-based function encoding.","solutions":["Ensure fn is a valid, non-nil Go function with an acceptable partition signature (input type matching the element, returning an int index in [0,n)).","Check the wrapped underlying error in the panic message for the exact marshaling failure.","Pass a plain named or literal function type rather than a dynamically built or exotic func value."],"exampleFix":"// before: nil function\nvar fn func(int) int\nbeam.Partition(s, n, fn, col) // panics encoding partition function\n\n// after\nfn := func(e int) int { return e % n }\nbeam.Partition(s, n, fn, col)","handlingStrategy":"validation","validationCode":"if fn == nil {\n    return errors.New(\"partition function must be non-nil\")\n}\n// ensure fn is a func with signature func(T) int","typeGuard":null,"tryCatchPattern":"defer func() { if r := recover(); r != nil { err = fmt.Errorf(\"partition fn encoding failed: %v\", r) } }()","preventionTips":["Never pass nil or non-func values as the partition function","Use a standard signature: input type matching the element, returning int","Read the wrapped error in the panic to identify the marshaling failure"],"tags":["go","apache-beam","partition","json","marshal","panic"],"backgroundTag":"json-marshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}