{"record":{"id":"c5b2de02577fe7cb","repo":"apache/beam","slug":"unexpected-number-type-v-c5b2de","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.tmpl","lineNumber":30,"sourceCode":"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\npackage stats\n\nimport (\n    \"fmt\"\n    \"reflect\"\n)\n\nfunc findSumFn(t reflect.Type) any {\n    switch t.String() {\n{{- range .X}}\n    case \"{{.Type}}\":\n\t\treturn sum{{.Name}}Fn\n{{- end}}\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"Unexpected number type: %v\", t))\n\t}\n}\n{{range .X}}\nfunc sum{{.Name}}Fn(x, y {{.Type}}) {{.Type}} {\n\treturn x + y\n}\n{{end}}\n","sourceCodeStart":12,"sourceCodeEnd":38,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/stats/sum_switch.tmpl#L12-L38","documentation":"In Apache Beam Go SDK's stats package, the sum helpers dispatch to a numeric-specific sum function via a generated switch on reflect.Type. If the PCollection element type is not among the generated numeric instantiations, the default branch panics with 'Unexpected number type'. This guards that Sum is only used on supported numeric types.","triggerScenarios":"Calling stats.Sum on a PCollection whose element type isn't in the generated list (e.g. strings, structs, complex numbers, defined types whose reflect.String() has no case).","commonSituations":"Summing over collections of custom value types, element types erased to interface{} by prior transforms, or using numeric types not instantiated by the codegen template.","solutions":["Ensure the PCollection element type is a supported numeric type before calling stats.Sum.","Add a beam.Map to convert elements to int64/float64 (etc.) before summing.","Explicitly cast defined types back to base numeric types upstream.","Regenerate/extend the stats template type list if a new numeric type must be supported."],"exampleFix":"// before\nstats.Sum(s, complexCol) // panics: complex128 unsupported\n\n// after\nmags := beam.Map(s, func(c complex128) float64 { return real(c) }, complexCol)\nstats.Sum(s, mags)","handlingStrategy":"validation","validationCode":"// Check element type before stats.Sum\nswitch col.Type().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    panic(\"stats.Sum requires a numeric element type\")\n}","typeGuard":"func isSummable(v any) bool {\n    switch reflect.ValueOf(v).Kind() {\n    case 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        return true\n    }\n    return false\n}","tryCatchPattern":"func safeSum(s beam.Scope, col beam.PCollection) (ret beam.PCollection) {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Printf(\"stats.Sum unsupported type: %v\", r)\n            ret = nil\n        }\n    }()\n    return stats.Sum(s, col)\n}","preventionTips":["Sum only int*/uint*/float* PCollections.","Convert strings/structs to numbers via beam.Map before Sum.","Cast defined types (type Price int64) back to base numeric types.","Unit-test pipeline construction so type panics surface in CI, not production runners."],"tags":["go","beam","stats","types"],"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"}