Skip to content

Commit 40480ea

Browse files
authored
ENH: Add pandas_gbq.context.dialect to modify default dialect (#235)
This will allow people to revert the dialect to legacy SQL when we switch the default or use standard SQL as the default earlier.
1 parent f9aa7a0 commit 40480ea

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

pandas_gbq/gbq.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class Context(object):
170170
def __init__(self):
171171
self._credentials = None
172172
self._project = None
173+
# dialect defaults to None so that read_gbq can stop warning if set.
174+
self._dialect = None
173175

174176
@property
175177
def credentials(self):
@@ -216,6 +218,28 @@ def project(self):
216218
def project(self, value):
217219
self._project = value
218220

221+
@property
222+
def dialect(self):
223+
"""str: Default dialect to use in :func:`pandas_gbq.read_gbq`.
224+
225+
Allowed values for the BigQuery SQL syntax dialect:
226+
227+
``'legacy'``
228+
Use BigQuery's legacy SQL dialect. For more information see
229+
`BigQuery Legacy SQL Reference
230+
<https://cloud.google.com/bigquery/docs/reference/legacy-sql>`__.
231+
``'standard'``
232+
Use BigQuery's standard SQL, which is
233+
compliant with the SQL 2011 standard. For more information
234+
see `BigQuery Standard SQL Reference
235+
<https://cloud.google.com/bigquery/docs/reference/standard-sql/>`__.
236+
"""
237+
return self._dialect
238+
239+
@dialect.setter
240+
def dialect(self, value):
241+
self._dialect = value
242+
219243

220244
# Create an empty context, used to cache credentials.
221245
context = Context()
@@ -728,12 +752,17 @@ def read_gbq(
728752
df: DataFrame
729753
DataFrame representing results of query.
730754
"""
755+
global context
756+
757+
if dialect is None:
758+
dialect = context.dialect
759+
731760
if dialect is None:
732761
dialect = "legacy"
733762
warnings.warn(
734763
'The default value for dialect is changing to "standard" in a '
735-
'future version. Pass in dialect="legacy" to disable this '
736-
"warning.",
764+
'future version. Pass in dialect="legacy" or set '
765+
'pandas_gbq.context.dialect="legacy" to disable this warning.',
737766
FutureWarning,
738767
stacklevel=2,
739768
)

tests/unit/test_context.py

+18
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,21 @@ def test_read_gbq_should_save_credentials(mock_get_credentials):
3636

3737
pandas_gbq.read_gbq("SELECT 1", dialect="standard")
3838
mock_get_credentials.assert_not_called()
39+
40+
41+
def test_read_gbq_should_use_dialect(mock_bigquery_client):
42+
import pandas_gbq
43+
44+
assert pandas_gbq.context.dialect is None
45+
pandas_gbq.context.dialect = "legacy"
46+
pandas_gbq.read_gbq("SELECT 1")
47+
48+
_, kwargs = mock_bigquery_client.query.call_args
49+
assert kwargs["job_config"].use_legacy_sql == True
50+
51+
pandas_gbq.context.dialect = "standard"
52+
pandas_gbq.read_gbq("SELECT 1")
53+
54+
_, kwargs = mock_bigquery_client.query.call_args
55+
assert kwargs["job_config"].use_legacy_sql == False
56+
pandas_gbq.context.dialect = None # Reset the global state.

0 commit comments

Comments
 (0)