Move some more tests and fix H2 tuple comparison issues

This commit is contained in:
Christian Beikov 2021-12-23 16:37:10 +01:00
parent 2f8d22e9e5
commit aec5ce9d8a
30 changed files with 161 additions and 74 deletions

View File

@ -57,7 +57,7 @@ public class CUBRIDSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -100,7 +100,7 @@ public class CacheSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -156,7 +156,7 @@ public class FirebirdSqlAstTranslator<T extends JdbcOperation> extends AbstractS
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -106,7 +106,7 @@ public class InformixSqlAstTranslator<T extends JdbcOperation> extends AbstractS
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -113,7 +113,7 @@ public class IngresSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -58,7 +58,7 @@ public class MaxDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -58,7 +58,7 @@ public class MimerSQLSqlAstTranslator<T extends JdbcOperation> extends AbstractS
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -97,7 +97,7 @@ public class RDMSOS2200SqlAstTranslator<T extends JdbcOperation> extends Abstrac
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -177,7 +177,7 @@ public class SybaseAnywhereSqlAstTranslator<T extends JdbcOperation> extends Abs
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -121,7 +121,7 @@ public class TeradataSqlAstTranslator<T extends JdbcOperation> extends AbstractS
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -128,7 +128,7 @@ public class TimesTenSqlAstTranslator<T extends JdbcOperation> extends AbstractS
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -246,7 +246,7 @@ public class DB2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAst
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -181,7 +181,7 @@ public class DerbySqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -17,9 +17,12 @@ import org.hibernate.sql.ast.tree.cte.CteStatement;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.SqlTuple;
import org.hibernate.sql.ast.tree.expression.SqlTupleContainer;
import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate;
import org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.SelectClause;
import org.hibernate.sql.exec.spi.JdbcOperation;
/**
@ -29,6 +32,8 @@ import org.hibernate.sql.exec.spi.JdbcOperation;
*/
public class H2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAstTranslator<T> {
private boolean renderAsArray;
public H2SqlAstTranslator(SessionFactoryImplementor sessionFactory, Statement statement) {
super( sessionFactory, statement );
}
@ -79,7 +84,40 @@ public class H2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAstT
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override
public void visitInSubQueryPredicate(InSubQueryPredicate inSubQueryPredicate) {
final SqlTuple lhsTuple;
if ( ( lhsTuple = SqlTupleContainer.getSqlTuple( inSubQueryPredicate.getTestExpression() ) ) != null
&& lhsTuple.getExpressions().size() != 1 ) {
inSubQueryPredicate.getTestExpression().accept( this );
if ( inSubQueryPredicate.isNegated() ) {
appendSql( " not" );
}
appendSql( " in" );
final boolean renderAsArray = this.renderAsArray;
this.renderAsArray = true;
inSubQueryPredicate.getSubQuery().accept( this );
this.renderAsArray = renderAsArray;
}
else {
super.visitInSubQueryPredicate( inSubQueryPredicate );
}
}
@Override
protected void visitSqlSelections(SelectClause selectClause) {
final boolean renderAsArray = this.renderAsArray;
this.renderAsArray = false;
if ( renderAsArray ) {
append( OPEN_PARENTHESIS );
}
super.visitSqlSelections( selectClause );
if ( renderAsArray ) {
append( CLOSE_PARENTHESIS );
}
}
@Override
@ -100,17 +138,20 @@ public class H2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAstT
@Override
protected boolean supportsRowValueConstructorSyntax() {
return false;
// Just a guess
return getDialect().getVersion().isSameOrAfter( 1, 4, 197 );
}
@Override
protected boolean supportsRowValueConstructorSyntaxInInList() {
return false;
// Just a guess
return getDialect().getVersion().isSameOrAfter( 1, 4, 197 );
}
@Override
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
// Just a guess
return getDialect().getVersion().isSameOrAfter( 1, 4, 197 );
}
@Override

View File

@ -155,7 +155,7 @@ public class HSQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
protected void renderComparison(Expression lhs, ComparisonOperator operator, Expression rhs) {

View File

@ -335,7 +335,7 @@ public class OracleSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -393,7 +393,7 @@ public class SQLServerSqlAstTranslator<T extends JdbcOperation> extends Abstract
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -81,7 +81,7 @@ public class SpannerSqlAstTranslator<T extends JdbcOperation> extends AbstractSq
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -308,7 +308,7 @@ public class SybaseASESqlAstTranslator<T extends JdbcOperation> extends Abstract
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -133,7 +133,7 @@ public class SybaseSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
List<SqlSelection> lhsExpressions,
SqlTuple tuple,
ComparisonOperator operator) {
emulateTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
emulateSelectTupleComparison( lhsExpressions, tuple.getExpressions(), operator, true );
}
@Override

View File

