Skip to content

feat: integrate jobs.query and stateless query for faster queries #1337

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

Merged
merged 13 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: support stateless queries
  • Loading branch information
alvarowolfx committed Feb 28, 2024
commit 0717724a81fc89c58fd273e1160d19b5d28f4ad0
21 changes: 10 additions & 11 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,7 @@ export class BigQuery extends Service {
callback?: JobCallback
): void | Promise<JobResponse> {
const options = typeof opts === 'object' ? opts : {query: opts};
this.trace('[createQueryJob]', options, callback);
if ((!options || !options.query) && !options.pageToken) {
throw new Error('A SQL query string is required.');
}
Expand Down Expand Up @@ -2143,7 +2144,7 @@ export class BigQuery extends Service {
return;
}

let nextQuery = extend({job}, options);
options = extend({job}, queryOpts, options);
if (res && res.jobComplete) {
let rows: any = [];
if (res.schema && res.rows) {
Expand All @@ -2152,20 +2153,16 @@ export class BigQuery extends Service {
parseJSON: options.parseJSON,
});
}
options.cachedRows = rows;
this.trace('[runJobsQuery] job complete');
if (res.pageToken) {
this.trace('[runJobsQuery] has more pages');
nextQuery = extend({job}, options, {
pageToken: res.pageToken,
cachedRows: rows,
});
job!.getQueryResults(nextQuery, callback as QueryRowsCallback);
return;
options.pageToken = res.pageToken;
} else {
this.trace('[runJobsQuery] no more pages');
(callback as SimpleQueryRowsCallback)(err, rows, res);
return;
}
job!.getQueryResults(options, callback as QueryRowsCallback);
return;
}
this.trace('[runJobsQuery] job not complete');
job!.getQueryResults(options, callback as QueryRowsCallback);
Expand All @@ -2176,8 +2173,8 @@ export class BigQuery extends Service {
* Check if the given Query can run using the `jobs.query` endpoint.
* Returns a bigquery.IQueryRequest that can be used to call `jobs.query`.
* Return undefined if is not possible to convert to a bigquery.IQueryRequest.
*
* @param query string | Query
*
* @param query string | Query
* @param options QueryOptions
* @returns bigquery.IQueryRequest | undefined
*/
Expand Down Expand Up @@ -2245,6 +2242,8 @@ export class BigQuery extends Service {
job = this.job(jobRef.jobId!, {
location: jobRef.location,
});
} else {
job = this.job(res.queryId!); // stateless query
}
callback!(null, job, res);
}
Expand Down
11 changes: 7 additions & 4 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,13 @@ class Job extends Operation {
typeof qs.timeoutMs === 'number' ? qs.timeoutMs : false;

if (options.cachedRows) {
const nextQuery = Object.assign({}, options, {
pageToken: options.pageToken,
});
delete nextQuery.cachedRows;
let nextQuery: QueryResultsOptions | null = null;
if (options.pageToken) {
nextQuery = Object.assign({}, options, {
pageToken: options.pageToken,
});
delete nextQuery.cachedRows;
}
callback!(null, options.cachedRows, nextQuery);
return;
}
Expand Down
16 changes: 16 additions & 0 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,10 @@ describe('BigQuery', () => {
callback(error, null, FAKE_RESPONSE);
};

bq.buildQueryRequest_ = (query: {}, options: {}) => {
return undefined;
};

bq.query(QUERY_STRING, (err: Error, rows: {}, resp: {}) => {
assert.strictEqual(err, error);
assert.strictEqual(rows, null);
Expand Down Expand Up @@ -2850,6 +2854,10 @@ describe('BigQuery', () => {
callback(null, fakeJob, FAKE_RESPONSE);
};

bq.buildQueryRequest_ = (query: {}, options: {}) => {
return undefined;
};

bq.query(QUERY_STRING, (err: Error, rows: {}, resp: {}) => {
assert.ifError(err);
assert.strictEqual(rows, FAKE_ROWS);
Expand Down Expand Up @@ -2901,6 +2909,10 @@ describe('BigQuery', () => {
callback(null, fakeJob, FAKE_RESPONSE);
};

bq.buildQueryRequest_ = (query: {}, opts: {}) => {
return undefined;
};

bq.query(QUERY_STRING, assert.ifError);
});

Expand All @@ -2918,6 +2930,10 @@ describe('BigQuery', () => {
callback(null, fakeJob, FAKE_RESPONSE);
};

bq.buildQueryRequest_ = (query: {}, opts: {}) => {
return undefined;
};

bq.query(QUERY_STRING, fakeOptions, assert.ifError);
});
});
Expand Down