Skip to content

getFunctionAliases fails for complex query #2441

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
TaridaGeorge opened this issue Feb 14, 2022 · 9 comments
Closed

getFunctionAliases fails for complex query #2441

TaridaGeorge opened this issue Feb 14, 2022 · 9 comments
Labels
in: query-parser Everything related to parsing JPQL or SQL status: feedback-provided Feedback has been provided type: bug A general bug

Comments

@TaridaGeorge
Copy link

If I use the nativeQuery approach and I have a query like this:

SELECT 
	CAST(('{' || string_agg(distinct array_to_string(c.institutes_ids, ','), ',') || '}') AS bigint[]) as institutesIds 
FROM
	city c

the getFunctionAliases method from org/springframework/data/jpa/repository/query/QueryUtils.java fails to take institutesIds as an alias and that's because that FUNCTION_PATTERN cannot handle complex cases like this.

What should we do about it?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 14, 2022
@schauder
Copy link
Contributor

Please post the stack trace and method this SQL statement is annotated to?

@schauder schauder added in: query-parser Everything related to parsing JPQL or SQL type: bug A general bug status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 15, 2022
@schauder
Copy link
Contributor

You can always implement it using a custom method implementation.

We are looking in a proper solution for this kind of problem here #2417

@TaridaGeorge
Copy link
Author

Thank you for your response schaunder!

This is the method from spring-data that I'm talking about:

	static Set<String> getFunctionAliases(String query) {

		Set<String> result = new HashSet<>();
		Matcher matcher = FUNCTION_PATTERN.matcher(query);

		while (matcher.find()) {

			String alias = matcher.group(1);

			if (StringUtils.hasText(alias)) {
				result.add(alias);
			}
		}

		return result;
	}

It gets as input a String - query. If the query is somehow complex like in my initial post this method fails to match all function aliases. There is no stacktrace because there is no exception thrown. The problem is that the result from return result does not contain the institutesIds alias as I was expecting it to do.

Looking at #2417 I think that using JSqlParser will be a proper solution for this.

Regarding "custom method implementation" can you guide me to how can I achieve this? Any reference to a tutorial or documentation will be helpful. Thank you!

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 15, 2022
@schauder
Copy link
Contributor

I was referring to the method in your repository, that you applied the @Query annotation to.

@schauder schauder added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Feb 15, 2022
@TaridaGeorge
Copy link
Author

It is something like this:

@Query("SELECT "  + 
	"CAST(('{' || string_agg(distinct array_to_string(c.institutes_ids, ','), ',') || '}') AS bigint[]) as institutesIds "  + 
"FROM " + 
	"city c",
nativeQuery = true)
Page<Object[]> findAllInstitutesIds()

@schauder
Copy link
Contributor

Could you return a list instead, or provide a countQuery?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 15, 2022
@TaridaGeorge
Copy link
Author

TaridaGeorge commented Feb 16, 2022

I've made a repo in order to explain better what I found to not work properly. There are 2 tests in the DemoApplicationTests class. In my opinion both of them should pass but they fail. You can find the repo here:

https://github.com/TaridaGeorge/spring-data-jpa-issue-2441

DiegoKrupitza added a commit to DiegoKrupitza/spring-data-jpa that referenced this issue Feb 16, 2022
The added test cases make sure the new implementation does not have the issue described in spring-projects#2441.

Closes spring-projects#2441
@DiegoKrupitza
Copy link
Contributor

DiegoKrupitza commented Feb 16, 2022

We are looking in a proper solution for this kind of problem here #2417
@schauder

I added 2 new test cases that make sure this issue is not a problem anymore when using JSQLParser. For the new implementation this is not a problem anymore.

@TaridaGeorge
Copy link
Author

Nice! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: query-parser Everything related to parsing JPQL or SQL status: feedback-provided Feedback has been provided type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants