Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit52f1167

Browse files
author
Jesse Whitehouse
committed
Update use_inline_params so that a warning log is emitted if inline
params are used while the server supports native parameters.The log message is suppressed if the user explicitly declares use_inline_paramsSigned-off-by: Jesse Whitehouse <jesse.whitehouse@databricks.com>
1 parentf0e1a51 commit52f1167

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

‎docs/parameters.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`<placeholder>`

‎src/databricks/sql/client.py‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def __init__(
4949
session_configuration:Dict[str,Any]=None,
5050
catalog:Optional[str]=None,
5151
schema:Optional[str]=None,
52-
use_inline_params:Optional[bool]=True,
5352
**kwargs,
5453
)->None:
5554
"""
@@ -222,7 +221,9 @@ def read(self) -> Optional[OAuthToken]:
222221
self.open=True
223222
logger.info("Successfully opened session "+str(self.get_session_id_hex()))
224223
self._cursors= []# type: List[Cursor]
225-
self.use_inline_params=use_inline_params
224+
225+
self._suppress_inline_warning="use_inline_params"inkwargs
226+
self.use_inline_params=kwargs.get("use_inline_params",True)
226227

227228
def__enter__(self):
228229
returnself
@@ -383,14 +384,30 @@ def _determine_parameter_approach(self) -> ParameterApproach:
383384
Else raise an exception.
384385
385386
Returns a ParameterApproach enumeration or raises an exception
387+
388+
If inline approach is used when the server supports native approach, a warning is logged
386389
"""
387390

391+
server_supports_native_approach= (
392+
self.connection.server_parameterized_queries_enabled(
393+
self.connection.protocol_version
394+
)
395+
)
396+
388397
ifself.connection.use_inline_params:
398+
if (
399+
server_supports_native_approach
400+
andnotself.connection._suppress_inline_warning
401+
):
402+
logger.warning(
403+
"This query will be executed with inline parameters."
404+
"Consider using native parameters."
405+
"Learn more: https://github.com/databricks/databricks-sql-python/tree/main/docs/parameters.md"
406+
"To suppress this warning, pass use_inline_params=True when creating the connection."
407+
)
389408
returnParameterApproach.INLINE
390409

391-
ifself.connection.server_parameterized_queries_enabled(
392-
self.connection.protocol_version
393-
):
410+
elifserver_supports_native_approach:
394411
returnParameterApproach.NATIVE
395412
else:
396413
raiseNotSupportedError(

‎tests/e2e/test_parameterized_queries.py‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def compute_says_it_doesnt_support_native_params(self):
109109
@contextmanager
110110
defconditional_protocol_patch(self,bypass=False):
111111
"""This fixture will be removed once dogfood advertises its protocol version correctly.
112-
113-
Note that there is an equivalent patch in sqlalchemy/test/test_suite.py which should be
112+
113+
Note that there is an equivalent patch in sqlalchemy/test/test_suite.py which should be
114114
removed at the same time as this one. That one is encapsulated in a function called
115115
start_protocol_patch()"""
116116

@@ -135,7 +135,7 @@ def _inline_roundtrip(self, params: dict):
135135
SELECT_QUERY=f"SELECT{target_column} `col` FROM pysql_e2e_inline_param_test_table LIMIT 1"
136136
DELETE_QUERY="DELETE FROM pysql_e2e_inline_param_test_table"
137137

138-
withself.connection(extra_params={"use_inline_params":True})asconn:
138+
withself.connection()asconn:
139139
withconn.cursor()ascursor:
140140
cursor.execute(INSERT_QUERY,parameters=params)
141141
withconn.cursor()ascursor:
@@ -198,6 +198,28 @@ def test_protocol_too_low(self, mock_parameterized_queries_enabled):
198198
ParameterApproach.NATIVE,params,bypass_patch=True
199199
)
200200

201+
@pytest.mark.parametrize("explicit_inline", (True,False))
202+
deftest_use_inline_by_default_with_warning(self,explicit_inline,caplog):
203+
"""
204+
use_inline_params should be True by default.
205+
If a user explicitly sets use_inline_params, don't warn them about it.
206+
"""
207+
208+
extra_args= {"use_inline_params":True}ifexplicit_inlineelse {}
209+
210+
withself.connection(extra_args)asconn:
211+
withconn.cursor()ascursor:
212+
withself.conditional_protocol_patch():
213+
cursor.execute("SELECT %(p)s",parameters={"p":1})
214+
ifexplicit_inline:
215+
assert (
216+
"Consider using native parameters."notincaplog.text
217+
),"Log message should be suppressed"
218+
else:
219+
assert (
220+
"Consider using native parameters."incaplog.text
221+
),"Log message should not be supressed"
222+
201223
@both_approaches
202224
deftest_primitive_inferred_none(self,approach:ParameterApproach,inline_table):
203225
params= {"p":None}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp