{"record":{"id":"2d58c6d2ce3d53f2","repo":"apache/beam","slug":"unexpected-number-type-v-max-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/max_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 findMaxFn(t reflect.Type) any {\n    switch t.String() {\n{{- range .X}}\n    case \"{{.Type}}\":\n\t\treturn max{{.Name}}Fn\n{{- end}}\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"Unexpected number type: %v\", t))\n\t}\n}\n{{range .X}}\nfunc max{{.Name}}Fn(x, y {{.Type}}) {{.Type}} {\n\tif x > y {\n\t\treturn x\n\t}\n\treturn y\n}\n{{end}}\n","sourceCodeStart":12,"sourceCodeEnd":41,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/stats/max_switch.tmpl#L12-L41","documentation":"In Apache Beam Go SDK's stats package, maxPerKey/Mean-like helpers dispatch to a numeric-specific max function via a generated switch on reflect.Type. If the requested type does not correspond to any generated instantiation (int, int8..., float64, etc.), the default branch panics. This is a defensive check that the API was only called for supported numeric types.","triggerScenarios":"Calling stats.Max (or the MaxPerKey/Max switch dispatcher) with a PCollection whose element type is not one of the generated numeric types — e.g. a struct, string, interface, uint64 on some builds, or an unnamed/aliased type whose reflect.String() doesn't match the case list.","commonSituations":"Feeding stats.Max a PCollection of strings or custom types, using type aliases that produce unexpected reflect type strings, or pipeline reshaping that loses the concrete numeric type (element type becomes interface{}).","solutions":["Ensure the input PCollection elements are one of the supported numeric types (ints, uints, float32/float64 as generated in the template).","Pre-map the PCollection to a supported numeric type with beam.Map before applying stats.Max.","Check for type alias/defined-type mismatches and convert with explicit casts.","If a legit numeric type is missing, regenerate templates or extend the stats package's X list for that type."],"exampleFix":"// before\nwords := beam.ParDo(s, func(w string) string { return w }, in)\nstats.Max(s, words) // panics: string\n\n// after\nlens := beam.Map(s, func(w string) int { return len(w) }, in)\nstats.Max(s, lens)","handlingStrategy":"validation","validationCode":"// Guard: only call stats.Max on supported numeric element types\nfunc maxSupported(c beam.PCollection) bool {\n    switch c.Type().String() {\n    case \"int\", \"int8\", \"int16\", \"int32\", \"int64\",\n        \"uint\", \"uint8\", \"uint16\", \"uint32\", \"uint64\",\n        \"float32\", \"float64\":\n        return true\n    }\n    return false\n}","typeGuard":"func isNumericForStats(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":"// Beam panics abort the pipeline; validate eagerly instead.\n// If you must catch:\nfunc safeMax(s beam.Scope, col beam.PCollection) (ret beam.PCollection) {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Printf(\"stats.Max failed: %v\", r)\n            ret = nil\n        }\n    }()\n    return stats.Max(s, col)\n}","preventionTips":["Feed stats.Max only int*/uint*/float* element types.","Convert non-numeric elements with beam.Map before aggregating.","Beware defined types (type MyInt int) — cast to base types first.","Check c.Type() in pipeline construction to fail fast."],"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"}