Skip to content

Commit bc30cdc

Browse files
committed
HHH-19240 Refactor IS predicate to single UnaryIsPredicate rule
(cherry picked from commit adcb5db)
1 parent 148a0b1 commit bc30cdc

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
@@ -2439,49 +2439,25 @@ public SqmBetweenPredicate visitBetweenPredicate(HqlParser.BetweenPredicateConte
24392439
);
24402440
}
24412441

2442-
2443-
@Override
2444-
public SqmNullnessPredicate visitIsNullPredicate(HqlParser.IsNullPredicateContext ctx) {
2445-
return new SqmNullnessPredicate(
2446-
(SqmExpression<?>) ctx.expression().accept( this ),
2447-
ctx.NOT() != null,
2448-
nodeBuilder()
2449-
);
2450-
}
2451-
2452-
@Override
2453-
public SqmEmptinessPredicate visitIsEmptyPredicate(HqlParser.IsEmptyPredicateContext ctx) {
2454-
SqmExpression<?> expression = (SqmExpression<?>) ctx.expression().accept(this);
2455-
if ( expression instanceof SqmPluralValuedSimplePath<?> pluralValuedSimplePath ) {
2456-
return new SqmEmptinessPredicate(
2457-
pluralValuedSimplePath,
2458-
ctx.NOT() != null,
2459-
nodeBuilder()
2460-
);
2461-
}
2462-
else {
2463-
throw new SemanticException( "Operand of 'is empty' operator must be a plural path", query );
2464-
}
2465-
}
2466-
24672442
@Override
2468-
public Object visitIsTruePredicate(HqlParser.IsTruePredicateContext ctx) {
2469-
return new SqmTruthnessPredicate(
2470-
(SqmExpression<?>) ctx.expression().accept( this ),
2471-
true,
2472-
ctx.NOT() != null,
2473-
nodeBuilder()
2474-
);
2475-
}
2476-
2477-
@Override
2478-
public Object visitIsFalsePredicate(HqlParser.IsFalsePredicateContext ctx) {
2479-
return new SqmTruthnessPredicate(
2480-
(SqmExpression<?>) ctx.expression().accept( this ),
2481-
false,
2482-
ctx.NOT() != null,
2483-
nodeBuilder()
2484-
);
2443+
public SqmPredicate visitUnaryIsPredicate(HqlParser.UnaryIsPredicateContext ctx) {
2444+
final var expression = (SqmExpression<?>) ctx.expression().accept( this );
2445+
final var negated = ctx.NOT() != null;
2446+
final var nodeBuilder = nodeBuilder();
2447+
return switch ( ((TerminalNode) ctx.getChild( ctx.getChildCount() - 1 )).getSymbol().getType() ) {
2448+
case HqlParser.NULL -> new SqmNullnessPredicate( expression, negated, nodeBuilder );
2449+
case HqlParser.EMPTY -> {
2450+
if ( expression instanceof SqmPluralValuedSimplePath<?> pluralValuedSimplePath ) {
2451+
yield new SqmEmptinessPredicate( pluralValuedSimplePath, negated, nodeBuilder );
2452+
}
2453+
else {
2454+
throw new SemanticException( "Operand of 'is empty' operator must be a plural path", query );
2455+
}
2456+
}
2457+
case HqlParser.TRUE -> new SqmTruthnessPredicate( expression, true, negated, nodeBuilder );
2458+
case HqlParser.FALSE -> new SqmTruthnessPredicate( expression, false, negated, nodeBuilder );
2459+
default -> throw new AssertionError( "Unknown unary is predicate: " + ctx.getChild( ctx.getChildCount() - 1 ) );
2460+
};
24852461
}
24862462

24872463
@Override

0 commit comments

Comments
 (0)