@malloydata/db-bigquery 0.0.22-dev230207182613 → 0.0.22-dev230207183024

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.
@@ -57,10 +57,10 @@ describe("db:BigQuery", () => {
57
57
  beforeAll(() => {
58
58
  bq = new bigquery_connection_1.BigQueryConnection("test");
59
59
  const files = {
60
- readURL: async (url) => {
60
+ "readURL": async (url) => {
61
61
  const filePath = (0, url_1.fileURLToPath)(url);
62
62
  return await util.promisify(fs.readFile)(filePath, "utf8");
63
- },
63
+ }
64
64
  };
65
65
  runtime = new malloy.Runtime(files, bq);
66
66
  });
@@ -75,11 +75,11 @@ describe("db:BigQuery", () => {
75
75
  it("gets table schema", async () => {
76
76
  const res = await bq.getTableFieldSchema(`malloy-data.faa.carriers`);
77
77
  expect(res.schema).toStrictEqual({
78
- fields: [
79
- { name: "code", type: "STRING" },
80
- { name: "name", type: "STRING" },
81
- { name: "nickname", type: "STRING" },
82
- ],
78
+ "fields": [
79
+ { "name": "code", "type": "STRING" },
80
+ { "name": "name", "type": "STRING" },
81
+ { "name": "nickname", "type": "STRING" }
82
+ ]
83
83
  });
84
84
  });
85
85
  it.todo("gets table structdefs");
@@ -124,7 +124,7 @@ describe("db:BigQuery", () => {
124
124
  const dataset = sdk.dataset(datasetName);
125
125
  if ((await dataset.exists())[0]) {
126
126
  await dataset.delete({
127
- force: true,
127
+ "force": true
128
128
  });
129
129
  }
130
130
  };
@@ -145,7 +145,7 @@ describe("db:BigQuery", () => {
145
145
  it("throws if table exist and overwriteExistingTable=false", async () => {
146
146
  const newDatasetResponse = await sdk.createDataset(datasetName);
147
147
  const dataset = newDatasetResponse[0];
148
- const tableMeta = { name: tableName };
148
+ const tableMeta = { "name": tableName };
149
149
  await dataset.createTable(tableName, tableMeta);
150
150
  await expect(async () => {
151
151
  await bq.manifestPermanentTable("SELECT 1 as t", datasetName, tableName, false, true);
@@ -163,7 +163,7 @@ describe("db:BigQuery", () => {
163
163
  // query the new table
164
164
  const [queryJob] = await sdk.createQueryJob(`SELECT * FROM ${datasetName}.${tableName}`);
165
165
  const [results] = await queryJob.getQueryResults();
166
- expect(results[0]).toStrictEqual({ t: 1 });
166
+ expect(results[0]).toStrictEqual({ "t": 1 });
167
167
  });
168
168
  });
169
169
  });
@@ -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 { PersistSQLResults, PooledConnection, QueryData, StructDef, StreamingConnection, MalloyQueryData, FieldTypeDef, SQLBlock, Connection, QueryDataRow } from "@malloydata/malloy";
4
+ import { Connection, FieldTypeDef, MalloyQueryData, PersistSQLResults, PooledConnection, QueryData, QueryDataRow, SQLBlock, StreamingConnection, StructDef } from "@malloydata/malloy";
5
5
  export interface BigQueryManagerOptions {
6
6
  credentials?: {
7
7
  clientId: string;
@@ -93,18 +93,18 @@ class BigQueryConnection {
93
93
  this.schemaCache = new Map();
94
94
  this.sqlSchemaCache = new Map();
95
95
  this.bqToMalloyTypes = {
96
- DATE: { type: "date" },
97
- STRING: { type: "string" },
98
- INTEGER: { type: "number", numberType: "integer" },
99
- INT64: { type: "number", numberType: "integer" },
100
- FLOAT: { type: "number", numberType: "float" },
101
- FLOAT64: { type: "number", numberType: "float" },
102
- NUMERIC: { type: "number", numberType: "float" },
103
- BIGNUMERIC: { type: "number", numberType: "float" },
104
- TIMESTAMP: { type: "timestamp" },
105
- BOOLEAN: { type: "boolean" },
106
- BOOL: { type: "boolean" },
107
- JSON: { type: "json" },
96
+ "DATE": { "type": "date" },
97
+ "STRING": { "type": "string" },
98
+ "INTEGER": { "type": "number", "numberType": "integer" },
99
+ "INT64": { "type": "number", "numberType": "integer" },
100
+ "FLOAT": { "type": "number", "numberType": "float" },
101
+ "FLOAT64": { "type": "number", "numberType": "float" },
102
+ "NUMERIC": { "type": "number", "numberType": "float" },
103
+ "BIGNUMERIC": { "type": "number", "numberType": "float" },
104
+ "TIMESTAMP": { "type": "timestamp" },
105
+ "BOOLEAN": { "type": "boolean" },
106
+ "BOOL": { "type": "boolean" },
107
+ "JSON": { "type": "json" }
108
108
  // TODO (https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#tablefieldschema):
109
109
  // BYTES
110
110
  // DATETIME
@@ -113,8 +113,8 @@ class BigQueryConnection {
113
113
  };
114
114
  this.name = name;
115
115
  this.bigQuery = new bigquery_1.BigQuery({
116
- userAgent: `Malloy/${malloy_1.Malloy.version}`,
117
- keyFilename: config.serviceAccountKeyPath,
116
+ "userAgent": `Malloy/${malloy_1.Malloy.version}`,
117
+ "keyFilename": config.serviceAccountKeyPath
118
118
  });
119
119
  // record project ID because for unclear reasons we have to modify the project ID on the SDK when
120
120
  // we want to use the tables API
@@ -156,8 +156,8 @@ class BigQueryConnection {
156
156
  const pageSize = (_a = options.rowLimit) !== null && _a !== void 0 ? _a : defaultOptions.rowLimit;
157
157
  try {
158
158
  const queryResultsOptions = {
159
- maxResults: pageSize,
160
- startIndex: rowIndex.toString(),
159
+ "maxResults": pageSize,
160
+ "startIndex": rowIndex.toString()
161
161
  };
162
162
  const jobResult = await this.createBigQueryJobAndGetResults(sqlCommand, undefined, queryResultsOptions);
163
163
  const totalRows = +(((_b = jobResult[2]) === null || _b === void 0 ? void 0 : _b.totalRows)
@@ -168,7 +168,7 @@ class BigQueryConnection {
168
168
  throw new Error("Schema not present");
169
169
  }
170
170
  // TODO even though we have 10 minute timeout limit, we still should confirm that resulting metadata has "jobComplete: true"
171
- const data = { rows: jobResult[0], totalRows };
171
+ const data = { "rows": jobResult[0], totalRows };
172
172
  const schema = (_d = jobResult[2]) === null || _d === void 0 ? void 0 : _d.schema;
173
173
  return { data, schema };
174
174
  }
@@ -181,22 +181,22 @@ class BigQueryConnection {
181
181
  return data;
182
182
  }
183
183
  async runSQLBlockAndFetchResultSchema(sqlBlock, options) {
184
- const { data, schema: schemaRaw } = await this._runSQL(sqlBlock.selectStr, options);
184
+ const { data, "schema": schemaRaw } = await this._runSQL(sqlBlock.selectStr, options);
185
185
  const schema = this.structDefFromSQLSchema(sqlBlock, schemaRaw);
186
186
  return { data, schema };
187
187
  }
188
188
  async downloadMalloyQuery(sqlCommand) {
189
189
  const job = await this.createBigQueryJob({
190
- query: sqlCommand,
190
+ "query": sqlCommand
191
191
  });
192
192
  return job.getQueryResultsStream();
193
193
  }
194
194
  async dryRunSQLQuery(sqlCommand) {
195
195
  try {
196
196
  const [result] = await this.bigQuery.createQueryJob({
197
- location: this.location,
198
- query: sqlCommand,
199
- dryRun: true,
197
+ "location": this.location,
198
+ "query": sqlCommand,
199
+ "dryRun": true
200
200
  });
201
201
  return result;
202
202
  }
@@ -215,7 +215,7 @@ class BigQueryConnection {
215
215
  }
216
216
  async getTableFieldSchema(tableURL) {
217
217
  var _a, _b;
218
- const { tablePath: tableName } = (0, malloy_1.parseTableURI)(tableURL);
218
+ const { "tablePath": tableName } = (0, malloy_1.parseTableURI)(tableURL);
219
219
  const segments = tableName.split(".");
220
220
  // paths can have two or three segments
221
221
  // if there are only two segments, assume the dataset is "local" to the current billing project
@@ -241,9 +241,9 @@ class BigQueryConnection {
241
241
  this.bigQuery.projectId = this.projectId;
242
242
  const [metadata] = await metadataPromise;
243
243
  return {
244
- schema: metadata.schema,
245
- needsPartitionPsuedoColumn: ((_a = metadata.timePartitioning) === null || _a === void 0 ? void 0 : _a.type) !== undefined &&
246
- ((_b = metadata.timePartitioning) === null || _b === void 0 ? void 0 : _b.field) === undefined,
244
+ "schema": metadata.schema,
245
+ "needsPartitionPsuedoColumn": ((_a = metadata.timePartitioning) === null || _a === void 0 ? void 0 : _a.type) !== undefined &&
246
+ ((_b = metadata.timePartitioning) === null || _b === void 0 ? void 0 : _b.field) === undefined
247
247
  };
248
248
  }
249
249
  catch (e) {
@@ -284,8 +284,8 @@ class BigQueryConnection {
284
284
  else {
285
285
  try {
286
286
  const [job] = await this.bigQuery.createQueryJob({
287
- location: this.location,
288
- query: sqlCommand,
287
+ "location": this.location,
288
+ "query": sqlCommand
289
289
  });
290
290
  let [metaData] = await job.getMetadata();
291
291
  // wait for job to complete, because we need the table name
@@ -332,9 +332,9 @@ class BigQueryConnection {
332
332
  throw new Error(`Table ${tableName} already exists`);
333
333
  }
334
334
  const [job] = await this.bigQuery.createQueryJob({
335
- query: sqlCommand,
336
- location: this.location,
337
- destination: table,
335
+ "query": sqlCommand,
336
+ "location": this.location,
337
+ "destination": table
338
338
  });
339
339
  // if creating this job didn't throw, there's an ID.
340
340
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -352,26 +352,32 @@ class BigQueryConnection {
352
352
  const malloyType = this.bqToMalloyTypes[type];
353
353
  if (malloyType) {
354
354
  const innerStructDef = {
355
- type: "struct",
355
+ "type": "struct",
356
356
  name,
357
- dialect: this.dialectName,
358
- structSource: { type: "nested" },
359
- structRelationship: { type: "nested", field: name, isArray: true },
360
- fields: [{ ...malloyType, name: "value" }],
357
+ "dialect": this.dialectName,
358
+ "structSource": { "type": "nested" },
359
+ "structRelationship": {
360
+ "type": "nested",
361
+ "field": name,
362
+ "isArray": true
363
+ },
364
+ "fields": [{ ...malloyType, "name": "value" }]
361
365
  };
362
366
  structDef.fields.push(innerStructDef);
363
367
  }
364
368
  }
365
369
  else if (["STRUCT", "RECORD"].includes(type)) {
366
370
  const innerStructDef = {
367
- type: "struct",
371
+ "type": "struct",
368
372
  name,
369
- dialect: this.dialectName,
370
- structSource: field.mode === "REPEATED" ? { type: "nested" } : { type: "inline" },
371
- structRelationship: field.mode === "REPEATED"
372
- ? { type: "nested", field: name, isArray: false }
373
- : { type: "inline" },
374
- fields: [],
373
+ "dialect": this.dialectName,
374
+ "structSource": field.mode === "REPEATED"
375
+ ? { "type": "nested" }
376
+ : { "type": "inline" },
377
+ "structRelationship": field.mode === "REPEATED"
378
+ ? { "type": "nested", "field": name, "isArray": false }
379
+ : { "type": "inline" },
380
+ "fields": []
375
381
  };
376
382
  this.addFieldsToStructDef(innerStructDef, field);
377
383
  structDef.fields.push(innerStructDef);
@@ -384,8 +390,8 @@ class BigQueryConnection {
384
390
  else {
385
391
  structDef.fields.push({
386
392
  name,
387
- type: "string",
388
- e: [`'BigQuery type "${type}" not supported by Malloy'`],
393
+ "type": "string",
394
+ "e": [`'BigQuery type "${type}" not supported by Malloy'`]
389
395
  });
390
396
  }
391
397
  }
@@ -402,37 +408,43 @@ class BigQueryConnection {
402
408
  }
403
409
  structDefFromTableSchema(tableURL, schemaInfo) {
404
410
  const structDef = {
405
- type: "struct",
406
- name: tableURL,
407
- dialect: this.dialectName,
408
- structSource: {
409
- type: "table",
410
- tablePath: this.tableURLtoTablePath(tableURL),
411
+ "type": "struct",
412
+ "name": tableURL,
413
+ "dialect": this.dialectName,
414
+ "structSource": {
415
+ "type": "table",
416
+ "tablePath": this.tableURLtoTablePath(tableURL)
411
417
  },
412
- structRelationship: { type: "basetable", connectionName: this.name },
413
- fields: [],
418
+ "structRelationship": {
419
+ "type": "basetable",
420
+ "connectionName": this.name
421
+ },
422
+ "fields": []
414
423
  };
415
424
  this.addFieldsToStructDef(structDef, schemaInfo.schema);
416
425
  if (schemaInfo.needsPartitionPsuedoColumn) {
417
426
  structDef.fields.push({
418
- type: "timestamp",
419
- name: "_PARTITIONTIME",
427
+ "type": "timestamp",
428
+ "name": "_PARTITIONTIME"
420
429
  });
421
430
  }
422
431
  return structDef;
423
432
  }
424
433
  structDefFromSQLSchema(sqlBlock, tableFieldSchema) {
425
434
  const structDef = {
426
- type: "struct",
427
- name: sqlBlock.name,
428
- dialect: this.dialectName,
429
- structSource: {
430
- type: "sql",
431
- method: "subquery",
432
- sqlBlock,
435
+ "type": "struct",
436
+ "name": sqlBlock.name,
437
+ "dialect": this.dialectName,
438
+ "structSource": {
439
+ "type": "sql",
440
+ "method": "subquery",
441
+ sqlBlock
442
+ },
443
+ "structRelationship": {
444
+ "type": "basetable",
445
+ "connectionName": this.name
433
446
  },
434
- structRelationship: { type: "basetable", connectionName: this.name },
435
- fields: [],
447
+ "fields": []
436
448
  };
437
449
  this.addFieldsToStructDef(structDef, tableFieldSchema);
438
450
  return structDef;
@@ -446,12 +458,12 @@ class BigQueryConnection {
446
458
  try {
447
459
  const tableFieldSchema = await this.getTableFieldSchema(tableURL);
448
460
  inCache = {
449
- schema: this.structDefFromTableSchema(tableURL, tableFieldSchema),
461
+ "schema": this.structDefFromTableSchema(tableURL, tableFieldSchema)
450
462
  };
451
463
  this.schemaCache.set(tableURL, inCache);
452
464
  }
453
465
  catch (error) {
454
- inCache = { error: error.message };
466
+ inCache = { "error": error.message };
455
467
  }
456
468
  }
457
469
  if (inCache.schema !== undefined) {
@@ -473,9 +485,9 @@ class BigQueryConnection {
473
485
  for (let retries = 0; retries < 3; retries++) {
474
486
  try {
475
487
  const [job] = await this.bigQuery.createQueryJob({
476
- location: this.location,
477
- query: sqlRef.selectStr,
478
- dryRun: true,
488
+ "location": this.location,
489
+ "query": sqlRef.selectStr,
490
+ "dryRun": true
479
491
  });
480
492
  return job.metadata.statistics.query.schema;
481
493
  }
@@ -492,11 +504,11 @@ class BigQueryConnection {
492
504
  try {
493
505
  const tableFieldSchema = await this.getSQLBlockSchema(sqlRef);
494
506
  inCache = {
495
- structDef: this.structDefFromSQLSchema(sqlRef, tableFieldSchema),
507
+ "structDef": this.structDefFromSQLSchema(sqlRef, tableFieldSchema)
496
508
  };
497
509
  }
498
510
  catch (error) {
499
- inCache = { error: error.message };
511
+ inCache = { "error": error.message };
500
512
  }
501
513
  this.sqlSchemaCache.set(key, inCache);
502
514
  }
@@ -508,8 +520,8 @@ class BigQueryConnection {
508
520
  async createBigQueryJobAndGetResults(sqlCommand, createQueryJobOptions, getQueryResultsOptions) {
509
521
  try {
510
522
  const job = await this.createBigQueryJob({
511
- query: sqlCommand,
512
- ...createQueryJobOptions,
523
+ "query": sqlCommand,
524
+ ...createQueryJobOptions
513
525
  });
514
526
  // TODO we should check if this is still required?
515
527
  // We do a simple retry-loop here, as a temporary fix for a transient
@@ -521,8 +533,8 @@ class BigQueryConnection {
521
533
  for (let retries = 0; retries < 3; retries++) {
522
534
  try {
523
535
  return await job.getQueryResults({
524
- timeoutMs: 1000 * 60 * 2,
525
- ...getQueryResultsOptions,
536
+ "timeoutMs": 1000 * 60 * 2,
537
+ ...getQueryResultsOptions
526
538
  });
527
539
  }
528
540
  catch (fetchError) {
@@ -537,10 +549,10 @@ class BigQueryConnection {
537
549
  }
538
550
  async createBigQueryJob(createQueryJobOptions) {
539
551
  const [job] = await this.bigQuery.createQueryJob({
540
- location: this.location,
541
- maximumBytesBilled: this.config.maximumBytesBilled || MAXIMUM_BYTES_BILLED,
542
- jobTimeoutMs: Number(this.config.timeoutMs) || TIMEOUT_MS,
543
- ...createQueryJobOptions,
552
+ "location": this.location,
553
+ "maximumBytesBilled": this.config.maximumBytesBilled || MAXIMUM_BYTES_BILLED,
554
+ "jobTimeoutMs": Number(this.config.timeoutMs) || TIMEOUT_MS,
555
+ ...createQueryJobOptions
544
556
  });
545
557
  return job;
546
558
  }
@@ -569,6 +581,6 @@ class BigQueryConnection {
569
581
  }
570
582
  exports.BigQueryConnection = BigQueryConnection;
571
583
  BigQueryConnection.DEFAULT_QUERY_OPTIONS = {
572
- rowLimit: 10,
584
+ "rowLimit": 10
573
585
  };
574
586
  //# sourceMappingURL=bigquery_connection.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-bigquery",
3
- "version": "0.0.22-dev230207182613",
3
+ "version": "0.0.22-dev230207183024",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  "dependencies": {
22
22
  "@google-cloud/bigquery": "^5.5.0",
23
23
  "@google-cloud/common": "^3.6.0",
24
- "@malloydata/malloy": "^0.0.22-dev230207182613",
24
+ "@malloydata/malloy": "^0.0.22-dev230207183024",
25
25
  "gaxios": "^4.2.0"
26
26
  }
27
27
  }