Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
improve clickhouse decimal logic [nt]
Browse files Browse the repository at this point in the history
  • Loading branch information
flarco committed Jan 19, 2024
1 parent ee99745 commit eb1465b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@ func (conn *BaseConn) GetNativeType(col iop.Column) (nativeType string, err erro
fmt.Sprintf("(%d)", length),
)
}
} else if strings.HasSuffix(nativeType, "(,)") {
} else if strings.Contains(nativeType, "(,)") {

scale := lo.Ternary(col.DbScale < ddlMinDecScale, ddlMinDecScale, col.DbScale)
scale = lo.Ternary(scale < col.Stats.MaxDecLen, col.Stats.MaxDecLen, scale)
Expand Down
10 changes: 10 additions & 0 deletions database/database_clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/flarco/dbio"
"github.com/flarco/dbio/iop"
"github.com/shopspring/decimal"
"github.com/spf13/cast"

"github.com/flarco/g"
Expand Down Expand Up @@ -146,6 +147,15 @@ func (conn *ClickhouseConn) BulkImportStream(tableFName string, ds *iop.Datastre
}

for row := range batch.Rows {
// set decimals correctly
for i, col := range batch.Columns {
if col.Type.IsDecimal() {
if val, err := decimal.NewFromString(cast.ToString(row[i])); err == nil {
row[i] = val
}
}
}

count++
// Do insert
ds.Context.Lock()
Expand Down
2 changes: 1 addition & 1 deletion database/templates/types_general_to_native.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ binary varbinary() bytes varbinary varbinary varbinary varbinary varchar(65535)
bool varchar(5) bool char(5) varchar(5) varchar(5) varchar(5) bool boolean boolean bool Nullable(String) bool bool
date timestamp(9) timestamp datetime(6) datetime2 datetime2 datetime2 timestamp timestamp text timestamp Nullable(Date) date date
datetime timestamp(9) timestamp datetime(6) datetime2 datetime2 datetime2 timestamp timestamp text timestamp Nullable(DateTime) datetime datetime
decimal number(,) numeric decimal(,) decimal(,) decimal(,) decimal(,) decimal(,) decimal(,) real numeric Nullable(Float64) decimal(,) decimal(,)
decimal number(,) numeric decimal(,) decimal(,) decimal(,) decimal(,) decimal(,) decimal(,) real numeric Nullable(Decimal(,)) decimal(,) decimal(,)
integer number(10) integer integer integer integer integer integer integer integer int64 Nullable(Int64) integer integer
json clob jsonb json nvarchar(max) nvarchar(65535) nvarchar(max) varchar(65535) variant text json Nullable(String) json json
smallint number(5) smallint smallint smallint smallint smallint smallint smallint integer int64 Nullable(Int32) smallint smallint
Expand Down
2 changes: 1 addition & 1 deletion iop/stream_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func (sp *StreamProcessor) CastVal(i int, val interface{}, col *Column) interfac

// max 9 decimals for bigquery compatibility
if sp.config.MaxDecimals > -1 {
nVal = math.Round(fVal*sp.config.MaxDecimals) / sp.config.MaxDecimals
nVal = cast.ToString(math.Round(fVal*sp.config.MaxDecimals) / sp.config.MaxDecimals)
} else {
nVal = val // use string to keep accuracy
}
Expand Down

0 comments on commit eb1465b

Please sign in to comment.