From a3dd07fa3815f834b2a993c7fe6b1f859617d2d0 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 15 Jan 2024 12:39:20 +0100 Subject: [PATCH] Update Eclipselink grammar to support = & != for null comparison. Eclipselink transforms = & and != in queries into IS and IS NOT. Closes #3314 Original pull request: #3316 --- .../data/jpa/repository/query/Eql.g4 | 2 +- .../data/jpa/repository/query/EqlQueryRenderer.java | 11 +++++++---- .../jpa/repository/query/EqlComplianceTests.java | 13 +++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4 b/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4 index 9bbf47a86..46748477a 100644 --- a/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4 +++ b/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4 @@ -362,7 +362,7 @@ like_expression ; null_comparison_expression - : (single_valued_path_expression | input_parameter | nullif_expression) IS (NOT)? NULL + : (single_valued_path_expression | input_parameter | nullif_expression) ((IS (NOT)?) | (op=(EQUAL | NOT_EQUAL))) NULL ; empty_collection_comparison_expression diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java index 8ece7c965..22a10faee 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EqlQueryRenderer.java @@ -1315,10 +1315,13 @@ class EqlQueryRenderer extends EqlBaseVisitor> { tokens.addAll(visit(ctx.nullif_expression())); } - tokens.add(new JpaQueryParsingToken(ctx.IS())); - - if (ctx.NOT() != null) { - tokens.add(new JpaQueryParsingToken(ctx.NOT())); + if (ctx.op != null) { + tokens.add(new JpaQueryParsingToken(ctx.op.getText())); + } else { + tokens.add(new JpaQueryParsingToken(ctx.IS())); + if (ctx.NOT() != null) { + tokens.add(new JpaQueryParsingToken(ctx.NOT())); + } } tokens.add(new JpaQueryParsingToken(ctx.NULL())); diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/EqlComplianceTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/EqlComplianceTests.java index 86046779e..124de35a3 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/EqlComplianceTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/EqlComplianceTests.java @@ -401,4 +401,17 @@ class EqlComplianceTests { assertQuery("SELECT b FROM Bundle b WHERE coalesce(b.deleted, false) AND b.latestImport = true"); assertQuery("SELECT b FROM Bundle b WHERE NOT coalesce(b.deleted, false) AND b.latestImport = true"); } + + @Test // GH-3314 + void isNullAndIsNotNull() { + + assertQuery("SELECT e FROM Employee e WHERE (e.active = null OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active = NULL OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active IS null OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active IS NULL OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active != null OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active != NULL OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active IS NOT null OR e.active = true)"); + assertQuery("SELECT e FROM Employee e WHERE (e.active IS NOT NULL OR e.active = true)"); + } }