{"record":{"id":"c6456ffb736a5a14","repo":"apache/beam","slug":"partitionfn-v-v-want-0-v","errorCode":null,"errorMessage":"partitionFn(%v) = %v, want [0,%v)","messagePattern":"partitionFn\\((.+?)\\) = (.+?), want \\[0,(.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/partition.go","lineNumber":128,"sourceCode":"\tn    int\n\tfn   reflectx.Func1x1\n}\n\nfunc (f *partitionFn) Name() string {\n\treturn f.name\n}\n\nfunc (f *partitionFn) Type() reflect.Type {\n\treturn f.t\n}\n\nfunc (f *partitionFn) Call(args []any) []any {\n\ttimestamp := args[0]\n\tvalue := args[1]\n\n\tn := f.fn.Call1x1(value).(int)\n\tif n < 0 || n >= f.n {\n\t\treturn []any{errors.Errorf(\"partitionFn(%v) = %v, want [0,%v)\", value, n, f.n)}\n\t}\n\n\temit := args[n+2]\n\treflectx.MakeFunc2x0(emit).Call2x0(timestamp, value)\n\n\tvar err error\n\treturn []any{err}\n}\n\n// partitionFnKV is a Func with the following underlying type:\n//\n//\tfn : (EventTime, K, V, emit_1, emit_2, ..., emit_N) -> error\n//\n// where emit_i : (EventTime, K, V) -> () and N is given by the encoded\n// partitionData value. For any input element, it invokes to the\n// given partition function to determine which emitter to use.\ntype partitionFnKV struct {\n\tname string","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/partition.go#L110-L146","documentation":"This error is returned by the DoFn wrapper partitionFn.Call when a user's partition function returns an index outside the valid range [0, n), where n is the number of output partitions declared for the Partition transform. The Beam Go SDK validates the index before dispatching the element to the corresponding emitter, because an out-of-range index has no matching output channel. The error carries the element value, the bad index, and the expected bound so the user can debug their function.","triggerScenarios":"Calling partitionFn.Call when the user-supplied partition function (invoked via f.fn.Call1x1(value)) returns a negative int or an int >= f.n for a given element.","commonSituations":"A partition function computing an index from data (e.g. len(s) % n written as len(s) itself, or dividing by zero leading to odd results), off-by-one errors like returning n for the 'last' bucket, or a function whose return type accidentally coerces to an unexpected int.","solutions":["Fix the partition function so it always returns an index in [0, n), e.g. clamp or modulo the result","Verify the number of partitions passed to beam.Partition matches the function's possible return values","Check the element that triggered the failure and add a guard/default branch in the function for edge-case inputs","Log or unit-test the partition function over representative elements before submitting the pipeline"],"exampleFix":"// before\nfunc partition(v string) int {\n  return len(v) // can exceed n\n}\n// after\nfunc partition(v string) int {\n  i := len(v) % 3\n  if i < 0 { i = 0 }\n  return i\n}","handlingStrategy":"validation","validationCode":"n := partitionFn(value)\nif n < 0 || n >= numPartitions {\n    return fmt.Errorf(\"partition index %d out of range [0,%d) for value %v\", n, numPartitions, value)\n}","typeGuard":null,"tryCatchPattern":"if err := beam.Partition(s, numPartitions, fn, input); err != nil {\n    log.Fatalf(\"partition fn out of range: %v\", err)\n}","preventionTips":["Always return an index derived via modulo of the declared partition count","Clamp or normalize negative results before returning","Unit test the partition function over edge-case inputs","Keep the function pure and total (no panics, no unbounded values)"],"tags":["go","apache-beam","partition","value-out-of-range"],"backgroundTag":"value-out-of-range","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"}