bito-code-review[bot] commented on code in PR #44604:
URL: https://github.com/apache/superset/pull/44604#discussion_r4110813658


##########
superset/sql/execution/executor.py:
##########
@@ -186,6 +186,60 @@ def build_statement_blocks(
     return parsed_script, blocks
 
 
+class _LimitedCursor:
+    """Bound cumulative cursor reads without bypassing engine fetch 
processing."""
+
+    def __init__(self, cursor: Any, limit: int) -> None:
+        """Wrap a cursor with a shared budget for all row-reading methods."""
+        self._cursor = cursor
+        self._remaining = limit
+
+    def __getattr__(self, name: str) -> Any:
+        """Delegate metadata and driver-specific methods to the real cursor."""
+        return getattr(self._cursor, name)
+
+    @property
+    def arraysize(self) -> int:
+        """Expose the driver's default fetch batch size."""
+        return self._cursor.arraysize
+
+    @arraysize.setter
+    def arraysize(self, value: int) -> None:
+        """Preserve engine-specific cursor batch-size configuration."""
+        self._cursor.arraysize = value
+
+    def fetchmany(self, size: int | None = None) -> list[Any]:
+        """Read no more than the remaining budget, including across batches."""
+        size = self.arraysize if size is None else size
+        size = max(0, min(size, self._remaining))
+        # Some drivers interpret zero as unbounded, so do not call them at all.
+        if not size:
+            return []
+        # Reserve before reading so an engine's error fallback cannot retry
+        # beyond the budget after a driver has partially consumed a batch.
+        self._remaining -= size
+        return self._cursor.fetchmany(size)

Review Comment:
   <!-- Bito Reply -->
   The suggestion is appropriate and improves the code. Decrementing the budget 
after the driver returns ensures that the budget is only consumed by rows 
actually retrieved, preventing the silent data loss described in the review 
comment when a driver error occurs.
   
   **superset/sql/execution/executor.py**
   ```
   def fetchmany(self, size: int | None = None) -> list[Any]:
           """Read no more than the remaining budget, including across 
batches."""
           size = self.arraysize if size is None else size
           size = max(0, min(size, self._remaining))
           if not size:
               return []
           rows = self._cursor.fetchmany(size)
           self._remaining -= len(rows)
           return rows
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to