diff --git a/go/libraries/doltcore/env/actions/infer_schema.go b/go/libraries/doltcore/env/actions/infer_schema.go index 797833e9d8..fabd2a4721 100644 --- a/go/libraries/doltcore/env/actions/infer_schema.go +++ b/go/libraries/doltcore/env/actions/infer_schema.go @@ -16,6 +16,7 @@ package actions import ( "context" + "errors" "io" "math" "strconv" @@ -217,9 +218,19 @@ func leastPermissiveNumericType(strVal string, floatThreshold float64) (ti typei // always parse as signed int i, err := strconv.ParseInt(strVal, 10, 64) + // use string for out of range + if errors.Is(err, strconv.ErrRange) { + return typeinfo.StringDefaultType + } if err != nil { return typeinfo.UnknownType } + + // handle leading zero case + if len(strVal) > 1 && strVal[0] == '0' { + return typeinfo.StringDefaultType + } + if i >= math.MinInt32 && i <= math.MaxInt32 { return typeinfo.Int32Type } else { diff --git a/go/libraries/doltcore/env/actions/infer_schema_test.go b/go/libraries/doltcore/env/actions/infer_schema_test.go index c603ecf4de..40bdedb68a 100644 --- a/go/libraries/doltcore/env/actions/infer_schema_test.go +++ b/go/libraries/doltcore/env/actions/infer_schema_test.go @@ -75,7 +75,7 @@ func TestLeastPermissiveNumericType(t *testing.T) { floatThreshold float64 expType typeinfo.TypeInfo }{ - {"zero", "0", 0.0, typeinfo.Uint32Type}, + {"zero", "0", 0.0, typeinfo.Int32Type}, {"zero float", "0.0", 0.0, typeinfo.Float32Type}, {"zero float with floatThreshold of 0.1", "0.0", 0.1, typeinfo.Int32Type}, {"negative float", "-1.3451234", 0.0, typeinfo.Float32Type}, @@ -85,8 +85,8 @@ func TestLeastPermissiveNumericType(t *testing.T) { {"all zeroes", "0000", 0.0, typeinfo.StringDefaultType}, {"leading zeroes", "01", 0.0, typeinfo.StringDefaultType}, {"negative int", "-1234", 0.0, typeinfo.Int32Type}, - {"fits in uint64 but not int64", strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.Uint64Type}, - {"negative less than math.MinInt64", "-" + strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.UnknownType}, + {"fits in uint64 but not int64", strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.StringDefaultType}, + {"negative less than math.MinInt64", "-" + strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.StringDefaultType}, {"math.MinInt64", strconv.FormatInt(math.MinInt64, 10), 0.0, typeinfo.Int64Type}, }