{"record":{"id":"869064ebae56e2c4","repo":"apache/beam","slug":"unexpected-number-type-v-sum-switch","errorCode":null,"errorMessage":"Unexpected number type: %v","messagePattern":"Unexpected number type: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/transforms/stats/sum_switch.go","lineNumber":52,"sourceCode":"\t\treturn sumInt32Fn\n\tcase \"int64\":\n\t\treturn sumInt64Fn\n\tcase \"uint\":\n\t\treturn sumUintFn\n\tcase \"uint8\":\n\t\treturn sumUint8Fn\n\tcase \"uint16\":\n\t\treturn sumUint16Fn\n\tcase \"uint32\":\n\t\treturn sumUint32Fn\n\tcase \"uint64\":\n\t\treturn sumUint64Fn\n\tcase \"float32\":\n\t\treturn sumFloat32Fn\n\tcase \"float64\":\n\t\treturn sumFloat64Fn\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"Unexpected number type: %v\", t))\n\t}\n}\n\nfunc sumIntFn(x, y int) int {\n\treturn x + y\n}\n\nfunc sumInt8Fn(x, y int8) int8 {\n\treturn x + y\n}\n\nfunc sumInt16Fn(x, y int16) int16 {\n\treturn x + y\n}\n\nfunc sumInt32Fn(x, y int32) int32 {\n\treturn x + y\n}","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/stats/sum_switch.go#L34-L70","documentation":"stats.Sum selects a per-type summation function via a switch on the element type's kind (sumIntFn, sumUint64Fn, sumFloat32Fn, sumFloat64Fn, etc.). If the element type is not one of the supported numeric kinds, findSumFn panics with 'Unexpected number type'. Only Go fixed numeric types can be summed by this transform.","triggerScenarios":"Calling stats.Sum(s, col) or SumPerKey on a PCollection whose element reflect.Kind is not in the switch (string, bool, struct, slice, uintptr, complex, etc.).","commonSituations":"Summing a string-encoded number without conversion; accidentally summing a struct/record column; using complex128 data; a custom named type whose kind maps to an unsupported case.","solutions":["Verify the element type is a supported numeric kind (int*/uint*/float32/float64) before calling stats.Sum.","Convert the data with a ParDo/Map to float64 or int64 prior to summing.","For complex or decimal types, implement a custom beam.Combine combine function instead of stats.Sum."],"exampleFix":"// before\nstats.Sum(s, recordCol) // recordCol is a struct -> panics\n\n// after\nvalues := beam.ParDo(s, func(r Record) float64 { return r.Amount }, recordCol)\nstats.Sum(s, values)","handlingStrategy":"validation","validationCode":"t := reflect.TypeOf(el)\nswitch t.Kind() {\ncase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n    reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,\n    reflect.Float32, reflect.Float64:\n    // ok\ndefault:\n    return fmt.Errorf(\"stats.Sum requires numeric element type, got %v\", t)\n}","typeGuard":"func isSummable(v any) bool {\n    k := reflect.TypeOf(v).Kind()\n    return k >= reflect.Int && k <= reflect.Float64 && k != reflect.Uintptr\n}","tryCatchPattern":null,"preventionTips":["Convert strings/decimals to float64 in an upstream ParDo before summing.","Avoid passing record structs directly to Sum; extract the numeric field first.","Keep a unit test asserting the PCollection coder type before aggregation."],"tags":["go","apache-beam","panic","unsupported-type","stats"],"backgroundTag":"unsupported-dtype","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}