Skip to content

Commit babdd66

Browse files
committed
Compile regex.
1 parent d9480f4 commit babdd66

File tree

1 file changed

+20
-4
lines changed
  • bigquery/google/cloud/bigquery

1 file changed

+20
-4
lines changed

bigquery/google/cloud/bigquery/job.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
_DONE_STATE = "DONE"
4747
_STOPPED_REASON = "stopped"
4848
_TIMEOUT_BUFFER_SECS = 0.1
49+
_CONTAINS_ORDER_BY = re.compile(r"ORDER\s+BY", re.IGNORECASE)
4950

5051
_ERROR_REASON_TO_EXCEPTION = {
5152
"accessDenied": http_client.FORBIDDEN,
@@ -94,11 +95,26 @@ def _error_result_to_exception(error_result):
9495

9596

9697
def _contains_order_by(query):
97-
"""Do we need to preserve the order of the query results?"""
98-
if query is None:
99-
return False
98+
"""Do we need to preserve the order of the query results?
10099
101-
return re.search(r"ORDER\s+BY", query, re.IGNORECASE) is not None
100+
This function has known false positives, such as with ordered window
101+
functions:
102+
103+
.. code-block:: sql
104+
105+
SELECT SUM(x) OVER (
106+
window_name
107+
PARTITION BY...
108+
ORDER BY...
109+
window_frame_clause)
110+
FROM ...
111+
112+
This false positive failure case means the behavior will be correct, but
113+
downloading results with the BigQuery Storage API may be slower than it
114+
otherwise would. This is preferable to the false negative case, where
115+
results are expected to be in order but are not (due to parallel reads).
116+
"""
117+
return query and _CONTAINS_ORDER_BY.search(query)
102118

103119

104120
class Compression(object):

0 commit comments

Comments
 (0)