@malloydata/db-bigquery 0.0.103-dev231115200636 → 0.0.103

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { RowMetadata } from '@google-cloud/bigquery';
2
2
  import bigquery from '@google-cloud/bigquery/build/src/types';
3
3
  import { ResourceStream } from '@google-cloud/paginator';
4
- import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection, QueryData, QueryDataRow, QueryRunStats, SQLBlock, StreamingConnection, StructDef } from '@malloydata/malloy';
4
+ import { Connection, MalloyQueryData, PersistSQLResults, PooledConnection, QueryData, QueryDataRow, QueryRunStats, RunSQLOptions, SQLBlock, StreamingConnection, StructDef } from '@malloydata/malloy';
5
5
  import { FetchSchemaOptions } from '@malloydata/malloy-interfaces';
6
6
  export interface BigQueryManagerOptions {
7
7
  credentials?: {
@@ -93,7 +93,7 @@ export declare class BigQueryConnection implements Connection, PersistSQLResults
93
93
  private createBigQueryJobAndGetResults;
94
94
  private createBigQueryJob;
95
95
  initiateJobAndGetLinkToConsole(sqlCommand: string, dryRun?: boolean): Promise<string>;
96
- runSQLStream(sqlCommand: string, options?: Partial<BigQueryQueryOptions>): AsyncIterableIterator<QueryDataRow>;
96
+ runSQLStream(sqlCommand: string, { rowLimit, abortSignal }?: RunSQLOptions): AsyncIterableIterator<QueryDataRow>;
97
97
  close(): Promise<void>;
98
98
  }
99
99
  export {};
@@ -137,21 +137,21 @@ class BigQueryConnection {
137
137
  get supportsNesting() {
138
138
  return true;
139
139
  }
140
- async _runSQL(sqlCommand, options = {}, rowIndex = 0) {
141
- var _a, _b, _c, _d;
140
+ async _runSQL(sqlCommand, { rowLimit, abortSignal } = {}, rowIndex = 0) {
141
+ var _a, _b, _c;
142
142
  const defaultOptions = this.readQueryOptions();
143
- const pageSize = (_a = options.rowLimit) !== null && _a !== void 0 ? _a : defaultOptions.rowLimit;
143
+ const pageSize = rowLimit !== null && rowLimit !== void 0 ? rowLimit : defaultOptions.rowLimit;
144
144
  try {
145
145
  const queryResultsOptions = {
146
146
  maxResults: pageSize,
147
147
  startIndex: rowIndex.toString(),
148
148
  };
149
- const jobResult = await this.createBigQueryJobAndGetResults(sqlCommand, undefined, queryResultsOptions);
150
- const totalRows = +(((_b = jobResult[2]) === null || _b === void 0 ? void 0 : _b.totalRows)
149
+ const jobResult = await this.createBigQueryJobAndGetResults(sqlCommand, undefined, queryResultsOptions, abortSignal);
150
+ const totalRows = +(((_a = jobResult[2]) === null || _a === void 0 ? void 0 : _a.totalRows)
151
151
  ? jobResult[2].totalRows
152
152
  : '0');
153
153
  // TODO even though we have 10 minute timeout limit, we still should confirm that resulting metadata has "jobComplete: true"
154
- const queryCostBytes = (_c = jobResult[2]) === null || _c === void 0 ? void 0 : _c.totalBytesProcessed;
154
+ const queryCostBytes = (_b = jobResult[2]) === null || _b === void 0 ? void 0 : _b.totalBytesProcessed;
155
155
  const data = {
156
156
  rows: jobResult[0],
157
157
  totalRows,
@@ -159,7 +159,7 @@ class BigQueryConnection {
159
159
  queryCostBytes: queryCostBytes ? +queryCostBytes : undefined,
160
160
  },
161
161
  };
162
- const schema = (_d = jobResult[2]) === null || _d === void 0 ? void 0 : _d.schema;
162
+ const schema = (_c = jobResult[2]) === null || _c === void 0 ? void 0 : _c.schema;
163
163
  return { data, schema };
164
164
  }
165
165
  catch (e) {
@@ -517,12 +517,16 @@ class BigQueryConnection {
517
517
  // TODO this needs to extend the wait for results using a timeout set by the user,
518
518
  // and probably needs to loop to check for results - BQ docs now say that after ~2min of waiting,
519
519
  // no matter what you set for timeoutMs, they will probably just return.
520
- async createBigQueryJobAndGetResults(sqlCommand, createQueryJobOptions, getQueryResultsOptions) {
520
+ async createBigQueryJobAndGetResults(sqlCommand, createQueryJobOptions, getQueryResultsOptions, abortSignal) {
521
521
  try {
522
522
  const job = await this.createBigQueryJob({
523
523
  query: sqlCommand,
524
524
  ...createQueryJobOptions,
525
525
  });
526
+ const cancel = () => {
527
+ job.cancel();
528
+ };
529
+ abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.addEventListener('abort', cancel);
526
530
  // TODO we should check if this is still required?
527
531
  // We do a simple retry-loop here, as a temporary fix for a transient
528
532
  // error in which sometimes requesting results from a job yields an
@@ -540,6 +544,9 @@ class BigQueryConnection {
540
544
  catch (fetchError) {
541
545
  lastFetchError = fetchError;
542
546
  }
547
+ finally {
548
+ abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.removeEventListener('abort', cancel);
549
+ }
543
550
  }
544
551
  throw lastFetchError;
545
552
  }
@@ -564,23 +571,23 @@ class BigQueryConnection {
564
571
  const url = `https://console.cloud.google.com/bigquery?project=${this.projectId}&j=bq:${this.location}:${job.id}&page=queryresults`;
565
572
  return url;
566
573
  }
567
- runSQLStream(sqlCommand, options = {}) {
568
- const bigQuery = this.bigQuery;
569
- function streamBigQuery(onError, onData, onEnd) {
574
+ runSQLStream(sqlCommand, { rowLimit, abortSignal } = {}) {
575
+ const streamBigQuery = (onError, onData, onEnd) => {
570
576
  let index = 0;
571
577
  function handleData(rowMetadata) {
572
578
  onData(rowMetadata);
573
579
  index += 1;
574
- if (options.rowLimit !== undefined && index >= options.rowLimit) {
580
+ if ((rowLimit !== undefined && index >= rowLimit) ||
581
+ (abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.aborted)) {
575
582
  this.end();
576
583
  }
577
584
  }
578
- bigQuery
585
+ this.bigQuery
579
586
  .createQueryStream(sqlCommand)
580
587
  .on('error', onError)
581
588
  .on('data', handleData)
582
589
  .on('end', onEnd);
583
- }
590
+ };
584
591
  return (0, malloy_1.toAsyncGenerator)(streamBigQuery);
585
592
  }
586
593
  async close() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-bigquery",
3
- "version": "0.0.103-dev231115200636",
3
+ "version": "0.0.103",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,8 +25,8 @@
25
25
  "@google-cloud/bigquery": "^7.3.0",
26
26
  "@google-cloud/common": "^5.0.1",
27
27
  "@google-cloud/paginator": "^5.0.0",
28
- "@malloydata/malloy": "^0.0.103-dev231115200636",
29
- "@malloydata/malloy-interfaces": "^0.0.103-dev231115200636",
28
+ "@malloydata/malloy": "^0.0.103",
29
+ "@malloydata/malloy-interfaces": "^0.0.103",
30
30
  "gaxios": "^4.2.0"
31
31
  }
32
32
  }