Skip to content

Introduce support for sorting by aliased projections #2865

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

gregturn
Copy link
Contributor

Original: #2863

@gregturn gregturn requested review from schauder and mp911de March 15, 2023 16:24
}

private boolean orderParameterIsAFunction(Sort.Order order) {
return order.getProperty().contains("(");
Copy link
Member

Choose a reason for hiding this comment

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

How is this even remotely possible? While properties are string, do we rewrite the sort to contain a function?

Copy link
Contributor Author

@gregturn gregturn Mar 20, 2023

Choose a reason for hiding this comment

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

This check for functions in Sort is something we already look for in QueryUtils.

https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java#L342-L357

In fact, we have existing test cases like this:

	@Test // DATAJPA-148
	void doesNotPrefixSortsIfFunction() {

		StringQuery query = new StringQuery("select p from Person p", true);
		Sort sort = Sort.by("sum(foo)");

		QueryEnhancer enhancer = getEnhancer(query);

		assertThatThrownBy(() -> enhancer.applySorting(sort, "p")) //
				.isInstanceOf(InvalidDataAccessApiUsageException.class);
	}

I'm simply trying to apply the same safeguards in our parser code.

Copy link
Member

@mp911de mp911de left a comment

Choose a reason for hiding this comment

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

Can we please have the same for JPQL? It's the same topic, after all. Please, also rebase this PR.

@mp911de mp911de assigned gregturn and unassigned mp911de Mar 28, 2023
@schauder schauder removed their request for review March 30, 2023 06:42
@gregturn gregturn changed the title Introduce support in HQL for sorting by aliased projections Introduce support for sorting by aliased projections Apr 4, 2023
gregturn added 2 commits April 5, 2023 10:49
If a projection of either an HQL or JPQL query is aliased, applied sorting should NOT result in that order parameter having the primary FROM clause's alias prefix to it. Same goes for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322.
Original pull request: #2865.
gregturn added a commit that referenced this pull request Apr 5, 2023
If a projection of either an HQL or JPQL query is aliased, do NOT prefix the FROM clause's alias prefix to any relevant applied sorting. Same for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322.
Original pull request: #2865.
@gregturn gregturn closed this Apr 5, 2023
gregturn added a commit that referenced this pull request Apr 5, 2023
If a projection of either an HQL or JPQL query is aliased, do NOT prefix the FROM clause's alias prefix to any relevant applied sorting. Same for function-based order by arguments.

Resolves #2863.
Related: #2626, #2322, #1655.
Original pull request: #2865.
@gregturn gregturn deleted the issue/gh-2863 branch April 5, 2023 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants