Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: infer Decimal #434

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions proto/col_auto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proto

import (
"strconv"
"strings"

"github.com/go-faster/errors"
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions proto/col_auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions proto/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading