From 24b7bfea2d1522b84605e637830d8479c4d3e098 Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Thu, 7 Sep 2023 12:03:54 -0400 Subject: [PATCH 1/2] Add failing test for result rows with the same column names --- .../test/integration/gh-issues/3062-tests.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/pg/test/integration/gh-issues/3062-tests.js diff --git a/packages/pg/test/integration/gh-issues/3062-tests.js b/packages/pg/test/integration/gh-issues/3062-tests.js new file mode 100644 index 000000000..7666751ed --- /dev/null +++ b/packages/pg/test/integration/gh-issues/3062-tests.js @@ -0,0 +1,21 @@ +'use strict' +const helper = require('../test-helper') +var assert = require('assert') +const suite = new helper.Suite() + +// https://github.com/brianc/node-postgres/issues/3062 +suite.testAsync('result fields with the same name should pick the last value', async () => { + const client = new helper.pg.Client() + await client.connect() + + const { rows: [shouldBeNullRow] } = await client.query('SELECT NULL AS test, 10 AS test, NULL AS test') + assert.equal(shouldBeNullRow.test, null) + + const { rows: [shouldBeTwelveRow] } = await client.query('SELECT NULL AS test, 10 AS test, 12 AS test') + assert.equal(shouldBeTwelveRow.test, 12) + + const { rows: [shouldBeAbcRow] } = await client.query(`SELECT NULL AS test, 10 AS test, 12 AS test, 'ABC' AS test`) + assert.equal(shouldBeAbcRow.test, 'ABC') + + await client.end() +}) From a9396d21f8623ea0f3a85286ec15f0043baab69c Mon Sep 17 00:00:00 2001 From: Sehrope Sarkuni Date: Thu, 7 Sep 2023 12:04:57 -0400 Subject: [PATCH 2/2] Fix handling of duplicate column names in results to ensure last value is populated Fixes handling of result rows that have the same column name duplicated in the results to ensure that the last value is the one returned to the user. This was the old behavior but unintentionally broken when the pre-built object optimization was added. --- packages/pg/lib/result.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/pg/lib/result.js b/packages/pg/lib/result.js index 187c0d016..f9053c7a9 100644 --- a/packages/pg/lib/result.js +++ b/packages/pg/lib/result.js @@ -67,6 +67,8 @@ class Result { var field = this.fields[i].name if (rawValue !== null) { row[field] = this._parsers[i](rawValue) + } else { + row[field] = null } } return row