# Buggy code
def _parse_search_registered_models_order_by(cls, order_by_list):
    """Sorts a set of registered models based on their natural ordering and an overriding set
    of order_bys. Registered models are naturally ordered first by name ascending.
    """
    clauses = []
    if order_by_list:
        for order_by_clause in order_by_list:
            (
                attribute_token,
                ascending,
            ) = SearchUtils.parse_order_by_for_search_registered_models(order_by_clause)
            if attribute_token == SqlRegisteredModel.name.key:
                field = SqlRegisteredModel.name
            elif attribute_token in SearchUtils.VALID_TIMESTAMP_ORDER_BY_KEYS:
                field = SqlRegisteredModel.last_updated_time
            else:
                raise MlflowException(
                    "Invalid order by key '{}' specified.".format(attribute_token)
                    + "Valid keys are "
                    + "'{}'".format(
                        SearchUtils.RECOMMENDED_ORDER_BY_KEYS_REGISTERED_MODELS
                    ),
                    error_code=INVALID_PARAMETER_VALUE,
                )
            if ascending:
                clauses.append(field.asc())
            else:
                clauses.append(field.desc())

    clauses.append(SqlRegisteredModel.name.asc())
    return clauses


# Traceback (sqlalchemy.exc.ProgrammingError)
Traceback (most recent call last):
File "/opt/app-root/lib64/python3.8/site-packages/mlflow/store/db/utils.py", line 76, in make_managed_session
yield session
File "/opt/app-root/lib64/python3.8/site-packages/mlflow/store/model_registry/sqlalchemy_store.py", line 331, in search_registered_models
sql_registered_models = query.all()
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/orm/query.py", line 3233, in all
return list(self)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/orm/query.py", line 3389, in __iter__
return self._execute_and_instances(context)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/orm/query.py", line 3414, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/engine/base.py", line 1095, in _execute_clauseelement
ret = self._execute_context(
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/engine/base.py", line 1249, in _execute_context
self._handle_dbapi_exception(
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/engine/base.py", line 1245, in _execute_context
self.dialect.do_execute(
File "/opt/app-root/lib64/python3.8/site-packages/sqlalchemy/engine/default.py", line 588, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]A column has been specified more than once in the order by list. Columns in the order by list must be unique. (169) (SQLExecDirectW)')
[SQL: SELECT TOP 11 registered_models.name AS registered_models_name, registered_models.creation_time AS registered_models_creation_time, registered_models.last_updated_time AS registered_models_last_updated_time, registered_models.description AS registered_models_description
FROM registered_models
WHERE lower(registered_models.name) LIKE lower(?) ORDER BY registered_models.name ASC, registered_models.name ASC]
[parameters: ('%%',)]
(Background on this error at: http://sqlalche.me/e/f405)

# Fixed code
def _parse_search_registered_models_order_by(cls, order_by_list):
    """Sorts a set of registered models based on their natural ordering and an overriding set
    of order_bys. Registered models are naturally ordered first by name ascending.
    """
    clauses = []
    observed_order_by_clauses = set()
    if order_by_list:
        for order_by_clause in order_by_list:
            (
                attribute_token,
                ascending,
            ) = SearchUtils.parse_order_by_for_search_registered_models(order_by_clause)
            if attribute_token == SqlRegisteredModel.name.key:
                field = SqlRegisteredModel.name
            elif attribute_token in SearchUtils.VALID_TIMESTAMP_ORDER_BY_KEYS:
                field = SqlRegisteredModel.last_updated_time
            else:
                raise MlflowException(
                    "Invalid order by key '{}' specified.".format(attribute_token)
                    + "Valid keys are "
                    + "'{}'".format(
                        SearchUtils.RECOMMENDED_ORDER_BY_KEYS_REGISTERED_MODELS
                    ),
                    error_code=INVALID_PARAMETER_VALUE,
                )
            if field.key in observed_order_by_clauses:
                raise MlflowException(
                    "`order_by` contains duplicate fields: {}".format(order_by_list)
                )
            observed_order_by_clauses.add(field.key)
            if ascending:
                clauses.append(field.asc())
            else:
                clauses.append(field.desc())

    if SqlRegisteredModel.name.key not in observed_order_by_clauses:
        clauses.append(SqlRegisteredModel.name.asc())
    return clauses