Skip to content

Commit 8bd0781

Browse files
sakthivelmaniigcf-owl-bot[bot]alkatrivedisurbhigarg92
authored
perf: Skip gRPC trailers for StreamingRead & ExecuteStreamingSql (#2313)
* perf: Skip gRPC trailers for StreamingRead & ExecuteStreamingSql * πŸ¦‰ Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add tests * fix lint issue --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: alkatrivedi <58396306+alkatrivedi@users.noreply.github.com> Co-authored-by: surbhigarg92 <surbhigarg.92@gmail.com>
1 parent 61c571c commit 8bd0781

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

β€Žsrc/partial-result-stream.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ export class PartialResultStream extends Transform implements ResultEvents {
245245
res = this._addChunk(chunk);
246246
}
247247

248+
if (chunk.last) {
249+
this.emit('end');
250+
return;
251+
}
252+
248253
if (res) {
249254
next();
250255
} else {

β€Žtest/spanner.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {TEST_INSTANCE_NAME} from './mockserver/mockinstanceadmin';
4040
import * as mockDatabaseAdmin from './mockserver/mockdatabaseadmin';
4141
import * as sinon from 'sinon';
4242
import {google} from '../protos/protos';
43-
import {ExecuteSqlRequest, RunResponse} from '../src/transaction';
43+
import {ExecuteSqlRequest, ReadRequest, RunResponse} from '../src/transaction';
4444
import {Row} from '../src/partial-result-stream';
4545
import {GetDatabaseOperationsOptions} from '../src/instance';
4646
import {
@@ -1390,6 +1390,89 @@ describe('Spanner with mock server', () => {
13901390
}
13911391
});
13921392

1393+
it('should return the results correctly when last field is present in PartialResultSet for query', async () => {
1394+
// Setup a query result with more than maxQueued (10) PartialResultSets.
1395+
// None of the PartialResultSets include a resume token.
1396+
const sql = 'SELECT C1 FROM TestTable';
1397+
const fields = [
1398+
protobuf.StructType.Field.create({
1399+
name: 'C1',
1400+
type: protobuf.Type.create({code: protobuf.TypeCode.STRING}),
1401+
}),
1402+
];
1403+
const metadata = new protobuf.ResultSetMetadata({
1404+
rowType: new protobuf.StructType({
1405+
fields,
1406+
}),
1407+
});
1408+
const results: PartialResultSet[] = [];
1409+
for (let i = 0; i < 2; i++) {
1410+
results.push(
1411+
PartialResultSet.create({
1412+
metadata,
1413+
values: [{stringValue: `V${i}`}],
1414+
last: i === 1,
1415+
}),
1416+
);
1417+
}
1418+
spannerMock.putStatementResult(
1419+
sql,
1420+
mock.StatementResult.resultSet(results),
1421+
);
1422+
1423+
const database = newTestDatabase();
1424+
const [rows] = await database.run(sql);
1425+
assert.equal(rows.length, 2);
1426+
await database.close();
1427+
});
1428+
1429+
it('should return the results correctly when last field is present in PartialResultSet for read', async () => {
1430+
// Setup a query result with more than maxQueued (10) PartialResultSets.
1431+
// None of the PartialResultSets include a resume token.
1432+
const fields = [
1433+
protobuf.StructType.Field.create({
1434+
name: 'C1',
1435+
type: protobuf.Type.create({code: protobuf.TypeCode.STRING}),
1436+
}),
1437+
];
1438+
const metadata = new protobuf.ResultSetMetadata({
1439+
rowType: new protobuf.StructType({
1440+
fields,
1441+
}),
1442+
});
1443+
const results: PartialResultSet[] = [];
1444+
for (let i = 0; i < 2; i++) {
1445+
results.push(
1446+
PartialResultSet.create({
1447+
metadata,
1448+
values: [{stringValue: `V${i}`}],
1449+
last: i === 0,
1450+
}),
1451+
);
1452+
}
1453+
const request = {
1454+
table: 'TestTable',
1455+
keySet: {
1456+
keys: [],
1457+
all: true,
1458+
ranges: [],
1459+
},
1460+
};
1461+
spannerMock.putReadRequestResult(
1462+
request,
1463+
mock.ReadRequestResult.resultSet(results),
1464+
);
1465+
1466+
const database = newTestDatabase();
1467+
const table = database.table('TestTable');
1468+
const query = {
1469+
columns: ['C1'],
1470+
};
1471+
const [rows] = await table.read(query);
1472+
assert.equal(rows.length, 1);
1473+
await database.close();
1474+
});
1475+
13931476
it('should handle missing parameters in query', async () => {
13941477
const sql =
13951478
'SELECT * FROM tableId WHERE namedParameter = @namedParameter';

0 commit comments

Comments
 (0)