Skip to content

Commit adcb5db

Browse files
committed
HHH-19240 Refactor IS predicate to single UnaryIsPredicate rule
1 parent 39ac255 commit adcb5db

File tree

2 files changed

+19
-46
lines changed

2 files changed

+19
-46
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,7 @@ whereClause
654654
predicate
655655
//highest to lowest precedence
656656
: LEFT_PAREN predicate RIGHT_PAREN # GroupedPredicate
657-
| expression IS NOT? NULL # IsNullPredicate
658-
| expression IS NOT? EMPTY # IsEmptyPredicate
659-
| expression IS NOT? TRUE # IsTruePredicate
660-
| expression IS NOT? FALSE # IsFalsePredicate
657+
| expression IS NOT? (NULL|EMPTY|TRUE|FALSE) # UnaryIsPredicate
661658
| expression IS NOT? DISTINCT FROM expression # IsDistinctFromPredicate
662659
| expression NOT? MEMBER OF? path # MemberOfPredicate
663660
| expression NOT? IN inList # InPredicate

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

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,49 +2404,25 @@ public SqmBetweenPredicate visitBetweenPredicate(HqlParser.BetweenPredicateConte
24042404
);
24052405
}
24062406

2407-
2408-
@Override
2409-
public SqmNullnessPredicate visitIsNullPredicate(HqlParser.IsNullPredicateContext ctx) {
2410-
return new SqmNullnessPredicate(
2411-
(SqmExpression<?>) ctx.expression().accept( this ),
2412-
ctx.NOT() != null,
2413-
nodeBuilder()
2414-
);
2415-
}
2416-
2417-
@Override
2418-
public SqmEmptinessPredicate visitIsEmptyPredicate(HqlParser.IsEmptyPredicateContext ctx) {
2419-
final var expression = (SqmExpression<?>) ctx.expression().accept(this);
2420-
if ( expression instanceof SqmPluralValuedSimplePath<?> pluralValuedSimplePath ) {
2421-
return new SqmEmptinessPredicate(
2422-
pluralValuedSimplePath,
2423-
ctx.NOT() != null,
2424-
nodeBuilder()
2425-
);
2426-
}
2427-
else {
2428-
throw new SemanticException( "Operand of 'is empty' operator must be a plural path", query );
2429-
}
2430-
}
2431-
2432-
@Override
2433-
public Object visitIsTruePredicate(HqlParser.IsTruePredicateContext ctx) {
2434-
return new SqmTruthnessPredicate(
2435-
(SqmExpression<?>) ctx.expression().accept( this ),
2436-
true,
2437-
ctx.NOT() != null,
2438-
nodeBuilder()
2439-
);
2440-
}
2441-
24422407
@Override
2443-
public Object visitIsFalsePredicate(HqlParser.IsFalsePredicateContext ctx) {
2444-
return new SqmTruthnessPredicate(
2445-
(SqmExpression<?>) ctx.expression().accept( this ),
2446-
false,
2447-
ctx.NOT() != null,
2448-
nodeBuilder()
2449-
);
2408+
public SqmPredicate visitUnaryIsPredicate(HqlParser.UnaryIsPredicateContext ctx) {
2409+
final var expression = (SqmExpression<?>) ctx.expression().accept( this );
2410+
final var negated = ctx.NOT() != null;
2411+
final var nodeBuilder = nodeBuilder();
2412+
return switch ( ((TerminalNode) ctx.getChild( ctx.getChildCount() - 1 )).getSymbol().getType() ) {
2413+
case HqlParser.NULL -> new SqmNullnessPredicate( expression, negated, nodeBuilder );
2414+
case HqlParser.EMPTY -> {
2415+
if ( expression instanceof SqmPluralValuedSimplePath<?> pluralValuedSimplePath ) {
2416+
yield new SqmEmptinessPredicate( pluralValuedSimplePath, negated, nodeBuilder );
2417+
}
2418+
else {
2419+
throw new SemanticException( "Operand of 'is empty' operator must be a plural path", query );
2420+
}
2421+
}
2422+
case HqlParser.TRUE -> new SqmTruthnessPredicate( expression, true, negated, nodeBuilder );
2423+
case HqlParser.FALSE -> new SqmTruthnessPredicate( expression, false, negated, nodeBuilder );
2424+
default -> throw new AssertionError( "Unknown unary is predicate: " + ctx.getChild( ctx.getChildCount() - 1 ) );
2425+
};
24502426
}
24512427

24522428
@Override

0 commit comments

Comments
 (0)