Skip to content

Commit

Permalink
Add support for scanning float4 into *float64. This is required to su…
Browse files Browse the repository at this point in the history
…pport scanning

float4 using a `sql.Scanner` (previously this resulted in "did not find a plan" error)

closes jackc#1911
  • Loading branch information
MattBrittan committed Feb 17, 2024
1 parent 5c63f64 commit f8fe85a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pgtype/float4.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ func (Float4Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPl
switch target.(type) {
case *float32:
return scanPlanBinaryFloat4ToFloat32{}
case *float64:
return scanPlanBinaryFloat4ToFloat64{}
case Float64Scanner:
return scanPlanBinaryFloat4ToFloat64Scanner{}
case Int64Scanner:
Expand All @@ -187,6 +189,8 @@ func (Float4Codec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPl
switch target.(type) {
case *float32:
return scanPlanTextAnyToFloat32{}
case *float64:
return scanPlanTextAnyToFloat64{}
case Float64Scanner:
return scanPlanTextAnyToFloat64Scanner{}
case Int64Scanner:
Expand Down Expand Up @@ -215,6 +219,24 @@ func (scanPlanBinaryFloat4ToFloat32) Scan(src []byte, dst any) error {
return nil
}

type scanPlanBinaryFloat4ToFloat64 struct{}

func (scanPlanBinaryFloat4ToFloat64) Scan(src []byte, dst any) error {
if src == nil {
return fmt.Errorf("cannot scan NULL into %T", dst)
}

if len(src) != 4 {
return fmt.Errorf("invalid length for float4: %v", len(src))
}

n := int32(binary.BigEndian.Uint32(src))
f := (dst).(*float64)
*f = float64(math.Float32frombits(uint32(n)))

return nil
}

type scanPlanBinaryFloat4ToFloat64Scanner struct{}

func (scanPlanBinaryFloat4ToFloat64Scanner) Scan(src []byte, dst any) error {
Expand Down

0 comments on commit f8fe85a

Please sign in to comment.