HHH-18046 Cast parameters used as arithmetic operands on DB2

This commit is contained in:
Christian Beikov 2024-05-02 12:47:04 +02:00
parent 55ff91ba17
commit 0b1ad4e410
23 changed files with 100 additions and 114 deletions

View File

@ -139,6 +139,11 @@ public class DB2LegacySqlAstTranslator<T extends JdbcOperation> extends Abstract
} }
} }
@Override
protected void visitArithmeticOperand(Expression expression) {
render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
}
@Override @Override
public void visitBooleanExpressionPredicate(BooleanExpressionPredicate booleanExpressionPredicate) { public void visitBooleanExpressionPredicate(BooleanExpressionPredicate booleanExpressionPredicate) {
if ( getDB2Version().isSameOrAfter( 11 ) ) { if ( getDB2Version().isSameOrAfter( 11 ) ) {

View File

@ -337,23 +337,8 @@ public class DerbyLegacySqlAstTranslator<T extends JdbcOperation> extends Abstra
} }
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { protected void visitArithmeticOperand(Expression expression) {
final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
if ( operator == BinaryArithmeticOperator.MODULO ) {
append( "mod" );
appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this );
appendSql( ',' );
arithmeticExpression.getRightHandOperand().accept( this );
appendSql( CLOSE_PARENTHESIS );
}
else {
appendSql( OPEN_PARENTHESIS );
render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( CLOSE_PARENTHESIS );
}
} }
} }

View File

@ -324,12 +324,17 @@ public class H2LegacySqlAstTranslator<T extends JdbcOperation> extends AbstractS
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
@Override
protected void visitArithmeticOperand(Expression expression) {
render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
}
@Override @Override
protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lockMode) { protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lockMode) {
final TableReference tableRef = tableGroup.getPrimaryTableReference(); final TableReference tableRef = tableGroup.getPrimaryTableReference();

View File

@ -352,22 +352,7 @@ public class HSQLLegacySqlAstTranslator<T extends JdbcOperation> extends Abstrac
} }
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { protected void visitArithmeticOperand(Expression expression) {
final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
if ( operator == BinaryArithmeticOperator.MODULO ) {
append( "mod" );
appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this );
appendSql( ',' );
arithmeticExpression.getRightHandOperand().accept( this );
appendSql( CLOSE_PARENTHESIS );
}
else {
appendSql( OPEN_PARENTHESIS );
render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( CLOSE_PARENTHESIS );
}
} }
} }

View File

@ -330,9 +330,9 @@ public class PostgreSQLLegacySqlAstTranslator<T extends JdbcOperation> extends A
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -523,9 +523,9 @@ public class SQLServerLegacySqlAstTranslator<T extends JdbcOperation> extends Ab
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -460,9 +460,9 @@ public class SybaseASELegacySqlAstTranslator<T extends JdbcOperation> extends Ab
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -219,9 +219,9 @@ public class SybaseAnywhereSqlAstTranslator<T extends JdbcOperation> extends Abs
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -242,9 +242,9 @@ public class SybaseLegacySqlAstTranslator<T extends JdbcOperation> extends Abstr
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -138,6 +138,11 @@ public class DB2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAst
} }
} }
@Override
protected void visitArithmeticOperand(Expression expression) {
render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
}
@Override @Override
public void visitBooleanExpressionPredicate(BooleanExpressionPredicate booleanExpressionPredicate) { public void visitBooleanExpressionPredicate(BooleanExpressionPredicate booleanExpressionPredicate) {
if ( getDB2Version().isSameOrAfter( 11 ) ) { if ( getDB2Version().isSameOrAfter( 11 ) ) {

View File

@ -337,22 +337,7 @@ public class DerbySqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
} }
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { protected void visitArithmeticOperand(Expression expression) {
final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
if ( operator == BinaryArithmeticOperator.MODULO ) {
append( "mod" );
appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this );
appendSql( ',' );
arithmeticExpression.getRightHandOperand().accept( this );
appendSql( CLOSE_PARENTHESIS );
}
else {
appendSql( OPEN_PARENTHESIS );
render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( CLOSE_PARENTHESIS );
}
} }
} }

View File

