Skip to content

Commit f1776f3

Browse files
fix fetchall_arrow and fetchmany_arrow
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent 7a5ae13 commit f1776f3

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

src/databricks/sql/result_set.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -501,22 +501,25 @@ def __init__(
501501
# Initialize queue for result data if not provided
502502
self.results = results_queue or JsonQueue([])
503503

504-
def _convert_json_table(self, rows):
504+
def _convert_json_to_arrow(self, rows):
505+
"""
506+
Convert raw data rows to Arrow table.
507+
"""
508+
columns = []
509+
num_cols = len(rows[0])
510+
for i in range(num_cols):
511+
columns.append([row[i] for row in rows])
512+
names = [col[0] for col in self.description]
513+
return pyarrow.Table.from_arrays(columns, names=names)
514+
515+
def _convert_json_types(self, rows):
505516
"""
506517
Convert raw data rows to Row objects with named columns based on description.
507518
Also converts string values to appropriate Python types based on column metadata.
508-
509-
Args:
510-
rows: List of raw data rows
511-
Returns:
512-
List of Row objects with named columns and converted values
513519
"""
514520
if not self.description or not rows:
515521
return rows
516522

517-
column_names = [col[0] for col in self.description]
518-
ResultRow = Row(*column_names)
519-
520523
# JSON + INLINE gives us string values, so we convert them to appropriate
521524
# types based on column metadata
522525
converted_rows = []
@@ -539,10 +542,28 @@ def _convert_json_table(self, rows):
539542
)
540543
converted_row.append(value)
541544

542-
converted_rows.append(ResultRow(*converted_row))
545+
converted_rows.append(converted_row)
543546

544547
return converted_rows
545548

549+
def _convert_json_table(self, rows):
550+
"""
551+
Convert raw data rows to Row objects with named columns based on description.
552+
Also converts string values to appropriate Python types based on column metadata.
553+
554+
Args:
555+
rows: List of raw data rows
556+
Returns:
557+
List of Row objects with named columns and converted values
558+
"""
559+
if not self.description or not rows:
560+
return rows
561+
562+
ResultRow = Row(*[col[0] for col in self.description])
563+
rows = self._convert_json_types(rows)
564+
565+
return [ResultRow(*row) for row in rows]
566+
546567
def fetchmany_json(self, size: int):
547568
"""
548569
Fetch the next set of rows as a columnar table.
@@ -593,7 +614,11 @@ def fetchmany_arrow(self, size: int) -> "pyarrow.Table":
593614
if size < 0:
594615
raise ValueError(f"size argument for fetchmany is {size} but must be >= 0")
595616

596-
results = self.results.next_n_rows(size)
617+
if not isinstance(self.results, JsonQueue):
618+
raise NotImplementedError("fetchmany_arrow only supported for JSON data")
619+
620+
rows = self._convert_json_types(self.results.next_n_rows(size))
621+
results = self._convert_json_to_arrow(rows)
597622
self._next_row_index += results.num_rows
598623

599624
return results
@@ -602,7 +627,11 @@ def fetchall_arrow(self) -> "pyarrow.Table":
602627
"""
603628
Fetch all remaining rows as an Arrow table.
604629
"""
605-
results = self.results.remaining_rows()
630+
if not isinstance(self.results, JsonQueue):
631+
raise NotImplementedError("fetchall_arrow only supported for JSON data")
632+
633+
rows = self._convert_json_types(self.results.remaining_rows())
634+
results = self._convert_json_to_arrow(rows)
606635
self._next_row_index += results.num_rows
607636

608637
return results

0 commit comments

Comments
 (0)