forked from googleapis/python-bigquery
/
routine.py
535 lines (432 loc) · 17 KB
/
routine.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# -*- coding: utf-8 -*-
#
# Copyright 2019 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.
"""Define resources for the BigQuery Routines API."""
from google.protobuf import json_format
import google.cloud._helpers
from google.cloud.bigquery import _helpers
import google.cloud.bigquery_v2.types
class Routine(object):
"""Resource representing a user-defined routine.
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines
Args:
routine_ref (Union[str, google.cloud.bigquery.routine.RoutineReference]):
A pointer to a routine. If ``routine_ref`` is a string, it must
included a project ID, dataset ID, and routine ID, each separated
by ``.``.
``**kwargs`` (Dict):
Initial property values.
"""
_PROPERTY_TO_API_FIELD = {
"arguments": "arguments",
"body": "definitionBody",
"created": "creationTime",
"etag": "etag",
"imported_libraries": "importedLibraries",
"language": "language",
"modified": "lastModifiedTime",
"reference": "routineReference",
"return_type": "returnType",
"type_": "routineType",
"description": "description",
"determinism_level": "determinismLevel",
}
def __init__(self, routine_ref, **kwargs):
if isinstance(routine_ref, str):
routine_ref = RoutineReference.from_string(routine_ref)
self._properties = {"routineReference": routine_ref.to_api_repr()}
for property_name in kwargs:
setattr(self, property_name, kwargs[property_name])
@property
def reference(self):
"""google.cloud.bigquery.routine.RoutineReference: Reference
describing the ID of this routine.
"""
return RoutineReference.from_api_repr(
self._properties[self._PROPERTY_TO_API_FIELD["reference"]]
)
@property
def path(self):
"""str: URL path for the routine's APIs."""
return self.reference.path
@property
def project(self):
"""str: ID of the project containing the routine."""
return self.reference.project
@property
def dataset_id(self):
"""str: ID of dataset containing the routine."""
return self.reference.dataset_id
@property
def routine_id(self):
"""str: The routine ID."""
return self.reference.routine_id
@property
def etag(self):
"""str: ETag for the resource (:data:`None` until set from the
server).
Read-only.
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["etag"])
@property
def type_(self):
"""str: The fine-grained type of the routine.
See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#RoutineType
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["type_"])
@type_.setter
def type_(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["type_"]] = value
@property
def created(self):
"""Optional[datetime.datetime]: Datetime at which the routine was
created (:data:`None` until set from the server).
Read-only.
"""
value = self._properties.get(self._PROPERTY_TO_API_FIELD["created"])
if value is not None and value != 0:
# value will be in milliseconds.
return google.cloud._helpers._datetime_from_microseconds(
1000.0 * float(value)
)
@property
def modified(self):
"""Optional[datetime.datetime]: Datetime at which the routine was
last modified (:data:`None` until set from the server).
Read-only.
"""
value = self._properties.get(self._PROPERTY_TO_API_FIELD["modified"])
if value is not None and value != 0:
# value will be in milliseconds.
return google.cloud._helpers._datetime_from_microseconds(
1000.0 * float(value)
)
@property
def language(self):
"""Optional[str]: The language of the routine.
Defaults to ``SQL``.
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["language"])
@language.setter
def language(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["language"]] = value
@property
def arguments(self):
"""List[google.cloud.bigquery.routine.RoutineArgument]: Input/output
argument of a function or a stored procedure.
In-place modification is not supported. To set, replace the entire
property value with the modified list of
:class:`~google.cloud.bigquery.routine.RoutineArgument` objects.
"""
resources = self._properties.get(self._PROPERTY_TO_API_FIELD["arguments"], [])
return [RoutineArgument.from_api_repr(resource) for resource in resources]
@arguments.setter
def arguments(self, value):
if not value:
resource = []
else:
resource = [argument.to_api_repr() for argument in value]
self._properties[self._PROPERTY_TO_API_FIELD["arguments"]] = resource
@property
def return_type(self):
"""google.cloud.bigquery_v2.types.StandardSqlDataType: Return type of
the routine.
If absent, the return type is inferred from
:attr:`~google.cloud.bigquery.routine.Routine.body` at query time in
each query that references this routine. If present, then the
evaluated result will be cast to the specified returned type at query
time.
See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#Routine.FIELDS.return_type
"""
resource = self._properties.get(self._PROPERTY_TO_API_FIELD["return_type"])
if not resource:
return resource
output = google.cloud.bigquery_v2.types.StandardSqlDataType()
raw_protobuf = json_format.ParseDict(
resource, output._pb, ignore_unknown_fields=True
)
return type(output).wrap(raw_protobuf)
@return_type.setter
def return_type(self, value):
if value:
resource = json_format.MessageToDict(value._pb)
else:
resource = None
self._properties[self._PROPERTY_TO_API_FIELD["return_type"]] = resource
@property
def imported_libraries(self):
"""List[str]: The path of the imported JavaScript libraries.
The :attr:`~google.cloud.bigquery.routine.Routine.language` must
equal ``JAVACRIPT``.
Examples:
Set the ``imported_libraries`` to a list of Google Cloud Storage
URIs.
.. code-block:: python
routine = bigquery.Routine("proj.dataset.routine_id")
routine.imported_libraries = [
"gs://cloud-samples-data/bigquery/udfs/max-value.js",
]
"""
return self._properties.get(
self._PROPERTY_TO_API_FIELD["imported_libraries"], []
)
@imported_libraries.setter
def imported_libraries(self, value):
if not value:
resource = []
else:
resource = value
self._properties[self._PROPERTY_TO_API_FIELD["imported_libraries"]] = resource
@property
def body(self):
"""str: The body of the routine."""
return self._properties.get(self._PROPERTY_TO_API_FIELD["body"])
@body.setter
def body(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["body"]] = value
@property
def description(self):
"""Optional[str]: Description of the routine (defaults to
:data:`None`).
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["description"])
@description.setter
def description(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["description"]] = value
@property
def determinism_level(self):
"""Optional[str]: (experimental) The determinism level of the JavaScript UDF
if defined.
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["determinism_level"])
@determinism_level.setter
def determinism_level(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["determinism_level"]] = value
@classmethod
def from_api_repr(cls, resource):
"""Factory: construct a routine given its API representation.
Args:
resource (Dict[str, object]):
Resource, as returned from the API.
Returns:
google.cloud.bigquery.routine.Routine:
Python object, as parsed from ``resource``.
"""
ref = cls(RoutineReference.from_api_repr(resource["routineReference"]))
ref._properties = resource
return ref
def to_api_repr(self):
"""Construct the API resource representation of this routine.
Returns:
Dict[str, object]: Routine represented as an API resource.
"""
return self._properties
def _build_resource(self, filter_fields):
"""Generate a resource for ``update``."""
return _helpers._build_resource_from_properties(self, filter_fields)
def __repr__(self):
return "Routine('{}.{}.{}')".format(
self.project, self.dataset_id, self.routine_id
)
class RoutineArgument(object):
"""Input/output argument of a function or a stored procedure.
See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#argument
Args:
``**kwargs`` (Dict):
Initial property values.
"""
_PROPERTY_TO_API_FIELD = {
"data_type": "dataType",
"kind": "argumentKind",
# Even though it's not necessary for field mapping to map when the
# property name equals the resource name, we add these here so that we
# have an exhaustive list of all properties.
"name": "name",
"mode": "mode",
}
def __init__(self, **kwargs):
self._properties = {}
for property_name in kwargs:
setattr(self, property_name, kwargs[property_name])
@property
def name(self):
"""Optional[str]: Name of this argument.
Can be absent for function return argument.
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["name"])
@name.setter
def name(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["name"]] = value
@property
def kind(self):
"""Optional[str]: The kind of argument, for example ``FIXED_TYPE`` or
``ANY_TYPE``.
See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#Argument.FIELDS.argument_kind
"""
return self._properties.get(self._PROPERTY_TO_API_FIELD["kind"])
@kind.setter
def kind(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["kind"]] = value
@property
def mode(self):
"""Optional[str]: The input/output mode of the argument."""
return self._properties.get(self._PROPERTY_TO_API_FIELD["mode"])
@mode.setter
def mode(self, value):
self._properties[self._PROPERTY_TO_API_FIELD["mode"]] = value
@property
def data_type(self):
"""Optional[google.cloud.bigquery_v2.types.StandardSqlDataType]: Type
of a variable, e.g., a function argument.
See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#Argument.FIELDS.data_type
"""
resource = self._properties.get(self._PROPERTY_TO_API_FIELD["data_type"])
if not resource:
return resource
output = google.cloud.bigquery_v2.types.StandardSqlDataType()
raw_protobuf = json_format.ParseDict(
resource, output._pb, ignore_unknown_fields=True
)
return type(output).wrap(raw_protobuf)
@data_type.setter
def data_type(self, value):
if value:
resource = json_format.MessageToDict(value._pb)
else:
resource = None
self._properties[self._PROPERTY_TO_API_FIELD["data_type"]] = resource
@classmethod
def from_api_repr(cls, resource):
"""Factory: construct a routine argument given its API representation.
Args:
resource (Dict[str, object]): Resource, as returned from the API.
Returns:
google.cloud.bigquery.routine.RoutineArgument:
Python object, as parsed from ``resource``.
"""
ref = cls()
ref._properties = resource
return ref
def to_api_repr(self):
"""Construct the API resource representation of this routine argument.
Returns:
Dict[str, object]: Routine argument represented as an API resource.
"""
return self._properties
def __eq__(self, other):
if not isinstance(other, RoutineArgument):
return NotImplemented
return self._properties == other._properties
def __ne__(self, other):
return not self == other
def __repr__(self):
all_properties = [
"{}={}".format(property_name, repr(getattr(self, property_name)))
for property_name in sorted(self._PROPERTY_TO_API_FIELD)
]
return "RoutineArgument({})".format(", ".join(all_properties))
class RoutineReference(object):
"""A pointer to a routine.
See:
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#routinereference
"""
def __init__(self):
self._properties = {}
@property
def project(self):
"""str: ID of the project containing the routine."""
return self._properties["projectId"]
@property
def dataset_id(self):
"""str: ID of dataset containing the routine."""
return self._properties["datasetId"]
@property
def routine_id(self):
"""str: The routine ID."""
return self._properties["routineId"]
@property
def path(self):
"""str: URL path for the routine's APIs."""
return "/projects/%s/datasets/%s/routines/%s" % (
self.project,
self.dataset_id,
self.routine_id,
)
@classmethod
def from_api_repr(cls, resource):
"""Factory: construct a routine reference given its API representation.
Args:
resource (Dict[str, object]):
Routine reference representation returned from the API.
Returns:
google.cloud.bigquery.routine.RoutineReference:
Routine reference parsed from ``resource``.
"""
ref = cls()
ref._properties = resource
return ref
@classmethod
def from_string(cls, routine_id, default_project=None):
"""Factory: construct a routine reference from routine ID string.
Args:
routine_id (str):
A routine ID in standard SQL format. If ``default_project``
is not specified, this must included a project ID, dataset
ID, and routine ID, each separated by ``.``.
default_project (Optional[str]):
The project ID to use when ``routine_id`` does not
include a project ID.
Returns:
google.cloud.bigquery.routine.RoutineReference:
Routine reference parsed from ``routine_id``.
Raises:
ValueError:
If ``routine_id`` is not a fully-qualified routine ID in
standard SQL format.
"""
proj, dset, routine = _helpers._parse_3_part_id(
routine_id, default_project=default_project, property_name="routine_id"
)
return cls.from_api_repr(
{"projectId": proj, "datasetId": dset, "routineId": routine}
)
def to_api_repr(self):
"""Construct the API resource representation of this routine reference.
Returns:
Dict[str, object]: Routine reference represented as an API resource.
"""
return self._properties
def __eq__(self, other):
"""Two RoutineReferences are equal if they point to the same routine."""
if not isinstance(other, RoutineReference):
return NotImplemented
return str(self) == str(other)
def __hash__(self):
return hash(str(self))
def __ne__(self, other):
return not self == other
def __repr__(self):
return "RoutineReference.from_string('{}')".format(str(self))
def __str__(self):
"""String representation of the reference.
This is a fully-qualified ID, including the project ID and dataset ID.
"""
return "{}.{}.{}".format(self.project, self.dataset_id, self.routine_id)