-
Notifications
You must be signed in to change notification settings - Fork 213
feat: query client based on autogen package #1491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: preview-9.x
Are you sure you want to change the base?
Changes from 2 commits
0819148
4d95943
8bafda6
46edd39
8c6287e
aa9e4b1
123f39b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,3 +46,4 @@ export { | |
| }; | ||
| import * as protos from '../protos/protos'; | ||
| export {protos}; | ||
| export * as query from './query'; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import {protos} from '../'; | ||
|
|
||
| /** | ||
| * QueryFromSQL creates a query configuration from a SQL string. | ||
| * @param {string} sql The SQL query. | ||
| * @returns {protos.google.cloud.bigquery.v2.IPostQueryRequest} | ||
| */ | ||
| export function queryFromSQL( | ||
| projectId: string, | ||
| sql: string, | ||
| ): protos.google.cloud.bigquery.v2.IPostQueryRequest { | ||
| return { | ||
| queryRequest: { | ||
| query: sql, | ||
| useLegacySql: {value: false}, | ||
| formatOptions: { | ||
| useInt64Timestamp: true, | ||
| }, | ||
| }, | ||
| projectId, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import { | ||
| BigQueryClient, | ||
| BigQueryClientOptions, | ||
| SubClientOptions, | ||
| } from '../bigquery'; | ||
| import {QueryJob, CallOptions} from './job'; | ||
| import {protos} from '../'; | ||
| import {queryFromSQL as builderQueryFromSQL} from './builder'; | ||
| import {QueryReader} from './reader'; | ||
|
|
||
| /** | ||
| * QueryClient is a client for running queries in BigQuery. | ||
| */ | ||
| export class QueryClient { | ||
| private client: BigQueryClient; | ||
| projectId: string; | ||
| private billingProjectId: string; | ||
|
|
||
| /** | ||
| * @param {BigQueryClientOptions} options - The configuration object. | ||
| */ | ||
| constructor( | ||
| options?: BigQueryClientOptions, | ||
| subClientOptions?: SubClientOptions, | ||
| ) { | ||
| this.client = new BigQueryClient(options, subClientOptions); | ||
| this.projectId = ''; | ||
| this.billingProjectId = ''; | ||
| void this.client.jobClient.getProjectId().then(projectId => { | ||
| this.projectId = projectId; | ||
| if (this.billingProjectId !== '') { | ||
| this.billingProjectId = projectId; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| setBillingProjectId(projectId: string) { | ||
| this.billingProjectId = projectId; | ||
| } | ||
|
|
||
| /** | ||
| * QueryFromSQL creates a query configuration from a SQL string. | ||
| * @param {string} sql The SQL query. | ||
| * @returns {protos.google.cloud.bigquery.v2.IPostQueryRequest} | ||
| */ | ||
| queryFromSQL(sql: string): protos.google.cloud.bigquery.v2.IPostQueryRequest { | ||
| const req = builderQueryFromSQL(this.projectId, sql); | ||
| return req; | ||
| } | ||
|
|
||
| /** | ||
| * NewQueryReader creates a new QueryReader. | ||
| * @returns {QueryReader} | ||
| */ | ||
| newQueryReader(): QueryReader { | ||
| return new QueryReader(this); | ||
| } | ||
|
|
||
| /** | ||
| * Runs a query and returns a QueryJob handle. | ||
| * | ||
| * @param {protos.google.cloud.bigquery.v2.IPostQueryRequest} request | ||
| * The request object that will be sent. | ||
| * @param {CallOptions} [options] | ||
| * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. | ||
| * @returns {Promise<QueryJob>} | ||
| */ | ||
| async startQuery( | ||
| request: protos.google.cloud.bigquery.v2.IPostQueryRequest, | ||
| options?: CallOptions, | ||
| ): Promise<QueryJob> { | ||
| const [response] = await this.client.jobClient.query(request, options); | ||
| return new QueryJob(this, response); | ||
| } | ||
|
|
||
| /** | ||
| * Runs a query and returns a QueryJob handle. | ||
| * | ||
| * @param {protos.google.cloud.bigquery.v2.IQueryRequest} request | ||
| * The request object that will be sent. | ||
| * @param {CallOptions} [options] | ||
| * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. | ||
| * @returns {Promise<QueryJob>} | ||
| */ | ||
| async startQueryRequest( | ||
| request: protos.google.cloud.bigquery.v2.IQueryRequest, | ||
| options?: CallOptions, | ||
| ): Promise<QueryJob> { | ||
| return this.startQuery( | ||
| { | ||
| queryRequest: request, | ||
| projectId: this.projectId, | ||
| }, | ||
| options, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Starts a new asynchronous job. | ||
| * | ||
| * @param {protos.google.cloud.bigquery.v2.IJob} job | ||
| * A job resource to insert | ||
| * @param {CallOptions} [options] | ||
| * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. | ||
| * @returns {Promise<QueryJob>} | ||
| */ | ||
| async startQueryJob( | ||
| job: protos.google.cloud.bigquery.v2.IJob, | ||
| options?: CallOptions, | ||
| ): Promise<QueryJob> { | ||
| const [response] = await this.client.jobClient.insertJob( | ||
| { | ||
| projectId: this.projectId, | ||
| job, | ||
| }, | ||
| options, | ||
| ); | ||
| return new QueryJob(this, {jobReference: response.jobReference}); | ||
| } | ||
|
|
||
| getBigQueryClient(): BigQueryClient { | ||
| return this.client; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will these utils only be useful for queries, or are they also useful with other clients? |
||
| export function civilDateString(d: Date): string { | ||
| return d.toISOString().slice(0, 10); | ||
| } | ||
|
|
||
| export function civilTimeString(value: string | Date): string { | ||
| if (value instanceof Date) { | ||
| const h = `${value.getHours()}`.padStart(2, '0'); | ||
| const m = `${value.getMinutes()}`.padStart(2, '0'); | ||
| const s = `${value.getSeconds()}`.padStart(2, '0'); | ||
| const f = `${value.getMilliseconds()}`.padStart(3, '0'); | ||
| return `${h}:${m}:${s}.${f}`; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| export function civilDateTimeString(value: Date | string): string { | ||
| if (value instanceof Date) { | ||
| let time; | ||
| if (value.getHours()) { | ||
| time = civilTimeString(value); | ||
| } | ||
| const y = `${value.getFullYear()}`.padStart(2, '0'); | ||
| const m = `${value.getMonth() + 1}`.padStart(2, '0'); | ||
| const d = `${value.getDate()}`.padStart(2, '0'); | ||
| time = time ? ' ' + time : ''; | ||
| return `${y}-${m}-${d}${time}`; | ||
| } | ||
| return value.replace(/^(.*)T(.*)Z$/, '$1 $2'); | ||
| } | ||
|
|
||
| export function timestampString(ts: Date): string { | ||
| return ts.toISOString().replace('T', ' ').replace('Z', ''); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| export {QueryClient} from './client'; | ||
| export {QueryJob} from './job'; | ||
| export {Row} from './row'; | ||
| export {RowIterator} from './iterator'; | ||
| export {queryFromSQL} from './builder'; | ||
| export {QueryReader, withPageToken} from './reader'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import {QueryJob} from './job'; | ||
| import {Row} from './row'; | ||
|
|
||
| /** | ||
| * RowIterator iterates over the results of a query. | ||
| */ | ||
| export class RowIterator { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use the iterator without the reader, or is this only ever used in the context of the reader? If only ever coupled with the reader, my nit would be to have them in the same file |
||
| private job: QueryJob; | ||
| private pageToken?: string; | ||
| private rows: Row[] = []; | ||
|
|
||
| constructor( | ||
| job: QueryJob, | ||
| opts?: { | ||
| rows?: Row[]; | ||
| pageToken?: string; | ||
| }, | ||
| ) { | ||
| this.job = job; | ||
| this.pageToken = opts?.pageToken; | ||
| this.rows = opts?.rows ?? []; | ||
| } | ||
|
|
||
| async fetchRows() { | ||
| const [rows, _, pageToken] = await this.job._getRows(this.pageToken); | ||
| this.rows = rows; | ||
| this.pageToken = pageToken || undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Asynchronously iterates over the rows in the query result. | ||
| */ | ||
| async *[Symbol.asyncIterator](): AsyncGenerator<Row> { | ||
| if (this.rows.length > 0) { | ||
| for (const row of this.rows) { | ||
| yield row; | ||
| } | ||
| } | ||
| while (this.pageToken) { | ||
| await this.fetchRows(); | ||
| for (const row of this.rows) { | ||
| yield row; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting - why have an entirely new client rather than have this be part of the central client?