From edbece8125f76d9c07559e4c5bd9a1461bbd2bb3 Mon Sep 17 00:00:00 2001 From: Cedomir Igaly Date: Thu, 30 May 2024 10:08:41 +0200 Subject: [PATCH] HHH-18201 - Do not make any assumptions when LHS operand of addition/subtraction is not known and RHS operand is of duration type. Allowed combinations are templral +/- duration duration +/- duration nut final decision can be only make knowing full context --- .../query/sqm/tree/expression/SqmBinaryArithmetic.java | 10 ++++++++++ .../java/org/hibernate/type/spi/TypeConfiguration.java | 10 ++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmBinaryArithmetic.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmBinaryArithmetic.java index 44999c9d68..dc80903ec5 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmBinaryArithmetic.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmBinaryArithmetic.java @@ -14,6 +14,12 @@ import org.hibernate.query.sqm.SqmExpressible; import org.hibernate.query.sqm.tree.SqmCopyContext; import org.hibernate.query.sqm.tree.select.SqmSelectableNode; +import org.checkerframework.checker.nullness.qual.Nullable; + +import static org.hibernate.query.sqm.BinaryArithmeticOperator.ADD; +import static org.hibernate.query.sqm.BinaryArithmeticOperator.SUBTRACT; +import static org.hibernate.type.spi.TypeConfiguration.isDuration; + /** * @author Steve Ebersole */ @@ -42,6 +48,10 @@ public class SqmBinaryArithmetic extends AbstractSqmExpression implements this.operator = operator; this.rhsOperand = rhsOperand; + if ( lhsOperand.getExpressible() == null && isDuration( rhsOperand.getExpressible() ) && + ( operator == ADD || operator == SUBTRACT ) ) { + return; + } this.lhsOperand.applyInferableType( rhsOperand.getNodeType() ); this.rhsOperand.applyInferableType( lhsOperand.getNodeType() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java b/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java index 585e61fac1..5d79fa24cf 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java +++ b/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java @@ -682,9 +682,15 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable { } } else if ( isDuration( secondType ) ) { - // it's either addition/subtraction of durations + // if firstType is not known, and operator is + // addition/subtraction, then this can be + // either addition/subtraction of duration + // to/from temporal or addition/subtraction of + // durations in this case we shall return null; + // otherwise, it's either addition/subtraction of durations // or prefix scalar multiplication of a duration - return secondType; +// return secondType; + return firstType == null ? null : secondType; } else if ( firstType==null && getSqlTemporalType( secondType ) != null ) { // subtraction of a date or timestamp from a