Skip to content

Commit dd6a724

Browse files
committed
slightly clean up handling of arithmetic types in SQM
1 parent 1144b60 commit dd6a724

File tree

8 files changed

+56
-41
lines changed

8 files changed

+56
-41
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/ConverterDescriptorImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext;
1212
import org.hibernate.type.descriptor.converter.internal.AttributeConverterBean;
1313
import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter;
14-
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
1514

1615

1716
class ConverterDescriptorImpl<X, Y> implements ConverterDescriptor<X, Y> {
@@ -55,7 +54,7 @@ public AutoApplicableConverterDescriptor getAutoApplyDescriptor() {
5554

5655
@Override
5756
public JpaAttributeConverter<X, Y> createJpaAttributeConverter(JpaAttributeConverterCreationContext context) {
58-
final JavaTypeRegistry javaTypeRegistry = context.getTypeConfiguration().getJavaTypeRegistry();
57+
final var javaTypeRegistry = context.getTypeConfiguration().getJavaTypeRegistry();
5958
final var converterBean = context.getManagedBeanRegistry().getBean( converterType );
6059
return new AttributeConverterBean<>(
6160
converterBean,

hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3772,7 +3772,7 @@ public SqmUnaryOperation<?> visitUnaryExpression(HqlParser.UnaryExpressionContex
37723772
final SqmExpression<?> expression = (SqmExpression<?>) ctx.expression().accept(this);
37733773
final UnaryArithmeticOperator operator = (UnaryArithmeticOperator) ctx.signOperator().accept(this);
37743774
TypecheckUtil.assertNumeric( expression, operator );
3775-
return new SqmUnaryOperation<>( operator, expression );
3775+
return new SqmUnaryOperation<>( operator, expression, nodeBuilder() );
37763776
}
37773777

37783778
@Override

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,8 @@ public <N extends Number> SqmExpression<N> neg(Expression<N> x) {
12191219
final SqmExpression<N> sqmExpression = (SqmExpression<N>) x;
12201220
return new SqmUnaryOperation<>(
12211221
UnaryArithmeticOperator.UNARY_MINUS,
1222-
sqmExpression
1222+
sqmExpression,
1223+
getNodeBuilder()
12231224
);
12241225
}
12251226

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import org.hibernate.query.sqm.DiscriminatorSqmPath;
108108
import org.hibernate.query.sqm.DynamicInstantiationNature;
109109
import org.hibernate.query.sqm.InterpretationException;
110+
import org.hibernate.query.sqm.SqmBindableType;
110111
import org.hibernate.query.sqm.SqmExpressible;
111112
import org.hibernate.query.sqm.SqmPathSource;
112113
import org.hibernate.query.sqm.SqmQuerySource;
@@ -5985,8 +5986,8 @@ private MappingModelExpressible<?> determineValueMapping(SqmExpression<?> sqmExp
59855986
return (MappingModelExpressible<?>) getTypeConfiguration().resolveArithmeticType(
59865987
// These casts should be safe, since the only JdbcMapping is BasicType
59875988
// which also implements SqmExpressible
5988-
lhs == null ? null : (SqmExpressible<?>) lhs.getSingleJdbcMapping(),
5989-
rhs == null ? null : (SqmExpressible<?>) rhs.getSingleJdbcMapping()
5989+
lhs == null ? null : (SqmBindableType<?>) lhs.getSingleJdbcMapping(),
5990+
rhs == null ? null : (SqmBindableType<?>) rhs.getSingleJdbcMapping()
59905991
);
59915992
}
59925993
}

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmBinaryArithmetic.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,28 @@ public SqmBinaryArithmetic(
3333
NodeBuilder nodeBuilder) {
3434
//noinspection unchecked
3535
super(
36-
(SqmBindableType<T>) nodeBuilder.getTypeConfiguration().resolveArithmeticType(
37-
lhsOperand.getExpressible(),
38-
rhsOperand.getExpressible(),
39-
operator
40-
),
36+
(SqmBindableType<T>) // TODO: this cast is unsound
37+
nodeBuilder.getTypeConfiguration().resolveArithmeticType(
38+
lhsOperand.getExpressible(),
39+
rhsOperand.getExpressible(),
40+
operator
41+
),
4142
nodeBuilder
4243
);
4344

4445
this.lhsOperand = lhsOperand;
4546
this.operator = operator;
4647
this.rhsOperand = rhsOperand;
4748

48-
if ( lhsOperand.getExpressible() == null && isDuration( rhsOperand.getExpressible() ) &&
49-
( operator == ADD || operator == SUBTRACT ) ) {
49+
final SqmBindableType<?> lhsExpressible = lhsOperand.getExpressible();
50+
final SqmBindableType<?> rhsExpressible = rhsOperand.getExpressible();
51+
if ( lhsExpressible == null
52+
&& isDuration( rhsExpressible )
53+
&& ( operator == ADD || operator == SUBTRACT ) ) {
5054
return;
5155
}
52-
this.lhsOperand.applyInferableType( rhsOperand.getExpressible() );
53-
this.rhsOperand.applyInferableType( lhsOperand.getExpressible() );
56+
this.lhsOperand.applyInferableType( rhsExpressible );
57+
this.rhsOperand.applyInferableType( lhsExpressible );
5458
}
5559

5660
public SqmBinaryArithmetic(

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/expression/SqmUnaryOperation.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.query.sqm.tree.expression;
66

7+
import org.hibernate.query.sqm.NodeBuilder;
78
import org.hibernate.query.sqm.SemanticQueryWalker;
89
import org.hibernate.query.sqm.SqmBindableType;
910
import org.hibernate.query.sqm.UnaryArithmeticOperator;
@@ -18,22 +19,27 @@ public class SqmUnaryOperation<T> extends AbstractSqmExpression<T> implements Sq
1819
private final UnaryArithmeticOperator operation;
1920
private final SqmExpression<T> operand;
2021

21-
public SqmUnaryOperation(UnaryArithmeticOperator operation, SqmExpression<T> operand) {
22+
public SqmUnaryOperation(
23+
UnaryArithmeticOperator operation,
24+
SqmExpression<T> operand,
25+
NodeBuilder nodeBuilder) {
26+
//noinspection unchecked
2227
this(
2328
operation,
2429
operand,
25-
//TODO: this is wrong, T represents the domain type, not the relational type
26-
operand.nodeBuilder().getTypeConfiguration().getBasicTypeForJavaType(
27-
operand.getExpressible().getRelationalJavaType().getJavaType()
28-
)
30+
(SqmBindableType<T>) // TODO: this cast is unsound
31+
nodeBuilder.getTypeConfiguration()
32+
.resolveArithmeticType( operand.getExpressible() ),
33+
nodeBuilder
2934
);
3035
}
3136

3237
public SqmUnaryOperation(
3338
UnaryArithmeticOperator operation,
3439
SqmExpression<T> operand,
35-
SqmBindableType<T> inherentType) {
36-
super( inherentType, operand.nodeBuilder() );
40+
SqmBindableType<T> inherentType,
41+
NodeBuilder nodeBuilder) {
42+
super( inherentType, nodeBuilder );
3743
this.operation = operation;
3844
this.operand = operand;
3945
}
@@ -49,7 +55,8 @@ public SqmUnaryOperation<T> copy(SqmCopyContext context) {
4955
new SqmUnaryOperation<>(
5056
operation,
5157
operand.copy( context ),
52-
getNodeType()
58+
getNodeType(),
59+
nodeBuilder()
5360
)
5461
);
5562
copyTo( expression, context );

hibernate-core/src/main/java/org/hibernate/type/BasicTypeRegistry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public <J> BasicType<J> resolve(Class<J> javaType, int sqlTypeCode) {
141141
return resolve( (java.lang.reflect.Type) javaType, sqlTypeCode );
142142
}
143143

144+
@Deprecated(since = "7.2") // due to the unbound type parameter
144145
public <J> BasicType<J> resolve(java.lang.reflect.Type javaType, int sqlTypeCode) {
145146
return resolve( getJavaTypeRegistry().getDescriptor( javaType ), sqlTypeCode );
146147
}

hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -684,22 +684,22 @@ public int hashCode() {
684684
/**
685685
* @see QueryHelper#highestPrecedenceType2
686686
*/
687-
public SqmExpressible<?> resolveArithmeticType(
688-
SqmExpressible<?> firstType,
689-
SqmExpressible<?> secondType,
687+
public SqmBindableType<?> resolveArithmeticType(
688+
SqmBindableType<?> firstType,
689+
SqmBindableType<?> secondType,
690690
BinaryArithmeticOperator operator) {
691691
return resolveArithmeticType( firstType, secondType );
692692
}
693693

694694
/**
695695
* Determine the result type of an arithmetic operation as defined by the
696-
* rules in section 6.5.8.1.
696+
* rules in section 6.5.8.1, taking converters into account.
697697
*
698698
* @see QueryHelper#highestPrecedenceType2
699699
*/
700-
public SqmExpressible<?> resolveArithmeticType(
701-
SqmExpressible<?> firstType,
702-
SqmExpressible<?> secondType) {
700+
public SqmBindableType<?> resolveArithmeticType(
701+
SqmBindableType<?> firstType,
702+
SqmBindableType<?> secondType) {
703703

704704
if ( getSqlTemporalType( firstType ) != null ) {
705705
if ( secondType==null || getSqlTemporalType( secondType ) != null ) {
@@ -731,20 +731,21 @@ else if ( firstType==null && getSqlTemporalType( secondType ) != null ) {
731731
}
732732

733733
if ( firstType != null && ( secondType == null
734-
|| firstType.getRelationalJavaType().isWider( secondType.getRelationalJavaType() ) ) ) {
735-
return resolveBasicArithmeticType( firstType );
734+
|| !secondType.getRelationalJavaType().isWider( firstType.getRelationalJavaType() ) ) ) {
735+
return resolveArithmeticType( firstType );
736736
}
737-
return secondType != null ? resolveBasicArithmeticType( secondType ) : null;
737+
return secondType != null ? resolveArithmeticType( secondType ) : null;
738738
}
739739

740-
private BasicType<?> resolveBasicArithmeticType(SqmExpressible<?> expressible) {
741-
if ( isNumberArray( expressible ) ) {
742-
return (BasicType<?>) expressible.getSqmType();
743-
}
744-
else {
745-
// Use the relational java type to account for possible converters
746-
return getBasicTypeForJavaType( expressible.getRelationalJavaType().getJavaTypeClass() );
747-
}
740+
/**
741+
* Determine the result type of a unary arithmetic operation,
742+
* taking converters into account.
743+
*/
744+
public SqmBindableType<?> resolveArithmeticType(SqmBindableType<?> expressible) {
745+
return isNumberArray( expressible )
746+
? expressible.getSqmType()
747+
// Use the relational java type to account for possible converters
748+
: getBasicTypeForJavaType( expressible.getRelationalJavaType().getJavaTypeClass() );
748749
}
749750

750751
private static boolean matchesJavaType(SqmExpressible<?> type, Class<?> javaType) {
@@ -797,6 +798,7 @@ public <J> BasicType<J> standardBasicTypeForJavaType(Class<J> javaType) {
797798
}
798799
}
799800

801+
@Deprecated(since = "7.2") // due to the unbound type parameter
800802
public BasicType<?> standardBasicTypeForJavaType(Type javaType) {
801803
if ( javaType == null ) {
802804
return null;

0 commit comments

Comments
 (0)