Skip to content

Commit 66ff70c

Browse files
authored
feat: Databoostenabled for Query and Read partitions (#1784)
1 parent fab9701 commit 66ff70c

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

samples/batch.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ async function createAndExecuteQueryPartitions(
4040
const database = instance.database(databaseId);
4141
const [transaction] = await database.createBatchTransaction();
4242

43-
const query = 'SELECT * FROM Singers';
43+
const query = {
44+
sql: 'SELECT * FROM Singers',
45+
// DataBoost option is an optional parameter which can also be used for partition read
46+
// and query to execute the request via spanner independent compute resources.
47+
dataBoostEnabled: false,
48+
};
4449

50+
// A Partition object is serializable and can be used from a different process.
4551
const [partitions] = await transaction.createQueryPartitions(query);
4652
console.log(`Successfully created ${partitions.length} query partitions.`);
4753

src/transaction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface ExecuteSqlRequest extends Statement, RequestOptions {
8484
seqno?: number;
8585
queryOptions?: IQueryOptions;
8686
requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
87+
dataBoostEnabled?: boolean | null;
8788
}
8889

8990
export interface KeyRange {
@@ -104,6 +105,7 @@ export interface ReadRequest extends RequestOptions {
104105
resumeToken?: Uint8Array | null;
105106
partitionToken?: Uint8Array | null;
106107
requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
108+
dataBoostEnabled?: boolean | null;
107109
}
108110

109111
export interface BatchUpdateError extends grpc.ServiceError {

system-test/spanner.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8358,6 +8358,94 @@ describe('Spanner', () => {
83588358
deadlineErrorInsteadOfAbort(done, PG_DATABASE, postgreSqlTable);
83598359
});
83608360
});
8361+
8362+
describe('batch transactions', () => {
8363+
before(done => {
8364+
if (!IS_EMULATOR_ENABLED) {
8365+
DATABASE.runTransaction((err, transaction) => {
8366+
assert.ifError(err);
8367+
8368+
transaction!.runUpdate(
8369+
{
8370+
sql:
8371+
'INSERT INTO ' +
8372+
TABLE_NAME +
8373+
' (Key, StringValue) VALUES(@key, @str)',
8374+
params: {
8375+
key: 'k998',
8376+
str: 'abc',
8377+
},
8378+
},
8379+
err => {
8380+
assert.ifError(err);
8381+
transaction!.commit(done);
8382+
}
8383+
);
8384+
});
8385+
} else {
8386+
done();
8387+
}
8388+
});
8389+
8390+
it('should create and execute a query partition', function (done) {
8391+
if (IS_EMULATOR_ENABLED) {
8392+
this.skip();
8393+
}
8394+
const selectQuery = {
8395+
sql: 'SELECT * FROM TxnTable where Key = "k998"',
8396+
};
8397+
8398+
let row_count = 0;
8399+
DATABASE.createBatchTransaction((err, transaction) => {
8400+
assert.ifError(err);
8401+
transaction!.createQueryPartitions(selectQuery, (err, partitions) => {
8402+
assert.ifError(err);
8403+
assert.deepStrictEqual(partitions.length, 1);
8404+
partitions.forEach(partition => {
8405+
transaction!.execute(partition, (err, results) => {
8406+
assert.ifError(err);
8407+
row_count += results.map(row => row.toJSON()).length;
8408+
assert.deepStrictEqual(row_count, 1);
8409+
transaction!.close();
8410+
done();
8411+
});
8412+
});
8413+
});
8414+
});
8415+
});
8416+
8417+
it('should create and execute a read partition', function (done) {
8418+
if (IS_EMULATOR_ENABLED) {
8419+
this.skip();
8420+
}
8421+
const key = 'k998';
8422+
const QUERY = {
8423+
table: googleSqlTable.name,
8424+
// Set databoostenabled to true for enabling serveless analytics.
8425+
dataBoostEnabled: false,
8426+
keys: [key],
8427+
columns: ['Key'],
8428+
};
8429+
8430+
let read_row_count = 0;
8431+
DATABASE.createBatchTransaction((err, transaction) => {
8432+
assert.ifError(err);
8433+
transaction!.createReadPartitions(QUERY, (err, partitions) => {
8434+
assert.ifError(err);
8435+
assert.deepStrictEqual(partitions.length, 1);
8436+
partitions.forEach(partition => {
8437+
transaction!.execute(partition, (err, results) => {
8438+
assert.ifError(err);
8439+
read_row_count += results.map(row => row.toJSON()).length;
8440+
assert.deepStrictEqual(read_row_count, 1);
8441+
transaction!.close();
8442+
done();
8443+
});
8444+
});
8445+
});
8446+
});
8447+
});
8448+
});
83618449
});
83628450
});
83638451

test/batch-transaction.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,14 @@ describe('BatchTransaction', () => {
149149
gaxOptions: GAX_OPTS,
150150
params: {},
151151
types: {},
152+
dataBoostEnabled: true,
152153
};
153154

154155
it('should make the correct request', () => {
155156
const fakeParams = {
156157
params: {a: 'b'},
157158
paramTypes: {a: 'string'},
159+
dataBoostEnabled: true,
158160
};
159161

160162
const expectedQuery = Object.assign({sql: QUERY.sql}, fakeParams);
@@ -304,13 +306,15 @@ describe('BatchTransaction', () => {
304306
keys: ['a', 'b'],
305307
ranges: [{}, {}],
306308
gaxOptions: GAX_OPTS,
309+
dataBoostEnabled: true,
307310
};
308311

309312
it('should make the correct request', () => {
310313
const fakeKeySet = {};
311314
const expectedQuery = {
312315
table: QUERY.table,
313316
keySet: fakeKeySet,
317+
dataBoostEnabled: true,
314318
};
315319

316320
const stub = sandbox.stub(batchTransaction, 'createPartitions_');
@@ -354,6 +358,30 @@ describe('BatchTransaction', () => {
354358
const query = stub.lastCall.args[0];
355359
assert.strictEqual(query, partition);
356360
});
361+
362+
it('should make read requests for read partitions with data boost enabled', () => {
363+
const partition = {table: 'abc', dataBoostEnabled: true};
364+
const stub = sandbox.stub(batchTransaction, 'read');
365+
366+
batchTransaction.execute(partition, assert.ifError);
367+
368+
const [table, options] = stub.lastCall.args;
369+
assert.strictEqual(table, partition.table);
370+
assert.strictEqual(options, partition);
371+
});
372+
373+
it('should make query requests for non-read partitions with data boost enabled', () => {
374+
const partition = {
375+
sql: 'SELECT * FROM Singers',
376+
dataBoostEnabled: true,
377+
};
378+
const stub = sandbox.stub(batchTransaction, 'run');
379+
380+
batchTransaction.execute(partition, assert.ifError);
381+
382+
const query = stub.lastCall.args[0];
383+
assert.strictEqual(query, partition);
384+
});
357385
});
358386

359387
describe('executeStream', () => {

0 commit comments

Comments
 (0)