Skip to content

Commit

Permalink
Fix serializing INTERVAL data types
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Feb 24, 2025
1 parent bac6b91 commit 7512ece
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ func (nullDecimal NullDecimal) String() string {

////////////////////////////////////////////////////////////////////////////////////////////////////

type NullInterval struct {
Present bool
Value duckDb.Interval
}

func (nullInterval *NullInterval) Scan(value interface{}) error {
if value == nil {
nullInterval.Present = false
return nil
}

nullInterval.Present = true
nullInterval.Value = value.(duckDb.Interval)
return nil
}

func (nullInterval NullInterval) String() string {
if nullInterval.Present {
return fmt.Sprintf("%d months %d days %d microseconds", nullInterval.Value.Months, nullInterval.Value.Days, nullInterval.Value.Micros)
}
return ""
}

////////////////////////////////////////////////////////////////////////////////////////////////////

type NullUint32 struct {
Present bool
Value uint32
Expand Down Expand Up @@ -565,6 +590,10 @@ func (queryHandler *QueryHandler) columnTypeOid(col *sql.ColumnType) uint32 {
return pgtype.UUIDOID
case "BLOB[]":
return pgtype.UUIDArrayOID
case "INTERVAL":
return pgtype.IntervalOID
case "INTERVAL[]":
return pgtype.IntervalArrayOID
default:
if strings.HasPrefix(col.DatabaseTypeName(), "DECIMAL") {
if strings.HasSuffix(col.DatabaseTypeName(), "[]") {
Expand Down Expand Up @@ -630,6 +659,9 @@ func (queryHandler *QueryHandler) generateDataRow(rows *sql.Rows, cols []*sql.Co
case "duckdb.Decimal":
var value NullDecimal
valuePtrs[i] = &value
case "duckdb.Interval":
var value NullInterval
valuePtrs[i] = &value
case "[]interface {}":
var value NullArray
valuePtrs[i] = &value
Expand Down Expand Up @@ -721,6 +753,12 @@ func (queryHandler *QueryHandler) generateDataRow(rows *sql.Rows, cols []*sql.Co
} else {
values = append(values, nil)
}
case *NullInterval:
if value.Present {
values = append(values, []byte(value.String()))
} else {
values = append(values, nil)
}
case *NullArray:
if value.Present {
values = append(values, []byte(value.String()))
Expand Down
5 changes: 5 additions & 0 deletions src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ func TestHandleQuery(t *testing.T) {
"types": {Uint32ToString(pgtype.UUIDOID)},
"values": {"58a7c845-af77-44b2-8664-7ca613d92f04"},
},
"SELECT '1 week'::INTERVAL AS interval": {
"description": {"interval"},
"types": {Uint32ToString(pgtype.IntervalOID)},
"values": {"0 months 7 days 0 microseconds"},
},

// SELECT * FROM function()
"SELECT * FROM pg_catalog.pg_get_keywords() LIMIT 1": {
Expand Down

0 comments on commit 7512ece

Please sign in to comment.