@malloydata/db-bigquery 0.0.49-dev230626172855 → 0.0.49-dev230626192330

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.
@@ -69,17 +69,16 @@ export declare class BigQueryConnection implements Connection, PersistSQLResults
69
69
  downloadMalloyQuery(sqlCommand: string): Promise<ResourceStream<RowMetadata>>;
70
70
  private dryRunSQLQuery;
71
71
  estimateQueryCost(sqlCommand: string): Promise<QueryRunStats>;
72
- structDefFromSQL(sqlCommand: string): Promise<StructDef>;
73
- getTableFieldSchema(tableURL: string): Promise<SchemaInfo>;
72
+ private normalizeTablePath;
73
+ getTableFieldSchema(tablePath: string): Promise<SchemaInfo>;
74
74
  executeSQLRaw(sqlCommand: string): Promise<QueryData>;
75
75
  test(): Promise<void>;
76
76
  manifestTemporaryTable(sqlCommand: string): Promise<string>;
77
77
  manifestPermanentTable(sqlCommand: string, datasetName: string, tableName: string, overwriteExistingTable?: boolean, createDataset?: boolean): Promise<string>;
78
78
  private addFieldsToStructDef;
79
- private tableURLtoTablePath;
80
79
  private structDefFromTableSchema;
81
80
  private structDefFromSQLSchema;
82
- fetchSchemaForTables(missing: string[]): Promise<{
81
+ fetchSchemaForTables(missing: Record<string, string>): Promise<{
83
82
  schemas: Record<string, StructDef>;
84
83
  errors: Record<string, string>;
85
84
  }>;
@@ -219,26 +219,21 @@ class BigQueryConnection {
219
219
  queryCostBytes: Number(dryRunResults.metadata.statistics.totalBytesProcessed),
220
220
  };
221
221
  }
222
- async structDefFromSQL(sqlCommand) {
223
- const dryRunResults = await this.dryRunSQLQuery(sqlCommand);
224
- const destinationTable = dryRunResults.metadata.configuration.query.destinationTable;
225
- return this.structDefFromTableSchema(`${destinationTable.projectId}.${destinationTable.datasetId}.${destinationTable.tableId}`, dryRunResults.metadata.statistics.query.schema);
222
+ normalizeTablePath(tablePath) {
223
+ if (tablePath.split('.').length === 2) {
224
+ return `${this.defaultProject}.${tablePath}`;
225
+ }
226
+ else {
227
+ return tablePath;
228
+ }
226
229
  }
227
- async getTableFieldSchema(tableURL) {
230
+ async getTableFieldSchema(tablePath) {
228
231
  var _a, _b, _c, _d;
229
- const { tablePath: tableName } = (0, malloy_1.parseTableURI)(tableURL);
230
- const segments = tableName.split('.');
231
- // paths can have two or three segments
232
- // if there are only two segments, assume the dataset is "local" to the current billing project
233
- let projectId, datasetNamePart, tableNamePart;
234
- if (segments.length === 2) {
235
- [datasetNamePart, tableNamePart] = segments;
236
- projectId = this.defaultProject;
232
+ const segments = this.normalizeTablePath(tablePath).split('.');
233
+ if (segments.length !== 3) {
234
+ throw new Error(`Improper table path: ${tablePath}. A table path requires 2 or 3 segments`);
237
235
  }
238
- else if (segments.length === 3)
239
- [projectId, datasetNamePart, tableNamePart] = segments;
240
- else
241
- throw new Error(`Improper table path: ${tableName}. A table path requires 2 or 3 segments`);
236
+ const [projectId, datasetNamePart, tableNamePart] = segments;
242
237
  try {
243
238
  // TODO The `dataset` API has no way to set a different `projectId` than the one stored in the BQ
244
239
  // instance. So we hack it until a better way exists: we set the `this.bigQuery.projectId`
@@ -247,7 +242,8 @@ class BigQueryConnection {
247
242
  // this is better than creating a new BQ instance every time we need to get a table schema.
248
243
  if (projectId)
249
244
  this.bigQuery.projectId = projectId;
250
- const needTableSuffixPseudoColumn = tableNamePart && tableNamePart[tableNamePart.length - 1] === '*';
245
+ const needTableSuffixPseudoColumn = tableNamePart !== undefined &&
246
+ tableNamePart[tableNamePart.length - 1] === '*';
251
247
  const table = this.bigQuery.dataset(datasetNamePart).table(tableNamePart);
252
248
  const metadataPromise = table.getMetadata();
253
249
  this.bigQuery.projectId = this.projectId;
@@ -405,23 +401,14 @@ class BigQueryConnection {
405
401
  }
406
402
  }
407
403
  }
408
- tableURLtoTablePath(tableURL) {
409
- const { tablePath } = (0, malloy_1.parseTableURI)(tableURL);
410
- if (tablePath.split('.').length === 2) {
411
- return `${this.defaultProject}.${tablePath}`;
412
- }
413
- else {
414
- return tablePath;
415
- }
416
- }
417
- structDefFromTableSchema(tableURL, schemaInfo) {
404
+ structDefFromTableSchema(tableKey, tablePath, schemaInfo) {
418
405
  const structDef = {
419
406
  type: 'struct',
420
- name: tableURL,
407
+ name: tableKey,
421
408
  dialect: this.dialectName,
422
409
  structSource: {
423
410
  type: 'table',
424
- tablePath: this.tableURLtoTablePath(tableURL),
411
+ tablePath,
425
412
  },
426
413
  structRelationship: {
427
414
  type: 'basetable',
@@ -472,25 +459,26 @@ class BigQueryConnection {
472
459
  async fetchSchemaForTables(missing) {
473
460
  const schemas = {};
474
461
  const errors = {};
475
- for (const tableURL of missing) {
476
- let inCache = this.schemaCache.get(tableURL);
462
+ for (const tableKey in missing) {
463
+ let inCache = this.schemaCache.get(tableKey);
477
464
  if (!inCache) {
465
+ const tablePath = this.normalizeTablePath(missing[tableKey]);
478
466
  try {
479
- const tableFieldSchema = await this.getTableFieldSchema(tableURL);
467
+ const tableFieldSchema = await this.getTableFieldSchema(tablePath);
480
468
  inCache = {
481
- schema: this.structDefFromTableSchema(tableURL, tableFieldSchema),
469
+ schema: this.structDefFromTableSchema(tableKey, tablePath, tableFieldSchema),
482
470
  };
483
- this.schemaCache.set(tableURL, inCache);
471
+ this.schemaCache.set(tableKey, inCache);
484
472
  }
485
473
  catch (error) {
486
474
  inCache = { error: error.message };
487
475
  }
488
476
  }
489
477
  if (inCache.schema !== undefined) {
490
- schemas[tableURL] = inCache.schema;
478
+ schemas[tableKey] = inCache.schema;
491
479
  }
492
480
  else {
493
- errors[tableURL] = inCache.error || 'Unknown schema fetch error';
481
+ errors[tableKey] = inCache.error || 'Unknown schema fetch error';
494
482
  }
495
483
  }
496
484
  return { schemas, errors };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-bigquery",
3
- "version": "0.0.49-dev230626172855",
3
+ "version": "0.0.49-dev230626192330",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "@google-cloud/bigquery": "^5.5.0",
26
26
  "@google-cloud/common": "^3.6.0",
27
27
  "@google-cloud/paginator": "^4.0.1",
28
- "@malloydata/malloy": "^0.0.49-dev230626172855",
28
+ "@malloydata/malloy": "^0.0.49-dev230626192330",
29
29
  "gaxios": "^4.2.0"
30
30
  }
31
31
  }