Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure stable secondary ordering #43085

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions airflow/api_fastapi/common/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from __future__ import annotations

import importlib
from abc import ABC, abstractmethod
from datetime import datetime
from typing import TYPE_CHECKING, Any, Callable, Generic, List, TypeVar
Expand Down Expand Up @@ -195,32 +194,27 @@ def to_orm(self, select: Select) -> Select:
# 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(), column.desc())
return select.order_by(nullscheck, column.desc(), primary_key_column)
else:
return select.order_by(nullscheck, column.asc(), column.asc())

def get_primary_key(self) -> str:
"""Get the primary key of the model of SortParam object."""
return inspect(self.model).primary_key[0].name
return select.order_by(nullscheck, column.asc(), primary_key_column)

@staticmethod
def get_primary_key_of_given_model_string(model_string: str) -> str:
"""
Get the primary key of given 'airflow.models' class as a string. The class should have driven be from 'airflow.models.base'.
def get_primary_key_column(self) -> Column:
"""Get the primary key column of the model of SortParam object."""
return inspect(self.model).primary_key[0]

:param model_string: The string representation of the model class.
:return: The primary key of the model class.
"""
dynamic_return_model = getattr(importlib.import_module("airflow.models"), model_string)
return inspect(dynamic_return_model).primary_key[0].name
def get_primary_key_string(self) -> str:
"""Get the primary key string of the model of SortParam object."""
return self.get_primary_key_column().name

def depends(self, *args: Any, **kwargs: Any) -> Self:
raise NotImplementedError("Use dynamic_depends, depends not implemented.")

def dynamic_depends(self) -> Callable:
def inner(order_by: str = self.get_primary_key()) -> SortParam:
return self.set_value(self.get_primary_key() if order_by == "" else order_by)
def inner(order_by: str = self.get_primary_key_string()) -> SortParam:
return self.set_value(self.get_primary_key_string() if order_by == "" else order_by)

return inner

Expand Down