Skip to content

Commit 871b395

Browse files
committed
Put in some unit tests, will add e2e
1 parent f0980ee commit 871b395

4 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/databricks/sql/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ def __del__(self):
225225
def get_session_id(self):
226226
return self.thrift_backend.handle_to_id(self._session_handle)
227227

228+
<<<<<<< HEAD
229+
=======
230+
def get_session_protocol_version(self):
231+
return self.thrift_backend.extract_protocol_version_from_handle(
232+
self._session_handle
233+
)
234+
235+
@staticmethod
236+
def server_parameterized_queries_enabled(protocolVersion):
237+
if protocolVersion and protocolVersion >= ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8:
238+
return True
239+
else:
240+
return False
241+
242+
>>>>>>> cd93869 (Put in some unit tests, will add e2e)
228243
def get_session_id_hex(self):
229244
return self.thrift_backend.handle_to_hex_id(self._session_handle)
230245

@@ -501,6 +516,13 @@ def execute(
501516
"""
502517
if parameters is None:
503518
parameters = []
519+
<<<<<<< HEAD
520+
=======
521+
elif (not self.server_parameterized_queries_enabled(self.connection._session_handle)):
522+
raise Error(
523+
"Parameterized operations are not supported by this server. DBR 14.2 is required."
524+
)
525+
>>>>>>> cd93869 (Put in some unit tests, will add e2e)
504526
else:
505527
parameters = named_parameters_to_tsparkparams(parameters)
506528

src/databricks/sql/thrift_backend.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def _check_protocol_version(self, t_open_session_resp):
506506
"SPARK_CLI_SERVICE_PROTOCOL_V2, "
507507
"instead got: {}".format(protocol_version)
508508
)
509-
509+
510510
def _check_initial_namespace(self, catalog, schema, response):
511511
if not (catalog or schema):
512512
return
@@ -569,7 +569,7 @@ def open_session(self, session_configuration, catalog, schema):
569569
response = self.make_request(self._client.OpenSession, open_session_req)
570570
self._check_initial_namespace(catalog, schema, response)
571571
self._check_protocol_version(response)
572-
return response.sessionHandle
572+
return self.get_session_handle_from_resp(response)
573573
except:
574574
self._transport.close()
575575
raise
@@ -1014,6 +1014,13 @@ def cancel_command(self, active_op_handle):
10141014
req = ttypes.TCancelOperationReq(active_op_handle)
10151015
self.make_request(self._client.CancelOperation, req)
10161016

1017+
@staticmethod
1018+
def get_session_handle_from_resp(t_open_session_resp):
1019+
sessionHandle = t_open_session_resp.sessionHandle
1020+
if sessionHandle.serverProtocolVersion == None:
1021+
sessionHandle.serverProtocolVersion = t_open_session_resp.serverProtocolVersion
1022+
return sessionHandle
1023+
10171024
@staticmethod
10181025
def handle_to_id(session_handle):
10191026
return session_handle.sessionId.guid

tests/e2e/common/parameterized_query_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def _get_one_result(self, query: str, parameters: Union[Dict, List[Dict]]) -> Tu
2929
with conn.cursor() as cursor:
3030
cursor.execute(query, parameters=parameters)
3131
return cursor.fetchone()
32+
33+
def _test_protocol_checking(self) -> Tuple:
34+
with self.connection() as conn:
35+
with conn.cursor() as cursor:
36+
cursor.execute(query, parameters=parameters)
37+
return cursor.fetchone()
3238

3339
def _quantize(self, input: Union[float, int], place_value=2) -> Decimal:
3440

tests/unit/test_parameters.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,29 @@
99
from databricks.sql.thrift_api.TCLIService.ttypes import (
1010
TSparkParameter,
1111
TSparkParameterValue,
12+
TSessionHandle,
13+
TOpenSessionResp
1214
)
1315
from databricks.sql.utils import DbSqlParameter, DbSqlType
1416
import pytest
1517

16-
from decimal import Decimal
18+
from databricks.sql.thrift_backend import ThriftBackend
19+
from databricks.sql.thrift_api.TCLIService import ttypes
1720

21+
from decimal import Decimal
22+
from databricks.sql.client import Connection
1823
from typing import List
1924

25+
class TestSessionHandleChecks(object):
26+
def test_get_session_handle(self):
27+
assert ThriftBackend.get_session_handle_from_resp(TOpenSessionResp(serverProtocolVersion=ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7, sessionHandle=TSessionHandle(1, None) )) == TSessionHandle(1, ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7)
28+
assert ThriftBackend.get_session_handle_from_resp(TOpenSessionResp(serverProtocolVersion=None, sessionHandle=TSessionHandle(1, None) )) == TSessionHandle(1, None)
29+
assert ThriftBackend.get_session_handle_from_resp(TOpenSessionResp(serverProtocolVersion=ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8, sessionHandle=TSessionHandle(1, None) )) == TSessionHandle(1, ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8)
30+
31+
def test_parameters_enabled(self):
32+
assert Connection.server_parameterized_queries_enabled(None) == False
33+
assert Connection.server_parameterized_queries_enabled(ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7) == False
34+
assert Connection.server_parameterized_queries_enabled(ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8) == True
2035

2136
class TestTSparkParameterConversion(object):
2237
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)