Skip to content

Commit fd33624

Browse files
committed
Add enum-like class for routine determinism level
1 parent 505037c commit fd33624

File tree

7 files changed

+66
-12
lines changed

7 files changed

+66
-12
lines changed

docs/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Routine
110110
.. autosummary::
111111
:toctree: generated
112112

113+
routine.DeterminismLevel
113114
routine.Routine
114115
routine.RoutineArgument
115116
routine.RoutineReference

google/cloud/bigquery/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
from google.cloud.bigquery.query import StructQueryParameter
7171
from google.cloud.bigquery.query import UDFResource
7272
from google.cloud.bigquery.retry import DEFAULT_RETRY
73+
from google.cloud.bigquery.routine import DeterminismLevel
7374
from google.cloud.bigquery.routine import Routine
7475
from google.cloud.bigquery.routine import RoutineArgument
7576
from google.cloud.bigquery.routine import RoutineReference
@@ -134,6 +135,7 @@
134135
"Compression",
135136
"CreateDisposition",
136137
"DestinationFormat",
138+
"DeterminismLevel",
137139
"ExternalSourceFormat",
138140
"Encoding",
139141
"QueryPriority",

google/cloud/bigquery/enums.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,20 @@ class WriteDisposition(object):
231231
WRITE_EMPTY = "WRITE_EMPTY"
232232
"""If the table already exists and contains data, a 'duplicate' error is
233233
returned in the job result."""
234+
235+
236+
class DeterminismLevel:
237+
"""Specifies determinism level for JavaScript user-defined functions (UDFs).
238+
239+
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#DeterminismLevel
240+
"""
241+
242+
DETERMINISM_LEVEL_UNSPECIFIED = "DETERMINISM_LEVEL_UNSPECIFIED"
243+
"""The determinism of the UDF is unspecified."""
244+
245+
DETERMINISTIC = "DETERMINISTIC"
246+
"""The UDF is deterministic, meaning that 2 function calls with the same inputs
247+
always produce the same result, even across 2 query runs."""
248+
249+
NOT_DETERMINISTIC = "NOT_DETERMINISTIC"
250+
"""The UDF is not deterministic."""
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""User-Defined Routines."""
16+
17+
18+
from google.cloud.bigquery.enums import DeterminismLevel
19+
from google.cloud.bigquery.routine.routine import Routine
20+
from google.cloud.bigquery.routine.routine import RoutineArgument
21+
from google.cloud.bigquery.routine.routine import RoutineReference
22+
23+
24+
__all__ = (
25+
"DeterminismLevel",
26+
"Routine",
27+
"RoutineArgument",
28+
"RoutineReference",
29+
)

tests/system/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2682,7 +2682,7 @@ def test_create_routine(self):
26822682
)
26832683
]
26842684
routine.body = "return maxValue(arr)"
2685-
routine.determinism_level = "DETERMINISTIC"
2685+
routine.determinism_level = bigquery.DeterminismLevel.DETERMINISTIC
26862686
query_string = "SELECT `{}`([-100.0, 3.14, 100.0, 42.0]) as max_value;".format(
26872687
str(routine.reference)
26882688
)

tests/unit/routine/test_routine.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pytest
1919

2020
import google.cloud._helpers
21+
from google.cloud import bigquery
2122
from google.cloud import bigquery_v2
2223

2324

@@ -73,7 +74,7 @@ def test_ctor_w_properties(target_class):
7374
)
7475
type_ = "SCALAR_FUNCTION"
7576
description = "A routine description."
76-
determinism_level = "NOT_DETERMINISTIC"
77+
determinism_level = bigquery.DeterminismLevel.NOT_DETERMINISTIC
7778

7879
actual_routine = target_class(
7980
routine_id,
@@ -94,7 +95,9 @@ def test_ctor_w_properties(target_class):
9495
assert actual_routine.return_type == return_type
9596
assert actual_routine.type_ == type_
9697
assert actual_routine.description == description
97-
assert actual_routine.determinism_level == "NOT_DETERMINISTIC"
98+
assert (
99+
actual_routine.determinism_level == bigquery.DeterminismLevel.NOT_DETERMINISTIC
100+
)
98101

99102

100103
def test_from_api_repr(target_class):
@@ -123,7 +126,7 @@ def test_from_api_repr(target_class):
123126
"routineType": "SCALAR_FUNCTION",
124127
"someNewField": "someValue",
125128
"description": "A routine description.",
126-
"determinismLevel": "DETERMINISTIC",
129+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISTIC,
127130
}
128131
actual_routine = target_class.from_api_repr(resource)
129132

@@ -214,7 +217,7 @@ def test_from_api_repr_w_unknown_fields(target_class):
214217
"returnType": {"typeKind": "INT64"},
215218
"routineType": "SCALAR_FUNCTION",
216219
"description": "A routine description.",
217-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
220+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
218221
},
219222
["arguments"],
220223
{"arguments": [{"name": "x", "dataType": {"typeKind": "INT64"}}]},
@@ -227,7 +230,7 @@ def test_from_api_repr_w_unknown_fields(target_class):
227230
"returnType": {"typeKind": "INT64"},
228231
"routineType": "SCALAR_FUNCTION",
229232
"description": "A routine description.",
230-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
233+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
231234
},
232235
["body"],
233236
{"definitionBody": "x * 3"},
@@ -240,7 +243,7 @@ def test_from_api_repr_w_unknown_fields(target_class):
240243
"returnType": {"typeKind": "INT64"},
241244
"routineType": "SCALAR_FUNCTION",
242245
"description": "A routine description.",
243-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
246+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
244247
},
245248
["language"],
246249
{"language": "SQL"},
@@ -253,7 +256,7 @@ def test_from_api_repr_w_unknown_fields(target_class):
253256
"returnType": {"typeKind": "INT64"},
254257
"routineType": "SCALAR_FUNCTION",
255258
"description": "A routine description.",
256-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
259+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
257260
},
258261
["return_type"],
259262
{"returnType": {"typeKind": "INT64"}},
@@ -266,7 +269,7 @@ def test_from_api_repr_w_unknown_fields(target_class):
266269
"returnType": {"typeKind": "INT64"},
267270
"routineType": "SCALAR_FUNCTION",
268271
"description": "A routine description.",
269-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
272+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
270273
},
271274
["type_"],
272275
{"routineType": "SCALAR_FUNCTION"},
@@ -279,7 +282,7 @@ def test_from_api_repr_w_unknown_fields(target_class):
279282
"returnType": {"typeKind": "INT64"},
280283
"routineType": "SCALAR_FUNCTION",
281284
"description": "A routine description.",
282-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
285+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
283286
},
284287
["description"],
285288
{"description": "A routine description."},
@@ -292,10 +295,12 @@ def test_from_api_repr_w_unknown_fields(target_class):
292295
"returnType": {"typeKind": "INT64"},
293296
"routineType": "SCALAR_FUNCTION",
294297
"description": "A routine description.",
295-
"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED",
298+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED,
296299
},
297300
["determinism_level"],
298-
{"determinismLevel": "DETERMINISM_LEVEL_UNSPECIFIED"},
301+
{
302+
"determinismLevel": bigquery.DeterminismLevel.DETERMINISM_LEVEL_UNSPECIFIED
303+
},
299304
),
300305
(
301306
{},

0 commit comments

Comments
 (0)