HHH-15805 Subquery with where condition on a column with columnDefinition results in wrong SQL grammer

This commit is contained in:
Andrea Boriero 2022-12-19 19:10:29 +01:00 committed by Andrea Boriero
parent 62b218d411
commit 1f630e4a5d
6 changed files with 104 additions and 0 deletions

View File

@ -6,10 +6,12 @@
*/
package org.hibernate.community.dialect;
import org.hibernate.dialect.MySQLSqlAstTranslator;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.CastTarget;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.Summarization;
@ -227,4 +229,15 @@ public class MariaDBLegacySqlAstTranslator<T extends JdbcOperation> extends Abst
private boolean supportsWindowFunctions() {
return dialect.getVersion().isSameOrAfter( 10, 2 );
}
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
if ( sqlType != null ) {
appendSql( sqlType );
}
else {
super.visitCastTarget( castTarget );
}
}
}

View File

@ -7,10 +7,12 @@
package org.hibernate.community.dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.MySQLSqlAstTranslator;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.CastTarget;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.Summarization;
@ -240,4 +242,15 @@ public class MySQLLegacySqlAstTranslator<T extends JdbcOperation> extends Abstra
public MySQLDialect getDialect() {
return (MySQLDialect) super.getDialect();
}
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, getDialect() );
if ( sqlType != null ) {
appendSql( sqlType );
}
else {
super.visitCastTarget( castTarget );
}
}
}

View File

@ -10,6 +10,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.CastTarget;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.Summarization;
@ -222,4 +223,16 @@ public class MariaDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSq
private boolean supportsWindowFunctions() {
return true;
}
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
if ( sqlType != null ) {
appendSql( sqlType );
}
else {
super.visitCastTarget( castTarget );
}
}
}

View File

@ -6,10 +6,14 @@
*/
package org.hibernate.dialect;
import java.util.Locale;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.CastTarget;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.Summarization;
@ -33,6 +37,41 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
super( sessionFactory, statement );
}
public static String getSqlType(CastTarget castTarget, Dialect dialect) {
final String sqlType = castTarget.getSqlType();
if ( sqlType != null ) {
int parenthesesIndex = sqlType.indexOf( '(' );
final String baseName = parenthesesIndex == -1 ? sqlType : sqlType.substring( 0, parenthesesIndex );
switch ( baseName.toLowerCase( Locale.ROOT ) ) {
case "bit":
return "unsigned";
case "tinyint":
case "smallint":
case "integer":
case "bigint":
return "signed";
case "float":
case "real":
case "double precision":
final int precision = castTarget.getPrecision() == null ?
dialect.getDefaultDecimalPrecision() :
castTarget.getPrecision();
final int scale = castTarget.getScale() == null ? Size.DEFAULT_SCALE : castTarget.getScale();
return "decimal(" + precision + "," + scale + ")";
case "char":
case "varchar":
case "nchar":
case "nvarchar":
return "char";
case "binary":
case "varbinary":
return "binary";
}
}
return sqlType;
}
@Override
protected void renderExpressionAsClauseItem(Expression expression) {
expression.accept( this );
@ -239,4 +278,15 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
public MySQLDialect getDialect() {
return (MySQLDialect) super.getDialect();
}
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = getSqlType( castTarget, getDialect() );
if ( sqlType != null ) {
appendSql( sqlType );
}
else {
super.visitCastTarget( castTarget );
}
}
}

View File

@ -11,6 +11,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.sql.ast.spi.AbstractSqlAstTranslator;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.ast.tree.expression.CastTarget;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.Summarization;
@ -189,4 +190,15 @@ public class TiDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
public TiDBDialect getDialect() {
return this.dialect;
}
@Override
public void visitCastTarget(CastTarget castTarget) {
String sqlType = MySQLSqlAstTranslator.getSqlType( castTarget, dialect );
if ( sqlType != null ) {
appendSql( sqlType );
}
else {
super.visitCastTarget( castTarget );
}
}
}

View File

@ -943,8 +943,10 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
@Override
public void visitSelectStatement(SelectStatement statement) {
final SqlAstNodeRenderingMode oldParameterRenderingMode = getParameterRenderingMode();
try {
statementStack.push( statement );
parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
final boolean needsParenthesis = !statement.getQueryPart().isRoot();
if ( needsParenthesis ) {
appendSql( OPEN_PARENTHESIS );
@ -956,6 +958,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
}
}
finally {
parameterRenderingMode = oldParameterRenderingMode;
statementStack.pop();
}
}