@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
@ -322,7 +323,7 @@ public abstract class AbstractEntityPersister
protected final String rowIdName;
// The optional SQL string defined in the where attribute
private final String sqlWhereString;
private final String sqlWhereStringTableExpression;
private final String sqlWhereStringTemplate;
//information about properties of this class,
@ -800,17 +801,26 @@ public abstract class AbstractEntityPersister
//WHERE STRING
sqlWhereString = StringHelper.isNotEmpty( bootDescriptor.getWhere() ) ?
"(" + bootDescriptor.getWhere() + ")" :
null;
sqlWhereStringTemplate = sqlWhereString == null ?
null :
Template.renderWhereStringTemplate(
sqlWhereString,
dialect,
factory.getQueryEngine().getSqmFunctionRegistry()
);
if ( StringHelper.isEmpty( bootDescriptor.getWhere() ) ) {
sqlWhereStringTableExpression = null;
sqlWhereStringTemplate = null;
}
else {
PersistentClass containingClass = bootDescriptor;
while ( containingClass.getSuperclass() != null ) {
final PersistentClass superclass = containingClass.getSuperclass();
if ( !Objects.equals( bootDescriptor.getWhere(), superclass.getWhere() ) ) {
break;
}
containingClass = superclass;
}
this.sqlWhereStringTableExpression = containingClass.getTable().getName();
sqlWhereStringTemplate = Template.renderWhereStringTemplate(
"(" + bootDescriptor.getWhere() + ")",
dialect,
factory.getQueryEngine().getSqmFunctionRegistry()
);
}
// PROPERTIES
int hydrateSpan = entityMetamodel.getPropertySpan();
@ -2731,7 +2741,7 @@ public abstract class AbstractEntityPersister
}
protected boolean hasWhere() {
return sqlWhereString != null;
return sqlWhereStringTemplate != null;
}
private void initOrdinaryPropertyPaths(Mapping mapping) throws MappingException {
@ -4029,14 +4039,17 @@ public abstract class AbstractEntityPersister
}
final String alias;
if ( tableGroup == null ) {
final TableReference tableReference;
if ( tableGroup == null || ( tableReference = tableGroup.resolveTableReference( sqlWhereStringTableExpression ) ) == null ) {
alias = null;
}
else if ( useQualifier && tableGroup.getPrimaryTableReference().getIdentificationVariable() != null ) {
alias = tableGroup.getPrimaryTableReference().getIdentificationVariable();
}
else {
alias = tableGroup.getPrimaryTableReference().getTableId();
if ( useQualifier && tableReference.getIdentificationVariable() != null ) {
alias = tableReference.getIdentificationVariable();
}
else {
alias = tableReference.getTableId();
}
}
final String fragment = StringHelper.replace( sqlWhereStringTemplate, Template.TEMPLATE, alias );

View File

@ -71,7 +71,9 @@ import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SqmExpressable;
import org.hibernate.query.sqm.SqmQuerySource;
import org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.spi.SqmCreationContext;
import org.hibernate.query.sqm.tree.SqmTypedNode;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
@ -1350,14 +1352,20 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
@Override
public <T> SqmFunction<T> function(String name, Class<T> type, Expression<?>[] args) {
final SqmFunctionDescriptor functionTemplate = getFunctionDescriptor( name );
SqmFunctionDescriptor functionTemplate = getFunctionDescriptor( name );
final BasicType<T> resultType = getTypeConfiguration().getBasicTypeForJavaType( type );
if ( functionTemplate == null ) {
throw new SemanticException( "Could not resolve function named `" + name + "`" );
functionTemplate = new NamedSqmFunctionDescriptor(
name,
true,
null,
StandardFunctionReturnTypeResolvers.invariant( resultType )
);
}
return functionTemplate.generateSqmExpression(
expressionList( args ),
getTypeConfiguration().getBasicTypeForJavaType( type ),
resultType,
getQueryEngine(),
getJpaMetamodel().getTypeConfiguration()
);

View File

@ -14,6 +14,7 @@ import java.util.Set;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
import org.hibernate.query.FetchClauseType;
import org.hibernate.query.SemanticException;
@ -26,6 +27,7 @@ import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SemanticQueryWalker;
import org.hibernate.query.sqm.tree.SqmNode;
import org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath;
import org.hibernate.query.sqm.tree.expression.SqmAliasedNodeRef;
import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.hibernate.query.sqm.tree.from.SqmAttributeJoin;
@ -401,6 +403,13 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
else if ( selectableNode instanceof SqmFrom<?, ?> ) {
collectSelectedFromSet( selectedFromSet, (SqmFrom<?, ?>) selectableNode );
}
else if ( selectableNode instanceof SqmEntityValuedSimplePath<?> ) {
final SqmEntityValuedSimplePath<?> path = (SqmEntityValuedSimplePath<?>) selectableNode;
if ( CollectionPart.Nature.fromNameExact( path.getReferencedPathSource().getPathName() ) != null
&& path.getLhs() instanceof SqmFrom<?, ?> ) {
collectSelectedFromSet( selectedFromSet, (SqmFrom<?, ?>) path.getLhs() );
}
}
}
private void collectSelectedFromSet(Set<SqmFrom<?, ?>> selectedFromSet, SqmFrom<?, ?> sqmFrom) {

View File

@ -1815,6 +1815,27 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
}
}
protected void emulateSelectTupleComparison(
List<SqlSelection> lhsSelections,
List<? extends SqlAstNode> rhsExpressions,
ComparisonOperator operator,
boolean indexOptimized) {
final List<? extends SqlAstNode> lhsExpressions;
if ( lhsSelections.size() == rhsExpressions.size() ) {
lhsExpressions = lhsSelections;
}
else if ( lhsSelections.size() == 1 ) {
lhsExpressions = SqlTupleContainer.getSqlTuple( lhsSelections.get( 0 ).getExpression() ).getExpressions();
}
else {
final List<Expression> list = new ArrayList<>( rhsExpressions.size() );
for ( SqlSelection lhsSelection : lhsSelections ) {
list.addAll( SqlTupleContainer.getSqlTuple( lhsSelection.getExpression() ).getExpressions() );
}
lhsExpressions = list;
}
emulateTupleComparison( lhsExpressions, rhsExpressions, operator, indexOptimized );
}
/**
* A tuple comparison like <code>(a, b) &gt; (1, 2)</code> can be emulated through it logical definition: <code>a &gt; 1 or a = 1 and b &gt; 2</code>.

View File

@ -1,4 +1,4 @@
package org.hibernate.query;
package org.hibernate.orm.test.query;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@ -27,8 +27,8 @@ public class IntegerRepresentationLiteralParsingExceptionTest extends BaseEntity
public void testAppropriateExceptionMessageGenerated() {
try {
doInJPA( this::entityManagerFactory, entityManager -> {
// -9223372036854775808 is beyond Long range, so an Exception will be thrown
entityManager.createQuery( "select count(*) from ExampleEntity where counter = -9223372036854775808L" )
// 9223372036854775808 is beyond Long range, so an Exception will be thrown
entityManager.createQuery( "select count(*) from ExampleEntity where counter = 9223372036854775808L" )
.getSingleResult();
} );
Assert.fail( "Exception should be thrown" );
@ -37,7 +37,7 @@ public class IntegerRepresentationLiteralParsingExceptionTest extends BaseEntity
// without fixing HHH-14213, the following exception would be thrown:
// "Could not parse literal [9223372036854775808L] as integer"
// which is confusing and misleading
Assert.assertTrue( e.getMessage().endsWith( " as java.lang.Long" ) );
Assert.assertTrue( e.getMessage().endsWith( " to Long" ) );
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.query.hhh12076;
package org.hibernate.orm.test.query.hhh12076;
import java.util.Date;
import java.util.HashMap;
@ -1078,9 +1078,9 @@ public class AnnotationMappingJoinClassTest extends BaseCoreFunctionalTestCase {
@OneToMany(mappedBy = "parent")
private Set<Task<?>> children = new HashSet<>();
@OneToMany(mappedBy = "status")
private Set<Task<?>> linkedTasks = new HashSet<>();
//
// @OneToMany(mappedBy = "status")
// private Set<Task<?>> linkedTasks = new HashSet<>();
public abstract T getLinked();
public abstract void setLinked(T linked);
@ -1183,13 +1183,13 @@ public class AnnotationMappingJoinClassTest extends BaseCoreFunctionalTestCase {
parent = parentTask;
}
public Set<Task<?>> getLinkedTasks() {
return linkedTasks;
}
public void setLinkedTasks(Set<Task<?>> linkedTasks) {
this.linkedTasks = linkedTasks;
}
// public Set<Task<?>> getLinkedTasks() {
// return linkedTasks;
// }
//
// public void setLinkedTasks(Set<Task<?>> linkedTasks) {
// this.linkedTasks = linkedTasks;
// }
public Date getStateTransitionDate() {
return stateTransitionDate;

View File

@ -1,4 +1,4 @@
package org.hibernate.query.hhh14112;
package org.hibernate.orm.test.query.hhh14112;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
@ -36,7 +36,7 @@ public class HHH14112Test extends BaseCoreFunctionalTestCase {
@Entity(name = "Super")
@Inheritance(strategy = InheritanceType.JOINED)
@Where(clause = "DELETED = false")
@Where(clause = "deleted = false")
public static class Super {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

View File

@ -1,4 +1,4 @@
package org.hibernate.query.hhh14116;
package org.hibernate.orm.test.query.hhh14116;
import java.util.Set;
import jakarta.persistence.ElementCollection;

View File

@ -1,4 +1,4 @@
package org.hibernate.query.hhh14154;
package org.hibernate.orm.test.query.hhh14154;
import java.util.Date;
import jakarta.persistence.Entity;

View File

@ -1,20 +1,17 @@
package org.hibernate.query.hhh14156;
package org.hibernate.orm.test.query.hhh14156;
import java.io.Serializable;
import java.util.Objects;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Nathan Xu
* @author Christian Beikov
@ -28,8 +25,6 @@ public class HHH14156Test extends BaseCoreFunctionalTestCase {
}
@Test
@SkipForDialect(value = SQLServerDialect.class, comment = "SQLServer doesn't support tuple comparisons")
@SkipForDialect(value = DerbyDialect.class, comment = "Derby doesn't support tuple comparisons")
public void testNoExceptionThrown() {
inTransaction( session ->
session.createQuery(