Skip to content

Commit 1f43e2e

Browse files
committed
Fix text format array decoding with a string of "NULL"
It was incorrectly being treated as NULL instead of 'NULL'. fixes #1494
1 parent b707fae commit 1f43e2e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pgtype/array_codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (c *ArrayCodec) decodeText(m *Map, arrayOID uint32, src []byte, array Array
303303
for i, s := range uta.Elements {
304304
elem := array.ScanIndex(i)
305305
var elemSrc []byte
306-
if s != "NULL" {
306+
if s != "NULL" || uta.Quoted[i] {
307307
elemSrc = []byte(s)
308308
}
309309

pgtype/array_codec_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,27 @@ func TestArrayCodecEncodeMultipleDimensionsRagged(t *testing.T) {
318318
defer rows.Close()
319319
})
320320
}
321+
322+
// https://github.com/jackc/pgx/issues/1494
323+
func TestArrayCodecDecodeTextArrayWithTextOfNULL(t *testing.T) {
324+
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
325+
{
326+
var actual []string
327+
err := conn.QueryRow(ctx, `select '{"foo", "NULL", " NULL "}'::text[]`).Scan(&actual)
328+
require.NoError(t, err)
329+
require.Equal(t, []string{"foo", "NULL", " NULL "}, actual)
330+
}
331+
332+
{
333+
var actual []pgtype.Text
334+
err := conn.QueryRow(ctx, `select '{"foo", "NULL", NULL, " NULL "}'::text[]`).Scan(&actual)
335+
require.NoError(t, err)
336+
require.Equal(t, []pgtype.Text{
337+
{String: "foo", Valid: true},
338+
{String: "NULL", Valid: true},
339+
{},
340+
{String: " NULL ", Valid: true},
341+
}, actual)
342+
}
343+
})
344+
}

0 commit comments

Comments
 (0)