From 527a8cc9c5f1650accf0ffbe9cc7110bdb6b3af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= Date: Sun, 13 Oct 2024 06:23:51 +0000 Subject: [PATCH] feat: infer Decimal --- proto/col_auto.go | 44 ++++++++++++++++++++++++++++++++++++++++++ proto/col_auto_test.go | 11 +++++++++++ proto/column.go | 1 + 3 files changed, 56 insertions(+) diff --git a/proto/col_auto.go b/proto/col_auto.go index bda616be..c72615da 100644 --- a/proto/col_auto.go +++ b/proto/col_auto.go @@ -1,6 +1,7 @@ package proto import ( + "strconv" "strings" "github.com/go-faster/errors" @@ -74,6 +75,49 @@ func (c *ColAuto) Infer(t ColumnType) error { c.Data = v c.DataType = t return nil + case ColumnTypeDecimal: + var prec int + precStr, _, _ := strings.Cut(string(t.Elem()), ",") + if precStr != "" { + var err error + precStr = strings.TrimSpace(precStr) + prec, err = strconv.Atoi(precStr) + if err != nil { + return errors.Wrap(err, "decimal") + } + } else { + prec = 10 + } + switch { + case prec >= 1 && prec < 10: + c.Data = new(ColDecimal32) + case prec >= 10 && prec < 19: + c.Data = new(ColDecimal64) + case prec >= 19 && prec < 39: + c.Data = new(ColDecimal128) + case prec >= 39 && prec < 77: + c.Data = new(ColDecimal256) + default: + return errors.Errorf("decimal precision %d out of range", prec) + } + c.DataType = t + return nil + case ColumnTypeDecimal32: + c.Data = new(ColDecimal32) + c.DataType = t + return nil + case ColumnTypeDecimal64: + c.Data = new(ColDecimal64) + c.DataType = t + return nil + case ColumnTypeDecimal128: + c.Data = new(ColDecimal128) + c.DataType = t + return nil + case ColumnTypeDecimal256: + c.Data = new(ColDecimal256) + c.DataType = t + return nil case ColumnTypeEnum8, ColumnTypeEnum16: v := new(ColEnum) if err := v.Infer(t); err != nil { diff --git a/proto/col_auto_test.go b/proto/col_auto_test.go index a889fd11..5c0b3f4b 100644 --- a/proto/col_auto_test.go +++ b/proto/col_auto_test.go @@ -46,6 +46,17 @@ func TestColAuto_Infer(t *testing.T) { ColumnTypeUUID, ColumnTypeArray.Sub(ColumnTypeUUID), ColumnTypeNullable.Sub(ColumnTypeUUID), + ColumnTypeDecimal, + ColumnTypeDecimal32, + ColumnTypeDecimal64, + ColumnTypeDecimal128, + ColumnTypeDecimal256, + "Decimal(2)", + "Decimal(20, 2)", + "Decimal32(1)", + "Decimal64(2)", + "Decimal128(3)", + "Decimal256(4)", } { r := AutoResult("foo") require.NoError(t, r.Data.(Inferable).Infer(columnType)) diff --git a/proto/column.go b/proto/column.go index cc734234..00d4123a 100644 --- a/proto/column.go +++ b/proto/column.go @@ -207,6 +207,7 @@ const ( ColumnTypeBool ColumnType = "Bool" ColumnTypeTuple ColumnType = "Tuple" ColumnTypeNullable ColumnType = "Nullable" + ColumnTypeDecimal ColumnType = "Decimal" ColumnTypeDecimal32 ColumnType = "Decimal32" ColumnTypeDecimal64 ColumnType = "Decimal64" ColumnTypeDecimal128 ColumnType = "Decimal128"