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

Nulls LAST / FIRST ignored in Version 1.4.0 #696

Open
ShaneLee opened this issue Dec 14, 2021 · 3 comments
Open

Nulls LAST / FIRST ignored in Version 1.4.0 #696

ShaneLee opened this issue Dec 14, 2021 · 3 comments
Labels
type: enhancement A general enhancement

Comments

@ShaneLee
Copy link

Hi,

I'm currently looking at using Criteria queries to query my database. I think I've found a bug where ordering of nulls is ignored. I have to do this programmatically, so can't get around it using @query.

I've written some basic unit tests for the R2DBCEntityTemplate which I think expose the issue (but I'm not sure if this is where the problem is or if I'm just wrong)


	@Test
	void shouldSelectByCriteriaWhereNullsLast() {

		recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());

		entityTemplate.select(Query.query(Criteria.where("name")
						.is("Walter"))
						.sort(Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST))), Person.class) //
				.as(StepVerifier::create) //
				.verifyComplete();

		StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("SELECT"));

		assertThat(statement.getSql())
				.isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 ORDER BY person.THE_NAME ASC NULLS LAST");
		assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
	}

	@Test
	void shouldSelectByCriteriaWhereNullsFirst() {

		recorder.addStubbing(s -> s.startsWith("SELECT"), Collections.emptyList());

		entityTemplate.select(Query.query(Criteria.where("name")
								.is("Walter"))
						.sort(Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_FIRST))), Person.class) //
				.as(StepVerifier::create) //
				.verifyComplete();

		StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("SELECT"));

		assertThat(statement.getSql())
				.isEqualTo("SELECT person.* FROM person WHERE person.THE_NAME = $1 ORDER BY person.THE_NAME ASC NULLS FIRST");
		assertThat(statement.getBindings()).hasSize(1).containsEntry(0, Parameter.from("Walter"));
	}

Any help with this is appreciated.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 14, 2021
@mp911de
Copy link
Member

mp911de commented Dec 14, 2021

Null sort precedence isn't supported yet.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 14, 2021
@ShaneLee
Copy link
Author

Happy to look at adding a PR if I can get some pointers about how this currently works

@mp911de
Copy link
Member

mp911de commented Dec 14, 2021

Null sorting precedence is subject to dialect specifics as some database are able to handle this on their own. Databases that do not support this require some kind of workaround (CASE/WHEN/THEN expressions along with sorting indicators). It depends on some functionality within Spring Data Relational (spring-projects/spring-data-relational#755).

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

No branches or pull requests

3 participants