{"record":{"id":"6f189f6904f2c569","repo":"apache/beam","slug":"n-must-be-0","errorCode":null,"errorMessage":"n must be > 0","messagePattern":"n must be > 0","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/partition.go","lineNumber":60,"sourceCode":"//\tfunc lenToTen(s string) int {\n//\t\tif len(s) > 9 {\n//\t\t\treturn 10\n//\t\t}\n//\t\treturn len(s)\n//\t}\n//\n//\t// Partition functions must be registered with Beam, and must not be closures.\n//\tfunc init() { register.Function1x1(lenToTen) }\n//\n//\t// The number of partitions goes up to 11 since we can return 0 through 10\n//\twordsByLength := beam.Partition(s, 11, lenToTen, inputStrings)\n//\n// T is permitted to be a KV.\nfunc Partition(s Scope, n int, fn any, col PCollection) []PCollection {\n\ts = s.Scope(fmt.Sprintf(\"Partition(%v)\", n))\n\n\tif n < 1 {\n\t\tpanic(\"n must be > 0\")\n\t}\n\tvar emit reflect.Type\n\tvar in []reflect.Type\n\tif typex.IsKV(col.Type()) {\n\t\tcomps := col.Type().Components()\n\t\tk, v := comps[0].Type(), comps[1].Type()\n\t\tfuncx.MustSatisfy(fn, funcx.Replace(funcx.Replace(sigKV, TType, k), UType, v))\n\t\temit = reflect.FuncOf([]reflect.Type{EventTimeType, k, v}, nil, false)\n\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","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/partition.go#L42-L78","documentation":"beam.Partition splits a PCollection into n collections using a user function. The library panics with \"n must be > 0\" when the requested partition count n is less than 1, because partitioning into zero or negative parts is meaningless.","triggerScenarios":"Calling beam.Partition(s, 0, fn, col) or with any negative n; typically n comes from a variable/config value that was never validated.","commonSituations":"Partition count read from a config file, CLI flag, or computed value that is 0 when a list/collection is empty; arithmetic like len(items)-1 yielding 0 for single-element input.","solutions":["Validate n >= 1 before calling Partition and choose a sensible default (e.g. 1).","Fix the computation that derives n so it cannot be 0 or negative for the actual input size.","If the input can be empty, branch early and skip the Partition transform entirely."],"exampleFix":"// before\nn := len(keys) - 1\nbeam.Partition(s, n, partitionFn, col)\n\n// after\nn := len(keys)\nif n < 1 {\n    n = 1\n}\nbeam.Partition(s, n, partitionFn, col)","handlingStrategy":"validation","validationCode":"if n < 1 {\n    return fmt.Errorf(\"partition count must be >= 1, got %d\", n)\n}","typeGuard":null,"tryCatchPattern":"defer func() { if r := recover(); r != nil { err = fmt.Errorf(\"Partition failed: %v\", r) } }()","preventionTips":["Validate any n derived from config, flags, or len() computations","Default to 1 partition when the computed count would be 0","Skip the Partition transform entirely for empty inputs"],"tags":["go","apache-beam","partition","panic","argument-validation"],"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"}