forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ORM/HR+Panache: Remove FETCH from count queries
We do this by turning to the ORM HQLParser for non-trivial queries, but only for them, because the parser is much more expensive than simple string manipulation, so we keep the fast/easy logic. Fixes quarkusio#26308
- Loading branch information
Showing
10 changed files
with
283 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...runtime/src/main/java/io/quarkus/panache/hibernate/common/runtime/CountParserVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.quarkus.panache.hibernate.common.runtime; | ||
|
||
import org.antlr.v4.runtime.tree.TerminalNode; | ||
import org.hibernate.grammars.hql.HqlParser.JoinContext; | ||
import org.hibernate.grammars.hql.HqlParser.QueryContext; | ||
import org.hibernate.grammars.hql.HqlParser.QueryOrderContext; | ||
import org.hibernate.grammars.hql.HqlParser.SelectClauseContext; | ||
import org.hibernate.grammars.hql.HqlParser.SimpleQueryGroupContext; | ||
import org.hibernate.grammars.hql.HqlParserBaseVisitor; | ||
|
||
public class CountParserVisitor extends HqlParserBaseVisitor<String> { | ||
|
||
private int inSimpleQueryGroup; | ||
private StringBuilder sb = new StringBuilder(); | ||
|
||
@Override | ||
public String visitSimpleQueryGroup(SimpleQueryGroupContext ctx) { | ||
inSimpleQueryGroup++; | ||
try { | ||
return super.visitSimpleQueryGroup(ctx); | ||
} finally { | ||
inSimpleQueryGroup--; | ||
} | ||
} | ||
|
||
@Override | ||
public String visitQuery(QueryContext ctx) { | ||
super.visitQuery(ctx); | ||
if (inSimpleQueryGroup == 1 && ctx.selectClause() == null) { | ||
// insert a count because there's no select | ||
sb.append(" select count( * )"); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String visitSelectClause(SelectClauseContext ctx) { | ||
if (ctx.SELECT() != null) { | ||
ctx.SELECT().accept(this); | ||
} | ||
if (ctx.DISTINCT() != null) { | ||
sb.append(" count("); | ||
ctx.DISTINCT().accept(this); | ||
if (ctx.selectionList().children.size() != 1) { | ||
// FIXME: error message should include query | ||
throw new RuntimeException("Cannot count on more than one column"); | ||
} | ||
ctx.selectionList().children.get(0).accept(this); | ||
sb.append(" )"); | ||
} else { | ||
sb.append(" count( * )"); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String visitJoin(JoinContext ctx) { | ||
if (inSimpleQueryGroup == 1 && ctx.FETCH() != null) { | ||
// ignore fetch joins for main query | ||
return null; | ||
} | ||
return super.visitJoin(ctx); | ||
} | ||
|
||
@Override | ||
public String visitQueryOrder(QueryOrderContext ctx) { | ||
if (inSimpleQueryGroup == 1) { | ||
// ignore order/limit/offset for main query | ||
return null; | ||
} | ||
return super.visitQueryOrder(ctx); | ||
} | ||
|
||
@Override | ||
public String visitTerminal(TerminalNode node) { | ||
append(node.getText()); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String defaultResult() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String aggregateResult(String aggregate, String nextResult) { | ||
if (nextResult != null) { | ||
append(nextResult); | ||
} | ||
return null; | ||
} | ||
|
||
private void append(String nextResult) { | ||
// don't add space at start, or around dots | ||
if (!sb.isEmpty() && sb.charAt(sb.length() - 1) != '.' && !nextResult.equals(".")) { | ||
sb.append(" "); | ||
} | ||
sb.append(nextResult); | ||
} | ||
|
||
public String result() { | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...e-common/runtime/src/test/java/io/quarkus/panache/hibernate/common/runtime/CountTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.panache.hibernate.common.runtime; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CountTest { | ||
@Test | ||
public void testParser() { | ||
// one column, order/limit/offset | ||
assertCountQueryUsingParser("select count( * ) from bar", "select foo from bar order by foo, bar ASC limit 2 offset 3"); | ||
// two columns | ||
assertCountQueryUsingParser("select count( * ) from bar", "select foo,gee from bar"); | ||
// one column distinct | ||
assertCountQueryUsingParser("select count( distinct foo ) from bar", "select distinct foo from bar"); | ||
// two columns distinct | ||
Assertions.assertThrows(RuntimeException.class, | ||
() -> assertCountQueryUsingParser("XX", "select distinct foo,gee from bar")); | ||
// nested order by not touched | ||
assertCountQueryUsingParser("select count( * ) from ( from entity order by id )", | ||
"select foo from (from entity order by id) order by foo, bar ASC"); | ||
// what happens to literals? | ||
assertCountQueryUsingParser("select count( * ) from bar where some = 2 and other = '23'", | ||
"select foo from bar where some = 2 and other = '23'"); | ||
// fetches are gone | ||
assertCountQueryUsingParser("select count( * ) from bar b", "select foo from bar b left join fetch b.things"); | ||
// non-fetches remain | ||
assertCountQueryUsingParser("select count( * ) from bar b left join b.things", | ||
"select foo from bar b left join b.things"); | ||
|
||
// inverted select | ||
assertCountQueryUsingParser("from bar select count( * )", "from bar select foo"); | ||
// from without select | ||
assertCountQueryUsingParser("from bar select count( * )", "from bar"); | ||
} | ||
|
||
@Test | ||
public void testFastVersion() { | ||
// one column, order/limit/offset | ||
assertFastCountQuery("SELECT COUNT(*) from bar", "select foo from bar order by foo, bar ASC limit 2 offset 3"); | ||
// two columns | ||
assertFastCountQuery("SELECT COUNT(*) from bar", "select foo,gee from bar"); | ||
// one column distinct | ||
assertFastCountQuery("SELECT COUNT(distinct foo) from bar", "select distinct foo from bar"); | ||
// two columns distinct | ||
Assertions.assertThrows(RuntimeException.class, () -> assertFastCountQuery("XX", "select distinct foo,gee from bar")); | ||
// nested order by not touched | ||
assertFastCountQuery("SELECT COUNT(*) from (from entity order by id)", | ||
"select foo from (from entity order by id) order by foo, bar ASC"); | ||
// what happens to literals? | ||
assertFastCountQuery("SELECT COUNT(*) from bar where some = 2 and other = '23'", | ||
"select foo from bar where some = 2 and other = '23'"); | ||
// fetches are gone | ||
assertFastCountQuery("select count( * ) from bar b", "select foo from bar b left join fetch b.things"); | ||
// non-fetches remain | ||
assertFastCountQuery("SELECT COUNT(*) from bar b left join b.things", "select foo from bar b left join b.things"); | ||
|
||
// inverted select | ||
assertFastCountQuery("from bar select count( * )", "from bar select foo"); | ||
// from without select | ||
assertFastCountQuery("SELECT COUNT(*) from bar", "from bar"); | ||
} | ||
|
||
private void assertCountQueryUsingParser(String expected, String selectQuery) { | ||
String countQuery = PanacheJpaUtil.getCountQueryUsingParser(selectQuery); | ||
Assertions.assertEquals(expected, countQuery); | ||
} | ||
|
||
private void assertFastCountQuery(String expected, String selectQuery) { | ||
String countQuery = PanacheJpaUtil.getFastCountQuery(selectQuery); | ||
Assertions.assertEquals(expected, countQuery); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters