@malloydata/db-bigquery 0.0.414 → 0.0.415

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.
@@ -84,6 +84,7 @@ export declare class BigQueryConnection extends BaseConnection implements Connec
84
84
  downloadMalloyQuery(sqlCommand: string): Promise<ResourceStream<RowMetadata>>;
85
85
  private dryRunSQLQuery;
86
86
  estimateQueryCost(sqlCommand: string): Promise<QueryRunStats>;
87
+ private decodeTablePath;
87
88
  private decodeTablePathSegments;
88
89
  getTableFieldSchema(tablePath: string): Promise<SchemaInfo>;
89
90
  executeSQLRaw(sqlCommand: string): Promise<QueryData>;
@@ -92,6 +93,8 @@ export declare class BigQueryConnection extends BaseConnection implements Connec
92
93
  manifestPermanentTable(sqlCommand: string, datasetName: string, tableName: string, overwriteExistingTable?: boolean, createDataset?: boolean): Promise<string>;
93
94
  private addFieldsToStructDef;
94
95
  fetchSelectSchema(sqlSource: SQLSourceRequest): Promise<SQLSourceDef | string>;
96
+ private qualifyTablePath;
97
+ private encodeProjectSegment;
95
98
  fetchTableSchema(tableName: string, tablePath: string): Promise<TableSourceDef | string>;
96
99
  private getSQLBlockSchema;
97
100
  private createBigQueryJobAndGetResults;
@@ -221,16 +221,19 @@ class BigQueryConnection extends connection_1.BaseConnection {
221
221
  queryCostBytes: Number(dryRunResults.metadata.statistics.totalBytesProcessed),
222
222
  };
223
223
  }
224
- // The whole-backtick form `` `proj.dataset.table` `` parses as one
225
- // quoted segment, but the metadata API still wants the parts
226
- // separately — split its decoded body on `.` to recover them.
227
- decodeTablePathSegments(tablePath) {
228
- const result = (0, malloy_1.decodeDottedTablePath)(tablePath, {
224
+ decodeTablePath(tablePath) {
225
+ return (0, malloy_1.decodeDottedTablePath)(tablePath, {
229
226
  quoteChar: '`',
230
227
  escapeStyle: 'backslash',
231
228
  bareIdentRegex: this.dialect.tablePathBareIdentRegex,
232
229
  dialectName: 'BigQuery',
233
230
  });
231
+ }
232
+ // The whole-backtick form `` `proj.dataset.table` `` parses as one
233
+ // quoted segment, but the metadata API still wants the parts
234
+ // separately — split its decoded body on `.` to recover them.
235
+ decodeTablePathSegments(tablePath) {
236
+ const result = this.decodeTablePath(tablePath);
234
237
  if (!result.ok)
235
238
  return [tablePath];
236
239
  if (result.segments.length === 1 && result.segments[0].quoted) {
@@ -423,18 +426,41 @@ class BigQueryConnection extends connection_1.BaseConnection {
423
426
  return error.message;
424
427
  }
425
428
  }
429
+ // tablePath is pasted into the FROM clause verbatim — we never edit the
430
+ // characters the user wrote. The default project is the one sanctioned
431
+ // addition: BigQuery resolves an unqualified `dataset.table` against the
432
+ // job's billing/ambient project, so when the user omits the project we
433
+ // prepend it as a new leading segment. We do this exactly when the user
434
+ // wrote a two-segment path; anything else (a single segment, an already-
435
+ // qualified path, or a path wrapped entirely in backticks — which decodes
436
+ // to one segment) is left as written, and the user supplies the project.
437
+ // The project is the one segment we render ourselves, so we quote it if
438
+ // BigQuery requires it (e.g. a legacy `domain.com:project` id).
439
+ qualifyTablePath(tablePath) {
440
+ const decoded = this.decodeTablePath(tablePath);
441
+ if (!decoded.ok || decoded.segments.length !== 2) {
442
+ return tablePath;
443
+ }
444
+ return `${this.encodeProjectSegment(this.projectId)}.${tablePath}`;
445
+ }
446
+ encodeProjectSegment(project) {
447
+ // Leave it bare only if the whole id is a bare table-path segment (the
448
+ // regex anchors the start, so require it to match end to end); otherwise
449
+ // quote it the way BigQuery would any other identifier.
450
+ const bare = this.dialect.tablePathBareIdentRegex.exec(project);
451
+ if (bare && bare[0] === project) {
452
+ return project;
453
+ }
454
+ return this.dialect.sqlQuoteIdentifier(project);
455
+ }
426
456
  async fetchTableSchema(tableName, tablePath) {
427
- // Keep the canonical tablePath (which may be backtick-wrapped for
428
- // wildcard tables) for downstream SQL emission. The metadata lookup
429
- // wants the unwrapped form, which `getTableFieldSchema` handles via
430
- // `normalizeTablePath`.
431
457
  try {
432
458
  const tableFieldSchema = await this.getTableFieldSchema(tablePath);
433
459
  const tableDef = {
434
460
  type: 'table',
435
461
  name: tableName,
436
462
  dialect: this.dialectName,
437
- tablePath,
463
+ tablePath: this.qualifyTablePath(tablePath),
438
464
  connection: this.name,
439
465
  fields: [],
440
466
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/db-bigquery",
3
- "version": "0.0.414",
3
+ "version": "0.0.415",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "@google-cloud/bigquery": "^7.3.0",
27
27
  "@google-cloud/common": "^5.0.1",
28
28
  "@google-cloud/paginator": "^5.0.0",
29
- "@malloydata/malloy": "0.0.414",
29
+ "@malloydata/malloy": "0.0.415",
30
30
  "gaxios": "^4.2.0"
31
31
  }
32
32
  }