Skip to content

Commit

Permalink
try: use empty struct to create null value
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman committed Jan 15, 2025
1 parent 86c1b8e commit a818751
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions ext/maxcompute/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
errs "errors"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -190,23 +189,13 @@ func createData(value interface{}, dt datatype.DataType) (data.Data, error) {
return data.BigInt(curr), nil
case datatype.DECIMAL:
if value == nil {
// get decimal precision and scale
decimalType, ok := dt.(datatype.DecimalType)
if !ok {
return nil, errors.WithStack(fmt.Errorf("dt is not a decimal"))
}
return data.NewDecimal(int(decimalType.Precision), int(decimalType.Scale), "0"), nil
return data.Decimal{}, nil
}

var curr float64
switch v := value.(type) {
case string:
// Parse decimal string to float
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, errors.WithStack(fmt.Errorf("failed to parse decimal string %s: %w", v, err))
}
curr = float64(f)
return data.DecimalFromStr(v)
case float64:
curr = v
case float32:
Expand Down Expand Up @@ -259,17 +248,17 @@ func createData(value interface{}, dt datatype.DataType) (data.Data, error) {
if value == nil {
switch dt.ID() {
case datatype.DATE:
return data.NewDate("0001-01-01")
return data.Date{}, nil
case datatype.DATETIME:
return data.NewDateTime("0001-01-01 00:00:00")
return data.DateTime{}, nil
case datatype.TIMESTAMP:
return data.NewTimestamp("0001-01-01 00:00:00.000")
return data.Timestamp{}, nil
case datatype.TIMESTAMP_NTZ:
return data.NewTimestampNtz("0001-01-01 00:00:00.000")
return data.TimestampNtz{}, nil
case datatype.CHAR:
return data.NewChar(dt.(datatype.CharType).Length, "")
return data.Char{}, nil
case datatype.VARCHAR:
return data.NewVarChar(dt.(datatype.VarcharType).Length, "")
return data.VarChar{}, nil
}
return data.String(""), nil
}
Expand Down

0 comments on commit a818751

Please sign in to comment.