jeroenrinzema/psql-wire

Numeric types are not supported properly

Closed this issue · 2 comments

If a column is defined with type oid.T_numeric (1700) the result is not properly converted:

  • SELECT 0.99::numeric' is returned as 99e-2

To add the proper support the type need to be registered manually:

connInfo.RegisterDataType(pgtype.DataType{
			Value: &shopspring.Numeric{},
			Name:  "numeric",
			OID:   pgtype.NumericOID,
		})

Working Example

package main

import (
	"context"
	"log"

	"github.com/jackc/pgtype"
	shopspring "github.com/jackc/pgtype/ext/shopspring-numeric"
	wire "github.com/jeroenrinzema/psql-wire"
	"github.com/lib/pq/oid"
)

func main() {
	log.Println("PostgreSQL server is up and running at [127.0.0.1:5432]")
	wire.ListenAndServe("127.0.0.1:5432", handle)
	select {}
}

var table = wire.Columns{
	{
		Table:  0,
		Name:   "name",
		Oid:    oid.T_text,
		Width:  256,
		Format: wire.TextFormat,
	},
	{
		Table:  0,
		Name:   "member",
		Oid:    oid.T_bool,
		Width:  1,
		Format: wire.TextFormat,
	},
	{
		Table:  0,
		Name:   "age",
		Oid:    oid.T_int4,
		Width:  1,
		Format: wire.TextFormat,
	},
	{
		Table:  0,
		Name:   "amount",
		Oid:    oid.T_numeric,
		Width:  1,
		Format: wire.TextFormat,
	},
}

func handle(ctx context.Context, query string, writer wire.DataWriter) error {
	log.Println("incoming SQL query:", query)
	connInfo := wire.TypeInfo(ctx)
	if connInfo != nil {
		connInfo.RegisterDataType(pgtype.DataType{
			Value: &shopspring.Numeric{},
			Name:  "numeric",
			OID:   pgtype.NumericOID,
		})
	}

	writer.Define(table)
	writer.Row([]any{"John", true, 29, 0.99})
	writer.Row([]any{"Marry", false, 21, 0.99})
	return writer.Complete("OK")
}

Reference: https://github.com/jackc/pgx/wiki/Numeric-and-decimal-support

Thanks for the detailed report! I will look into a way how we could include support for numeric types.

@sandromello I have just opened a PR to include the ability to extend the underlying connection types. Please take a look and let me know if this resolves your issue.