Skip to content

Commit 5b3681e

Browse files
committed
fix: cast DuckDB HUGEINT to pgtype.Numeric
1 parent a3950a7 commit 5b3681e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

pgserver/iter.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql/driver"
66
"fmt"
77
"io"
8+
"math/big"
89
"strings"
910

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

8889
decimals []int
8990
lists []int
91+
hugeInts []int
9092
}
9193

9294
func NewSqlRowIter(rows *stdsql.Rows, schema sql.Schema) (*SqlRowIter, error) {
@@ -116,7 +118,14 @@ func NewSqlRowIter(rows *stdsql.Rows, schema sql.Schema) (*SqlRowIter, error) {
116118
}
117119
}
118120

119-
iter := &SqlRowIter{rows, columns, schema, buf, ptrs, decimals, lists}
121+
var hugeInts []int
122+
for i, t := range columns {
123+
if t.DatabaseTypeName() == "HUGEINT" {
124+
hugeInts = append(hugeInts, i)
125+
}
126+
}
127+
128+
iter := &SqlRowIter{rows, columns, schema, buf, ptrs, decimals, lists, hugeInts}
120129
if logrus.GetLevel() >= logrus.DebugLevel {
121130
logrus.Debugf("New " + iter.String() + "\n")
122131
}
@@ -197,6 +206,21 @@ func (iter *SqlRowIter) Next(ctx *sql.Context) (sql.Row, error) {
197206
iter.buffer[idx] = pgtype.FlatArray[any](list)
198207
}
199208

209+
for _, idx := range iter.hugeInts {
210+
switch v := iter.buffer[idx].(type) {
211+
case nil:
212+
continue
213+
case *big.Int:
214+
var n pgtype.Numeric
215+
if err := n.Scan(v.String()); err != nil {
216+
return nil, err
217+
}
218+
iter.buffer[idx] = n
219+
default:
220+
return nil, fmt.Errorf("unexpected type %T for big.Int value", v)
221+
}
222+
}
223+
200224
// Prune or fill the values to match the schema
201225
width := len(iter.schema) // the desired width
202226
if width == 0 {

0 commit comments

Comments
 (0)