Skip to content

Feature: Support order by SimpleFunction and SimpleExpression #1348

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

Closed
wants to merge 2 commits into from

Conversation

koenpunt
Copy link
Contributor

@koenpunt koenpunt commented Oct 5, 2022

To allow ordering results on more than just plain column names I implemented support for SimpleFunctions and SimpleExpressions (created using Expressions.just(..)).

Before if you would try to use a SimpleFunction expression in orderBy it did result in invalid SQL, because it completely omitted the function; ORDER BY ASC.

With these changes you can now use SimpleFunctions and SimpleExpressions as order by expression.

E.g.

orderBy(OrderByField.from(Expressions.just("1"))).asc() => 

   ORDER BY 1 ASC

orderBy(SimpleFunction.create("GREATEST", table1.column("updated_at"), table2.column("updated_at"))).asc() => 

   ORDER BY GREATEST(table1.updated_at, table2.updated_at) ASC

Related: #1113

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 5, 2022
@koenpunt koenpunt force-pushed the order-by-expressions branch 3 times, most recently from aad6bdd to 46b0e12 Compare October 5, 2022 14:11
To allow ordering results on for example the highest updated date, when
joining with multiple tables;

    ORDER BY GREATEST(table1.created_at, table2.created_at) ASC
To allow sorting on arbitrary SQL, for example the column index instead
of a name. But also more advanced things like functions that can't be
expressed using a `SimpleFunction`.

    orderBy(OrderByField.from(Expressions.just("1")).asc()) => ORDER BY 1 ASC
@koenpunt koenpunt force-pushed the order-by-expressions branch from 46b0e12 to e32c7f4 Compare October 5, 2022 14:13
@schauder schauder added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 11, 2022
@schauder schauder added this to the 3.0 RC1 (2022.0.0) milestone Oct 11, 2022
schauder pushed a commit that referenced this pull request Oct 11, 2022
This allows ordering by functions like:

    ORDER BY GREATEST(table1.created_at, table2.created_at) ASC

or by arbitrary SQL snippets

    orderBy(OrderByField.from(Expressions.just("1")).asc()) => ORDER BY 1 ASC

Original pull request #1348
schauder added a commit that referenced this pull request Oct 11, 2022
Removes code duplication.

Original pull request #1348
@schauder
Copy link
Contributor

Thanks, this merged.

@schauder schauder closed this Oct 11, 2022
@koenpunt koenpunt deleted the order-by-expressions branch October 12, 2022 09:28
@koenpunt
Copy link
Contributor Author

Is there a change this is going to be backported to 2.4.x? And then in turn to the spring-data-r2dbc package? Or do we have to wait for spring-boot 3 to be able to use this functionality?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants