From 7b064afbd33729ae15d06aba1bdb5cf17b2d5dca Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 30 Jan 2020 06:31:42 +0100 Subject: [PATCH] Use type inference for every added when/otherwise arm for case expressions --- .../sqm/tree/expression/SqmCaseSearched.java | 18 ++++++++++++++++-- .../sqm/tree/expression/SqmCaseSimple.java | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSearched.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSearched.java index a07591fbdf08..2c79537bbe7e 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSearched.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSearched.java @@ -12,6 +12,7 @@ import org.hibernate.query.criteria.JpaExpression; import org.hibernate.query.criteria.JpaSearchedCase; +import org.hibernate.query.internal.QueryHelper; import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.SqmExpressable; import org.hibernate.query.sqm.SemanticQueryWalker; @@ -45,12 +46,25 @@ public SqmExpression getOtherwise() { public void when(SqmPredicate predicate, SqmExpression result) { whenFragments.add( new WhenFragment<>( predicate, result ) ); - applyInferableType( result.getNodeType() ); + applyInferableResultType( result.getNodeType() ); } public void otherwise(SqmExpression otherwiseExpression) { this.otherwise = otherwiseExpression; - applyInferableType( otherwiseExpression.getNodeType() ); + applyInferableResultType( otherwiseExpression.getNodeType() ); + } + + private void applyInferableResultType(SqmExpressable type) { + if ( type == null ) { + return; + } + + final SqmExpressable oldType = getNodeType(); + + final SqmExpressable newType = QueryHelper.highestPrecedenceType2( oldType, type ); + if ( newType != null && newType != oldType ) { + internalApplyInferableType( newType ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSimple.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSimple.java index a6e433909af2..f1dbbd1a5afd 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSimple.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmCaseSimple.java @@ -13,6 +13,7 @@ import org.hibernate.query.criteria.JpaExpression; import org.hibernate.query.criteria.JpaSimpleCase; +import org.hibernate.query.internal.QueryHelper; import org.hibernate.query.sqm.NodeBuilder; import org.hibernate.query.sqm.SqmExpressable; import org.hibernate.query.sqm.SemanticQueryWalker; @@ -52,13 +53,26 @@ public SqmExpression getOtherwise() { public void otherwise(SqmExpression otherwiseExpression) { this.otherwise = otherwiseExpression; - applyInferableType( otherwiseExpression.getNodeType() ); + applyInferableResultType( otherwiseExpression.getNodeType() ); } public void when(SqmExpression test, SqmExpression result) { whenFragments.add( new WhenFragment<>( test, result ) ); - applyInferableType( result.getNodeType() ); + applyInferableResultType( result.getNodeType() ); + } + + private void applyInferableResultType(SqmExpressable type) { + if ( type == null ) { + return; + } + + final SqmExpressable oldType = getNodeType(); + + final SqmExpressable newType = QueryHelper.highestPrecedenceType2(oldType, type ); + if ( newType != null && newType != oldType ) { + internalApplyInferableType( newType ); + } } @Override