{"record":{"id":"590a2b8b12ab9af0","repo":"apache/beam","slug":"size-returned-expected-to-be-non-negative-but-received-v","errorCode":null,"errorMessage":"size returned expected to be non-negative but received %v.","messagePattern":"size returned expected to be non-negative but received (.+?)\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/core/runtime/exec/sdf.go","lineNumber":214,"sourceCode":"\tws := elm.Elm2.(*FullValue).Elm2\n\n\t// If receiving directly from a datasource,\n\t// the element may not be wrapped in a *FullValue\n\tmainElm := convertIfNeeded(elm.Elm, &FullValue{})\n\n\tsplitRests, err := n.splitInv.Invoke(ctx, mainElm, rest)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfor _, splitRest := range splitRests {\n\t\tsize, err := n.sizeInv.Invoke(ctx, mainElm, splitRest)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif size < 0 {\n\t\t\terr := errors.Errorf(\"size returned expected to be non-negative but received %v.\", size)\n\t\t\treturn errors.WithContextf(err, \"%v\", n)\n\t\t}\n\t\toutput := &FullValue{}\n\n\t\toutput.Timestamp = elm.Timestamp\n\t\toutput.Windows = elm.Windows\n\t\toutput.Elm = &FullValue{Elm: mainElm, Elm2: &FullValue{Elm: splitRest, Elm2: ws}}\n\t\toutput.Elm2 = size\n\n\t\tif err := n.Out.ProcessElement(ctx, output, values...); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// FinishBundle resets the invokers.","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/core/runtime/exec/sdf.go#L196-L232","documentation":"In ProcessSizedElementsAndRestrictions.ProcessElement, the size function (sizeInv) invoked on the element and remaining restriction must return a non-negative size. A user-supplied Size() method returning a negative value is a contract violation, so the SDF aborts with this contextualized error.","triggerScenarios":"A splittable DoFn whose Size() method (sizeFn) returns a negative value for a given element + restriction during ProcessElement — e.g. computing size from a byte length that underflowed or a lookup that returned -1 as a sentinel.","commonSituations":"Size() implementations using len() on data whose length computation underflows, returning -1 for 'unknown' size, or a size based on a restriction that is inverted/empty.","solutions":["Fix the Size() method to return >= 0 for all valid element/restriction pairs","Replace sentinel -1 returns with a proper default (e.g. 0 or an estimated size)","Clamp the computed size: if size < 0 { size = 0 } (or fail fast with a descriptive error)","Test Size() against edge-case elements (empty restrictions, zero-length payloads)"],"exampleFix":"// before\nfunc (fn *myFn) Size(elm string, rest RangeRestriction) (float64, error) {\n    return int64(len(rest.End) - len(rest.Start)) // can be negative\n}\n// after\nfunc (fn *myFn) Size(elm string, rest RangeRestriction) (float64, error) {\n    size := rest.End - rest.Start\n    if size < 0 {\n        return 0, fmt.Errorf(\"invalid restriction %v\", rest)\n    }\n    return float64(size) * bytesPerElement, nil\n}","handlingStrategy":"validation","validationCode":"// Guard inside the Size() implementation\nfunc (fn *myFn) Size(elm string, rest RangeRestriction) (float64, error) {\n    size := rest.End - rest.Start\n    if size < 0 {\n        return 0, fmt.Errorf(\"invalid restriction %v: negative size\", rest)\n    }\n    return float64(size), nil\n}","typeGuard":"func nonNegativeSize(s float64) float64 { if s < 0 { return 0 }; return s }","tryCatchPattern":"if err := plan.Execute(ctx); err != nil {\n    if strings.Contains(err.Error(), \"non-negative\") {\n        log.Printf(\"Size() contract violated: %v\", err)\n    }\n    return err\n}","preventionTips":["Never return -1 as an 'unknown size' sentinel from Size()","Unit-test Size() for empty restrictions and zero-length elements","Compute sizes with unsigned/checked arithmetic to avoid underflow","Clamp negative results to 0 as a last resort"],"tags":["go","apache-beam","splittable-dofn","sdf"],"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-14T16:17:12.679Z"}