google-cloud-bigquery 3.31.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. google/cloud/bigquery/__init__.py +249 -0
  2. google/cloud/bigquery/_helpers.py +1102 -0
  3. google/cloud/bigquery/_http.py +47 -0
  4. google/cloud/bigquery/_job_helpers.py +600 -0
  5. google/cloud/bigquery/_pandas_helpers.py +1181 -0
  6. google/cloud/bigquery/_pyarrow_helpers.py +147 -0
  7. google/cloud/bigquery/_tqdm_helpers.py +137 -0
  8. google/cloud/bigquery/_versions_helpers.py +264 -0
  9. google/cloud/bigquery/client.py +4406 -0
  10. google/cloud/bigquery/dataset.py +1076 -0
  11. google/cloud/bigquery/dbapi/__init__.py +87 -0
  12. google/cloud/bigquery/dbapi/_helpers.py +522 -0
  13. google/cloud/bigquery/dbapi/connection.py +128 -0
  14. google/cloud/bigquery/dbapi/cursor.py +586 -0
  15. google/cloud/bigquery/dbapi/exceptions.py +58 -0
  16. google/cloud/bigquery/dbapi/types.py +96 -0
  17. google/cloud/bigquery/encryption_configuration.py +84 -0
  18. google/cloud/bigquery/enums.py +389 -0
  19. google/cloud/bigquery/exceptions.py +35 -0
  20. google/cloud/bigquery/external_config.py +1188 -0
  21. google/cloud/bigquery/format_options.py +147 -0
  22. google/cloud/bigquery/iam.py +38 -0
  23. google/cloud/bigquery/job/__init__.py +87 -0
  24. google/cloud/bigquery/job/base.py +1116 -0
  25. google/cloud/bigquery/job/copy_.py +282 -0
  26. google/cloud/bigquery/job/extract.py +271 -0
  27. google/cloud/bigquery/job/load.py +985 -0
  28. google/cloud/bigquery/job/query.py +2498 -0
  29. google/cloud/bigquery/magics/__init__.py +20 -0
  30. google/cloud/bigquery/magics/line_arg_parser/__init__.py +34 -0
  31. google/cloud/bigquery/magics/line_arg_parser/exceptions.py +25 -0
  32. google/cloud/bigquery/magics/line_arg_parser/lexer.py +200 -0
  33. google/cloud/bigquery/magics/line_arg_parser/parser.py +484 -0
  34. google/cloud/bigquery/magics/line_arg_parser/visitors.py +159 -0
  35. google/cloud/bigquery/magics/magics.py +776 -0
  36. google/cloud/bigquery/model.py +517 -0
  37. google/cloud/bigquery/opentelemetry_tracing.py +164 -0
  38. google/cloud/bigquery/py.typed +2 -0
  39. google/cloud/bigquery/query.py +1344 -0
  40. google/cloud/bigquery/retry.py +207 -0
  41. google/cloud/bigquery/routine/__init__.py +33 -0
  42. google/cloud/bigquery/routine/routine.py +744 -0
  43. google/cloud/bigquery/schema.py +896 -0
  44. google/cloud/bigquery/standard_sql.py +389 -0
  45. google/cloud/bigquery/table.py +3594 -0
  46. google/cloud/bigquery/version.py +15 -0
  47. google/cloud/bigquery_v2/__init__.py +56 -0
  48. google/cloud/bigquery_v2/types/__init__.py +54 -0
  49. google/cloud/bigquery_v2/types/encryption_config.py +48 -0
  50. google/cloud/bigquery_v2/types/model.py +1994 -0
  51. google/cloud/bigquery_v2/types/model_reference.py +57 -0
  52. google/cloud/bigquery_v2/types/standard_sql.py +156 -0
  53. google/cloud/bigquery_v2/types/table_reference.py +80 -0
  54. google_cloud_bigquery-3.31.0.dist-info/LICENSE +202 -0
  55. google_cloud_bigquery-3.31.0.dist-info/METADATA +203 -0
  56. google_cloud_bigquery-3.31.0.dist-info/RECORD +58 -0
  57. google_cloud_bigquery-3.31.0.dist-info/WHEEL +5 -0
  58. google_cloud_bigquery-3.31.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,207 @@
