{"record":{"id":"617f74422feba750","repo":"apache/beam","slug":"fraction-must-be-between-0-and-1","errorCode":null,"errorMessage":"fraction must be between 0 and 1","messagePattern":"fraction must be between 0 and 1","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/id_range_tracker.go","lineNumber":98,"sourceCode":"}\n\n// GetError returns the error associated with the tracker, if any.\nfunc (rt *idRangeTracker) GetError() error {\n\treturn rt.err\n}\n\n// TrySplit splits the underlying restriction into a primary and residual restriction based on the\n// fraction of remaining work the primary should be responsible for. The restriction may be modified\n// as a result of the split. The primary is a copy of the tracker's restriction after the split.\n// If the fraction is 1 or all work has already been claimed, returns the full restriction as the\n// primary and nil as the residual. If the fraction is 0, stops the tracker, cuts off any remaining\n// work from its underlying restriction, and returns a residual representing all remaining work.\n// If the fraction is between 0 and 1, attempts to split the remaining work of the underlying\n// restriction into two sub-restrictions based on the fraction and assigns them to the primary and\n// residual respectively. Returns an error if the split cannot be performed.\nfunc (rt *idRangeTracker) TrySplit(fraction float64) (primary, residual any, err error) {\n\tif fraction < 0 || fraction > 1 {\n\t\treturn nil, nil, errors.New(\"fraction must be between 0 and 1\")\n\t}\n\n\tdone, remaining := rt.cutRestriction()\n\n\tif fraction == 1 || remaining.Count == 0 {\n\t\treturn rt.rest, nil, nil\n\t}\n\n\tif fraction == 0 {\n\t\trt.rest = done\n\t\treturn rt.rest, remaining, nil\n\t}\n\n\tctx := context.Background()\n\n\tprimaryRem, resid, err := remaining.FractionSplits(ctx, rt.collection, fraction)\n\tif err != nil {\n\t\treturn nil, nil, err","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/id_range_tracker.go#L80-L116","documentation":"idRangeTracker.TrySplit implements the Beam restriction-tracker split protocol for MongoDB _id range restrictions. The split fraction must lie in [0,1]; anything outside cannot be interpreted as a proportional split, so the tracker refuses immediately. This is a defensive check required by the Beam split contract.","triggerScenarios":"Calling TrySplit(fraction) with fraction < 0 or fraction > 1, e.g. TrySplit(1.5) or TrySplit(-0.1), whether invoked directly in tests or via a custom SplittableDoFn runner/splitter that computes a bad fraction.","commonSituations":"Custom split logic computing a fraction from a mis-derived ratio (e.g. integer division or mixing up numerator/denominator); framework code passing an unclamped user-configured split percentage like 150 instead of 1.5 or 0.5.","solutions":["Clamp or validate the fraction before calling TrySplit: math.Max(0, math.Min(1, fraction)).","If the fraction is computed from a percentage, divide by 100 (150% -> 1.5 is still invalid; a split of 'all work' is fraction 1).","Review custom runner/splitter code deriving the fraction to fix the ratio computation at the source."],"exampleFix":"// before\nprimary, residual, err := tracker.TrySplit(splitPct) // splitPct = 150\n// after\nf := math.Max(0, math.Min(1, splitPct/100))\nprimary, residual, err := tracker.TrySplit(f)","handlingStrategy":"validation","validationCode":"if fraction < 0 || fraction > 1 {\n    return fmt.Errorf(\"split fraction %v out of [0,1]\", fraction)\n}\ntracker.TrySplit(fraction)","typeGuard":"func validFraction(f float64) bool { return f >= 0 && f <= 1 && !math.IsNaN(f) }","tryCatchPattern":"primary, residual, err := tracker.TrySplit(f)\nif err != nil {\n    return fmt.Errorf(\"split failed: %w\", err)\n}","preventionTips":["Clamp computed fractions with math.Max(0, math.Min(1, f)) before splitting.","Convert percentages to fractions in one place, not at call sites.","Unit-test split helpers with boundary values 0, 0.5, and 1."],"tags":["go","apache-beam","mongodb","split-fraction","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-15T13:17:12.816Z"}