-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy paththrift_backend.py
1108 lines (976 loc) · 44.3 KB
/
thrift_backend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from decimal import Decimal
import errno
import logging
import math
import time
import uuid
import threading
from typing import List, Union
from databricks.sql.thrift_api.TCLIService.ttypes import TOperationState
try:
import pyarrow
except ImportError:
pyarrow = None
import thrift.transport.THttpClient
import thrift.protocol.TBinaryProtocol
import thrift.transport.TSocket
import thrift.transport.TTransport
import urllib3.exceptions
import databricks.sql.auth.thrift_http_client
from databricks.sql.auth.thrift_http_client import CommandType
from databricks.sql.auth.authenticators import AuthProvider
from databricks.sql.thrift_api.TCLIService import TCLIService, ttypes
from databricks.sql import *
from databricks.sql.exc import MaxRetryDurationError
from databricks.sql.thrift_api.TCLIService.TCLIService import (
Client as TCLIServiceClient,
)
from databricks.sql.utils import (
ExecuteResponse,
_bound,
RequestErrorInfo,
NoRetryReason,
ResultSetQueueFactory,
convert_arrow_based_set_to_arrow_table,
convert_decimals_in_arrow_table,
convert_column_based_set_to_arrow_table,
)
from databricks.sql.types import SSLOptions
logger = logging.getLogger(__name__)
unsafe_logger = logging.getLogger("databricks.sql.unsafe")
unsafe_logger.setLevel(logging.DEBUG)
# To capture these logs in client code, add a non-NullHandler.
# See our e2e test suite for an example with logging.FileHandler
unsafe_logger.addHandler(logging.NullHandler())
# Disable propagation so that handlers for `databricks.sql` don't pick up these messages
unsafe_logger.propagate = False
THRIFT_ERROR_MESSAGE_HEADER = "x-thriftserver-error-message"
DATABRICKS_ERROR_OR_REDIRECT_HEADER = "x-databricks-error-or-redirect-message"
DATABRICKS_REASON_HEADER = "x-databricks-reason-phrase"
TIMESTAMP_AS_STRING_CONFIG = "spark.thriftserver.arrowBasedRowSet.timestampAsString"
DEFAULT_SOCKET_TIMEOUT = float(900)
# see Connection.__init__ for parameter descriptions.
# - Min/Max avoids unsustainable configs (sane values are far more constrained)
# - 900s attempts-duration lines up w ODBC/JDBC drivers (for cluster startup > 10 mins)
_retry_policy = { # (type, default, min, max)
"_retry_delay_min": (float, 1, 0.1, 60),
"_retry_delay_max": (float, 60, 5, 3600),
"_retry_stop_after_attempts_count": (int, 30, 1, 60),
"_retry_stop_after_attempts_duration": (float, 900, 1, 86400),
"_retry_delay_default": (float, 5, 1, 60),
}
class ThriftBackend:
CLOSED_OP_STATE = ttypes.TOperationState.CLOSED_STATE
ERROR_OP_STATE = ttypes.TOperationState.ERROR_STATE
_retry_delay_min: float
_retry_delay_max: float
_retry_stop_after_attempts_count: int
_retry_stop_after_attempts_duration: float
_retry_delay_default: float
def __init__(
self,
server_hostname: str,
port,
http_path: str,
http_headers,
auth_provider: AuthProvider,
ssl_options: SSLOptions,
staging_allowed_local_path: Union[None, str, List[str]] = None,
**kwargs,
):
# Internal arguments in **kwargs:
# _user_agent_entry
# Tag to add to User-Agent header. For use by partners.
# _username, _password
# Username and password Basic authentication (no official support)
# _connection_uri
# Overrides server_hostname and http_path.
# RETRY/ATTEMPT POLICY
# _retry_delay_min (default: 1)
# _retry_delay_max (default: 60)
# {min,max} pre-retry delay bounds
# _retry_delay_default (default: 5)
# Only used when GetOperationStatus fails due to a TCP/OS Error.
# _retry_stop_after_attempts_count (default: 30)
# total max attempts during retry sequence
# _retry_stop_after_attempts_duration (default: 900)
# total max wait duration during retry sequence
# (Note this will stop _before_ intentionally exceeding; thus if the
# next calculated pre-retry delay would go past
# _retry_stop_after_attempts_duration, stop now.)
#
# _retry_stop_after_attempts_count
# The maximum number of times we should retry retryable requests (defaults to 24)
# _retry_dangerous_codes
# An iterable of integer HTTP status codes. ExecuteStatement commands will be retried if these codes are received.
# (defaults to [])
# _socket_timeout
# The timeout in seconds for socket send, recv and connect operations. Should be a positive float or integer.
# (defaults to 900)
# _enable_v3_retries
# Whether to use the DatabricksRetryPolicy implemented in urllib3
# (defaults to True)
# _retry_max_redirects
# An integer representing the maximum number of redirects to follow for a request.
# This number must be <= _retry_stop_after_attempts_count.
# (defaults to None)
# max_download_threads
# Number of threads for handling cloud fetch downloads. Defaults to 10
port = port or 443
if kwargs.get("_connection_uri"):
uri = kwargs.get("_connection_uri")
elif server_hostname and http_path:
uri = "{host}:{port}/{path}".format(
host=server_hostname.rstrip("/"), port=port, path=http_path.lstrip("/")
)
if not uri.startswith("https://"):
uri = "https://" + uri
else:
raise ValueError("No valid connection settings.")
self.staging_allowed_local_path = staging_allowed_local_path
self._initialize_retry_args(kwargs)
self._use_arrow_native_complex_types = kwargs.get(
"_use_arrow_native_complex_types", True
)
self._use_arrow_native_decimals = kwargs.get("_use_arrow_native_decimals", True)
self._use_arrow_native_timestamps = kwargs.get(
"_use_arrow_native_timestamps", True
)
# Cloud fetch
self.max_download_threads = kwargs.get("max_download_threads", 10)
self._ssl_options = ssl_options
self._auth_provider = auth_provider
# Connector version 3 retry approach
self.enable_v3_retries = kwargs.get("_enable_v3_retries", True)
if not self.enable_v3_retries:
logger.warning(
"Legacy retry behavior is enabled for this connection."
" This behaviour is deprecated and will be removed in a future release."
)
self.force_dangerous_codes = kwargs.get("_retry_dangerous_codes", [])
additional_transport_args = {}
_max_redirects: Union[None, int] = kwargs.get("_retry_max_redirects")
if _max_redirects:
if _max_redirects > self._retry_stop_after_attempts_count:
logger.warn(
"_retry_max_redirects > _retry_stop_after_attempts_count so it will have no affect!"
)
urllib3_kwargs = {"redirect": _max_redirects}
else:
urllib3_kwargs = {}
if self.enable_v3_retries:
self.retry_policy = databricks.sql.auth.thrift_http_client.DatabricksRetryPolicy(
delay_min=self._retry_delay_min,
delay_max=self._retry_delay_max,
stop_after_attempts_count=self._retry_stop_after_attempts_count,
stop_after_attempts_duration=self._retry_stop_after_attempts_duration,
delay_default=self._retry_delay_default,
force_dangerous_codes=self.force_dangerous_codes,
urllib3_kwargs=urllib3_kwargs,
)
additional_transport_args["retry_policy"] = self.retry_policy
self._transport = databricks.sql.auth.thrift_http_client.THttpClient(
auth_provider=self._auth_provider,
uri_or_host=uri,
ssl_options=self._ssl_options,
**additional_transport_args, # type: ignore
)
timeout = kwargs.get("_socket_timeout", DEFAULT_SOCKET_TIMEOUT)
# setTimeout defaults to 15 minutes and is expected in ms
self._transport.setTimeout(timeout and (float(timeout) * 1000.0))
self._transport.setCustomHeaders(dict(http_headers))
protocol = thrift.protocol.TBinaryProtocol.TBinaryProtocol(self._transport)
self._client = TCLIService.Client(protocol)
try:
self._transport.open()
except:
self._transport.close()
raise
self._request_lock = threading.RLock()
# TODO: Move this bounding logic into DatabricksRetryPolicy for v3 (PECO-918)
def _initialize_retry_args(self, kwargs):
# Configure retries & timing: use user-settings or defaults, and bound
# by policy. Log.warn when given param gets restricted.
for key, (type_, default, min, max) in _retry_policy.items():
given_or_default = type_(kwargs.get(key, default))
bound = _bound(min, max, given_or_default)
setattr(self, key, bound)
logger.debug(
"retry parameter: {} given_or_default {}".format(key, given_or_default)
)
if bound != given_or_default:
logger.warning(
"Override out of policy retry parameter: "
+ "{} given {}, restricted to {}".format(
key, given_or_default, bound
)
)
# Fail on retry delay min > max; consider later adding fail on min > duration?
if (
self._retry_stop_after_attempts_count > 1
and self._retry_delay_min > self._retry_delay_max
):
raise ValueError(
"Invalid configuration enables retries with retry delay min(={}) > max(={})".format(
self._retry_delay_min, self._retry_delay_max
)
)
@staticmethod
def _check_response_for_error(response):
if response.status and response.status.statusCode in [
ttypes.TStatusCode.ERROR_STATUS,
ttypes.TStatusCode.INVALID_HANDLE_STATUS,
]:
raise DatabaseError(response.status.errorMessage)
@staticmethod
def _extract_error_message_from_headers(headers):
err_msg = ""
if THRIFT_ERROR_MESSAGE_HEADER in headers:
err_msg = headers[THRIFT_ERROR_MESSAGE_HEADER]
if DATABRICKS_ERROR_OR_REDIRECT_HEADER in headers:
if (
err_msg
): # We don't expect both to be set, but log both here just in case
err_msg = "Thriftserver error: {}, Databricks error: {}".format(
err_msg, headers[DATABRICKS_ERROR_OR_REDIRECT_HEADER]
)
else:
err_msg = headers[DATABRICKS_ERROR_OR_REDIRECT_HEADER]
if DATABRICKS_REASON_HEADER in headers:
err_msg += ": " + headers[DATABRICKS_REASON_HEADER]
if not err_msg:
# if authentication token is invalid we need this branch
if DATABRICKS_REASON_HEADER in headers:
err_msg += ": " + headers[DATABRICKS_REASON_HEADER]
return err_msg
def _handle_request_error(self, error_info, attempt, elapsed):
max_attempts = self._retry_stop_after_attempts_count
max_duration_s = self._retry_stop_after_attempts_duration
if (
error_info.retry_delay is not None
and elapsed + error_info.retry_delay > max_duration_s
):
no_retry_reason = NoRetryReason.OUT_OF_TIME
elif error_info.retry_delay is not None and attempt >= max_attempts:
no_retry_reason = NoRetryReason.OUT_OF_ATTEMPTS
elif error_info.retry_delay is None:
no_retry_reason = NoRetryReason.NOT_RETRYABLE
else:
no_retry_reason = None
full_error_info_context = error_info.full_info_logging_context(
no_retry_reason, attempt, max_attempts, elapsed, max_duration_s
)
if no_retry_reason is not None:
user_friendly_error_message = error_info.user_friendly_error_message(
no_retry_reason, attempt, elapsed
)
network_request_error = RequestError(
user_friendly_error_message, full_error_info_context, error_info.error
)
logger.info(network_request_error.message_with_context())
raise network_request_error
logger.info(
"Retrying request after error in {} seconds: {}".format(
error_info.retry_delay, full_error_info_context
)
)
time.sleep(error_info.retry_delay)
# FUTURE: Consider moving to https://github.com/litl/backoff or
# https://github.com/jd/tenacity for retry logic.
def make_request(self, method, request):
"""Execute given request, attempting retries when
1. Receiving HTTP 429/503 from server
2. OSError is raised during a GetOperationStatus
For delay between attempts, honor the given Retry-After header, but with bounds.
Use lower bound of expontial-backoff based on _retry_delay_min,
and upper bound of _retry_delay_max.
Will stop retry attempts if total elapsed time + next retry delay would exceed
_retry_stop_after_attempts_duration.
"""
# basic strategy: build range iterator rep'ing number of available
# retries. bounds can be computed from there. iterate over it with
# retries until success or final failure achieved.
t0 = time.time()
def get_elapsed():
return time.time() - t0
def bound_retry_delay(attempt, proposed_delay):
"""bound delay (seconds) by [min_delay*1.5^(attempt-1), max_delay]"""
delay = int(proposed_delay)
delay = max(delay, self._retry_delay_min * math.pow(1.5, attempt - 1))
delay = min(delay, self._retry_delay_max)
return delay
def extract_retry_delay(attempt):
# encapsulate retry checks, returns None || delay-in-secs
# Retry IFF 429/503 code + Retry-After header set
http_code = getattr(self._transport, "code", None)
retry_after = getattr(self._transport, "headers", {}).get("Retry-After", 1)
if http_code in [429, 503]:
# bound delay (seconds) by [min_delay*1.5^(attempt-1), max_delay]
return bound_retry_delay(attempt, int(retry_after))
return None
def attempt_request(attempt):
# splits out lockable attempt, from delay & retry loop
# returns tuple: (method_return, delay_fn(), error, error_message)
# - non-None method_return -> success, return and be done
# - non-None retry_delay -> sleep delay before retry
# - error, error_message always set when available
error, error_message, retry_delay = None, None, None
try:
this_method_name = getattr(method, "__name__")
logger.debug("Sending request: {}(<REDACTED>)".format(this_method_name))
unsafe_logger.debug("Sending request: {}".format(request))
# These three lines are no-ops if the v3 retry policy is not in use
if self.enable_v3_retries:
this_command_type = CommandType.get(this_method_name)
self._transport.set_retry_command_type(this_command_type)
self._transport.startRetryTimer()
response = method(request)
# We need to call type(response) here because thrift doesn't implement __name__ attributes for thrift responses
logger.debug(
"Received response: {}(<REDACTED>)".format(type(response).__name__)
)
unsafe_logger.debug("Received response: {}".format(response))
return response
except urllib3.exceptions.HTTPError as err:
# retry on timeout. Happens a lot in Azure and it is safe as data has not been sent to server yet
# TODO: don't use exception handling for GOS polling...
gos_name = TCLIServiceClient.GetOperationStatus.__name__
if method.__name__ == gos_name:
delay_default = (
self.enable_v3_retries
and self.retry_policy.delay_default
or self._retry_delay_default
)
retry_delay = bound_retry_delay(attempt, delay_default)
logger.info(
f"GetOperationStatus failed with HTTP error and will be retried: {str(err)}"
)
else:
raise err
except OSError as err:
error = err
error_message = str(err)
# fmt: off
# The built-in errno package encapsulates OSError codes, which are OS-specific.
# log.info for errors we believe are not unusual or unexpected. log.warn for
# for others like EEXIST, EBADF, ERANGE which are not expected in this context.
#
# I manually tested this retry behaviour using mitmweb and confirmed that
# GetOperationStatus requests are retried when I forced network connection
# interruptions / timeouts / reconnects. See #24 for more info.
# | Debian | Darwin |
info_errs = [ # |--------|--------|
errno.ESHUTDOWN, # | 32 | 32 |
errno.EAFNOSUPPORT, # | 97 | 47 |
errno.ECONNRESET, # | 104 | 54 |
errno.ETIMEDOUT, # | 110 | 60 |
]
gos_name = TCLIServiceClient.GetOperationStatus.__name__
# retry on timeout. Happens a lot in Azure and it is safe as data has not been sent to server yet
if method.__name__ == gos_name or err.errno == errno.ETIMEDOUT:
retry_delay = bound_retry_delay(attempt, self._retry_delay_default)
# fmt: on
log_string = f"{gos_name} failed with code {err.errno} and will attempt to retry"
if err.errno in info_errs:
logger.info(log_string)
else:
logger.warning(log_string)
except Exception as err:
error = err
retry_delay = extract_retry_delay(attempt)
error_message = ThriftBackend._extract_error_message_from_headers(
getattr(self._transport, "headers", {})
)
finally:
# Calling `close()` here releases the active HTTP connection back to the pool
self._transport.close()
return RequestErrorInfo(
error=error,
error_message=error_message,
retry_delay=retry_delay,
http_code=getattr(self._transport, "code", None),
method=method.__name__,
request=request,
)
# The real work:
# - for each available attempt:
# lock-and-attempt
# return on success
# if available: bounded delay and retry
# if not: raise error
max_attempts = self._retry_stop_after_attempts_count
# use index-1 counting for logging/human consistency
for attempt in range(1, max_attempts + 1):
# We have a lock here because .cancel can be called from a separate thread.
# We do not want threads to be simultaneously sharing the Thrift Transport
# because we use its state to determine retries
with self._request_lock:
response_or_error_info = attempt_request(attempt)
elapsed = get_elapsed()
# conditions: success, non-retry-able, no-attempts-left, no-time-left, delay+retry
if not isinstance(response_or_error_info, RequestErrorInfo):
# log nothing here, presume that main request logging covers
response = response_or_error_info
ThriftBackend._check_response_for_error(response)
return response
error_info = response_or_error_info
# The error handler will either sleep or throw an exception
self._handle_request_error(error_info, attempt, elapsed)
def _check_protocol_version(self, t_open_session_resp):
protocol_version = t_open_session_resp.serverProtocolVersion
if protocol_version < ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V2:
raise OperationalError(
"Error: expected server to use a protocol version >= "
"SPARK_CLI_SERVICE_PROTOCOL_V2, "
"instead got: {}".format(protocol_version)
)
def _check_initial_namespace(self, catalog, schema, response):
if not (catalog or schema):
return
if (
response.serverProtocolVersion
< ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V4
):
raise InvalidServerResponseError(
"Setting initial namespace not supported by the DBR version, "
"Please use a Databricks SQL endpoint or a cluster with DBR >= 9.0."
)
if catalog:
if not response.canUseMultipleCatalogs:
raise InvalidServerResponseError(
"Unexpected response from server: Trying to set initial catalog to {}, "
+ "but server does not support multiple catalogs.".format(catalog) # type: ignore
)
def _check_session_configuration(self, session_configuration):
# This client expects timetampsAsString to be false, so we do not allow users to modify that
if (
session_configuration.get(TIMESTAMP_AS_STRING_CONFIG, "false").lower()
!= "false"
):
raise Error(
"Invalid session configuration: {} cannot be changed "
"while using the Databricks SQL connector, it must be false not {}".format(
TIMESTAMP_AS_STRING_CONFIG,
session_configuration[TIMESTAMP_AS_STRING_CONFIG],
)
)
def open_session(self, session_configuration, catalog, schema):
try:
self._transport.open()
session_configuration = {
k: str(v) for (k, v) in (session_configuration or {}).items()
}
self._check_session_configuration(session_configuration)
# We want to receive proper Timestamp arrow types.
# We set it also in confOverlay in TExecuteStatementReq on a per query basic,
# but it doesn't hurt to also set for the whole session.
session_configuration[TIMESTAMP_AS_STRING_CONFIG] = "false"
if catalog or schema:
initial_namespace = ttypes.TNamespace(
catalogName=catalog, schemaName=schema
)
else:
initial_namespace = None
open_session_req = ttypes.TOpenSessionReq(
client_protocol_i64=ttypes.TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7,
client_protocol=None,
initialNamespace=initial_namespace,
canUseMultipleCatalogs=True,
configuration=session_configuration,
)
response = self.make_request(self._client.OpenSession, open_session_req)
self._check_initial_namespace(catalog, schema, response)
self._check_protocol_version(response)
return response
except:
self._transport.close()
raise
def close_session(self, session_handle) -> None:
req = ttypes.TCloseSessionReq(sessionHandle=session_handle)
try:
self.make_request(self._client.CloseSession, req)
finally:
self._transport.close()
def _check_command_not_in_error_or_closed_state(
self, op_handle, get_operations_resp
):
if get_operations_resp.operationState == ttypes.TOperationState.ERROR_STATE:
if get_operations_resp.displayMessage:
raise ServerOperationError(
get_operations_resp.displayMessage,
{
"operation-id": op_handle
and self.guid_to_hex_id(op_handle.operationId.guid),
"diagnostic-info": get_operations_resp.diagnosticInfo,
},
)
else:
raise ServerOperationError(
get_operations_resp.errorMessage,
{
"operation-id": op_handle
and self.guid_to_hex_id(op_handle.operationId.guid),
"diagnostic-info": None,
},
)
elif get_operations_resp.operationState == ttypes.TOperationState.CLOSED_STATE:
raise DatabaseError(
"Command {} unexpectedly closed server side".format(
op_handle and self.guid_to_hex_id(op_handle.operationId.guid)
),
{
"operation-id": op_handle
and self.guid_to_hex_id(op_handle.operationId.guid)
},
)
def _poll_for_status(self, op_handle):
req = ttypes.TGetOperationStatusReq(
operationHandle=op_handle,
getProgressUpdate=False,
)
return self.make_request(self._client.GetOperationStatus, req)
def _create_arrow_table(self, t_row_set, lz4_compressed, schema_bytes, description):
if t_row_set.columns is not None:
(
arrow_table,
num_rows,
) = convert_column_based_set_to_arrow_table(t_row_set.columns, description)
elif t_row_set.arrowBatches is not None:
(arrow_table, num_rows,) = convert_arrow_based_set_to_arrow_table(
t_row_set.arrowBatches, lz4_compressed, schema_bytes
)
else:
raise OperationalError("Unsupported TRowSet instance {}".format(t_row_set))
return convert_decimals_in_arrow_table(arrow_table, description), num_rows
def _get_metadata_resp(self, op_handle):
req = ttypes.TGetResultSetMetadataReq(operationHandle=op_handle)
return self.make_request(self._client.GetResultSetMetadata, req)
@staticmethod
def _hive_schema_to_arrow_schema(t_table_schema):
def map_type(t_type_entry):
if t_type_entry.primitiveEntry:
return {
ttypes.TTypeId.BOOLEAN_TYPE: pyarrow.bool_(),
ttypes.TTypeId.TINYINT_TYPE: pyarrow.int8(),
ttypes.TTypeId.SMALLINT_TYPE: pyarrow.int16(),
ttypes.TTypeId.INT_TYPE: pyarrow.int32(),
ttypes.TTypeId.BIGINT_TYPE: pyarrow.int64(),
ttypes.TTypeId.FLOAT_TYPE: pyarrow.float32(),
ttypes.TTypeId.DOUBLE_TYPE: pyarrow.float64(),
ttypes.TTypeId.STRING_TYPE: pyarrow.string(),
ttypes.TTypeId.TIMESTAMP_TYPE: pyarrow.timestamp("us", None),
ttypes.TTypeId.BINARY_TYPE: pyarrow.binary(),
ttypes.TTypeId.ARRAY_TYPE: pyarrow.string(),
ttypes.TTypeId.MAP_TYPE: pyarrow.string(),
ttypes.TTypeId.STRUCT_TYPE: pyarrow.string(),
ttypes.TTypeId.UNION_TYPE: pyarrow.string(),
ttypes.TTypeId.USER_DEFINED_TYPE: pyarrow.string(),
ttypes.TTypeId.DECIMAL_TYPE: pyarrow.string(),
ttypes.TTypeId.NULL_TYPE: pyarrow.null(),
ttypes.TTypeId.DATE_TYPE: pyarrow.date32(),
ttypes.TTypeId.VARCHAR_TYPE: pyarrow.string(),
ttypes.TTypeId.CHAR_TYPE: pyarrow.string(),
ttypes.TTypeId.INTERVAL_YEAR_MONTH_TYPE: pyarrow.string(),
ttypes.TTypeId.INTERVAL_DAY_TIME_TYPE: pyarrow.string(),
}[t_type_entry.primitiveEntry.type]
else:
# Current thriftserver implementation should always return a primitiveEntry,
# even for complex types
raise OperationalError(
"Thrift protocol error: t_type_entry not a primitiveEntry"
)
def convert_col(t_column_desc):
return pyarrow.field(
t_column_desc.columnName, map_type(t_column_desc.typeDesc.types[0])
)
return pyarrow.schema([convert_col(col) for col in t_table_schema.columns])
@staticmethod
def _col_to_description(col):
type_entry = col.typeDesc.types[0]
if type_entry.primitiveEntry:
name = ttypes.TTypeId._VALUES_TO_NAMES[type_entry.primitiveEntry.type]
# Drop _TYPE suffix
cleaned_type = (name[:-5] if name.endswith("_TYPE") else name).lower()
else:
raise OperationalError(
"Thrift protocol error: t_type_entry not a primitiveEntry"
)
if type_entry.primitiveEntry.type == ttypes.TTypeId.DECIMAL_TYPE:
qualifiers = type_entry.primitiveEntry.typeQualifiers.qualifiers
if qualifiers and "precision" in qualifiers and "scale" in qualifiers:
precision, scale = (
qualifiers["precision"].i32Value,
qualifiers["scale"].i32Value,
)
else:
raise OperationalError(
"Decimal type did not provide typeQualifier precision, scale in "
"primitiveEntry {}".format(type_entry.primitiveEntry)
)
else:
precision, scale = None, None
return col.columnName, cleaned_type, None, None, precision, scale, None
@staticmethod
def _hive_schema_to_description(t_table_schema):
return [
ThriftBackend._col_to_description(col) for col in t_table_schema.columns
]
def _results_message_to_execute_response(self, resp, operation_state):
if resp.directResults and resp.directResults.resultSetMetadata:
t_result_set_metadata_resp = resp.directResults.resultSetMetadata
else:
t_result_set_metadata_resp = self._get_metadata_resp(resp.operationHandle)
if t_result_set_metadata_resp.resultFormat not in [
ttypes.TSparkRowSetType.ARROW_BASED_SET,
ttypes.TSparkRowSetType.COLUMN_BASED_SET,
ttypes.TSparkRowSetType.URL_BASED_SET,
]:
raise OperationalError(
"Expected results to be in Arrow or column based format, "
"instead they are: {}".format(
ttypes.TSparkRowSetType._VALUES_TO_NAMES[
t_result_set_metadata_resp.resultFormat
]
)
)
direct_results = resp.directResults
has_been_closed_server_side = direct_results and direct_results.closeOperation
has_more_rows = (
(not direct_results)
or (not direct_results.resultSet)
or direct_results.resultSet.hasMoreRows
)
description = self._hive_schema_to_description(
t_result_set_metadata_resp.schema
)
if pyarrow:
schema_bytes = (
t_result_set_metadata_resp.arrowSchema
or self._hive_schema_to_arrow_schema(t_result_set_metadata_resp.schema)
.serialize()
.to_pybytes()
)
else:
schema_bytes = None
lz4_compressed = t_result_set_metadata_resp.lz4Compressed
is_staging_operation = t_result_set_metadata_resp.isStagingOperation
if direct_results and direct_results.resultSet:
assert direct_results.resultSet.results.startRowOffset == 0
assert direct_results.resultSetMetadata
arrow_queue_opt = ResultSetQueueFactory.build_queue(
row_set_type=t_result_set_metadata_resp.resultFormat,
t_row_set=direct_results.resultSet.results,
arrow_schema_bytes=schema_bytes,
max_download_threads=self.max_download_threads,
lz4_compressed=lz4_compressed,
description=description,
ssl_options=self._ssl_options,
)
else:
arrow_queue_opt = None
return ExecuteResponse(
arrow_queue=arrow_queue_opt,
status=operation_state,
has_been_closed_server_side=has_been_closed_server_side,
has_more_rows=has_more_rows,
lz4_compressed=lz4_compressed,
is_staging_operation=is_staging_operation,
command_handle=resp.operationHandle,
description=description,
arrow_schema_bytes=schema_bytes,
)
def get_execution_result(self, op_handle, cursor):
assert op_handle is not None
req = ttypes.TFetchResultsReq(
operationHandle=ttypes.TOperationHandle(
op_handle.operationId,
op_handle.operationType,
False,
op_handle.modifiedRowCount,
),
maxRows=cursor.arraysize,
maxBytes=cursor.buffer_size_bytes,
orientation=ttypes.TFetchOrientation.FETCH_NEXT,
includeResultSetMetadata=True,
)
resp = self.make_request(self._client.FetchResults, req)
t_result_set_metadata_resp = resp.resultSetMetadata
lz4_compressed = t_result_set_metadata_resp.lz4Compressed
is_staging_operation = t_result_set_metadata_resp.isStagingOperation
has_more_rows = resp.hasMoreRows
description = self._hive_schema_to_description(
t_result_set_metadata_resp.schema
)
schema_bytes = (
t_result_set_metadata_resp.arrowSchema
or self._hive_schema_to_arrow_schema(t_result_set_metadata_resp.schema)
.serialize()
.to_pybytes()
)
queue = ResultSetQueueFactory.build_queue(
row_set_type=resp.resultSetMetadata.resultFormat,
t_row_set=resp.results,
arrow_schema_bytes=schema_bytes,
max_download_threads=self.max_download_threads,
lz4_compressed=lz4_compressed,
description=description,
ssl_options=self._ssl_options,
)
return ExecuteResponse(
arrow_queue=queue,
status=resp.status,
has_been_closed_server_side=False,
has_more_rows=has_more_rows,
lz4_compressed=lz4_compressed,
is_staging_operation=is_staging_operation,
command_handle=op_handle,
description=description,
arrow_schema_bytes=schema_bytes,
)
def _wait_until_command_done(self, op_handle, initial_operation_status_resp):
if initial_operation_status_resp:
self._check_command_not_in_error_or_closed_state(
op_handle, initial_operation_status_resp
)
operation_state = (
initial_operation_status_resp
and initial_operation_status_resp.operationState
)
while not operation_state or operation_state in [
ttypes.TOperationState.RUNNING_STATE,
ttypes.TOperationState.PENDING_STATE,
]:
poll_resp = self._poll_for_status(op_handle)
operation_state = poll_resp.operationState
self._check_command_not_in_error_or_closed_state(op_handle, poll_resp)
return operation_state
def get_query_state(self, op_handle) -> "TOperationState":
poll_resp = self._poll_for_status(op_handle)
operation_state = poll_resp.operationState
self._check_command_not_in_error_or_closed_state(op_handle, poll_resp)
return operation_state
@staticmethod
def _check_direct_results_for_error(t_spark_direct_results):
if t_spark_direct_results:
if t_spark_direct_results.operationStatus:
ThriftBackend._check_response_for_error(
t_spark_direct_results.operationStatus
)
if t_spark_direct_results.resultSetMetadata:
ThriftBackend._check_response_for_error(
t_spark_direct_results.resultSetMetadata
)
if t_spark_direct_results.resultSet:
ThriftBackend._check_response_for_error(
t_spark_direct_results.resultSet
)
if t_spark_direct_results.closeOperation:
ThriftBackend._check_response_for_error(
t_spark_direct_results.closeOperation
)
def execute_command(
self,
operation,
session_handle,
max_rows,
max_bytes,
lz4_compression,
cursor,
use_cloud_fetch=True,
parameters=[],
async_op=False,
):
assert session_handle is not None
spark_arrow_types = ttypes.TSparkArrowTypes(
timestampAsArrow=self._use_arrow_native_timestamps,
decimalAsArrow=self._use_arrow_native_decimals,
complexTypesAsArrow=self._use_arrow_native_complex_types,
# TODO: The current Arrow type used for intervals can not be deserialised in PyArrow
# DBR should be changed to use month_day_nano_interval
intervalTypesAsArrow=False,
)
req = ttypes.TExecuteStatementReq(
sessionHandle=session_handle,
statement=operation,
runAsync=True,
getDirectResults=ttypes.TSparkGetDirectResults(
maxRows=max_rows, maxBytes=max_bytes
),
canReadArrowResult=True if pyarrow else False,
canDecompressLZ4Result=lz4_compression,
canDownloadResult=use_cloud_fetch,
confOverlay={
# We want to receive proper Timestamp arrow types.
"spark.thriftserver.arrowBasedRowSet.timestampAsString": "false"
},
useArrowNativeTypes=spark_arrow_types,
parameters=parameters,
)
resp = self.make_request(self._client.ExecuteStatement, req)
if async_op:
self._handle_execute_response_async(resp, cursor)
else:
return self._handle_execute_response(resp, cursor)
def get_catalogs(self, session_handle, max_rows, max_bytes, cursor):
assert session_handle is not None
req = ttypes.TGetCatalogsReq(
sessionHandle=session_handle,
getDirectResults=ttypes.TSparkGetDirectResults(
maxRows=max_rows, maxBytes=max_bytes
),
)
resp = self.make_request(self._client.GetCatalogs, req)
return self._handle_execute_response(resp, cursor)
def get_schemas(
self,
session_handle,
max_rows,
max_bytes,
cursor,
catalog_name=None,
schema_name=None,
):
assert session_handle is not None
req = ttypes.TGetSchemasReq(
sessionHandle=session_handle,
getDirectResults=ttypes.TSparkGetDirectResults(
maxRows=max_rows, maxBytes=max_bytes
),
catalogName=catalog_name,
schemaName=schema_name,
)
resp = self.make_request(self._client.GetSchemas, req)
return self._handle_execute_response(resp, cursor)
def get_tables(
self,
session_handle,
max_rows,
max_bytes,
cursor,
catalog_name=None,
schema_name=None,
table_name=None,
table_types=None,
):
assert session_handle is not None
req = ttypes.TGetTablesReq(
sessionHandle=session_handle,
getDirectResults=ttypes.TSparkGetDirectResults(
maxRows=max_rows, maxBytes=max_bytes
),
catalogName=catalog_name,
schemaName=schema_name,
tableName=table_name,
tableTypes=table_types,
)
resp = self.make_request(self._client.GetTables, req)
return self._handle_execute_response(resp, cursor)
def get_columns(
self,
session_handle,
max_rows,
max_bytes,
cursor,
catalog_name=None,
schema_name=None,
table_name=None,
column_name=None,
):
assert session_handle is not None
req = ttypes.TGetColumnsReq(
sessionHandle=session_handle,
getDirectResults=ttypes.TSparkGetDirectResults(
maxRows=max_rows, maxBytes=max_bytes
),
catalogName=catalog_name,
schemaName=schema_name,