diff --git a/grafast/dataplan-pg/src/executor.ts b/grafast/dataplan-pg/src/executor.ts index 36c9868d91..dec51168f2 100644 --- a/grafast/dataplan-pg/src/executor.ts +++ b/grafast/dataplan-pg/src/executor.ts @@ -490,7 +490,8 @@ ${duration} const batchSize = batch.length; for (let batchIndex = 0; batchIndex < batchSize; batchIndex++) { const { queryValues, resultIndex } = batch[batchIndex]; - const identifiersJSON = JSON.stringify(queryValues); // PERF: Canonical? Manual for perf? + const queryValuesObj = { ...queryValues }; + const identifiersJSON = JSON.stringify(queryValuesObj); // PERF: Canonical? Manual for perf? const existingResult = scopedCache.get(identifiersJSON); if (existingResult) { if (debugVerbose.enabled) { diff --git a/grafast/dataplan-pg/src/steps/pgSelect.ts b/grafast/dataplan-pg/src/steps/pgSelect.ts index 80aa52e071..5b208dbcd1 100644 --- a/grafast/dataplan-pg/src/steps/pgSelect.ts +++ b/grafast/dataplan-pg/src/steps/pgSelect.ts @@ -3155,7 +3155,7 @@ function buildTheQuery< const extraSelects: SQL[] = []; const identifierIndexOffset = - extraSelects.push(sql`${identifiersAlias}.idx`) - 1; + extraSelects.push(sql`(${identifiersAlias}.ord - 1)`) - 1; // PERF: try and re-use existing trueOrderBySQL selection? const rowNumberIndexOffset = forceOrder || limit != null || offset != null @@ -3233,19 +3233,13 @@ function buildTheQuery< */ const text = `\ select ${wrapperAliasText}.* -from (select ids.ordinality - 1 as idx${ - queryValues.length > 0 - ? `, ${queryValues - .map(({ codec }, idx) => { - return `(ids.value->>${idx})::${ - sql.compile(codec.sqlType).text - } as "id${idx}"`; - }) - .join(", ")}` - : "" - } from json_array_elements($${ - rawSqlValues.length + 1 - }::json) with ordinality as ids) as ${identifiersAliasText}, +from rows from (json_to_recordset($${rawSqlValues.length + 1}::json) as (${queryValues + .map(({ codec }, idx) => `"${idx}" ${sql.compile(codec.sqlType).text}`) + .join(", ")})) +with ordinality as ${identifiersAliasText} (${[ + ...queryValues.map((_, idx) => `"id${idx}"`), + "ord", + ].join(", ")}), ${lateralText};`; return { text, rawSqlValues, identifierIndex }; diff --git a/postgraphile/postgraphile/__tests__/queries/v4/issue2210.sql b/postgraphile/postgraphile/__tests__/queries/v4/issue2210.sql index b43c361e5d..2a39e979c3 100644 --- a/postgraphile/postgraphile/__tests__/queries/v4/issue2210.sql +++ b/postgraphile/postgraphile/__tests__/queries/v4/issue2210.sql @@ -8,12 +8,13 @@ from "issue_2210"."some_messages"($1::"uuid") as __some_messages__ limit 51; select __test_user_result__.* -from (select ids.ordinality - 1 as idx, (ids.value->>0)::"uuid" as "id0" from json_array_elements($1::json) with ordinality as ids) as __test_user_identifiers__, +from rows from (json_to_recordset($1::json) as ("0" "uuid")) +with ordinality as __test_user_identifiers__ ("id0", ord), lateral ( select __test_user__."id" as "0", __test_user__."name" as "1", - __test_user_identifiers__.idx as "2" + (__test_user_identifiers__.ord - 1) as "2" from "issue_2210"."test_user" as __test_user__ where ( __test_user__."id" = __test_user_identifiers__."id0"