Skip to content

Commit

Permalink
Fix text format array decoding with a string of "NULL"
Browse files Browse the repository at this point in the history
It was incorrectly being treated as NULL instead of 'NULL'.

fixes #1494
  • Loading branch information
jackc committed Feb 11, 2023
1 parent b707fae commit 1f43e2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgtype/array_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (c *ArrayCodec) decodeText(m *Map, arrayOID uint32, src []byte, array Array
for i, s := range uta.Elements {
elem := array.ScanIndex(i)
var elemSrc []byte
if s != "NULL" {
if s != "NULL" || uta.Quoted[i] {
elemSrc = []byte(s)
}

Expand Down
24 changes: 24 additions & 0 deletions pgtype/array_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,27 @@ func TestArrayCodecEncodeMultipleDimensionsRagged(t *testing.T) {
defer rows.Close()
})
}

// https://github.com/jackc/pgx/issues/1494
func TestArrayCodecDecodeTextArrayWithTextOfNULL(t *testing.T) {
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
{
var actual []string
err := conn.QueryRow(ctx, `select '{"foo", "NULL", " NULL "}'::text[]`).Scan(&actual)
require.NoError(t, err)
require.Equal(t, []string{"foo", "NULL", " NULL "}, actual)
}

{
var actual []pgtype.Text
err := conn.QueryRow(ctx, `select '{"foo", "NULL", NULL, " NULL "}'::text[]`).Scan(&actual)
require.NoError(t, err)
require.Equal(t, []pgtype.Text{
{String: "foo", Valid: true},
{String: "NULL", Valid: true},
{},
{String: " NULL ", Valid: true},
}, actual)
}
})
}

0 comments on commit 1f43e2e

Please sign in to comment.