-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathresult_set.py
More file actions
265 lines (202 loc) · 8.27 KB
/
result_set.py
File metadata and controls
265 lines (202 loc) · 8.27 KB
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
from __future__ import annotations
from typing import Any, List, Optional, TYPE_CHECKING
import logging
from databricks.sql.backend.sea.models.base import ResultData, ResultManifest
from databricks.sql.backend.sea.utils.conversion import SqlTypeConverter
try:
import pyarrow
except ImportError:
pyarrow = None
if TYPE_CHECKING:
from databricks.sql.client import Connection
from databricks.sql.backend.sea.backend import SeaDatabricksClient
from databricks.sql.types import Row
from databricks.sql.backend.sea.queue import JsonQueue, SeaResultSetQueueFactory
from databricks.sql.backend.types import ExecuteResponse
from databricks.sql.result_set import ResultSet
logger = logging.getLogger(__name__)
class SeaResultSet(ResultSet):
"""ResultSet implementation for SEA backend."""
def __init__(
self,
connection: Connection,
execute_response: ExecuteResponse,
sea_client: SeaDatabricksClient,
result_data: ResultData,
manifest: ResultManifest,
buffer_size_bytes: int = 104857600,
arraysize: int = 10000,
):
"""
Initialize a SeaResultSet with the response from a SEA query execution.
Args:
connection: The parent connection
execute_response: Response from the execute command
sea_client: The SeaDatabricksClient instance for direct access
buffer_size_bytes: Buffer size for fetching results
arraysize: Default number of rows to fetch
result_data: Result data from SEA response
manifest: Manifest from SEA response
"""
self.manifest = manifest
statement_id = execute_response.command_id.to_sea_statement_id()
if statement_id is None:
raise ValueError("Command ID is not a SEA statement ID")
results_queue = SeaResultSetQueueFactory.build_queue(
result_data,
self.manifest,
statement_id,
ssl_options=connection.session.ssl_options,
description=execute_response.description,
max_download_threads=sea_client.max_download_threads,
sea_client=sea_client,
lz4_compressed=execute_response.lz4_compressed,
)
# Call parent constructor with common attributes
super().__init__(
connection=connection,
backend=sea_client,
arraysize=arraysize,
buffer_size_bytes=buffer_size_bytes,
command_id=execute_response.command_id,
status=execute_response.status,
has_been_closed_server_side=execute_response.has_been_closed_server_side,
results_queue=results_queue,
description=execute_response.description,
is_staging_operation=execute_response.is_staging_operation,
lz4_compressed=execute_response.lz4_compressed,
arrow_schema_bytes=execute_response.arrow_schema_bytes,
)
def _convert_json_types(self, row: List[str]) -> List[Any]:
"""
Convert string values in the row to appropriate Python types based on column metadata.
"""
# JSON + INLINE gives us string values, so we convert them to appropriate
# types based on column metadata
converted_row = []
for i, value in enumerate(row):
column_name = self.description[i][0]
column_type = self.description[i][1]
precision = self.description[i][4]
scale = self.description[i][5]
converted_value = SqlTypeConverter.convert_value(
value,
column_type,
column_name=column_name,
precision=precision,
scale=scale,
)
converted_row.append(converted_value)
return converted_row
def _convert_json_to_arrow_table(self, rows: List[List[str]]) -> "pyarrow.Table":
"""
Convert raw data rows to Arrow table.
Args:
rows: List of raw data rows
Returns:
PyArrow Table containing the converted values
"""
if not rows:
return pyarrow.Table.from_pydict({})
# create a generator for row conversion
converted_rows_iter = (self._convert_json_types(row) for row in rows)
cols = list(map(list, zip(*converted_rows_iter)))
names = [col[0] for col in self.description]
return pyarrow.Table.from_arrays(cols, names=names)
def _create_json_table(self, rows: List[List[str]]) -> List[Row]:
"""
Convert raw data rows to Row objects with named columns based on description.
Args:
rows: List of raw data rows
Returns:
List of Row objects with named columns and converted values
"""
ResultRow = Row(*[col[0] for col in self.description])
return [ResultRow(*self._convert_json_types(row)) for row in rows]
def fetchmany_json(self, size: int) -> List[List[str]]:
"""
Fetch the next set of rows as a columnar table.
Args:
size: Number of rows to fetch
Returns:
Columnar table containing the fetched rows
Raises:
ValueError: If size is negative
"""
if size < 0:
raise ValueError(f"size argument for fetchmany is {size} but must be >= 0")
results = self.results.next_n_rows(size)
self._next_row_index += len(results)
return results
def fetchall_json(self) -> List[List[str]]:
"""
Fetch all remaining rows as a columnar table.
Returns:
Columnar table containing all remaining rows
"""
results = self.results.remaining_rows()
self._next_row_index += len(results)
return results
def fetchmany_arrow(self, size: int) -> "pyarrow.Table":
"""
Fetch the next set of rows as an Arrow table.
Args:
size: Number of rows to fetch
Returns:
PyArrow Table containing the fetched rows
Raises:
ImportError: If PyArrow is not installed
ValueError: If size is negative
"""
if size < 0:
raise ValueError(f"size argument for fetchmany is {size} but must be >= 0")
results = self.results.next_n_rows(size)
if isinstance(self.results, JsonQueue):
results = self._convert_json_to_arrow_table(results)
self._next_row_index += results.num_rows
return results
def fetchall_arrow(self) -> "pyarrow.Table":
"""
Fetch all remaining rows as an Arrow table.
"""
results = self.results.remaining_rows()
if isinstance(self.results, JsonQueue):
results = self._convert_json_to_arrow_table(results)
self._next_row_index += results.num_rows
return results
def fetchone(self) -> Optional[Row]:
"""
Fetch the next row of a query result set, returning a single sequence,
or None when no more data is available.
Returns:
A single Row object or None if no more rows are available
"""
if isinstance(self.results, JsonQueue):
res = self._create_json_table(self.fetchmany_json(1))
else:
res = self._convert_arrow_table(self.fetchmany_arrow(1))
return res[0] if res else None
def fetchmany(self, size: int) -> List[Row]:
"""
Fetch the next set of rows of a query result, returning a list of rows.
Args:
size: Number of rows to fetch (defaults to arraysize if None)
Returns:
List of Row objects
Raises:
ValueError: If size is negative
"""
if isinstance(self.results, JsonQueue):
return self._create_json_table(self.fetchmany_json(size))
else:
return self._convert_arrow_table(self.fetchmany_arrow(size))
def fetchall(self) -> List[Row]:
"""
Fetch all remaining rows of a query result, returning them as a list of rows.
Returns:
List of Row objects containing all remaining rows
"""
if isinstance(self.results, JsonQueue):
return self._create_json_table(self.fetchall_json())
else:
return self._convert_arrow_table(self.fetchall_arrow())