Skip to content

Commit 0692e0c

Browse files
committed
HHH-19240 Reduce memory consumption by left factoring some HQL parse rules
1 parent 97d87c4 commit 0692e0c

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
@@ -1831,8 +1831,34 @@ public Object visitGeneralPathExpression(HqlParser.GeneralPathExpressionContext
18311831
}
18321832

18331833
@Override
1834-
public SqmExpression<?> visitFunctionExpression(HqlParser.FunctionExpressionContext ctx) {
1835-
return (SqmExpression<?>) ctx.function().accept( this );
1834+
public Object visitFunctionExpression(HqlParser.FunctionExpressionContext ctx) {
1835+
final var slicedFragmentsCtx = ctx.slicedPathAccessFragment();
1836+
if ( slicedFragmentsCtx != null ) {
1837+
final List<HqlParser.ExpressionContext> slicedFragments = slicedFragmentsCtx.expression();
1838+
return getFunctionDescriptor( "array_slice" ).generateSqmExpression(
1839+
List.of(
1840+
(SqmTypedNode<?>) visitFunction( ctx.function() ),
1841+
(SqmTypedNode<?>) slicedFragments.get( 0 ).accept( this ),
1842+
(SqmTypedNode<?>) slicedFragments.get( 1 ).accept( this )
1843+
),
1844+
null,
1845+
queryEngine()
1846+
);
1847+
}
1848+
else {
1849+
final var function = (SqmExpression<?>) visitFunction( ctx.function() );
1850+
final var indexedPathAccessFragment = ctx.indexedPathAccessFragment();
1851+
final var pathContinuation = ctx.pathContinuation();
1852+
if ( indexedPathAccessFragment == null && pathContinuation == null ) {
1853+
return function;
1854+
}
1855+
else {
1856+
return visitPathContinuation(
1857+
visitIndexedPathAccessFragment( (SemanticPathPart) function, indexedPathAccessFragment ),
1858+
pathContinuation
1859+
);
1860+
}
1861+
}
18361862
}
18371863

18381864
@Override
@@ -3533,11 +3559,6 @@ else if ( attributes.size() >1 ) {
35333559
throw new FunctionArgumentException( "Argument '" + sqmPath.getNavigablePath()
35343560
+ "' of 'naturalid()' does not resolve to an entity type" );
35353561
}
3536-
//
3537-
// @Override
3538-
// public Object visitToOneFkExpression(HqlParser.ToOneFkExpressionContext ctx) {
3539-
// return visitToOneFkReference( (HqlParser.ToOneFkReferenceContext) ctx.getChild( 0 ) );
3540-
// }
35413562

35423563
@Override
35433564
public SqmFkExpression<?> visitToOneFkReference(HqlParser.ToOneFkReferenceContext ctx) {
@@ -5673,33 +5694,6 @@ else if ( ctx.collectionValueNavigablePath() != null ) {
56735694
else if ( ctx.mapKeyNavigablePath() != null ) {
56745695
return visitMapKeyNavigablePath( ctx.mapKeyNavigablePath() );
56755696
}
5676-
else if ( ctx.toOneFkReference() != null ) {
5677-
return visitToOneFkReference( ctx.toOneFkReference() );
5678-
}
5679-
else if ( ctx.function() != null ) {
5680-
final var slicedFragmentsCtx = ctx.slicedPathAccessFragment();
5681-
if ( slicedFragmentsCtx != null ) {
5682-
final List<HqlParser.ExpressionContext> slicedFragments = slicedFragmentsCtx.expression();
5683-
return getFunctionDescriptor( "array_slice" ).generateSqmExpression(
5684-
List.of(
5685-
(SqmTypedNode<?>) visitFunction( ctx.function() ),
5686-
(SqmTypedNode<?>) slicedFragments.get( 0 ).accept( this ),
5687-
(SqmTypedNode<?>) slicedFragments.get( 1 ).accept( this )
5688-
),
5689-
null,
5690-
queryEngine()
5691-
);
5692-
}
5693-
else {
5694-
return visitPathContinuation(
5695-
visitIndexedPathAccessFragment(
5696-
(SemanticPathPart) visitFunction( ctx.function() ),
5697-
ctx.indexedPathAccessFragment()
5698-
),
5699-
ctx.pathContinuation()
5700-
);
5701-
}
5702-
}
57035697
else if ( ctx.simplePath() != null && ctx.indexedPathAccessFragment() != null ) {
57045698
return visitIndexedPathAccessFragment( visitSimplePath( ctx.simplePath() ), ctx.indexedPathAccessFragment() );
57055699
}

0 commit comments

Comments
 (0)