pierrejeambrun commented on code in PR #47440:
URL: https://github.com/apache/airflow/pull/47440#discussion_r1982931110


##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -164,6 +164,7 @@ def depends_search(value: str | None = 
Query(alias=pattern_name, default=None))
 
 class SortParam(BaseParam[str]):
     """Order result by the attribute."""
+    MAX_SORT_PARAMS = 2

Review Comment:
   I think we can increase that to maybe 5 ? 



##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -201,16 +219,30 @@ def to_orm(self, select: Select) -> Select:
         # MySQL does not support `nullslast`, and True/False ordering depends 
on the
         # database implementation.
         nullscheck = case((column.isnot(None), 0), else_=1)
+        
+        return (nullscheck, column.desc() if order_by.startswith("-") else 
column.asc())
+
+    def to_orm(self, select: Select) -> Select:
+        if self.skip_none is False:
+            raise ValueError(f"Cannot set 'skip_none' to False on a 
{type(self)}")
+
+        if self.value is None:
+            self.value = self.get_primary_key_string()
+
+        order_by_list = self.value.split(",") if self.value else []
+
+        order_by_columns = self.get_order_by_columns(order_by_list)

Review Comment:
   The multiple query param orders will not be passed like this 
`order_by=-criteria1,criteria2`, but `order_by=-criteria1&order_by=criteria2` 
to be consistent with the `exploded` way passing query parameters list that 
FastAPI is defaulting too.



##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -173,14 +174,31 @@ def __init__(
         self.model = model
         self.to_replace = to_replace
 
-    def to_orm(self, select: Select) -> Select:
-        if self.skip_none is False:
-            raise ValueError(f"Cannot set 'skip_none' to False on a 
{type(self)}")
+    def get_order_by_columns(self, order_by_list: list[str]) -> list:
+        """Generates order_by conditions based on the given sorting 
parameters."""
+        if len(order_by_list) > MAX_SORT_PARAMS:
+            raise HTTPException(
+                400, 
+                f"Ordering with more than two parameters is not allowed. 
Provided: {order_by_list}"

Review Comment:
   `two` shouldn't be hardcoded but come from the `MAX_SORT_PARAMS` value.
   
   We need a test for that.



##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -173,14 +174,31 @@ def __init__(
         self.model = model
         self.to_replace = to_replace
 
-    def to_orm(self, select: Select) -> Select:
-        if self.skip_none is False:
-            raise ValueError(f"Cannot set 'skip_none' to False on a 
{type(self)}")
+    def get_order_by_columns(self, order_by_list: list[str]) -> list:

Review Comment:
   typing for `list` (list of what ?)



##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -173,14 +174,31 @@ def __init__(
         self.model = model
         self.to_replace = to_replace
 
-    def to_orm(self, select: Select) -> Select:
-        if self.skip_none is False:
-            raise ValueError(f"Cannot set 'skip_none' to False on a 
{type(self)}")
+    def get_order_by_columns(self, order_by_list: list[str]) -> list:
+        """Generates order_by conditions based on the given sorting 
parameters."""
+        if len(order_by_list) > MAX_SORT_PARAMS:
+            raise HTTPException(
+                400, 
+                f"Ordering with more than two parameters is not allowed. 
Provided: {order_by_list}"
+            )
 
-        if self.value is None:
-            self.value = self.get_primary_key_string()
+        order_by_columns = []
+
+        if len(order_by_list) >= 1:
+            
order_by_columns.append(self.get_column_with_sort(order_by_list[0]))
+
+        if len(order_by_list) == 2:
+            
order_by_columns.append(self.get_column_with_sort(order_by_list[1]))
 
-        lstriped_orderby = self.value.lstrip("-")
+        primary_key_name = self.get_primary_key_string()
+        if not (order_by_list and primary_key_name in [item.lstrip("-") for 
item in order_by_list]):
+            
order_by_columns.append(self.get_column_with_sort(primary_key_name))
+
+        return order_by_columns
+
+    def get_column_with_sort(self, order_by: str):

Review Comment:
   return type annotation



##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -173,14 +174,31 @@ def __init__(
         self.model = model
         self.to_replace = to_replace
 
-    def to_orm(self, select: Select) -> Select:
-        if self.skip_none is False:
-            raise ValueError(f"Cannot set 'skip_none' to False on a 
{type(self)}")
+    def get_order_by_columns(self, order_by_list: list[str]) -> list:
+        """Generates order_by conditions based on the given sorting 
parameters."""
+        if len(order_by_list) > MAX_SORT_PARAMS:
+            raise HTTPException(
+                400, 
+                f"Ordering with more than two parameters is not allowed. 
Provided: {order_by_list}"
+            )
 
-        if self.value is None:
-            self.value = self.get_primary_key_string()
+        order_by_columns = []
+
+        if len(order_by_list) >= 1:
+            
order_by_columns.append(self.get_column_with_sort(order_by_list[0]))
+
+        if len(order_by_list) == 2:
+            
order_by_columns.append(self.get_column_with_sort(order_by_list[1]))

Review Comment:
   for loop (because if MAX_SORT_PARAMS == 5, we don't want to do copy/past 
this 5 times)



##########
airflow/api_fastapi/common/parameters.py:
##########
@@ -201,16 +219,30 @@ def to_orm(self, select: Select) -> Select:
         # MySQL does not support `nullslast`, and True/False ordering depends 
on the
         # database implementation.
         nullscheck = case((column.isnot(None), 0), else_=1)
+        
+        return (nullscheck, column.desc() if order_by.startswith("-") else 
column.asc())
+
+    def to_orm(self, select: Select) -> Select:
+        if self.skip_none is False:
+            raise ValueError(f"Cannot set 'skip_none' to False on a 
{type(self)}")
+
+        if self.value is None:
+            self.value = self.get_primary_key_string()
+
+        order_by_list = self.value.split(",") if self.value else []
+
+        order_by_columns = self.get_order_by_columns(order_by_list)
 
         # Reset default sorting
         select = select.order_by(None)
 
-        primary_key_column = self.get_primary_key_column()
-
-        if self.value[0] == "-":
-            return select.order_by(nullscheck, column.desc(), 
primary_key_column.desc())
+        if order_by_columns:
+            select = select.order_by(*[col for pair in order_by_columns for 
col in pair])
         else:
-            return select.order_by(nullscheck, column.asc(), 
primary_key_column.asc())
+            primary_key_column = self.get_primary_key_column()

Review Comment:
   `primary_key_column` should always be used as the last sort criteria, even 
when multiple sorts are provided. This will ensure that a consistent order is 
returned. 
   
   For instance if the user provides 2 sort criteria, and that there are values 
for which those two criteria are the same, we then need to sort on the 
primary_key to ensure order consistency.



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

Reply via email to