Skip to content

Commit 9923c74

Browse files
author
Jesse
authored
Use native parameter approach by default (#277)
Signed-off-by: Jesse Whitehouse <jesse.whitehouse@databricks.com>
1 parent f56defc commit 9923c74

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

src/databricks/sql/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
apilevel = "2.0"
77
threadsafety = 1 # Threads may share the module, but not connections.
88

9-
# Python extended format codes, e.g. ...WHERE name=%(name)s
10-
# Note that when we switch to ParameterApproach.NATIVE, paramstyle will be `named`
11-
paramstyle = "pyformat"
9+
paramstyle = "named"
1210

1311

1412
class DBAPITypeObject(object):

src/databricks/sql/client.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,26 @@ def __init__(
6363
Http Bearer access token, e.g. Databricks Personal Access Token.
6464
Unless if you use auth_type=`databricks-oauth` you need to pass `access_token.
6565
Examples:
66+
```
6667
connection = sql.connect(
6768
server_hostname='dbc-12345.staging.cloud.databricks.com',
6869
http_path='sql/protocolv1/o/6789/12abc567',
6970
access_token='dabpi12345678'
7071
)
72+
```
7173
:param http_headers: An optional list of (k, v) pairs that will be set as Http headers on every request
7274
:param session_configuration: An optional dictionary of Spark session parameters. Defaults to None.
7375
Execute the SQL command `SET -v` to get a full list of available commands.
7476
:param catalog: An optional initial catalog to use. Requires DBR version 9.0+
7577
:param schema: An optional initial schema to use. Requires DBR version 9.0+
7678
7779
Other Parameters:
78-
use_inline_params: `boolean`, optional (default is True)
80+
use_inline_params: `boolean` | str, optional (default is False)
7981
When True, parameterized calls to cursor.execute() will try to render parameter values inline with the
8082
query text instead of using native bound parameters supported in DBR 14.1 and above. This connector will attempt to
81-
sanitise parameterized inputs to prevent SQL injection. Before you can switch this to False, you must
82-
update your queries to use the PEP-249 `named` paramstyle instead of the `pyformat` paramstyle used
83-
in INLINE mode.
83+
sanitise parameterized inputs to prevent SQL injection. The inline parameter approach is maintained for
84+
legacy purposes and will be deprecated in a future release. When this parameter is `True` you will see
85+
a warning log message. To suppress this log message, set `use_inline_params="silent"`.
8486
auth_type: `str`, optional
8587
`databricks-oauth` : to use oauth with fine-grained permission scopes, set to `databricks-oauth`.
8688
This is currently in private preview for Databricks accounts on AWS.
@@ -128,6 +130,7 @@ def read(self) -> Optional[OAuthToken]:
128130
own implementation of OAuthPersistence.
129131
130132
Examples:
133+
```
131134
# for development only
132135
from databricks.sql.experimental.oauth_persistence import DevOnlyFilePersistence
133136
@@ -137,6 +140,7 @@ def read(self) -> Optional[OAuthToken]:
137140
auth_type="databricks-oauth",
138141
experimental_oauth_persistence=DevOnlyFilePersistence("~/dev-oauth.json")
139142
)
143+
```
140144
141145
142146
"""
@@ -223,8 +227,36 @@ def read(self) -> Optional[OAuthToken]:
223227
logger.info("Successfully opened session " + str(self.get_session_id_hex()))
224228
self._cursors = [] # type: List[Cursor]
225229

226-
self._suppress_inline_warning = "use_inline_params" in kwargs
227-
self.use_inline_params = kwargs.get("use_inline_params", True)
230+
self.use_inline_params = self._set_use_inline_params_with_warning(
231+
kwargs.get("use_inline_params", False)
232+
)
233+
234+
def _set_use_inline_params_with_warning(self, value: Union[bool, str]):
235+
"""Valid values are True, False, and "silent"
236+
237+
False: Use native parameters
238+
True: Use inline parameters and log a warning
239+
"silent": Use inline parameters and don't log a warning
240+
"""
241+
242+
if value is False:
243+
return False
244+
245+
if value not in [True, "silent"]:
246+
raise ValueError(
247+
f"Invalid value for use_inline_params: {value}. "
248+
+ 'Valid values are True, False, and "silent"'
249+
)
250+
251+
if value is True:
252+
logger.warning(
253+
"Parameterised queries executed with this client will use the inline parameter approach."
254+
"This approach will be deprecated in a future release. Consider using native parameters."
255+
"Learn more: https://github.com/databricks/databricks-sql-python/tree/main/docs/parameters.md"
256+
'To suppress this warning, set use_inline_params="silent"'
257+
)
258+
259+
return value
228260

229261
def __enter__(self):
230262
return self
@@ -395,23 +427,7 @@ def _determine_parameter_approach(
395427
if params is None:
396428
return ParameterApproach.NONE
397429

398-
server_supports_native_approach = (
399-
self.connection.server_parameterized_queries_enabled(
400-
self.connection.protocol_version
401-
)
402-
)
403-
404430
if self.connection.use_inline_params:
405-
if (
406-
server_supports_native_approach
407-
and not self.connection._suppress_inline_warning
408-
):
409-
logger.warning(
410-
"This query will be executed with inline parameters."
411-
"Consider using native parameters."
412-
"Learn more: https://github.com/databricks/databricks-sql-python/tree/main/docs/parameters.md"
413-
"To suppress this warning, pass use_inline_params=True when creating the connection."
414-
)
415431
return ParameterApproach.INLINE
416432

417433
else:
@@ -635,7 +651,7 @@ def execute(
635651
636652
This behaviour is controlled by the `use_inline_params` argument passed when building a connection.
637653
638-
The syntax for these approaches is different:
654+
The paramstyle for these approaches is different:
639655
640656
If the connection was instantiated with use_inline_params=False, then parameters
641657
should be given in PEP-249 `named` paramstyle like :param_name

tests/e2e/test_parameterized_queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ def test_use_inline_by_default_with_warning(self, explicit_inline, caplog):
271271
cursor.execute("SELECT %(p)s", parameters={"p": 1})
272272
if explicit_inline:
273273
assert (
274-
"Consider using native parameters." not in caplog.text
274+
"Consider using native parameters." in caplog.text
275275
), "Log message should be suppressed"
276276
else:
277277
assert (
278-
"Consider using native parameters." in caplog.text
278+
"Consider using native parameters." not in caplog.text
279279
), "Log message should not be supressed"
280280

281281

0 commit comments

Comments
 (0)