From 5f5229f84c5b96426e9cf7b285667cc31a6bafeb Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Sat, 14 Nov 2026 15:13:51 -0600 Subject: [PATCH 1/5] fix(neo4j): a junction without criteria adds nothing, and an empty block keeps its match in an or An empty and { } or or { } left a dangling AND or OR in the Cypher, which Neo4j rejected. Blank expressions are dropped from a junction. A block without criteria inside an or no longer becomes an existence branch: it keeps the match, so it still asks for an associated entity, as on the datastores that join. Fixes #14635 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy index 8b38c9a5305..afcd639a123 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy @@ -190,6 +190,7 @@ class Neo4jQuery extends Query { CypherExpression handle(CriterionScope scope, Query.Conjunction criterion) { def inner = ((Query.Junction) criterion).criteria .collect { Query.Criterion it -> dispatchCriterion(it, scope).toString() } + .findAll { String expression -> expression } .join(CriterionHandler.OPERATOR_AND) return new CypherExpression(inner ? "( $inner )" : inner) } @@ -203,6 +204,7 @@ class Neo4jQuery extends Query { try { def inner = ((Query.Junction) criterion).criteria .collect { Query.Criterion it -> dispatchCriterion(it, scope).toString() } + .findAll { String expression -> expression } .join(CriterionHandler.OPERATOR_OR) return new CypherExpression(inner ? "( $inner )" : inner) } finally { @@ -759,9 +761,10 @@ class Neo4jQuery extends Query { if (scope.entity.isRelationshipEntity()) { return dispatchCriterion(criterion.criteria, scope.forEnd(association)) } - if (scope.builder.inDisjunction) { + if (scope.builder.inDisjunction && !criterion.criteria.getCriteria().isEmpty()) { // a block in a disjunction is true for a node that has a matching neighbour, and false for one that - // has none, so the other branches still see the nodes without; a match would drop them first + // has none, so the other branches still see the nodes without; a match would drop them first. + // A block without criteria is no branch: it keeps the match, so it still asks for a neighbour. CriterionScope.Existence existence = scope.intoExistence(criterion) String conditions = dispatchCriterion(criterion.criteria, existence.scope).toString() return new CypherExpression("size([${existence.pattern}${conditions ? ' WHERE ' + conditions : ''} | 1]) > 0".toString()) From 05af39a10431f5e6e0ffe67ff64ce6824b81fe46 Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Sun, 15 Nov 2026 09:26:29 -0600 Subject: [PATCH 2/5] test(neo4j): empty junctions and an empty block in an or Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../gorm/tests/EmptyJunctionSpec.groovy | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/EmptyJunctionSpec.groovy diff --git a/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/EmptyJunctionSpec.groovy b/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/EmptyJunctionSpec.groovy new file mode 100644 index 00000000000..4efc9d81bc7 --- /dev/null +++ b/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/EmptyJunctionSpec.groovy @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package grails.gorm.tests + +import org.apache.grails.data.neo4j.core.Neo4jGormDatastoreSpec +import spock.lang.Issue + +/** + * A junction without criteria adds nothing to the Cypher, and a block without criteria inside an or still asks for an + * associated node, as it does on the datastores that join. + */ +@Issue('14635') +class EmptyJunctionSpec extends Neo4jGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(NullTestFolder) + } + + private void seed() { + NullTestFolder other = new NullTestFolder(name: 'Other').save(flush: true, failOnError: true) + new NullTestFolder(name: 'Root', parent: other).save(flush: true, failOnError: true) + NullTestFolder.withSession { it.clear() } + } + + void "an empty or beside a property adds nothing"() { + given: + seed() + + when: + List folders = NullTestFolder.createCriteria().list { + or {} + eq('name', 'Root') + } + + then: + folders*.name == ['Root'] + } + + void "an empty and beside a property adds nothing"() { + given: + seed() + + when: + List folders = NullTestFolder.createCriteria().list { + and {} + eq('name', 'Root') + } + + then: + folders*.name == ['Root'] + } + + void "an empty or alone matches every node"() { + given: + seed() + + when: + List folders = NullTestFolder.createCriteria().list { or {} } + + then: + folders*.name.sort() == ['Other', 'Root'] + } + + void "an empty association block in an or still requires an associated node"() { + given: + seed() + + when: + List folders = NullTestFolder.createCriteria().list { + or { + parent {} + eq('name', 'Other') + } + } + + then: 'the block adds no branch, and the node without a parent is left out' + folders.isEmpty() + } +} From d58326dfc3c270e469f2e0a2f8a11058879d7634 Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Sun, 15 Nov 2026 20:42:42 -0600 Subject: [PATCH 3/5] fix(neo4j): an empty junction is true wherever it stands A junction without criteria produced no Cypher, so the callers dropped it: or {} found no restriction, not { and {} } found every node, and an empty not {} was accepted. Hibernate 5, the reference, treats an empty junction as true: it is neutral in an and, makes the or it stands in true and makes a negation false; an empty not {} is an error. The Cypher builder now answers a junction with no criteria with true and rejects an empty negation. A block without criteria still keeps the match and adds no condition, so it is no branch of the or it stands in. Adds the shared table CriteriaEmptyJunctionSpec (52 shapes, Hibernate 5's results as the expected values); Neo4j skips a negated association block (it asks whether a neighbour exists) and the two detached inList rows (its inList reads only the listed values and ignores a subquery). Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../gorm/neo4j/engine/Neo4jQuery.groovy | 19 ++- .../tests/CriteriaEmptyJunctionSpec.groovy | 156 ++++++++++++++++++ 2 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy index afcd639a123..aabb2371be8 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy @@ -182,12 +182,21 @@ class Neo4jQuery extends Query { } ] as Map, ProjectionHandler> + /** + * A junction with no criteria is true wherever it stands, as it is on Hibernate 5: it adds no restriction in an and, + * makes the or it stands in true, and makes a negation false. + */ + private static final CypherExpression EMPTY_JUNCTION = new CypherExpression('true') + public static Map, CriterionHandler> CRITERION_HANDLERS = [ (Query.Conjunction): new CriterionHandler() { @Override @CompileStatic CypherExpression handle(CriterionScope scope, Query.Conjunction criterion) { + if (((Query.Junction) criterion).criteria.isEmpty()) { + return EMPTY_JUNCTION + } def inner = ((Query.Junction) criterion).criteria .collect { Query.Criterion it -> dispatchCriterion(it, scope).toString() } .findAll { String expression -> expression } @@ -200,6 +209,9 @@ class Neo4jQuery extends Query { @Override @CompileStatic CypherExpression handle(CriterionScope scope, Query.Disjunction criterion) { + if (((Query.Junction) criterion).criteria.isEmpty()) { + return EMPTY_JUNCTION + } scope.builder.enterDisjunction() try { def inner = ((Query.Junction) criterion).criteria @@ -218,6 +230,9 @@ class Neo4jQuery extends Query { @CompileStatic CypherExpression handle(CriterionScope scope, Query.Negation criterion) { List criteria = criterion.criteria + if (criteria.isEmpty()) { + throw new IllegalArgumentException('Logical expression [not] must contain at least 1 expression') + } def disjunction = new Query.Disjunction(criteria) new CypherExpression("NOT (${dispatchCriterion(disjunction, scope)})") } @@ -769,7 +784,9 @@ class Neo4jQuery extends Query { String conditions = dispatchCriterion(criterion.criteria, existence.scope).toString() return new CypherExpression("size([${existence.pattern}${conditions ? ' WHERE ' + conditions : ''} | 1]) > 0".toString()) } - return dispatchCriterion(criterion.criteria, scope.into(criterion)) + CypherExpression conditions = dispatchCriterion(criterion.criteria, scope.into(criterion)) + // a block without criteria keeps the match but adds no condition: its empty conjunction is not a branch + return criterion.criteria.getCriteria().isEmpty() ? new CypherExpression('') : conditions } /** diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy new file mode 100644 index 00000000000..d2debc28479 --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.tests + +import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec +import org.apache.grails.data.testing.tck.domains.ChildEntity +import org.apache.grails.data.testing.tck.domains.TestEntity +import spock.lang.IgnoreIf +import spock.lang.Issue +import spock.lang.Unroll + +/** + * An empty junction, {@code or {}} or {@code and {}}, in every position a criteria can put one: alone, beside a property, in + * another junction, under a negation, in an association block, and in each form a query takes (list, count, get, paged and + * ordered, projected, detached, in a subquery). The expected values are what Hibernate 5, the reference, returns, so each + * datastore answers the same: an empty junction is true wherever it stands, so it adds no restriction beside a property, + * makes the {@code or} it stands in true, is neutral in an {@code and}, and makes a negation false. Three shapes are H5 + * behaviours that are kept and named here: an empty {@code not {}} is an error, and a negated association block, and an + * association block with no criteria beside another branch of an {@code or}, still require an associated entity. + * + * Each row lists the datastores that skip it until their own fix is merged, by the {@code .gorm.suite} property: + * the in-memory store and Neo4j skip every row (they treat an empty junction as matching nothing), MongoDB has no association + * blocks and one subquery row to fix, and Hibernate 5 skips the three association blocks in an {@code or} until the fix of + * #16931 is merged up from 7.0.x. Each datastore's change removes its own skips. + */ +@Issue('https://github.com/apache/grails-core/issues/14635') +class CriteriaEmptyJunctionSpec extends GrailsDataTckSpec { + + void setupSpec() { + manager.registerDomainClasses(TestEntity, ChildEntity) + } + + /** with has a child, other has a child, orphan has none */ + private void seed() { + new TestEntity(name: 'with', child: new ChildEntity(name: 'kid')).save(flush: true, failOnError: true) + new TestEntity(name: 'other', child: new ChildEntity(name: 'stranger')).save(flush: true, failOnError: true) + new TestEntity(name: 'orphan').save(flush: true, failOnError: true) + manager.session.clear() + } + + /** a query that fails is the string THROWS, whatever the datastore calls the error */ + private static Object outcome(Closure query) { + try { + return query.call() + } + catch (Throwable ignored) { + return 'THROWS' + } + } + + @Unroll + @IgnoreIf({ data.skip.any { String store -> System.getProperty(store + '.gorm.suite') == 'true' } }) + void 'an empty junction in #form'() { + given: + seed() + + when: + Object found = outcome { -> TestEntity.createCriteria().list(body)*.name.unique().sort() } + + then: + found == expected + + where: + [form, body, expected, skip] << LISTS + } + + @Unroll + @IgnoreIf({ data.skip.any { String store -> System.getProperty(store + '.gorm.suite') == 'true' } }) + void 'an empty junction in the query form #form'() { + given: + seed() + + when: + Object found = outcome(body) + + then: + found == expected + + where: + [form, body, expected, skip] << QUERIES + } + + private static final List LISTS = [ + ['or {}', { -> or { } }, ['orphan', 'other', 'with'], ['simple']], + ['and {}', { -> and { } }, ['orphan', 'other', 'with'], ['simple']], + ['not { or {} }', { -> not { or { } } }, [], ['simple']], + ['not { and {} }', { -> not { and { } } }, [], ['simple']], + ['or {} + eq name=orphan', { -> or { }; eq('name', 'orphan') }, ['orphan'], ['simple']], + ['and {} + eq name=orphan', { -> and { }; eq('name', 'orphan') }, ['orphan'], ['simple']], + ['not { or {} } + eq name=orphan', { -> not { or { } }; eq('name', 'orphan') }, [], ['simple']], + ['not { and {} } + eq name=orphan', { -> not { and { } }; eq('name', 'orphan') }, [], ['simple']], + ['or { or {}; eq name=orphan }', { -> or { or { }; eq('name', 'orphan') } }, ['orphan', 'other', 'with'], ['simple']], + ['or { and {}; eq name=orphan }', { -> or { and { }; eq('name', 'orphan') } }, ['orphan', 'other', 'with'], ['simple']], + ['and { or {}; eq name=orphan }', { -> and { or { }; eq('name', 'orphan') } }, ['orphan'], ['simple']], + ['and { and {}; eq name=orphan }', { -> and { and { }; eq('name', 'orphan') } }, ['orphan'], ['simple']], + ['child { or {} }', { -> child { or { } } }, ['other', 'with'], ['simple', 'mongodb']], + ['child { and {} }', { -> child { and { } } }, ['other', 'with'], ['simple', 'mongodb']], + ['child { not { or {} } }', { -> child { not { or { } } } }, [], ['simple', 'mongodb']], + ['child { not { and {} } }', { -> child { not { and { } } } }, [], ['simple', 'mongodb']], + ['child { or {}; eq name=kid }', { -> child { or { }; eq('name', 'kid') } }, ['with'], ['simple', 'mongodb']], + ['child { and {}; eq name=kid }', { -> child { and { }; eq('name', 'kid') } }, ['with'], ['simple', 'mongodb']], + ['child { not { and {} }; eq name=kid }', { -> child { not { and { } }; eq('name', 'kid') } }, [], ['simple', 'mongodb']], + ['child { not { or {} }; eq name=kid }', { -> child { not { or { } }; eq('name', 'kid') } }, [], ['simple', 'mongodb']], + ['or { child { and {} }; eq name=orphan }', { -> or { child { and { } }; eq('name', 'orphan') } }, ['orphan', 'other', 'with'], ['simple', 'mongodb', 'hibernate5']], + ['or { child { or {} }; eq name=orphan }', { -> or { child { or { } }; eq('name', 'orphan') } }, ['orphan', 'other', 'with'], ['simple', 'mongodb', 'hibernate5']], + ['child {} (an empty block)', { -> child { } }, ['other', 'with'], ['simple', 'mongodb']], + ['or { child {}; eq name=orphan }', { -> or { child { }; eq('name', 'orphan') } }, [], ['simple', 'mongodb']], + ['not { not { or {} } }', { -> not { not { or { } } } }, ['orphan', 'other', 'with'], ['simple']], + ['or { and { or {} } }', { -> or { and { or { } } } }, ['orphan', 'other', 'with'], ['simple']], + ['and { or { or {} } }', { -> and { or { or { } } } }, ['orphan', 'other', 'with'], ['simple']], + ['or { or {}; and {} }', { -> or { or { }; and { } } }, ['orphan', 'other', 'with'], ['simple']], + ['and { or {}; or {} }', { -> and { or { }; or { } } }, ['orphan', 'other', 'with'], ['simple']], + ['or { or {} }', { -> or { or { } } }, ['orphan', 'other', 'with'], ['simple']], + ['not {}', { -> not { } }, 'THROWS', ['simple']], + ['not { or {}; eq name=orphan }', { -> not { or { }; eq('name', 'orphan') } }, [], ['simple']], + ['not { and {}; eq name=orphan }', { -> not { and { }; eq('name', 'orphan') } }, [], ['simple']], + ['not { child { or {} } }', { -> not { child { or { } } } }, [], ['simple', 'mongodb', 'neo4j']], + ['child { or { or {}; eq name=kid } }', { -> child { or { or { }; eq('name', 'kid') } } }, ['other', 'with'], ['simple', 'mongodb']], + ['or { child { not { or {} } }; eq name=orphan }', { -> or { child { not { or { } } }; eq('name', 'orphan') } }, ['orphan'], ['simple', 'mongodb', 'hibernate5']], + ['or { eq name=orphan; or {} }', { -> or { eq('name', 'orphan'); or { } } }, ['orphan', 'other', 'with'], ['simple']], + ['or { eq name=orphan; and {} }', { -> or { eq('name', 'orphan'); and { } } }, ['orphan', 'other', 'with'], ['simple']], + ['and { eq name=orphan; or {} }', { -> and { eq('name', 'orphan'); or { } } }, ['orphan'], ['simple']] + ] + + private static final List QUERIES = [ + ['count { or {} }', { -> TestEntity.createCriteria().count { or { } } }, 3, ['simple']], + ['count { not { or {} } }', { -> TestEntity.createCriteria().count { not { or { } } } }, 0, ['simple']], + ['count { not { and {} } }', { -> TestEntity.createCriteria().count { not { and { } } } }, 0, ['simple']], + ['get { not { and {} } } (no row matches)', { -> TestEntity.createCriteria().get { not { and { } } }?.name }, null, ['simple']], + ['get { or {}; eq name=orphan }', { -> TestEntity.createCriteria().get { or { }; eq('name', 'orphan') }?.name }, 'orphan', ['simple']], + ['list(max:1) { or {}; order name }', { -> TestEntity.createCriteria().list(max: 1) { or { }; order('name') }*.name }, ['orphan'], ['simple']], + ['list(max:1) { not { and {} }; order name }', { -> TestEntity.createCriteria().list(max: 1) { not { and { } }; order('name') }*.name }, [], ['simple']], + ['projections name { or {} }', { -> TestEntity.createCriteria().list { projections { property('name') }; or { } }.sort() }, ['orphan', 'other', 'with'], ['simple']], + ['DetachedCriteria { or {} } list', { -> new grails.gorm.DetachedCriteria(TestEntity).build { or { } }.list()*.name.sort() }, ['orphan', 'other', 'with'], ['simple']], + ['DetachedCriteria { not { and {} } } list', { -> new grails.gorm.DetachedCriteria(TestEntity).build { not { and { } } }.list()*.name.sort() }, [], ['simple']], + ['DetachedCriteria { or {} } count', { -> new grails.gorm.DetachedCriteria(TestEntity).build { or { } }.count() }, 3, ['simple']], + ['inList id of a detached { or {} }', { -> TestEntity.createCriteria().list { inList('id', new grails.gorm.DetachedCriteria(TestEntity).build { or { } }.id()) }*.name.sort() }, ['orphan', 'other', 'with'], ['simple', 'mongodb', 'neo4j']], + ['inList id of a detached { not { and {} } }', { -> TestEntity.createCriteria().list { inList('id', new grails.gorm.DetachedCriteria(TestEntity).build { not { and { } } }.id()) }*.name.sort() }, [], ['simple', 'neo4j']] + ] +} From f8f04ac6c4d5060a316b4f4ecc80c51269880ec5 Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Mon, 16 Nov 2026 07:24:06 -0600 Subject: [PATCH 4/5] fix(neo4j): a negated association block asks for a neighbour that fails it, and a negation with nothing to negate is refused not { parent {} } failed to parse: the block adds a match and an empty expression, which the negation wrapped as NOT (). A negation with nothing left to negate now throws the same IllegalArgumentException an empty not {} does, as Hibernate 5 does. not { child { or {} } } returned the node without a child, because the block was an existence check under a NOT. A block with criteria is a join on Hibernate 5, so its negation is a neighbour that fails the criteria and a node without a neighbour is not in it. The negation is now the conjunction of the negations of its criteria, with such a block as an existence check of a neighbour that fails its criteria. CriteriaEmptyJunctionSpec gains three rows (not of a block with an and, with nothing, with a property) and Neo4j no longer skips not { child { or {} } }. H5, H7 with the fix of #17010 and Neo4j agree on all of them. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../gorm/neo4j/engine/Neo4jQuery.groovy | 43 ++++++++++++++++++- .../tests/CriteriaEmptyJunctionSpec.groovy | 5 ++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy index aabb2371be8..8d48cc24b5a 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy @@ -233,8 +233,47 @@ class Neo4jQuery extends Query { if (criteria.isEmpty()) { throw new IllegalArgumentException('Logical expression [not] must contain at least 1 expression') } - def disjunction = new Query.Disjunction(criteria) - new CypherExpression("NOT (${dispatchCriterion(disjunction, scope)})") + // a block with criteria asks for a neighbour, as a join does, so its negation is a neighbour that fails them: a node + // without a neighbour is not in it. The other criteria are negated together + List others = [] + List negatedBlocks = [] + for (Query.Criterion child : criteria) { + if (negatesAsExistence(scope, child)) { + negatedBlocks << negatedBlock(scope, (AssociationQuery) child) + } + else { + others << child + } + } + List parts = [] + String negated = others.isEmpty() ? '' : dispatchCriterion(new Query.Disjunction(others), scope).toString() + if (negated) { + parts << "NOT ($negated)".toString() + } + parts.addAll(negatedBlocks) + if (parts.isEmpty()) { + // nothing is left to negate, as in a negation of a block without criteria: Hibernate 5 refuses it + throw new IllegalArgumentException('Logical expression [not] must contain at least 1 expression') + } + new CypherExpression(parts.size() == 1 ? parts[0] : "( ${parts.join(CriterionHandler.OPERATOR_AND)} )".toString()) + } + + private boolean negatesAsExistence(CriterionScope scope, Query.Criterion child) { + child instanceof AssociationQuery && !((AssociationQuery) child).criteria.getCriteria().isEmpty() && + !(((AssociationQuery) child).association instanceof Embedded) && !scope.entity.isRelationshipEntity() + } + + private String negatedBlock(CriterionScope scope, AssociationQuery block) { + CriterionScope.Existence existence = scope.intoExistence(block) + scope.builder.enterDisjunction() + String conditions + try { + conditions = dispatchCriterion(block.criteria, existence.scope).toString() + } + finally { + scope.builder.leaveDisjunction() + } + "size([${existence.pattern} WHERE NOT (${conditions ?: 'true'}) | 1]) > 0".toString() } }, (Query.Equals): new CriterionHandler() { diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy index d2debc28479..5ac74478e0d 100644 --- a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy @@ -130,7 +130,10 @@ class CriteriaEmptyJunctionSpec extends GrailsDataTckSpec { ['not {}', { -> not { } }, 'THROWS', ['simple']], ['not { or {}; eq name=orphan }', { -> not { or { }; eq('name', 'orphan') } }, [], ['simple']], ['not { and {}; eq name=orphan }', { -> not { and { }; eq('name', 'orphan') } }, [], ['simple']], - ['not { child { or {} } }', { -> not { child { or { } } } }, [], ['simple', 'mongodb', 'neo4j']], + ['not { child { or {} } }', { -> not { child { or { } } } }, [], ['simple', 'mongodb']], + ['not { child { and {} } }', { -> not { child { and { } } } }, [], ['simple', 'mongodb']], + ['not { child {} } (nothing to negate)', { -> not { child { } } }, 'THROWS', ['simple', 'mongodb']], + ['not { child { eq name=kid } }', { -> not { child { eq('name', 'kid') } } }, ['other'], ['simple', 'mongodb']], ['child { or { or {}; eq name=kid } }', { -> child { or { or { }; eq('name', 'kid') } } }, ['other', 'with'], ['simple', 'mongodb']], ['or { child { not { or {} } }; eq name=orphan }', { -> or { child { not { or { } } }; eq('name', 'orphan') } }, ['orphan'], ['simple', 'mongodb', 'hibernate5']], ['or { eq name=orphan; or {} }', { -> or { eq('name', 'orphan'); or { } } }, ['orphan', 'other', 'with'], ['simple']], From 66e42140a8bb383516661198b58e0362bf9f0209 Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Tue, 17 Nov 2026 13:23:11 -0600 Subject: [PATCH 5/5] fix(neo4j): a junction whose children add no expression is true, as a junction without children is or { or { child {} }; eq('name', 'orphan') } returned nothing: the inner junction holds only an empty association block, which adds no expression, so the junction added nothing and was dropped from the outer or. A junction whose children add no expression is true wherever it stands, as an empty one is. Hibernate 5, the reference, returns the nodes that have a neighbour for this shape, and so does Neo4j now. A negation of such a junction still has nothing to negate and is refused, as on Hibernate 5. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../datastore/gorm/neo4j/engine/Neo4jQuery.groovy | 10 +++++++--- .../testing/tck/tests/CriteriaEmptyJunctionSpec.groovy | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy index 8d48cc24b5a..ac65893a9ba 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/engine/Neo4jQuery.groovy @@ -201,7 +201,8 @@ class Neo4jQuery extends Query { .collect { Query.Criterion it -> dispatchCriterion(it, scope).toString() } .findAll { String expression -> expression } .join(CriterionHandler.OPERATOR_AND) - return new CypherExpression(inner ? "( $inner )" : inner) + // a junction whose children add no expression, as one that holds only an empty block, is true like one without children + return inner ? new CypherExpression("( $inner )") : EMPTY_JUNCTION } }, (Query.Disjunction): new CriterionHandler() { @@ -218,7 +219,8 @@ class Neo4jQuery extends Query { .collect { Query.Criterion it -> dispatchCriterion(it, scope).toString() } .findAll { String expression -> expression } .join(CriterionHandler.OPERATOR_OR) - return new CypherExpression(inner ? "( $inner )" : inner) + // a junction whose children add no expression, as one that holds only an empty block, is true like one without children + return inner ? new CypherExpression("( $inner )") : EMPTY_JUNCTION } finally { scope.builder.leaveDisjunction() } @@ -246,7 +248,9 @@ class Neo4jQuery extends Query { } } List parts = [] - String negated = others.isEmpty() ? '' : dispatchCriterion(new Query.Disjunction(others), scope).toString() + // a disjunction whose children add no expression is the shared true value: there is nothing to negate + CypherExpression disjunction = others.isEmpty() ? null : dispatchCriterion(new Query.Disjunction(others), scope) + String negated = disjunction == null || disjunction.is(EMPTY_JUNCTION) ? '' : disjunction.toString() if (negated) { parts << "NOT ($negated)".toString() } diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy index 5ac74478e0d..63002d63068 100644 --- a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/CriteriaEmptyJunctionSpec.groovy @@ -119,6 +119,7 @@ class CriteriaEmptyJunctionSpec extends GrailsDataTckSpec { ['child { not { or {} }; eq name=kid }', { -> child { not { or { } }; eq('name', 'kid') } }, [], ['simple', 'mongodb']], ['or { child { and {} }; eq name=orphan }', { -> or { child { and { } }; eq('name', 'orphan') } }, ['orphan', 'other', 'with'], ['simple', 'mongodb', 'hibernate5']], ['or { child { or {} }; eq name=orphan }', { -> or { child { or { } }; eq('name', 'orphan') } }, ['orphan', 'other', 'with'], ['simple', 'mongodb', 'hibernate5']], + ['or { or { child {} }; eq name=orphan }', { -> or { or { child { } }; eq('name', 'orphan') } }, ['other', 'with'], ['simple', 'mongodb']], ['child {} (an empty block)', { -> child { } }, ['other', 'with'], ['simple', 'mongodb']], ['or { child {}; eq name=orphan }', { -> or { child { }; eq('name', 'orphan') } }, [], ['simple', 'mongodb']], ['not { not { or {} } }', { -> not { not { or { } } } }, ['orphan', 'other', 'with'], ['simple']],