Skip to content

HHH-19497 Test for composition of 'not' 'in' restrictions #10412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7664,14 +7664,17 @@ else if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) {
appendSql( CLOSE_PARENTHESIS );
}
else {
String separator = NO_SEPARATOR;
if (inListPredicate.isNegated()) {
appendSql("not ");
}
appendSql( OPEN_PARENTHESIS );
for ( Expression expression : listExpressions ) {
appendSql( separator );
String separator = NO_SEPARATOR;
for (Expression expression : listExpressions) {
appendSql(separator);
emulateTupleComparison(
lhsTuple.getExpressions(),
getSqlTuple( expression ).getExpressions(),
comparisonOperator,
SqlTupleContainer.getSqlTuple(expression).getExpressions(),
ComparisonOperator.EQUAL,
true
);
separator = " or ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and test. I'm just curious why you didn't change the junction type here instead. Am I missing some subtle case where using not ( (x=x1 and y=y1) or (x=x2 and y=y2) ) is "more" correct over ( (x<>x1 or y<>y1) and (x<>x2 or y<>y2) )?

Suggested change
separator = " or ";
separator = inListPredicate.isNegated() ? " and " : " or ";

I also did some testing with null values on PostgreSQL and to me it seems that both kinds of expressions are semantically equivalent. With an increasing amount of in-list items (3+), the not approach is more compact, which is why I would even prefer that. Since you seem to be on DB2, could you please let me know if there is a performance difference between the two predicate styles i.e. are the query execution plans the same?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.jpa.criteria.basic;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.ManyToOne;

import java.util.Objects;

/**
* Entity used in <code>org.hibernate.orm.test.jpa.criteria.basic.NegatedInPredicateTest</code>.
*
* @author Mike Mannion
*/
@Entity
public class Foo {

@Id
Long id;

@ManyToOne
@JoinColumns({
@JoinColumn(name = "FK_CODE", referencedColumnName = "CODE"),
@JoinColumn(name = "FK_CONTEXT", referencedColumnName = "CONTEXT")
})
FooType fooType;

public Foo() {
// For JPA
}

public Foo(Long id, FooType fooType) {
this.id = id;
this.fooType = fooType;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;

Foo customer = (Foo) o;
return Objects.equals(id, customer.id) && Objects.equals(fooType, customer.fooType);
}

@Override
public int hashCode() {
int result = Objects.hashCode(id);
result = 31 * result + Objects.hashCode(fooType);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.jpa.criteria.basic;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

/**
* Entity used in <code>org.hibernate.orm.test.jpa.criteria.basic.NegatedInPredicateTest</code>.
*
* @author Mike Mannion
*/
@Entity
public class FooType {
@Id
@Column(name = "CODE")
String code;

@Column(name = "CONTEXT")
String context;

public FooType() {
// For JPA
}

FooType(String code, String context) {
this.code = code;
this.context = context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.jpa.criteria.basic;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


/**
* Add test which composes not with in. Test introduced after discovery the negated in
* fails under dialects without record-level construction, such as DB2.
*
* @author Mike Mannion
*/
@JiraKey(value = "HHH-19497")
@DomainModel(
annotatedClasses = {Foo.class, FooType.class}
)
@SessionFactory
class NegatedInPredicateTest {

public static final FooType FOO_TYPE1 = new FooType( "ft1", "ctx1" );
public static final FooType FOO_TYPE2 = new FooType( "ft2", "ctx1" );

@BeforeEach
void setup(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
em.persist( FOO_TYPE1 );
em.persist( FOO_TYPE2 );

Foo foo1 = new Foo( 1L, FOO_TYPE1 );
Foo foo2 = new Foo( 2L, FOO_TYPE1 );
Foo foo3 = new Foo( 3L, FOO_TYPE2 );

em.persist( foo1 );
em.persist( foo2 );
em.persist( foo3 );
}
);
}

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
em.createQuery( "delete from Foo" ).executeUpdate();
em.createQuery( "delete from FooType" ).executeUpdate();
}
);
}

@Test
void testSanity(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Foo> cq = cb.createQuery( Foo.class );
Root<Foo> root = cq.from( Foo.class );
assertThat( em.createQuery( cq.select( root ) ).getResultList() )
.hasSize( 3 );
}
);
}

@Test
void testNegatedPredicate(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Foo> cq = cb.createQuery( Foo.class );
Root<Foo> root = cq.from( Foo.class );
cq.select( root )
.where( cb.not( root.get( "fooType" ).in( List.of( FOO_TYPE1, FOO_TYPE2 ) ) ) );
assertThat( em.createQuery( cq ).getResultList() )
.isEmpty();
}
);
}

@Test
void testNonNegatedInPredicate(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Foo> cq = cb.createQuery( Foo.class );
Root<Foo> root = cq.from( Foo.class );
cq.select( root )
.where( root.get( "fooType" ).in( List.of( FOO_TYPE1, FOO_TYPE2 ) ) );
assertThat( em.createQuery( cq ).getResultList() )
.hasSize( 3 );

}
);
}

}