forked from apache/arrow-adbc
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdriver.go
More file actions
316 lines (270 loc) · 13.8 KB
/
Copy pathdriver.go
File metadata and controls
316 lines (270 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.
package bigquery
import (
"context"
"fmt"
"runtime/debug"
"strings"
"cloud.google.com/go/bigquery"
"github.com/apache/arrow-adbc/go/adbc"
"github.com/apache/arrow-adbc/go/adbc/driver/internal/driverbase"
"github.com/apache/arrow-go/v18/arrow/memory"
)
const (
OptionStringAuthType = "adbc.bigquery.sql.auth_type"
OptionStringAPIEndpoint = "adbc.bigquery.sql.api_endpoint"
OptionStringLocation = "adbc.bigquery.sql.location"
OptionStringProjectID = "adbc.bigquery.sql.project_id"
OptionStringDatasetID = "adbc.bigquery.sql.dataset_id"
OptionStringTableID = "adbc.bigquery.sql.table_id"
OptionValueAuthTypeDefault = "adbc.bigquery.sql.auth_type.auth_bigquery"
OptionValueAuthTypeJSONCredentialFile = "adbc.bigquery.sql.auth_type.json_credential_file"
OptionValueAuthTypeJSONCredentialString = "adbc.bigquery.sql.auth_type.json_credential_string"
OptionStringAuthCredentials = "adbc.bigquery.sql.auth_credentials"
OptionValueAuthTypeTemporaryAccessToken = "adbc.bigquery.sql.auth_type.temporary_access_token"
OptionStringAuthAccessToken = "adbc.bigquery.sql.auth.access_token"
OptionStringAuthQuotaProject = "adbc.bigquery.sql.auth.quota_project"
OptionValueAuthTypeUserAuthentication = "adbc.bigquery.sql.auth_type.user_authentication"
OptionStringAuthClientID = "adbc.bigquery.sql.auth.client_id"
OptionStringAuthClientSecret = "adbc.bigquery.sql.auth.client_secret"
OptionStringAuthRefreshToken = "adbc.bigquery.sql.auth.refresh_token"
OptionStringAuthAccessTokenEndpoint = "adbc.bigquery.sql.auth.access_token_endpoint"
OptionStringAuthAccessTokenServerName = "adbc.bigquery.sql.auth.access_token_server_name"
// External-account (Workload Identity Federation): the subject token is
// obtained from an external OAuth2 IdP via the client-credentials grant and
// exchanged at Google STS.
OptionValueAuthTypeExternalAccount = "adbc.bigquery.sql.auth_type.external_account"
OptionStringAuthExternalAccountAudience = "adbc.bigquery.sql.auth.external_account.audience"
OptionStringAuthExternalAccountImpersonationURL = "adbc.bigquery.sql.auth.external_account.impersonation_url"
OptionStringAuthExternalAccountRequestURL = "adbc.bigquery.sql.auth.external_account.request_url"
OptionStringAuthExternalAccountRequestData = "adbc.bigquery.sql.auth.external_account.request_data"
// OptionStringQueryParameterMode specifies if the query uses positional syntax ("?")
// or the named syntax ("@p"). It is illegal to mix positional and named syntax.
// Default is OptionValueQueryParameterModePositional.
OptionStringQueryParameterMode = "adbc.bigquery.sql.query.parameter_mode"
OptionValueQueryParameterModeNamed = "adbc.bigquery.sql.query.parameter_mode_named"
OptionValueQueryParameterModePositional = "adbc.bigquery.sql.query.parameter_mode_positional"
OptionStringQueryDestinationTable = "adbc.bigquery.sql.query.destination_table"
OptionStringQueryDefaultProjectID = "adbc.bigquery.sql.query.default_project_id"
OptionStringQueryDefaultDatasetID = "adbc.bigquery.sql.query.default_dataset_id"
OptionStringQueryCreateDisposition = "adbc.bigquery.sql.query.create_disposition"
OptionStringQueryWriteDisposition = "adbc.bigquery.sql.query.write_disposition"
OptionStringQueryLabels = "adbc.bigquery.sql.query.labels"
OptionBoolQueryDisableQueryCache = "adbc.bigquery.sql.query.disable_query_cache"
OptionBoolDisableFlattenedResults = "adbc.bigquery.sql.query.disable_flattened_results"
OptionBoolQueryAllowLargeResults = "adbc.bigquery.sql.query.allow_large_results"
OptionStringQueryPriority = "adbc.bigquery.sql.query.priority"
OptionIntQueryMaxBillingTier = "adbc.bigquery.sql.query.max_billing_tier"
OptionIntQueryMaxBytesBilled = "adbc.bigquery.sql.query.max_bytes_billed"
OptionBoolQueryUseLegacySQL = "adbc.bigquery.sql.query.use_legacy_sql"
OptionBoolQueryDryRun = "adbc.bigquery.sql.query.dry_run"
OptionBoolQueryCreateSession = "adbc.bigquery.sql.query.create_session"
OptionIntQueryJobTimeout = "adbc.bigquery.sql.query.job_timeout"
OptionIntQueryResultBufferSize = "adbc.bigquery.sql.query.result_buffer_size"
OptionIntQueryPrefetchConcurrency = "adbc.bigquery.sql.query.prefetch_concurrency"
// OptionBoolUseStorageApiDisabledClient instructs the driver to use the legacy RowIterator API
// instead of the Storage Read API. This is required for queries that reference
// pseudo-columns like _PARTITIONDATE and _PARTITIONTIME.
OptionBoolUseStorageApiDisabledClient = "adbc.bigquery.sql.query.use_storage_api_disabled_client"
defaultQueryResultBufferSize = 200
defaultQueryPrefetchConcurrency = 10
DefaultAccessTokenEndpoint = "https://accounts.google.com/o/oauth2/token"
DefaultAccessTokenServerName = "google.com"
// Google STS endpoint and default subject-token type for external-account.
DefaultSTSTokenURL = "https://sts.googleapis.com/v1/token"
DefaultSubjectTokenType = "urn:ietf:params:oauth:token-type:jwt"
OptionStringIngestFileDelimiter = "adbc.bigquery.ingest.csv_delimiter"
OptionStringIngestPath = "adbc.bigquery.ingest.csv_filepath"
OptionStringIngestSchema = "adbc.bigquery.ingest.csv_schema"
OptionStringDataprocReqRegion = "adbc.bigquery.dataproc.compute_region"
OptionStringDataprocReqProject = "adbc.bigquery.dataproc.project"
OptionIntDataprocReqPoolingTimeout = "adbc.bigquery.dataproc.pooling_timeout"
OptionStringCreateBatchReqParent = "adbc.bigquery.create_batch.parent"
OptionStringCreateBatchReqBatchYML = "adbc.bigquery.create_batch.batch_yml"
OptionStringCreateBatchReqBatchId = "adbc.bigquery.create_batch.batch_id"
OptionStringDataprocSubmitJobReqClusterName = "adbc.bigquery.dataproc.submit_job.cluster_name"
OptionStringDataprocSubmitJobReqGCSPath = "adbc.bigquery.dataproc.submit_job.gcs_path"
OptionStringWriteGCSBucket = "adbc.bigquery.write_gcs.bucket"
OptionStringWriteGCSObjectName = "adbc.bigquery.write_gcs.object_name"
OptionStringWriteGCSContent = "adbc.bigquery.write_gcs.content"
OptionStringNotebookExecuteJobGscPath = "adbc.bigquery.notebook_execute_job.gsc_path"
OptionStringNotebookExecuteJobModelFileName = "adbc.bigquery.notebook_execute_job.model_file_name"
OptionStringNotebookExecuteJobModelName = "adbc.bigquery.notebook_execute_job.model_name"
OptionStringNotebookExecuteJobGscBucket = "adbc.bigquery.notebook_execute_job.gsc_bucket"
OptionStringNotebookExecuteJobTemplateId = "adbc.bigquery.notebook_execute_job.template_id"
OptionStringNotebookExecuteJobParent = "adbc.bigquery.notebook_execute_job.parent"
OptionStringNotebookExecuteJobProject = "adbc.bigquery.notebook_execute_job.project"
OptionStringNotebookExecuteJobRegion = "adbc.bigquery.notebook_execute_job.region"
OptionJsonUpdateTableColumnsDescription = "adbc.bigquery.table.update_columns_description"
OptionJsonUpdateTableColumnsPolicyTags = "adbc.bigquery.table.update_columns_policy_tags"
OptionJsonAuthorizeViewToDatasets = "adbc.bigquery.dataset.authorize_view_to_datasets"
OptionStringUpdateTableDescriptionValue = "adbc.bigquery.table.update_description"
// WithAppDefaultCredentials instructs the driver to authenticate using
// Application Default Credentials (ADC).
OptionValueAuthTypeAppDefaultCredentials = "adbc.bigquery.sql.auth_type.app_default_credentials"
// WithJSONCredentials instructs the driver to authenticate using the
// given JSON credentials. The value should be a byte array representing
// the JSON credentials.
OptionValueAuthTypeJSONCredentials = "adbc.bigquery.sql.auth_type.json_credentials"
// WithOAuthClientIDs instructs the driver to authenticate using the given
// OAuth client ID and client secret. The value should be a string array
// of length 2, where the first element is the client ID and the second
// is the client secret.
OptionValueAuthTypeOAuthClientIDs = "adbc.bigquery.sql.auth_type.oauth_client_ids"
// OptionStringImpersonateTargetPrincipal instructs the driver to impersonate the
// given service account email.
OptionStringImpersonateTargetPrincipal = "adbc.bigquery.sql.impersonate.target_principal"
// OptionStringImpersonateDelegates instructs the driver to impersonate using the
// given comma-separated list of service account emails in the delegation
// chain.
OptionStringImpersonateDelegates = "adbc.bigquery.sql.impersonate.delegates"
// OptionStringImpersonateScopes instructs the driver to impersonate using the
// given comma-separated list of OAuth 2.0 scopes.
OptionStringImpersonateScopes = "adbc.bigquery.sql.impersonate.scopes"
// OptionStringImpersonateLifetime instructs the driver to impersonate for the
// given duration (e.g. "3600s").
OptionStringImpersonateLifetime = "adbc.bigquery.sql.impersonate.lifetime"
// OptionBoolQueryLinkFailedJob instructs the driver to construct a link to the
// query job if it fails to run.
OptionBoolQueryLinkFailedJob = "adbc.bigquery.sql.query.link_failed_job"
// OptionBoolStatementFetchJobStats instructs the driver to fetch full
// BigQuery job statistics
//
// Since these stats costs extra API calls, it is disabled by default.
OptionBoolStatementFetchJobStats = "adbc.bigquery.statement.fetch_job_stats"
OptionStringCopyTableSource = "adbc.bigquery.copy_table.source"
OptionStringCopyTableDestination = "adbc.bigquery.copy_table.destination"
OptionStringCopyTableWriteDisposition = "adbc.bigquery.copy_table.write_disposition"
)
var (
infoVendorVersion string
)
func init() {
if info, ok := debug.ReadBuildInfo(); ok {
for _, dep := range info.Deps {
switch dep.Path {
case "cloud.google.com/go/bigquery":
infoVendorVersion = dep.Version
}
}
}
}
type driverImpl struct {
driverbase.DriverImplBase
}
// NewDriver creates a new BigQuery driver using the given Arrow allocator.
func NewDriver(alloc memory.Allocator) adbc.Driver {
info := driverbase.DefaultDriverInfo("BigQuery")
if infoVendorVersion != "" {
if err := info.RegisterInfoCode(adbc.InfoVendorVersion, infoVendorVersion); err != nil {
panic(err)
}
}
return driverbase.NewDriver(&driverImpl{
DriverImplBase: driverbase.NewDriverImplBase(info, alloc),
})
}
func (d *driverImpl) NewDatabase(opts map[string]string) (adbc.Database, error) {
return d.NewDatabaseWithContext(context.Background(), opts)
}
func (d *driverImpl) NewDatabaseWithContext(ctx context.Context, opts map[string]string) (adbc.Database, error) {
dbBase, err := driverbase.NewDatabaseImplBase(ctx, &d.DriverImplBase)
if err != nil {
return nil, err
}
db := &databaseImpl{
DatabaseImplBase: dbBase,
authType: OptionValueAuthTypeDefault,
}
if err := db.SetOptions(opts); err != nil {
return nil, err
}
return driverbase.NewDatabase(db), nil
}
func parseParts(defaultProjectID, defaultDatasetID, value string) (string, string, string, error) {
parts := strings.Split(value, ".")
switch len(parts) {
case 1:
return defaultProjectID, defaultDatasetID, parts[0], nil
case 2:
return defaultProjectID, parts[0], parts[1], nil
case 3:
return parts[0], parts[1], parts[2], nil
default:
return "", "", "", adbc.Error{
Code: adbc.StatusInvalidArgument,
Msg: fmt.Sprintf("invalid table reference %q (want [[project.]dataset.]table)", value),
}
}
}
// This takes a partially qualified table string in format [project.][.dataset.]table
// and returns a bigquery.Table object.
// If project or dataset is not provided, the default project ID and dataset ID from the statement is used
// Returns an error if the format is invalid.
func stringToTable(st *statement, value string) (*bigquery.Table, error) {
defaultProjectID := st.cnxn.catalog
defaultDatasetID := st.cnxn.dbSchema
projectID, datasetID, tableID, err := parseParts(defaultProjectID, defaultDatasetID, value)
if err != nil {
return nil, err
}
return st.cnxn.table(projectID, datasetID, tableID), nil
}
func stringToTableCreateDisposition(value string) (bigquery.TableCreateDisposition, error) {
v := bigquery.TableCreateDisposition(value)
switch v {
case bigquery.CreateIfNeeded, bigquery.CreateNever:
return v, nil
default:
return v, adbc.Error{
Code: adbc.StatusInvalidArgument,
Msg: fmt.Sprintf("unknown table create disposition value `%s`", v),
}
}
}
func stringToTableWriteDisposition(value string) (bigquery.TableWriteDisposition, error) {
v := bigquery.TableWriteDisposition(value)
switch v {
case bigquery.WriteAppend, bigquery.WriteTruncate, bigquery.WriteEmpty:
return v, nil
default:
return v, adbc.Error{
Code: adbc.StatusInvalidArgument,
Msg: fmt.Sprintf("unknown table write disposition value `%s`", v),
}
}
}
func stringToQueryPriority(value string) (bigquery.QueryPriority, error) {
v := bigquery.QueryPriority(value)
switch v {
case bigquery.BatchPriority, bigquery.InteractivePriority:
return v, nil
default:
return v, adbc.Error{
Code: adbc.StatusInvalidArgument,
Msg: fmt.Sprintf("unknown priority value `%s`", v),
}
}
}
func tableToString(value *bigquery.Table) string {
if value == nil {
return ""
} else {
return fmt.Sprintf("%s.%s.%s", value.ProjectID, value.DatasetID, value.TableID)
}
}