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: integrate jobs.query and stateless query for faster queries #1337

Merged
merged 13 commits into from
Mar 27, 2024
Merged
Prev Previous commit
Next Next commit
feat: only use jobCreationMode with preview feature enable
  • Loading branch information
alvarowolfx committed Mar 19, 2024
commit 9a2cce5299dce6b03ccd9bd3a2da92bd6fcfd33a
21 changes: 21 additions & 0 deletions src/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
* We will create a table with the correct schema, import the public CSV file
* into that table, and query it for data.
*
* This client supports enabling query-related preview features via environmental
* variables. By setting the environment variable QUERY_PREVIEW_ENABLED to the string
* "TRUE", the client will enable preview features, though behavior may still be
* controlled via the bigquery service as well. Currently, the feature(s) in scope
* include: stateless queries (query execution without corresponding job metadata).
*
* @class
*
* See {@link https://cloud.google.com/bigquery/what-is-bigquery| What is BigQuery?}
Expand Down Expand Up @@ -322,18 +328,19 @@
export class BigQuery extends Service {
location?: string;
private _universeDomain: string;
private _enableQueryPreview: boolean;

createQueryStream(options?: Query | string): ResourceStream<RowMetadata> {

Check warning on line 333 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<RowMetadata>({}, () => {});
}

getDatasetsStream(options?: GetDatasetsOptions): ResourceStream<Dataset> {

Check warning on line 338 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Dataset>({}, () => {});
}

getJobsStream(options?: GetJobsOptions): ResourceStream<Job> {

Check warning on line 343 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

'options' is defined but never used
// placeholder body, overwritten in constructor
return new ResourceStream<Job>({}, () => {});
}
Expand Down Expand Up @@ -379,6 +386,14 @@

super(config, options);

const QUERY_PREVIEW_ENABLED = process.env.QUERY_PREVIEW_ENABLED;
this._enableQueryPreview = false;
if (typeof QUERY_PREVIEW_ENABLED === 'string') {
if (QUERY_PREVIEW_ENABLED.toUpperCase() === 'TRUE') {
this._enableQueryPreview = true;
}
}

this._universeDomain = universeDomain;
this.location = options.location;
/**
Expand Down Expand Up @@ -1534,7 +1549,7 @@
const parameterMode = is.array(params) ? 'positional' : 'named';
const queryParameters: bigquery.IQueryParameter[] = [];
if (parameterMode === 'named') {
const namedParams = params as {[param: string]: any};

Check warning on line 1552 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
for (const namedParameter in namedParams) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to use for (const namedParameter of Object.getOwnPropertyNames(namedParams)) or check each namedParameter against namedParams.hasOwnProperty().

const value = namedParams[namedParameter];
let queryParameter;
Expand Down Expand Up @@ -2177,7 +2192,7 @@

options = extend({job}, queryOpts, options);
if (res && res.jobComplete) {
let rows: any = [];

Check warning on line 2195 in src/bigquery.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
if (res.schema && res.rows) {
rows = BigQuery.mergeSchemaWithRows_(res.schema, res.rows, {
wrapIntegers: options.wrapIntegers!, // TODO: fix default value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (re: not sure if this was moved from elsewhere or new code) but I wanted to ping in case the TODO isn't intentional.

Expand All @@ -2195,6 +2210,9 @@
job!.getQueryResults(options, callback as QueryRowsCallback);
return;
}
if (options.timeoutMs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just delete, no need for the if.

delete options.timeoutMs;
}
this.trace_('[runJobsQuery] job not complete');
job!.getQueryResults(options, callback as QueryRowsCallback);
});
Expand Down Expand Up @@ -2273,6 +2291,9 @@
if (req.maxResults) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary workaround?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gonna remove that

req.jobCreationMode = 'JOB_CREATION_REQUIRED';
}
if (!this._enableQueryPreview) {
delete req.jobCreationMode;
}
const {parameterMode, params} = this.buildQueryParams_(
queryObj.params,
queryObj.types
Expand Down
1 change: 1 addition & 0 deletions test/bigquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe('BigQuery', () => {
Object.assign(fakeUtil, originalFakeUtil);
BigQuery = Object.assign(BigQuery, BigQueryCached);
bq = new BigQuery({projectId: PROJECT_ID});
bq._enableQueryPreview = true;
});

after(() => {
Expand Down