apache/beam · error
Unexpected number type
Error message
Unexpected number type: %v
What it means
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.
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.
Example fix
// before
stats.Sum(s, complexCol) // panics: complex128 unsupported
// after
mags := beam.Map(s, func(c complex128) float64 { return real(c) }, complexCol)
stats.Sum(s, mags) Defensive patterns
Strategy: validation
Validate before calling
// Check element type before stats.Sum
switch col.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
// ok
default:
panic("stats.Sum requires a numeric element type")
} Type guard
func isSummable(v any) bool {
switch reflect.ValueOf(v).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return true
}
return false
} Try / catch
func safeSum(s beam.Scope, col beam.PCollection) (ret beam.PCollection) {
defer func() {
if r := recover(); r != nil {
log.Printf("stats.Sum unsupported type: %v", r)
ret = nil
}
}()
return stats.Sum(s, col)
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Unexpected number type
- Unexpected number type
- bad output type
- Node type not bound
- observed PCollection has incompatible type
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/c5b2de02577fe7cb.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/go/pkg/beam/transforms/stats/sum_switch.tmpl:30
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stats
import (
"fmt"
"reflect"
)
func findSumFn(t reflect.Type) any {
switch t.String() {
{{- range .X}}
case "{{.Type}}":
return sum{{.Name}}Fn
{{- end}}
default:
panic(fmt.Sprintf("Unexpected number type: %v", t))
}
}
{{range .X}}
func sum{{.Name}}Fn(x, y {{.Type}}) {{.Type}} {
return x + y
}
{{end}}
View on GitHub (pinned to 12126d8942)