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

Self changes of column names to non-exist column names by PanacheJpaUtil#getFastCountQuery #42064

Closed
eKangu opened this issue Jul 23, 2024 · 4 comments · Fixed by #43963
Closed
Labels
area/hibernate-orm Hibernate ORM area/panache kind/bug Something isn't working
Milestone

Comments

@eKangu
Copy link

eKangu commented Jul 23, 2024

Describe the bug

Bug appears in version 3.12.0.CR1 https://github.com/quarkusio/quarkus/releases/tag/3.12.0.CR1
and in all higher versions.
It is likely caused by the change introduced in this pull request:
#38931
, specifically in io.quarkus.panache.hibernate.common.runtime.PanacheJpaUtil#getFastCountQuery:
d3e578e#diff-88f819be30dca99e30b631c785af5ac15b5ff4b5b91a0ffbace7a5d361ae2b6bR55

When I call the hasNextPage() method (io.quarkus.hibernate.orm.panache.PanacheQuery#hasNextPage) on a query in HQL , which contains an entity field with name written in camel case (eg. groupId)), I receive the following error:

Could not interpret path expression 'groupid'
org.hibernate.query.SemanticException: Could not interpret path expression 'groupid'
	at org.hibernate.query.hql.internal.BasicDotIdentifierConsumer$BaseLocalSequencePart.resolvePathPart(BasicDotIdentifierConsumer.java:241)
	at org.hibernate.query.hql.internal.BasicDotIdentifierConsumer.consumeIdentifier(BasicDotIdentifierConsumer.java:92)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimplePath(SemanticQueryBuilder.java:5259)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitIndexedPathAccessFragment(SemanticQueryBuilder.java:5218)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGeneralPathFragment(SemanticQueryBuilder.java:5193)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGeneralPathExpression(SemanticQueryBuilder.java:1886)
	at org.hibernate.grammars.hql.HqlParser$GeneralPathExpressionContext.accept(HqlParser.java:7967)
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46)
	at org.hibernate.grammars.hql.HqlParserBaseVisitor.visitBarePrimaryExpression(HqlParserBaseVisitor.java:777)
	at org.hibernate.grammars.hql.HqlParser$BarePrimaryExpressionContext.accept(HqlParser.java:7425)
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46)
	at org.hibernate.grammars.hql.HqlParserBaseVisitor.visitExpressionOrPredicate(HqlParserBaseVisitor.java:917)
	at org.hibernate.grammars.hql.HqlParser$ExpressionOrPredicateContext.accept(HqlParser.java:8195)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitFinalFunctionArgument(SemanticQueryBuilder.java:4296)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGenericFunctionArguments(SemanticQueryBuilder.java:4273)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGenericFunctionArguments(SemanticQueryBuilder.java:275)
	at org.hibernate.grammars.hql.HqlParser$GenericFunctionArgumentsContext.accept(HqlParser.java:12784)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.getFunctionArguments(SemanticQueryBuilder.java:4176)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGenericFunction(SemanticQueryBuilder.java:4041)
	at org.hibernate.grammars.hql.HqlParser$GenericFunctionContext.accept(HqlParser.java:12413)
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46)
	at org.hibernate.grammars.hql.HqlParserBaseVisitor.visitFunction(HqlParserBaseVisitor.java:1309)
	at org.hibernate.grammars.hql.HqlParser$FunctionContext.accept(HqlParser.java:11891)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitFunctionExpression(SemanticQueryBuilder.java:1897)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitFunctionExpression(SemanticQueryBuilder.java:275)
	at org.hibernate.grammars.hql.HqlParser$FunctionExpressionContext.accept(HqlParser.java:7847)
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46)
	at org.hibernate.grammars.hql.HqlParserBaseVisitor.visitBarePrimaryExpression(HqlParserBaseVisitor.java:777)
	at org.hibernate.grammars.hql.HqlParser$BarePrimaryExpressionContext.accept(HqlParser.java:7425)
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46)
	at org.hibernate.grammars.hql.HqlParserBaseVisitor.visitExpressionOrPredicate(HqlParserBaseVisitor.java:917)
	at org.hibernate.grammars.hql.HqlParser$ExpressionOrPredicateContext.accept(HqlParser.java:8195)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectableNode(SemanticQueryBuilder.java:1430)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelection(SemanticQueryBuilder.java:1407)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectClause(SemanticQueryBuilder.java:1400)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuery(SemanticQueryBuilder.java:1249)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:1035)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:275)
	at org.hibernate.grammars.hql.HqlParser$QuerySpecExpressionContext.accept(HqlParser.java:2132)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:1020)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:275)
	at org.hibernate.grammars.hql.HqlParser$SimpleQueryGroupContext.accept(HqlParser.java:2003)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectStatement(SemanticQueryBuilder.java:490)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:449)
	at org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:322)
	at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:71)
	at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:145)
	at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:132)
	at org.hibernate.internal.AbstractSharedSessionContract.interpretHql(AbstractSharedSessionContract.java:802)
	at org.hibernate.internal.AbstractSharedSessionContract.interpretAndCreateSelectionQuery(AbstractSharedSessionContract.java:779)
	at org.hibernate.internal.AbstractSharedSessionContract.createSelectionQuery(AbstractSharedSessionContract.java:770)
	at io.quarkus.hibernate.orm.runtime.session.TransactionScopedSession.createSelectionQuery(TransactionScopedSession.java:1269)
	at org.hibernate.engine.spi.SessionLazyDelegator.createSelectionQuery(SessionLazyDelegator.java:744)
	at org.hibernate.Session_OpdLahisOZ9nWRPXMsEFQmQU03A_Synthetic_ClientProxy.createSelectionQuery(Unknown Source)
	at io.quarkus.hibernate.orm.panache.common.runtime.CommonPanacheQueryImpl.count(CommonPanacheQueryImpl.java:279)
	at io.quarkus.hibernate.orm.panache.common.runtime.CommonPanacheQueryImpl.pageCount(CommonPanacheQueryImpl.java:229)
	at io.quarkus.hibernate.orm.panache.common.runtime.CommonPanacheQueryImpl.hasNextPage(CommonPanacheQueryImpl.java:219)
	at io.quarkus.hibernate.orm.panache.runtime.PanacheQueryImpl.hasNextPage(PanacheQueryImpl.java:81)
	

