Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Databoostenabled for Query and Read partitions #1784

Merged
merged 21 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion samples/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ async function createAndExecuteQueryPartitions(
const database = instance.database(databaseId);
const [transaction] = await database.createBatchTransaction();

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

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

Expand Down
2 changes: 2 additions & 0 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface ExecuteSqlRequest extends Statement, RequestOptions {
seqno?: number;
queryOptions?: IQueryOptions;
requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
dataBoostEnabled?: boolean | null;
}

export interface KeyRange {
Expand All @@ -104,6 +105,7 @@ export interface ReadRequest extends RequestOptions {
resumeToken?: Uint8Array | null;
partitionToken?: Uint8Array | null;
requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
dataBoostEnabled?: boolean | null;
}

export interface BatchUpdateError extends grpc.ServiceError {
Expand Down
88 changes: 88 additions & 0 deletions system-test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8358,6 +8358,94 @@ describe('Spanner', () => {
deadlineErrorInsteadOfAbort(done, PG_DATABASE, postgreSqlTable);
});
});

describe('batch transactions', () => {
before(done => {
if (!IS_EMULATOR_ENABLED) {
DATABASE.runTransaction((err, transaction) => {
assert.ifError(err);

transaction!.runUpdate(
{
sql:
'INSERT INTO ' +
TABLE_NAME +
' (Key, StringValue) VALUES(@key, @str)',
params: {
key: 'k998',
str: 'abc',
},
},
err => {
assert.ifError(err);
transaction!.commit(done);
}
);
});
} else {
done();
}
});

it('should create and execute a query partition', function (done) {
if (IS_EMULATOR_ENABLED) {
this.skip();
}
const selectQuery = {
sql: 'SELECT * FROM TxnTable where Key = "k998"',
};

let row_count = 0;
DATABASE.createBatchTransaction((err, transaction) => {
assert.ifError(err);
transaction!.createQueryPartitions(selectQuery, (err, partitions) => {
assert.ifError(err);
assert.deepStrictEqual(partitions.length, 1);
partitions.forEach(partition => {
transaction!.execute(partition, (err, results) => {
assert.ifError(err);
row_count += results.map(row => row.toJSON()).length;
assert.deepStrictEqual(row_count, 1);
transaction!.close();
done();
});
});
});
});
});

it('should create and execute a read partition', function (done) {
if (IS_EMULATOR_ENABLED) {
this.skip();
}
const key = 'k998';
const QUERY = {
table: googleSqlTable.name,
// Set databoostenabled to true for enabling serveless analytics.
dataBoostEnabled: false,
keys: [key],
columns: ['Key'],
};

let read_row_count = 0;
DATABASE.createBatchTransaction((err, transaction) => {
assert.ifError(err);
transaction!.createReadPartitions(QUERY, (err, partitions) => {
assert.ifError(err);
assert.deepStrictEqual(partitions.length, 1);
partitions.forEach(partition => {
transaction!.execute(partition, (err, results) => {
assert.ifError(err);
read_row_count += results.map(row => row.toJSON()).length;
assert.deepStrictEqual(read_row_count, 1);
transaction!.close();
done();
});
});
});
});
});
});
});
});

Expand Down
28 changes: 28 additions & 0 deletions test/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ describe('BatchTransaction', () => {
gaxOptions: GAX_OPTS,
params: {},
types: {},
dataBoostEnabled: true,
};

it('should make the correct request', () => {
const fakeParams = {
params: {a: 'b'},
paramTypes: {a: 'string'},
dataBoostEnabled: true,
};

const expectedQuery = Object.assign({sql: QUERY.sql}, fakeParams);
Expand Down Expand Up @@ -304,13 +306,15 @@ describe('BatchTransaction', () => {
keys: ['a', 'b'],
ranges: [{}, {}],
gaxOptions: GAX_OPTS,
dataBoostEnabled: true,
};

it('should make the correct request', () => {
const fakeKeySet = {};
const expectedQuery = {
table: QUERY.table,
keySet: fakeKeySet,
dataBoostEnabled: true,
};

const stub = sandbox.stub(batchTransaction, 'createPartitions_');
Expand Down Expand Up @@ -354,6 +358,30 @@ describe('BatchTransaction', () => {
const query = stub.lastCall.args[0];
assert.strictEqual(query, partition);
});

it('should make read requests for read partitions with data boost enabled', () => {
const partition = {table: 'abc', dataBoostEnabled: true};
const stub = sandbox.stub(batchTransaction, 'read');

batchTransaction.execute(partition, assert.ifError);

const [table, options] = stub.lastCall.args;
assert.strictEqual(table, partition.table);
assert.strictEqual(options, partition);
});

it('should make query requests for non-read partitions with data boost enabled', () => {
const partition = {
sql: 'SELECT * FROM Singers',
dataBoostEnabled: true,
};
const stub = sandbox.stub(batchTransaction, 'run');

batchTransaction.execute(partition, assert.ifError);

const query = stub.lastCall.args[0];
assert.strictEqual(query, partition);
});
});

describe('executeStream', () => {
Expand Down