Skip to content

Commit

Permalink
HHH-17454 Correct type checking with concrete generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed Nov 22, 2023
1 parent b3b0a8d commit c6bc135
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
package org.hibernate.query.sqm.internal;

import jakarta.persistence.criteria.Expression;
import jakarta.persistence.metamodel.EmbeddableType;
import jakarta.persistence.metamodel.EntityType;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.model.domain.DomainType;
import org.hibernate.metamodel.model.domain.TupleType;
import org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPathSource;
import org.hibernate.metamodel.model.domain.internal.EmbeddedSqmPathSource;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.SemanticException;
import org.hibernate.query.sqm.BinaryArithmeticOperator;
Expand Down Expand Up @@ -119,10 +119,10 @@ public static boolean areTypesComparable(

// for embeddables, the embeddable class must match exactly

if ( lhsType instanceof EmbeddedSqmPathSource && rhsType instanceof EmbeddedSqmPathSource ) {
if ( lhsType instanceof EmbeddableType<?> && rhsType instanceof EmbeddableType<?> ) {
return areEmbeddableTypesComparable(
(EmbeddedSqmPathSource<?>) lhsType,
(EmbeddedSqmPathSource<?>) rhsType
(EmbeddableType<?>) lhsType,
(EmbeddableType<?>) rhsType
);
}

Expand All @@ -134,8 +134,8 @@ public static boolean areTypesComparable(

// allow comparing an embeddable against a tuple literal

if ( lhsType instanceof EmbeddedSqmPathSource<?> && rhsType instanceof TupleType
|| rhsType instanceof EmbeddedSqmPathSource<?> && lhsType instanceof TupleType ) {
if ( lhsType instanceof EmbeddableType<?> && rhsType instanceof TupleType
|| rhsType instanceof EmbeddableType<?> && lhsType instanceof TupleType ) {
// TODO: do something meaningful here
return true;
}
Expand Down Expand Up @@ -192,10 +192,10 @@ public static boolean areTypesComparable(
}

private static boolean areEmbeddableTypesComparable(
EmbeddedSqmPathSource<?> lhsType,
EmbeddedSqmPathSource<?> rhsType) {
EmbeddableType<?> lhsType,
EmbeddableType<?> rhsType) {
// no polymorphism for embeddable types
return rhsType.getNodeJavaType() == lhsType.getNodeJavaType();
return rhsType.getJavaType() == lhsType.getJavaType();
}

private static boolean areTupleTypesComparable(
Expand Down Expand Up @@ -340,9 +340,9 @@ public static void assertComparable(Expression<?> x, Expression<?> y, SessionFac
}

// allow comparing literal null to things
if ( !(left instanceof SqmLiteralNull) && !(right instanceof SqmLiteralNull) ) {
final SqmExpressible<?> leftType = left.getNodeType();
final SqmExpressible<?> rightType = right.getNodeType();
if ( !( left instanceof SqmLiteralNull ) && !( right instanceof SqmLiteralNull ) ) {
final SqmExpressible<?> leftType = getNodeType( left );
final SqmExpressible<?> rightType = getNodeType( right );
if ( !areTypesComparable( leftType, rightType, factory ) ) {
throw new SemanticException(
String.format(
Expand All @@ -355,6 +355,12 @@ public static void assertComparable(Expression<?> x, Expression<?> y, SessionFac
}
}

private static SqmExpressible<?> getNodeType(SqmExpression<?> expression) {
return expression instanceof SqmPath<?> ?
( (SqmPath<?>) expression ).getResolvedModel().getSqmType() :
expression.getNodeType();
}

/**
* @see TypecheckUtil#assertComparable(Expression, Expression, SessionFactoryImplementor)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ public Class<T> getBindableJavaType() {

@Override
public DomainType<T> getSqmType() {
return getNodeType().getSqmType();
//noinspection unchecked
return (DomainType<T>) getResolvedModel().getSqmType();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public SqmExpressible<T> getExpressible() {

@Override
public DomainType<T> getSqmType() {
return getReferencedPathSource().getSqmType();
//noinspection unchecked
return (DomainType<T>) getResolvedModel().getSqmType();
}

@Override
Expand Down

0 comments on commit c6bc135

Please sign in to comment.