It seems that this issue is caused by converting the part of the query to lowercase, which is later used in the target query::
String firstSelection = selectMatcher.group(1).trim().toLowerCase(Locale.ROOT);

This string is then used in:
countQuery = “SELECT COUNT(” + firstSelection + “) ” + selectMatcher.group(3);

Expected behavior

Method does not modify column name in query

Actual behavior

Metod modify column name in query

How to Reproduce?

  1. Use entity/table with column named in camelCase:
    SmeEntity:
@Entity
@Table(name = "some_entity")
public class SomeEntity extends PanacheEntityBase {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    @Column(name = "group_id")
    String groupId;
}
  1. Create an HQL query containing a camel case entity field (e.g., groupId) eg.:
PanacheQuery<String> query = SomeEntity.find("select distinct groupId from SomeEntity")
                                                             .project(String.class)
                                                             .page(Page.ofSize(2));
  1. Call the hasNextPage() method on this query. For example as I did::
boolean doNext;
        do {
            // some code
            doNext = query.hasNextPage();
            if (doNext)
                query = query.nextPage();
        } while (doNext);
  1. Observe the error related to the path expression.

Output of uname -a or ver

MINGW64_NT-10.0-22631 Tytan 3.4.10-87d57229.x86_64 2024-02-14 20:17 UTC x86_64 Msys

Output of java -version

java version "17.0.9" 2023-10-17 LTS Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201) Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)

Quarkus version or git rev

3.12.3

Build tool (ie. output of mvnw --version or gradlew --version)

Gradle 8.3

Additional information

No response

@eKangu eKangu added the kind/bug Something isn't working label Jul 23, 2024
@quarkus-bot quarkus-bot bot added area/hibernate-orm Hibernate ORM area/panache area/persistence OBSOLETE, DO NOT USE labels Jul 23, 2024
Copy link

quarkus-bot bot commented Jul 23, 2024

/cc @FroMage (panache), @gsmet (hibernate-orm), @loicmathieu (panache), @yrodiere (hibernate-orm)

@eKangu eKangu changed the title SemanticException when use io.quarkus.panache.hibernate.common.runtime.PanacheJpaUtil#getFastCountQuery Self changes of column names to non-exist column names by PanacheJpaUtil#getFastCountQuery Jul 25, 2024
@yrodiere yrodiere removed the area/persistence OBSOLETE, DO NOT USE label Aug 14, 2024
@zanella
Copy link

zanella commented Oct 17, 2024

@FroMage Any special reason to apply the toLowerCase ? I hit the same bug, makes it impossible to migrate to 3.12

My query SELECT DISTINCT ContractContractEntity FROM ContractContractEntity ... becomes SELECT COUNT(distinct contractcontractentity) FROM ContractContractEntity ... and then Hibernate can no longer find the entity.

@FroMage
Copy link
Member

FroMage commented Oct 18, 2024

To be honest, I thought I'd gotten rid of that function months ago…

@FroMage
Copy link
Member

FroMage commented Oct 18, 2024

Ah, I didn't dream it. #41620 gets rid of it but I was waiting for HR to be merged. It is released now so I'll get on to finishing that PR.

@quarkus-bot quarkus-bot bot added this to the 3.17 - main milestone Oct 18, 2024
@gsmet gsmet modified the milestones: 3.17 - main, 3.16.0 Oct 21, 2024
@gsmet gsmet modified the milestones: 3.16.0, 3.15.2 Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM area/panache kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants