From 88bdfbfbd4c0cec58b17b8b09eb8a164151c8a7c Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 2 May 2024 12:47:04 +0200 Subject: [PATCH] HHH-18046 Cast parameters used as arithmetic operands on DB2 --- .../dialect/DB2LegacySqlAstTranslator.java | 5 ++ .../dialect/DerbyLegacySqlAstTranslator.java | 19 +------- .../dialect/H2LegacySqlAstTranslator.java | 9 +++- .../dialect/HSQLLegacySqlAstTranslator.java | 19 +------- .../PostgreSQLLegacySqlAstTranslator.java | 4 +- .../SQLServerLegacySqlAstTranslator.java | 4 +- .../SybaseASELegacySqlAstTranslator.java | 4 +- .../SybaseAnywhereSqlAstTranslator.java | 4 +- .../dialect/SybaseLegacySqlAstTranslator.java | 4 +- .../dialect/DB2SqlAstTranslator.java | 5 ++ .../dialect/DerbySqlAstTranslator.java | 19 +------- .../hibernate/dialect/H2SqlAstTranslator.java | 9 +++- .../dialect/HANASqlAstTranslator.java | 4 +- .../dialect/HSQLSqlAstTranslator.java | 19 +------- .../dialect/MariaDBSqlAstTranslator.java | 4 +- .../dialect/MySQLSqlAstTranslator.java | 4 +- .../dialect/PostgreSQLSqlAstTranslator.java | 4 +- .../dialect/SQLServerSqlAstTranslator.java | 4 +- .../dialect/SybaseASESqlAstTranslator.java | 4 +- .../dialect/SybaseSqlAstTranslator.java | 4 +- .../dialect/TiDBSqlAstTranslator.java | 4 +- .../sql/ast/spi/AbstractSqlAstTranslator.java | 12 +++-- .../hibernate/orm/test/hql/InferenceTest.java | 46 ++++++++++++++----- 23 files changed, 100 insertions(+), 114 deletions(-) diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacySqlAstTranslator.java index 9c7dba7a46..c56a215b57 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacySqlAstTranslator.java @@ -139,6 +139,11 @@ public class DB2LegacySqlAstTranslator extends Abstract } } + @Override + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + } + @Override public void visitBooleanExpressionPredicate(BooleanExpressionPredicate booleanExpressionPredicate) { if ( getDB2Version().isSameOrAfter( 11 ) ) { diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacySqlAstTranslator.java index bb654ab6ec..3aea1a5ebc 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacySqlAstTranslator.java @@ -337,23 +337,8 @@ public class DerbyLegacySqlAstTranslator extends Abstra } @Override - public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { - final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); - 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 ); - } + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); } } diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacySqlAstTranslator.java index f43168433f..4739a94288 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacySqlAstTranslator.java @@ -324,12 +324,17 @@ public class H2LegacySqlAstTranslator extends AbstractS @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } + @Override + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + } + @Override protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lockMode) { final TableReference tableRef = tableGroup.getPrimaryTableReference(); diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacySqlAstTranslator.java index 79c5fc1e69..8f7e958ee2 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacySqlAstTranslator.java @@ -352,22 +352,7 @@ public class HSQLLegacySqlAstTranslator extends Abstrac } @Override - public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { - final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); - 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 ); - } + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); } } diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/PostgreSQLLegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/PostgreSQLLegacySqlAstTranslator.java index cd6291a228..4dae9260da 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/PostgreSQLLegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/PostgreSQLLegacySqlAstTranslator.java @@ -330,9 +330,9 @@ public class PostgreSQLLegacySqlAstTranslator extends A @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacySqlAstTranslator.java index 318be8b403..20495884f9 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacySqlAstTranslator.java @@ -523,9 +523,9 @@ public class SQLServerLegacySqlAstTranslator extends Ab @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseASELegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseASELegacySqlAstTranslator.java index 811b399ade..be42647e43 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseASELegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseASELegacySqlAstTranslator.java @@ -460,9 +460,9 @@ public class SybaseASELegacySqlAstTranslator extends Ab @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseAnywhereSqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseAnywhereSqlAstTranslator.java index 9dfb1b221c..841594f12e 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseAnywhereSqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseAnywhereSqlAstTranslator.java @@ -219,9 +219,9 @@ public class SybaseAnywhereSqlAstTranslator extends Abs @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseLegacySqlAstTranslator.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseLegacySqlAstTranslator.java index 8e4352f39f..602992915c 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseLegacySqlAstTranslator.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseLegacySqlAstTranslator.java @@ -242,9 +242,9 @@ public class SybaseLegacySqlAstTranslator extends Abstr @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DB2SqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/DB2SqlAstTranslator.java index ccf72844a6..f9f73464ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DB2SqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DB2SqlAstTranslator.java @@ -138,6 +138,11 @@ public class DB2SqlAstTranslator extends AbstractSqlAst } } + @Override + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + } + @Override public void visitBooleanExpressionPredicate(BooleanExpressionPredicate booleanExpressionPredicate) { if ( getDB2Version().isSameOrAfter( 11 ) ) { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java index cca3841c41..2788fca3a6 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java @@ -337,22 +337,7 @@ public class DerbySqlAstTranslator extends AbstractSqlA } @Override - public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { - final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); - 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 ); - } + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/H2SqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/H2SqlAstTranslator.java index 4bb4a5fd6e..62b049c6c2 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2SqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2SqlAstTranslator.java @@ -298,12 +298,17 @@ public class H2SqlAstTranslator extends SqlAstTranslato @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - render( arithmeticExpression.getLeftHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - render( arithmeticExpression.getRightHandOperand(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } + @Override + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); + } + @Override protected boolean renderPrimaryTableReference(TableGroup tableGroup, LockMode lockMode) { final TableReference tableRef = tableGroup.getPrimaryTableReference(); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/HANASqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/HANASqlAstTranslator.java index e8d063f32e..f4fbe42ee4 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/HANASqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/HANASqlAstTranslator.java @@ -53,9 +53,9 @@ public class HANASqlAstTranslator extends AbstractSqlAs public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { appendSql( "cast(" ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( " as int)" ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java index 691ffd7bdf..f458720b9b 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java @@ -342,23 +342,8 @@ public class HSQLSqlAstTranslator extends AbstractSqlAs } @Override - public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { - final BinaryArithmeticOperator operator = arithmeticExpression.getOperator(); - 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 ); - } + protected void visitArithmeticOperand(Expression expression) { + render( expression, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/MariaDBSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/MariaDBSqlAstTranslator.java index 302b624fb8..78272afe71 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/MariaDBSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/MariaDBSqlAstTranslator.java @@ -56,9 +56,9 @@ public class MariaDBSqlAstTranslator extends AbstractSq public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( " div " ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLSqlAstTranslator.java index bdd27029c5..6be62a55c4 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/MySQLSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/MySQLSqlAstTranslator.java @@ -106,9 +106,9 @@ public class MySQLSqlAstTranslator extends AbstractSqlA public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( " div " ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLSqlAstTranslator.java index 11c804945e..57552d2704 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLSqlAstTranslator.java @@ -328,9 +328,9 @@ public class PostgreSQLSqlAstTranslator extends SqlAstT @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerSqlAstTranslator.java index d9c66a3c4f..e1973f09a7 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerSqlAstTranslator.java @@ -492,9 +492,9 @@ public class SQLServerSqlAstTranslator extends SqlAstTr @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASESqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASESqlAstTranslator.java index 9647b96113..fa67be5630 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASESqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseASESqlAstTranslator.java @@ -443,9 +443,9 @@ public class SybaseASESqlAstTranslator extends Abstract @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseSqlAstTranslator.java index 6c2703e111..e4774530af 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseSqlAstTranslator.java @@ -241,9 +241,9 @@ public class SybaseSqlAstTranslator extends AbstractSql @Override public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/TiDBSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/TiDBSqlAstTranslator.java index 7e2ed150b6..1a43202390 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/TiDBSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/TiDBSqlAstTranslator.java @@ -59,9 +59,9 @@ public class TiDBSqlAstTranslator extends AbstractSqlAs public void visitBinaryArithmeticExpression(BinaryArithmeticExpression arithmeticExpression) { if ( isIntegerDivisionEmulationRequired( arithmeticExpression ) ) { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( " div " ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java index 48c318966e..7d7985304d 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java @@ -7190,20 +7190,24 @@ public abstract class AbstractSqlAstTranslator implemen if ( operator == BinaryArithmeticOperator.MODULO ) { append( "mod" ); appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( ',' ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } else { appendSql( OPEN_PARENTHESIS ); - arithmeticExpression.getLeftHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getLeftHandOperand() ); appendSql( arithmeticExpression.getOperator().getOperatorSqlTextString() ); - arithmeticExpression.getRightHandOperand().accept( this ); + visitArithmeticOperand( arithmeticExpression.getRightHandOperand() ); appendSql( CLOSE_PARENTHESIS ); } } + protected void visitArithmeticOperand(Expression expression) { + expression.accept( this ); + } + @Override public void visitDuration(Duration duration) { duration.getMagnitude().accept( this ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java index a93d49cf9c..3cb4c790c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/hql/InferenceTest.java @@ -8,12 +8,13 @@ package org.hibernate.orm.test.hql; 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.orm.junit.JiraKey; +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -24,6 +25,7 @@ import jakarta.persistence.Id; import jakarta.persistence.TypedQuery; import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertThat; @@ -41,15 +43,22 @@ public class InferenceTest extends BaseCoreFunctionalTestCase { }; } - @Before - public void setUp() { - doInHibernate( this::sessionFactory, session -> { - person = new Person(); - person.setName("Johannes"); - person.setSurname("Buehler"); - session.persist(person); - } ); - } + @Before + public void setUp() { + doInHibernate( this::sessionFactory, session -> { + person = new Person(); + person.setName( "Johannes" ); + person.setSurname( "Buehler" ); + session.persist( person ); + } ); + } + + @After + public void cleanUp() { + doInHibernate( this::sessionFactory, session -> { + session.createMutationQuery( "delete from Person" ).executeUpdate(); + } ); + } @Test public void testBinaryArithmeticInference() { @@ -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 cq = cb.createQuery( Double.class ); + JpaRoot 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") public static class Person {