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

Commit

Permalink
add SetLengthPrecisionScale [nt]
Browse files Browse the repository at this point in the history
  • Loading branch information
flarco committed Jan 23, 2024
1 parent aa8f464 commit d155b99
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,9 @@ func SQLColumns(colTypes []ColumnType, conn Connection) (columns iop.Columns) {
}
}

// parse length, precision, scale manually specified in mapping
col.SetLengthPrecisionScale()

// g.Trace("col %s (%s -> %s) has %d length, %d scale, sourced: %t", colType.Name(), colType.DatabaseTypeName(), Type, length, scale, ok)

columns[i] = col
Expand Down
2 changes: 2 additions & 0 deletions database/database_bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ func (conn *BigQueryConn) getItColumns(itSchema bigquery.Schema) (cols iop.Colum
Type: NativeTypeToGeneral(field.Name, string(field.Type), conn),
DbType: string(field.Type),
}
cols[i].SetLengthPrecisionScale()

if g.In(field.Type, bigquery.NumericFieldType, bigquery.FloatFieldType) {
bQTC.numericCols = append(bQTC.numericCols, i)
} else if field.Type == "DATETIME" || field.Type == bigquery.TimestampFieldType {
Expand Down
1 change: 1 addition & 0 deletions database/schemata.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (s *Schemata) Columns() map[string]iop.Column {
for _, column := range table.Columns {
// get general type
column.Type = NativeTypeToGeneral(column.Name, column.DbType, s.conn)
column.SetLengthPrecisionScale()
key := strings.ToLower(g.F("%s.%s.%s.%s", db.Name, schema.Name, table.Name, column.Name))
columns[key] = column
}
Expand Down
2 changes: 1 addition & 1 deletion database/templates/types_native_to_general.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ clickhouse string text col_string string TRUE TRUE
clickhouse tuple string col_tuple tuple TRUE TRUE
clickhouse uint16 integer col_uint16 uint16 TRUE TRUE
clickhouse uint32 bigint col_uint32 uint32 TRUE TRUE
clickhouse uint64 decimal col_uint64 uint64 TRUE TRUE
clickhouse uint64 decimal(38,9) col_uint64 uint64 TRUE TRUE
clickhouse uint8 integer col_uint8 uint8 TRUE TRUE
clickhouse json json col_json json FALSE FALSE
clickhouse map json col_map json FALSE FALSE
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/docker/docker v24.0.7+incompatible
github.com/dustin/go-humanize v1.0.1
github.com/flarco/bigquery v0.0.9
github.com/flarco/g v0.1.67
github.com/flarco/g v0.1.68
github.com/go-sql-driver/mysql v1.5.0
github.com/godror/godror v0.31.0
github.com/google/uuid v1.5.0
Expand Down
37 changes: 37 additions & 0 deletions iop/datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,43 @@ func MakeRowsChan() chan []any {
return make(chan []any)
}

const regexExtractPrecisionScale = `[a-zA-Z]+ *\( *(\d+) *(, *\d+)* *\)`

// SetLengthPrecisionScale parse length, precision, scale
func (col *Column) SetLengthPrecisionScale() {
colType := strings.TrimSpace(string(col.Type))
if !strings.HasSuffix(colType, ")") {
return
}

// fix type value
parts := strings.Split(colType, "(")
col.Type = ColumnType(strings.TrimSpace(parts[0]))

matches := g.Matches(colType, regexExtractPrecisionScale)
if len(matches) == 1 {
vals := matches[0].Group

if len(vals) > 0 {
vals[0] = strings.TrimSpace(vals[0])
// grab length or precision
if col.Type.IsString() {
col.Stats.MaxLen = cast.ToInt(vals[0])
} else if col.Type.IsNumber() {
col.DbPrecision = cast.ToInt(vals[0])
}
}

if len(vals) > 1 {
vals[1] = strings.TrimSpace(strings.ReplaceAll(vals[1], ",", ""))
// grab scale
if col.Type.IsNumber() {
col.DbScale = cast.ToInt(vals[1])
}
}
}
}

func (col *Column) Key() string {
parts := []string{}
if col.Database != "" {
Expand Down

0 comments on commit d155b99

Please sign in to comment.