Skip to content

Commit bb3e94d

Browse files
introduce row_limit
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent a74d279 commit bb3e94d

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

src/databricks/sql/backend/databricks_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def execute_command(
8585
parameters: List,
8686
async_op: bool,
8787
enforce_embedded_schema_correctness: bool,
88+
row_limit: Optional[int] = None,
8889
) -> Union["ResultSet", None]:
8990
"""
9091
Executes a SQL command or query within the specified session.
@@ -103,6 +104,7 @@ def execute_command(
103104
parameters: List of parameters to bind to the query
104105
async_op: Whether to execute the command asynchronously
105106
enforce_embedded_schema_correctness: Whether to enforce schema correctness
107+
row_limit: Maximum number of rows to fetch overall. Only supported for SEA protocol.
106108
107109
Returns:
108110
If async_op is False, returns a ResultSet object containing the

src/databricks/sql/backend/sea/backend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def execute_command(
405405
parameters: List[Dict[str, Any]],
406406
async_op: bool,
407407
enforce_embedded_schema_correctness: bool,
408+
row_limit: Optional[int] = None,
408409
) -> Union["ResultSet", None]:
409410
"""
410411
Execute a SQL command using the SEA backend.
@@ -462,7 +463,7 @@ def execute_command(
462463
format=format,
463464
wait_timeout=(WaitTimeout.ASYNC if async_op else WaitTimeout.SYNC).value,
464465
on_wait_timeout="CONTINUE",
465-
row_limit=max_rows,
466+
row_limit=row_limit,
466467
parameters=sea_parameters if sea_parameters else None,
467468
result_compression=result_compression,
468469
)

src/databricks/sql/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def cursor(
341341
self,
342342
arraysize: int = DEFAULT_ARRAY_SIZE,
343343
buffer_size_bytes: int = DEFAULT_RESULT_BUFFER_SIZE_BYTES,
344+
row_limit: int = None,
344345
) -> "Cursor":
345346
"""
346347
Return a new Cursor object using the connection.
@@ -355,6 +356,7 @@ def cursor(
355356
self.session.backend,
356357
arraysize=arraysize,
357358
result_buffer_size_bytes=buffer_size_bytes,
359+
row_limit=row_limit,
358360
)
359361
self._cursors.append(cursor)
360362
return cursor
@@ -388,6 +390,7 @@ def __init__(
388390
backend: DatabricksClient,
389391
result_buffer_size_bytes: int = DEFAULT_RESULT_BUFFER_SIZE_BYTES,
390392
arraysize: int = DEFAULT_ARRAY_SIZE,
393+
row_limit: Optional[int] = None,
391394
) -> None:
392395
"""
393396
These objects represent a database cursor, which is used to manage the context of a fetch
@@ -397,11 +400,17 @@ def __init__(
397400
visible by other cursors or connections.
398401
"""
399402

403+
if not self.connection.session.use_sea and row_limit is not None:
404+
logger.warning(
405+
"Row limit is only supported for SEA protocol. Ignoring row_limit."
406+
)
407+
400408
self.connection = connection
401409
self.rowcount = -1 # Return -1 as this is not supported
402410
self.buffer_size_bytes = result_buffer_size_bytes
403411
self.active_result_set: Union[ResultSet, None] = None
404412
self.arraysize = arraysize
413+
self.row_limit = row_limit
405414
# Note that Cursor closed => active result set closed, but not vice versa
406415
self.open = True
407416
self.executing_command_id = None

src/databricks/sql/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ def _create_backend(
9797
kwargs: dict,
9898
) -> DatabricksClient:
9999
"""Create and return the appropriate backend client."""
100-
use_sea = kwargs.get("use_sea", False)
100+
self.use_sea = kwargs.get("use_sea", False)
101101

102102
databricks_client_class: Type[DatabricksClient]
103-
if use_sea:
103+
if self.use_sea:
104104
logger.debug("Creating SEA backend client")
105105
databricks_client_class = SeaDatabricksClient
106106
else:

0 commit comments

Comments
 (0)