@ -298,12 +298,17 @@ public class H2SqlAstTranslator<T extends JdbcOperation> extends SqlAstTranslato
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
@Override
protected void visitArithmeticOperand(Expression expression) {
render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
}
@Override @Override
protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lockMode) { protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lockMode) {
final TableReference tableRef = tableGroup.getPrimaryTableReference(); final TableReference tableRef = tableGroup.getPrimaryTableReference();

View File

@ -53,9 +53,9 @@ public class HANASqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) {
appendSql( "cast(" ); appendSql( "cast(" );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( " as int)" ); appendSql( " as int)" );
} }
else { else {

View File

@ -342,23 +342,8 @@ public class HSQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
} }
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { protected void visitArithmeticOperand(Expression expression) {
final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
if ( operator == BinaryArithmeticOperator.MODULO ) {
append( "mod" );
appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this );
appendSql( ',' );
arithmeticExpression.getRightHandOperand().accept( this );
appendSql( CLOSE_PARENTHESIS );
}
else {
appendSql( OPEN_PARENTHESIS );
render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
appendSql( CLOSE_PARENTHESIS );
}
} }
} }

View File

@ -56,9 +56,9 @@ public class MariaDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSq
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( " div " ); appendSql( " div " );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
else { else {

View File

@ -106,9 +106,9 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( " div " ); appendSql( " div " );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
else { else {

View File

@ -328,9 +328,9 @@ public class PostgreSQLSqlAstTranslator<T extends JdbcOperation> extends SqlAstT
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
} }

View File

@ -492,9 +492,9 @@ public class SQLServerSqlAstTranslator<T extends JdbcOperation> extends SqlAstTr
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -443,9 +443,9 @@ public class SybaseASESqlAstTranslator<T extends JdbcOperation> extends Abstract
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -241,9 +241,9 @@ public class SybaseSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
@Override @Override
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -59,9 +59,9 @@ public class TiDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) {
if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( " div " ); appendSql( " div " );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
else { else {

View File

@ -7190,20 +7190,24 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
if ( operator == BinaryArithmeticOperator.MODULO ) { if ( operator == BinaryArithmeticOperator.MODULO ) {
append( "mod" ); append( "mod" );
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( ',' ); appendSql( ',' );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
else { else {
appendSql( OPEN_PARENTHESIS ); appendSql( OPEN_PARENTHESIS );
arithmeticExpression.getLeftHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() );
appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() );
arithmeticExpression.getRightHandOperand().accept( this ); visitArithmeticOperand( arithmeticExpression.getRightHandOperand() );
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }
} }
protected void visitArithmeticOperand(Expression expression) {
expression.accept( this );
}
@Override @Override
public void visitDuration(Duration duration) { public void visitDuration(Duration duration) {
duration.getMagnitude().accept( this ); duration.getMagnitude().accept( this );

View File

@ -8,12 +8,13 @@ package org.hibernate.orm.test.hql;
import java.util.List; import java.util.List;
import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.query.criteria.JpaRoot;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -24,6 +25,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.TypedQuery; import jakarta.persistence.TypedQuery;
import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -45,9 +47,16 @@ public class InferenceTest extends BaseCoreFunctionalTestCase {
public void setUp() { public void setUp() {
doInHibernate( this::sessionFactory, session -> { doInHibernate( this::sessionFactory, session -> {
person = new Person(); person = new Person();
person.setName("Johannes"); person.setName( "Johannes" );
person.setSurname("Buehler"); person.setSurname( "Buehler" );
session.persist(person); session.persist( person );
} );
}
@After
public void cleanUp() {
doInHibernate( this::sessionFactory, session -> {
session.createMutationQuery( "delete from Person" ).executeUpdate();
} ); } );
} }
@ -72,6 +81,19 @@ public class InferenceTest extends BaseCoreFunctionalTestCase {
} }
@Test
@JiraKey("HHH-18046")
public void testBinaryArithmeticParameterInference() {
doInHibernate( this::sessionFactory, session -> {
HibernateCriteriaBuilder cb = session.getCriteriaBuilder();
JpaCriteriaQuery<Double> cq = cb.createQuery( Double.class );
JpaRoot<Person> root = cq.from( Person.class );
cq.select( cb.toDouble( cb.prod( root.get( "id" ), 0.5f ) ) );
Double result = session.createQuery( cq ).getSingleResult();
assertThat( result, is( person.getId() * 0.5d ) );
} );
}
@Entity(name = "Person") @Entity(name = "Person")
public static class Person { public static class Person {