apache/beam · error

n must be > 0

Error message

n must be > 0

What it means

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.

Source

Thrown at sdks/go/pkg/beam/partition.go:60

//	func lenToTen(s string) int {
//		if len(s) > 9 {
//			return 10
//		}
//		return len(s)
//	}
//
//	// Partition functions must be registered with Beam, and must not be closures.
//	func init() { register.Function1x1(lenToTen) }
//
//	// The number of partitions goes up to 11 since we can return 0 through 10
//	wordsByLength := beam.Partition(s, 11, lenToTen, inputStrings)
//
// T is permitted to be a KV.
func Partition(s Scope, n int, fn any, col PCollection) []PCollection {
	s = s.Scope(fmt.Sprintf("Partition(%v)", n))

	if n < 1 {
		panic("n must be > 0")
	}
	var emit reflect.Type
	var in []reflect.Type
	if typex.IsKV(col.Type()) {
		comps := col.Type().Components()
		k, v := comps[0].Type(), comps[1].Type()
		funcx.MustSatisfy(fn, funcx.Replace(funcx.Replace(sigKV, TType, k), UType, v))
		emit = reflect.FuncOf([]reflect.Type{EventTimeType, k, v}, nil, false)
		in = []reflect.Type{EventTimeType, k, v}
	} else {
		t := col.Type().Type()
		funcx.MustSatisfy(fn, funcx.Replace(sig, TType, t))
		emit = reflect.FuncOf([]reflect.Type{EventTimeType, t}, nil, false)
		in = []reflect.Type{EventTimeType, t}
	}

	// The partitionFn is a DoFn with a signature that is dependent on the input, so
	// neither reflection nor type-specialization is adequate. Instead, it uses a

View on GitHub (pinned to 12126d8942)

Solutions

  1. Validate n >= 1 before calling Partition and choose a sensible default (e.g. 1).
  2. Fix the computation that derives n so it cannot be 0 or negative for the actual input size.
  3. If the input can be empty, branch early and skip the Partition transform entirely.

Example fix

// before
n := len(keys) - 1
beam.Partition(s, n, partitionFn, col)

// after
n := len(keys)
if n < 1 {
    n = 1
}
beam.Partition(s, n, partitionFn, col)
Defensive patterns

Strategy: validation

Validate before calling

if n < 1 {
    return fmt.Errorf("partition count must be >= 1, got %d", n)
}

Try / catch

defer func() { if r := recover(); r != nil { err = fmt.Errorf("Partition failed: %v", r) } }()

Prevention

When it happens

Trigger: Calling beam.Partition(s, 0, fn, col) or with any negative n; typically n comes from a variable/config value that was never validated.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/6f189f6904f2c569. Report an issue: GitHub.