Skip to content

Commit b7dc189

Browse files
Niall Egansusodapop
authored andcommitted
Add arg for setting session id from the Python client
This PR adds an arg to the new CmdExec Python client to be able to explicitly set the session id instead of having to set it in a header. Author: Niall Egan <niall.egan@databricks.com>
1 parent b0afc6b commit b7dc189

File tree

1 file changed

+29
-2
lines changed
  • cmdexec/clients/python/src/databricks/sql

1 file changed

+29
-2
lines changed

cmdexec/clients/python/src/databricks/sql/client.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,32 @@
2424

2525
_TIMESTAMP_PATTERN = re.compile(r'(\d+-\d+-\d+ \d+:\d+:\d+(\.\d{,6})?)')
2626

27-
TYPES_CONVERTER = {"decimal": Decimal}
27+
28+
def _parse_timestamp(value):
29+
if type(value) is datetime.datetime:
30+
# The cmd exec server will return a datetime.datetime, so no further parsing is needed
31+
return value
32+
elif value:
33+
match = _TIMESTAMP_PATTERN.match(value)
34+
if match:
35+
if match.group(2):
36+
format = '%Y-%m-%d %H:%M:%S.%f'
37+
# use the pattern to truncate the value
38+
value = match.group()
39+
else:
40+
format = '%Y-%m-%d %H:%M:%S'
41+
value = datetime.datetime.strptime(value, format)
42+
return value
43+
else:
44+
raise Exception('Cannot convert "{}" into a datetime'.format(value))
45+
else:
46+
return None
47+
48+
49+
TYPES_CONVERTER = {
50+
"decimal": Decimal,
51+
"timestamp": _parse_timestamp,
52+
}
2853

2954

3055
class Connection:
@@ -56,6 +81,8 @@ def __init__(self, server_hostname, http_path, access_token, metadata=None, **kw
5681
# verification. If not provide, uses system truststore.
5782
# _tls_client_cert_file, _tls_client_cert_key_file
5883
# Set client SSL certificate.
84+
# _session_id
85+
# Specify the session id of the connection. For Redash use only.
5986

6087
self.host = server_hostname
6188
self.port = kwargs.get("_port", 443)
@@ -94,7 +121,7 @@ def __init__(self, server_hostname, http_path, access_token, metadata=None, **kw
94121

95122
open_session_request = messages_pb2.OpenSessionRequest(
96123
configuration={},
97-
client_session_id=None,
124+
client_session_id=kwargs.get("_session_id"),
98125
)
99126

100127
resp = self.base_client.make_request(self.base_client.stub.OpenSession,

0 commit comments

Comments
 (0)