Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit 91f5afd

Browse files
authored
fix: createQueryPartition with query params (#2244)
Create Query Partition with query params throw error, as params are encoded twice , once while creating partition and once while executing ``` const query = { sql: 'SELECT * FROM Singers where SingerId > @id', params: { id: 1200, }, }; const [partitions] = await transaction.createQueryPartitions(query); partitions.forEach(partition => { transaction.execute(partition).then(results => { const rows = results[0].map(row => row.toJSON()); row_count += rows.length; }); }); ```
1 parent 9673e26 commit 91f5afd

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/transaction.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ export interface Statement {
127127
sql: string;
128128
params?: {[param: string]: Value};
129129
types?: Type | {[param: string]: Value};
130+
// This property is used internally as a mapping for types. Do not set it manually
131+
paramTypes?: {[k: string]: google.spanner.v1.Type} | null;
130132
}
131133

132134
export interface ExecuteSqlRequest extends Statement, RequestOptions {
@@ -1500,10 +1502,11 @@ export class Snapshot extends EventEmitter {
15001502
static encodeParams(request: ExecuteSqlRequest) {
15011503
const typeMap = request.types || {};
15021504

1503-
const params: p.IStruct = {};
1504-
const paramTypes: {[field: string]: spannerClient.spanner.v1.Type} = {};
1505+
const params: p.IStruct = {fields: request.params?.fields || {}};
1506+
const paramTypes: {[field: string]: spannerClient.spanner.v1.Type} =
1507+
request.paramTypes || {};
15051508

1506-
if (request.params) {
1509+
if (request.params && !request.params.fields) {
15071510
const fields = {};
15081511

15091512
Object.keys(request.params).forEach(param => {

system-test/spanner.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9420,7 +9420,10 @@ describe('Spanner', () => {
94209420
this.skip();
94219421
}
94229422
const selectQuery = {
9423-
sql: 'SELECT * FROM TxnTable where Key = "k998"',
9423+
sql: 'SELECT * FROM TxnTable where Key = @id',
9424+
params: {
9425+
id: 'k998',
9426+
},
94249427
};
94259428

94269429
let row_count = 0;

0 commit comments

Comments
 (0)