Skip to content

Commit

Permalink
fix: cast DuckDB HUGEINT to pgtype.Numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
VWagen1989 committed Jan 8, 2025
1 parent a3950a7 commit 5b3681e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pgserver/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"fmt"
"io"
"math/big"
"strings"

"github.com/dolthub/go-mysql-server/sql"
Expand Down Expand Up @@ -87,6 +88,7 @@ type SqlRowIter struct {

decimals []int
lists []int
hugeInts []int
}

func NewSqlRowIter(rows *stdsql.Rows, schema sql.Schema) (*SqlRowIter, error) {
Expand Down Expand Up @@ -116,7 +118,14 @@ func NewSqlRowIter(rows *stdsql.Rows, schema sql.Schema) (*SqlRowIter, error) {
}
}

iter := &SqlRowIter{rows, columns, schema, buf, ptrs, decimals, lists}
var hugeInts []int
for i, t := range columns {
if t.DatabaseTypeName() == "HUGEINT" {
hugeInts = append(hugeInts, i)
}
}

iter := &SqlRowIter{rows, columns, schema, buf, ptrs, decimals, lists, hugeInts}
if logrus.GetLevel() >= logrus.DebugLevel {
logrus.Debugf("New " + iter.String() + "\n")
}
Expand Down Expand Up @@ -197,6 +206,21 @@ func (iter *SqlRowIter) Next(ctx *sql.Context) (sql.Row, error) {
iter.buffer[idx] = pgtype.FlatArray[any](list)
}

for _, idx := range iter.hugeInts {
switch v := iter.buffer[idx].(type) {
case nil:
continue
case *big.Int:
var n pgtype.Numeric
if err := n.Scan(v.String()); err != nil {
return nil, err
}
iter.buffer[idx] = n
default:
return nil, fmt.Errorf("unexpected type %T for big.Int value", v)
}
}

// Prune or fill the values to match the schema
width := len(iter.schema) // the desired width
if width == 0 {
Expand Down

0 comments on commit 5b3681e

Please sign in to comment.