Copilot commented on code in PR #34583:
URL: https://github.com/apache/superset/pull/34583#discussion_r2258583452


##########
superset/db_engine_specs/mssql.py:
##########
@@ -163,6 +164,33 @@ def extract_error_message(cls, ex: Exception) -> str:
             )
         return f"{cls.engine} error: {cls._extract_error_message(ex)}"
 
+    @classmethod
+    def adjust_query_for_offset(cls, qry: Select) -> Select:
+        """
+        Modify SQLAlchemy query to add ORDER BY when OFFSET is present.
+
+        MSSQL requires an ORDER BY clause when using OFFSET. If no ORDER BY
+        is present but OFFSET is specified, we add a default ORDER BY clause.
+        """
+        # Check if this query has OFFSET but no ORDER BY by inspecting query 
structure
+        try:
+            # Try to access the internal query structure safely
+            has_offset = getattr(qry, "_offset", None) is not None
+            has_order_by = bool(getattr(qry, "_order_by_clauses", ()))
+
+            if has_offset and not has_order_by:
+                if qry.selected_columns:
+                    first_column = list(qry.selected_columns)[0]
+                    qry = qry.order_by(first_column)
+                else:
+                    # Fallback to literal if no columns are available
+                    qry = qry.order_by(cast(literal("1"), String))

Review Comment:
   Using a cast literal for ordering is unusual and may cause performance 
issues. Consider using a more standard approach like `text('1')` or ordering by 
a constant expression that doesn't require casting.
   ```suggestion
                       qry = qry.order_by(literal(1))
   ```



##########
superset/db_engine_specs/mssql.py:
##########
@@ -163,6 +164,33 @@ def extract_error_message(cls, ex: Exception) -> str:
             )
         return f"{cls.engine} error: {cls._extract_error_message(ex)}"
 
+    @classmethod
+    def adjust_query_for_offset(cls, qry: Select) -> Select:
+        """
+        Modify SQLAlchemy query to add ORDER BY when OFFSET is present.
+
+        MSSQL requires an ORDER BY clause when using OFFSET. If no ORDER BY
+        is present but OFFSET is specified, we add a default ORDER BY clause.
+        """
+        # Check if this query has OFFSET but no ORDER BY by inspecting query 
structure
+        try:
+            # Try to access the internal query structure safely
+            has_offset = getattr(qry, "_offset", None) is not None
+            has_order_by = bool(getattr(qry, "_order_by_clauses", ()))
+
+            if has_offset and not has_order_by:
+                if qry.selected_columns:
+                    first_column = list(qry.selected_columns)[0]

Review Comment:
   The condition `qry.selected_columns` may not work as expected for all 
SQLAlchemy Select objects. This attribute might not exist or behave 
consistently across different SQLAlchemy versions. Consider using `qry.columns` 
or a more robust method to check for selected columns.
   ```suggestion
                   # Try to get the selected columns in a version-independent 
way
                   selected_columns = (
                       getattr(qry, "selected_columns", None)
                       or getattr(qry, "exported_columns", None)
                       or getattr(qry, "_raw_columns", None)
                   )
                   if selected_columns:
                       first_column = list(selected_columns)[0]
   ```



-- 
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