Skip to content

Commit 71ce6f5

Browse files
committed
HHH-19240 Reduce memory consumption by left factoring some HQL parse rules
(cherry picked from commit 0692e0c)
1 parent c9a0d6e commit 71ce6f5

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

hibernate-core/src/main/antlr/org/hibernate/grammars/hql/HqlParser.g4

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,13 @@ pathContinuation
427427
* * VALUE( path )
428428
* * KEY( path )
429429
* * path[ selector ]
430-
* * ARRAY_GET( embeddableArrayPath, index ).path
431-
* * COALESCE( array1, array2 )[ selector ].path
432430
*/
433431
syntacticDomainPath
434432
: treatedNavigablePath
435433
| collectionValueNavigablePath
436434
| mapKeyNavigablePath
437435
| simplePath indexedPathAccessFragment
438436
| simplePath slicedPathAccessFragment
439-
| toOneFkReference
440-
| function pathContinuation
441-
| function indexedPathAccessFragment pathContinuation?
442-
| function slicedPathAccessFragment
443437
;
444438

445439
/**
@@ -748,7 +742,14 @@ primaryExpression
748742
| entityVersionReference # EntityVersionExpression
749743
| entityNaturalIdReference # EntityNaturalIdExpression
750744
| syntacticDomainPath pathContinuation? # SyntacticPathExpression
751-
| function # FunctionExpression
745+
// ARRAY_GET( embeddableArrayPath, index ).path
746+
// COALESCE( array1, array2 )[ selector ].path
747+
// COALESCE( array1, array2 )[ start : end ]
748+
| function (
749+
pathContinuation
750+
| slicedPathAccessFragment
751+
| indexedPathAccessFragment pathContinuation?
752+
)? # FunctionExpression
752753
| generalPathFragment # GeneralPathExpression
753754
;
754755

@@ -1108,6 +1109,7 @@ function
11081109
| columnFunction
11091110
| jsonFunction
11101111
| xmlFunction
1112+
| toOneFkReference
11111113
| genericFunction
11121114
;
11131115

hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,8 +1860,34 @@ public Object visitGeneralPathExpression(HqlParser.GeneralPathExpressionContext
18601860
}
18611861

18621862
@Override
1863-
public SqmExpression<?> visitFunctionExpression(HqlParser.FunctionExpressionContext ctx) {
1864-
return (SqmExpression<?>) ctx.function().accept( this );
1863+
public Object visitFunctionExpression(HqlParser.FunctionExpressionContext ctx) {
1864+
final var slicedFragmentsCtx = ctx.slicedPathAccessFragment();
1865+
if ( slicedFragmentsCtx != null ) {
1866+
final List<HqlParser.ExpressionContext> slicedFragments = slicedFragmentsCtx.expression();
1867+
return getFunctionDescriptor( "array_slice" ).generateSqmExpression(
1868+
List.of(
1869+
(SqmTypedNode<?>) visitFunction( ctx.function() ),
1870+
(SqmTypedNode<?>) slicedFragments.get( 0 ).accept( this ),
1871+
(SqmTypedNode<?>) slicedFragments.get( 1 ).accept( this )
1872+
),
1873+
null,
1874+
queryEngine()
1875+
);
1876+
}
1877+
else {
1878+
final var function = (SqmExpression<?>) visitFunction( ctx.function() );
1879+
final var indexedPathAccessFragment = ctx.indexedPathAccessFragment();
1880+
final var pathContinuation = ctx.pathContinuation();
1881+
if ( indexedPathAccessFragment == null && pathContinuation == null ) {
1882+
return function;
1883+
}
1884+
else {
1885+
return visitPathContinuation(
1886+
visitIndexedPathAccessFragment( (SemanticPathPart) function, indexedPathAccessFragment ),
1887+
pathContinuation
1888+
);
1889+
}
1890+
}
18651891
}
18661892

18671893
@Override
@@ -3548,11 +3574,6 @@ else if ( attributes.size() >1 ) {
35483574
throw new FunctionArgumentException( "Argument '" + sqmPath.getNavigablePath()
35493575
+ "' of 'naturalid()' does not resolve to an entity type" );
35503576
}
3551-
//
3552-
// @Override
3553-
// public Object visitToOneFkExpression(HqlParser.ToOneFkExpressionContext ctx) {
3554-
// return visitToOneFkReference( (HqlParser.ToOneFkReferenceContext) ctx.getChild( 0 ) );
3555-
// }
35563577

35573578
@Override
35583579
public SqmFkExpression<?> visitToOneFkReference(HqlParser.ToOneFkReferenceContext ctx) {
@@ -5719,33 +5740,6 @@ else if ( ctx.collectionValueNavigablePath() != null ) {
57195740
else if ( ctx.mapKeyNavigablePath() != null ) {
57205741
return visitMapKeyNavigablePath( ctx.mapKeyNavigablePath() );
57215742
}
5722-
else if ( ctx.toOneFkReference() != null ) {
5723-
return visitToOneFkReference( ctx.toOneFkReference() );
5724-
}
5725-
else if ( ctx.function() != null ) {
5726-
final var slicedFragmentsCtx = ctx.slicedPathAccessFragment();
5727-
if ( slicedFragmentsCtx != null ) {
5728-
final List<HqlParser.ExpressionContext> slicedFragments = slicedFragmentsCtx.expression();
5729-
return getFunctionDescriptor( "array_slice" ).generateSqmExpression(
5730-
List.of(
5731-
(SqmTypedNode<?>) visitFunction( ctx.function() ),
5732-
(SqmTypedNode<?>) slicedFragments.get( 0 ).accept( this ),
5733-
(SqmTypedNode<?>) slicedFragments.get( 1 ).accept( this )
5734-
),
5735-
null,
5736-
queryEngine()
5737-
);
5738-
}
5739-
else {
5740-
return visitPathContinuation(
5741-
visitIndexedPathAccessFragment(
5742-
(SemanticPathPart) visitFunction( ctx.function() ),
5743-
ctx.indexedPathAccessFragment()
5744-
),
5745-
ctx.pathContinuation()
5746-
);
5747-
}
5748-
}
57495743
else if ( ctx.simplePath() != null && ctx.indexedPathAccessFragment() != null ) {
57505744
return visitIndexedPathAccessFragment( visitSimplePath( ctx.simplePath() ), ctx.indexedPathAccessFragment() );
57515745
}

0 commit comments

Comments
 (0)