Skip to content

clarify semantics in order_by docstring #518

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

Merged
merged 1 commit into from
Nov 3, 2024
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
14 changes: 13 additions & 1 deletion src/datachain/lib/dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,22 @@ def _extend_to_data_model(self, method_name, *args, **kwargs):

@resolve_columns
def order_by(self, *args, descending: bool = False) -> "Self":
"""Orders by specified set of signals.
"""Orders by specified set of columns.

Parameters:
descending (bool): Whether to sort in descending order or not.

Example:
```py
dc.order_by("similarity_score", descending=True).limit(10)
```

Note:
Order is not guaranteed when steps are added after an `order_by` statement.
I.e. when using `from_dataset` an `order_by` statement should be used if
the order of the records in the chain is important.
Using `order_by` directly before `limit` will give expected results.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume collect, show, to_parquet - are not considered as steps and will give also a "desired" output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collect - should but not guranteed (explanation in the original ticket #477)
show - yes
to_parquet - should but not guaranteed (uses collect_flatten)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, probably needs to be a separate ticket then - e.g. combine things like order_by().collect() into a single statement, wdyt? (can be / should be implemented separately)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean adding methods like collect_ordered OR adding an explicit order_by param to those methods or patching those functions? Is there a specific use case we're targeting or are we just trying to avoid confusion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a basic expectation that if you do order_by().collect() that you are getting a sorted result? collect is a way to iterate on the results I guess.

I mean that collect probably should be implemented similar to show() - something like ``order_by().collect()` should be a single SQL query probably

See https://github.com/iterative/datachain/issues/477 for further details.
"""
if descending:
args = tuple(sqlalchemy.desc(a) for a in args)
Expand Down
Loading