|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABC |
| 4 | +from typing import List, Optional, Tuple |
| 5 | + |
| 6 | +from databricks.sql.backend.sea.backend import SeaDatabricksClient |
| 7 | +from databricks.sql.backend.sea.models.base import ResultData, ResultManifest |
| 8 | +from databricks.sql.utils import ResultSetQueue |
| 9 | + |
| 10 | + |
| 11 | +class SeaResultSetQueueFactory(ABC): |
| 12 | + @staticmethod |
| 13 | + def build_queue( |
| 14 | + sea_result_data: ResultData, |
| 15 | + manifest: Optional[ResultManifest], |
| 16 | + statement_id: str, |
| 17 | + description: List[Tuple] = [], |
| 18 | + max_download_threads: Optional[int] = None, |
| 19 | + sea_client: Optional[SeaDatabricksClient] = None, |
| 20 | + lz4_compressed: bool = False, |
| 21 | + ) -> ResultSetQueue: |
| 22 | + """ |
| 23 | + Factory method to build a result set queue for SEA backend. |
| 24 | +
|
| 25 | + Args: |
| 26 | + sea_result_data (ResultData): Result data from SEA response |
| 27 | + manifest (ResultManifest): Manifest from SEA response |
| 28 | + statement_id (str): Statement ID for the query |
| 29 | + description (List[List[Any]]): Column descriptions |
| 30 | + schema_bytes (bytes): Arrow schema bytes |
| 31 | + max_download_threads (int): Maximum number of download threads |
| 32 | + ssl_options (SSLOptions): SSL options for downloads |
| 33 | + sea_client (SeaDatabricksClient): SEA client for fetching additional links |
| 34 | + lz4_compressed (bool): Whether the data is LZ4 compressed |
| 35 | +
|
| 36 | + Returns: |
| 37 | + ResultSetQueue: The appropriate queue for the result data |
| 38 | + """ |
| 39 | + |
| 40 | + if sea_result_data.data is not None: |
| 41 | + # INLINE disposition with JSON_ARRAY format |
| 42 | + return JsonQueue(sea_result_data.data) |
| 43 | + elif sea_result_data.external_links is not None: |
| 44 | + # EXTERNAL_LINKS disposition |
| 45 | + raise NotImplementedError( |
| 46 | + "EXTERNAL_LINKS disposition is not implemented for SEA backend" |
| 47 | + ) |
| 48 | + return JsonQueue([]) |
| 49 | + |
| 50 | + |
| 51 | +class JsonQueue(ResultSetQueue): |
| 52 | + """Queue implementation for JSON_ARRAY format data.""" |
| 53 | + |
| 54 | + def __init__(self, data_array): |
| 55 | + """Initialize with JSON array data.""" |
| 56 | + self.data_array = data_array |
| 57 | + self.cur_row_index = 0 |
| 58 | + self.num_rows = len(data_array) |
| 59 | + |
| 60 | + def next_n_rows(self, num_rows): |
| 61 | + """Get the next n rows from the data array.""" |
| 62 | + length = min(num_rows, self.num_rows - self.cur_row_index) |
| 63 | + slice = self.data_array[self.cur_row_index : self.cur_row_index + length] |
| 64 | + self.cur_row_index += length |
| 65 | + return slice |
| 66 | + |
| 67 | + def remaining_rows(self): |
| 68 | + """Get all remaining rows from the data array.""" |
| 69 | + slice = self.data_array[self.cur_row_index :] |
| 70 | + self.cur_row_index += len(slice) |
| 71 | + return slice |
0 commit comments