1
+ # Copyright 2018 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
+ from google.api_core import exceptions
16
+ from google.api_core import retry
17
+ import google.api_core.future.polling
18
+ from google.auth import exceptions as auth_exceptions # type: ignore
19
+ import requests.exceptions
20
+
21
+
22
+ _RETRYABLE_REASONS = frozenset(
23
+ ["rateLimitExceeded", "backendError", "internalError", "badGateway"]
24
+ )
25
+
26
+ _UNSTRUCTURED_RETRYABLE_TYPES = (
27
+ ConnectionError,
28
+ exceptions.TooManyRequests,
29
+ exceptions.InternalServerError,
30
+ exceptions.BadGateway,
31
+ exceptions.ServiceUnavailable,
32
+ requests.exceptions.ChunkedEncodingError,
33
+ requests.exceptions.ConnectionError,
34
+ requests.exceptions.Timeout,
35
+ auth_exceptions.TransportError,
36
+ )
37
+
38
+ _DEFAULT_RETRY_DEADLINE = 10.0 * 60.0 # 10 minutes
39
+
40
+ # Ambiguous errors (e.g. internalError, backendError, rateLimitExceeded) retry
41
+ # until the full `_DEFAULT_RETRY_DEADLINE`. This is because the
42
+ # `jobs.getQueryResults` REST API translates a job failure into an HTTP error.
43
+ #
44
+ # TODO(https://github.com/googleapis/python-bigquery/issues/1903): Investigate
45
+ # if we can fail early for ambiguous errors in `QueryJob.result()`'s call to
46
+ # the `jobs.getQueryResult` API.
47
+ #
48
+ # We need `_DEFAULT_JOB_DEADLINE` to be some multiple of
49
+ # `_DEFAULT_RETRY_DEADLINE` to allow for a few retries after the retry
50
+ # timeout is reached.
51
+ #
52
+ # Note: This multiple should actually be a multiple of
53
+ # (2 * _DEFAULT_RETRY_DEADLINE). After an ambiguous exception, the first
54
+ # call from `job_retry()` refreshes the job state without actually restarting
55
+ # the query. The second `job_retry()` actually restarts the query. For a more
56
+ # detailed explanation, see the comments where we set `restart_query_job = True`
57
+ # in `QueryJob.result()`'s inner `is_job_done()` function.
58
+ _DEFAULT_JOB_DEADLINE = 2.0 * (2.0 * _DEFAULT_RETRY_DEADLINE)
59
+
60
+
61
+ def _should_retry(exc):
62
+ """Predicate for determining when to retry.
63
+
64
+ We retry if and only if the 'reason' is 'backendError'
65
+ or 'rateLimitExceeded'.
66
+ """
67
+ if not hasattr(exc, "errors") or len(exc.errors) == 0:
68
+ # Check for unstructured error returns, e.g. from GFE
69
+ return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)
70
+
71
+ reason = exc.errors[0]["reason"]
72
+ return reason in _RETRYABLE_REASONS
73
+
74
+
75
+ DEFAULT_RETRY = retry.Retry(predicate=_should_retry, deadline=_DEFAULT_RETRY_DEADLINE)
76
+ """The default retry object.
77
+
78
+ Any method with a ``retry`` parameter will be retried automatically,
79
+ with reasonable defaults. To disable retry, pass ``retry=None``.
80
+ To modify the default retry behavior, call a ``with_XXX`` method
81
+ on ``DEFAULT_RETRY``. For example, to change the deadline to 30 seconds,
82
+ pass ``retry=bigquery.DEFAULT_RETRY.with_deadline(30)``.
83
+ """
84
+
85
+
86
+ def _should_retry_get_job_conflict(exc):
87
+ """Predicate for determining when to retry a jobs.get call after a conflict error.
88
+
89
+ Sometimes we get a 404 after a Conflict. In this case, we
90
+ have pretty high confidence that by retrying the 404, we'll
91
+ (hopefully) eventually recover the job.
92
+ https://github.com/googleapis/python-bigquery/issues/2134
93
+
94
+ Note: we may be able to extend this to user-specified predicates
95
+ after https://github.com/googleapis/python-api-core/issues/796
96
+ to tweak existing Retry object predicates.
97
+ """
98
+ return isinstance(exc, exceptions.NotFound) or _should_retry(exc)
99
+
100
+
101
+ # Pick a deadline smaller than our other deadlines since we want to timeout
102
+ # before those expire.
103
+ _DEFAULT_GET_JOB_CONFLICT_DEADLINE = _DEFAULT_RETRY_DEADLINE / 3.0
104
+ _DEFAULT_GET_JOB_CONFLICT_RETRY = retry.Retry(
105
+ predicate=_should_retry_get_job_conflict,
106
+ deadline=_DEFAULT_GET_JOB_CONFLICT_DEADLINE,
107
+ )
108
+ """Private, may be removed in future."""
109
+
110
+
111
+ # Note: Take care when updating DEFAULT_TIMEOUT to anything but None. We
112
+ # briefly had a default timeout, but even setting it at more than twice the
113
+ # theoretical server-side default timeout of 2 minutes was not enough for
114
+ # complex queries. See:
115
+ # https://github.com/googleapis/python-bigquery/issues/970#issuecomment-921934647
116
+ DEFAULT_TIMEOUT = None
117
+ """The default API timeout.
118
+
119
+ This is the time to wait per request. To adjust the total wait time, set a
120
+ deadline on the retry object.
121
+ """
122
+
123
+ job_retry_reasons = (
124
+ "rateLimitExceeded",
125
+ "backendError",
126
+ "internalError",
127
+ "jobRateLimitExceeded",
128
+ )
129
+
130
+
131
+ def _job_should_retry(exc):
132
+ # Sometimes we have ambiguous errors, such as 'backendError' which could
133
+ # be due to an API problem or a job problem. For these, make sure we retry
134
+ # our is_job_done() function.
135
+ #
136
+ # Note: This won't restart the job unless we know for sure it's because of
137
+ # the job status and set restart_query_job = True in that loop. This means
138
+ # that we might end up calling this predicate twice for the same job
139
+ # but from different paths: (1) from jobs.getQueryResults RetryError and
140
+ # (2) from translating the job error from the body of a jobs.get response.
141
+ #
142
+ # Note: If we start retrying job types other than queries where we don't
143
+ # call the problematic getQueryResults API to check the status, we need
144
+ # to provide a different predicate, as there shouldn't be ambiguous
145
+ # errors in those cases.
146
+ if isinstance(exc, exceptions.RetryError):
147
+ exc = exc.cause
148
+
149
+ # Per https://github.com/googleapis/python-bigquery/issues/1929, sometimes
150
+ # retriable errors make their way here. Because of the separate
151
+ # `restart_query_job` logic to make sure we aren't restarting non-failed
152
+ # jobs, it should be safe to continue and not totally fail our attempt at
153
+ # waiting for the query to complete.
154
+ if _should_retry(exc):
155
+ return True
156
+
157
+ if not hasattr(exc, "errors") or len(exc.errors) == 0:
158
+ return False
159
+
160
+ reason = exc.errors[0]["reason"]
161
+ return reason in job_retry_reasons
162
+
163
+
164
+ DEFAULT_JOB_RETRY = retry.Retry(
165
+ predicate=_job_should_retry, deadline=_DEFAULT_JOB_DEADLINE
166
+ )
167
+ """
168
+ The default job retry object.
169
+ """
170
+
171
+
172
+ def _query_job_insert_should_retry(exc):
173
+ # Per https://github.com/googleapis/python-bigquery/issues/2134, sometimes
174
+ # we get a 404 error. In this case, if we get this far, assume that the job
175
+ # doesn't actually exist and try again. We can't add 404 to the default
176
+ # job_retry because that happens for errors like "this table does not
177
+ # exist", which probably won't resolve with a retry.
178
+ if isinstance(exc, exceptions.RetryError):
179
+ exc = exc.cause
180
+
181
+ if isinstance(exc, exceptions.NotFound):
182
+ message = exc.message
183
+ # Don't try to retry table/dataset not found, just job not found.
184
+ # The URL contains jobs, so use whitespace to disambiguate.
185
+ return message is not None and " job" in message.lower()
186
+
187
+ return _job_should_retry(exc)
188
+
189
+
190
+ _DEFAULT_QUERY_JOB_INSERT_RETRY = retry.Retry(
191
+ predicate=_query_job_insert_should_retry,
192
+ # jobs.insert doesn't wait for the job to complete, so we don't need the
193
+ # long _DEFAULT_JOB_DEADLINE for this part.
194
+ deadline=_DEFAULT_RETRY_DEADLINE,
195
+ )
196
+ """Private, may be removed in future."""
197
+
198
+
199
+ DEFAULT_GET_JOB_TIMEOUT = 128
200
+ """
201
+ Default timeout for Client.get_job().
202
+ """
203
+
204
+ POLLING_DEFAULT_VALUE = google.api_core.future.polling.PollingFuture._DEFAULT_VALUE
205
+ """
206
+ Default value defined in google.api_core.future.polling.PollingFuture.
207
+ """
@@ -0,0 +1,33 @@
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
+ from google.cloud.bigquery.routine.routine import RoutineType
23
+ from google.cloud.bigquery.routine.routine import RemoteFunctionOptions
24
+
25
+
26
+ __all__ = (
27
+ "DeterminismLevel",
28
+ "Routine",
29
+ "RoutineArgument",
30
+ "RoutineReference",
31
+ "RoutineType",
32
+ "RemoteFunctionOptions",
33
+ )