| 61 | } |
| 62 | |
| 63 | export class BigQueryDbAdapter implements IDbAdapter { |
| 64 | private bigQueryCredentials: dataform.IBigQuery; |
| 65 | private pool: PromisePoolExecutor; |
| 66 | private clientProvider: BigQueryClientProvider; |
| 67 | |
| 68 | constructor( |
| 69 | credentials: dataform.IBigQuery, |
| 70 | options?: { |
| 71 | concurrencyLimit?: number; |
| 72 | clientProvider?: BigQueryClientProvider; |
| 73 | } |
| 74 | ) { |
| 75 | this.bigQueryCredentials = credentials; |
| 76 | this.clientProvider = options?.clientProvider || createBigQueryClientProvider(credentials); |
| 77 | |
| 78 | // Bigquery allows 50 concurrent queries, and a rate limit of 100/user/second by default. |
| 79 | // These limits should be safely low enough for most projects. |
| 80 | this.pool = new PromisePoolExecutor({ |
| 81 | concurrencyLimit: options?.concurrencyLimit, |
| 82 | frequencyWindow: 1000, |
| 83 | frequencyLimit: 30 |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | public async execute( |
| 88 | statement: string, |
| 89 | options: { |
| 90 | params?: { [name: string]: any }; |
| 91 | onCancel?: OnCancel; |
| 92 | interactive?: boolean; |
| 93 | rowLimit?: number; |
| 94 | byteLimit?: number; |
| 95 | bigquery?: IBigQueryExecutionOptions; |
| 96 | } = { interactive: false, rowLimit: 1000, byteLimit: 1024 * 1024 } |
| 97 | ): Promise<IExecutionResult> { |
| 98 | if (options?.interactive && options?.bigquery?.labels) { |
| 99 | throw new Error("BigQuery job labels may not be set for interactive queries."); |
| 100 | } |
| 101 | |
| 102 | if (!statement) { |
| 103 | throw new Error("Query string cannot be empty"); |
| 104 | } |
| 105 | return this.pool |
| 106 | .addSingleTask({ |
| 107 | generator: () => |
| 108 | options?.interactive |
| 109 | ? this.runQuery( |
| 110 | statement, |
| 111 | options?.params, |
| 112 | options?.rowLimit, |
| 113 | options?.byteLimit, |
| 114 | options.bigquery?.location |
| 115 | ) |
| 116 | : this.createQueryJob( |
| 117 | statement, |
| 118 | options?.params, |
| 119 | options?.rowLimit, |
| 120 | options?.byteLimit, |
nothing calls this directly
no outgoing calls
no test coverage detected