Skip to content

Commit

Permalink
Merge pull request #434 from ClickHouse/decimal-inference
Browse files Browse the repository at this point in the history
feat: infer Decimal
  • Loading branch information
ernado authored Oct 15, 2024
2 parents e52bd23 + 527a8cc commit 5f13ec1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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

0 comments on commit 5f13ec1

Please sign in to comment.