HHH-18447 Try using native cast for string to boolean

This commit is contained in:
Christian Beikov 2024-08-12 17:21:32 +02:00
parent b179881847
commit ff57a6ced0
61 changed files with 595 additions and 256 deletions

View File

@ -393,20 +393,26 @@ public class AltibaseDialect extends Dialect {
}
break;
case INTEGER_BOOLEAN:
result = BooleanDecoder.toIntegerBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "1", "0" )
: BooleanDecoder.toIntegerBoolean( from );
if ( result != null ) {
return result;
}
break;
case YN_BOOLEAN:
result = BooleanDecoder.toYesNoBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
: BooleanDecoder.toYesNoBoolean( from );
if ( result != null ) {
return result;
}
break;
case BOOLEAN:
case TF_BOOLEAN:
result = BooleanDecoder.toTrueFalseBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'T'", "'F'" )
: BooleanDecoder.toTrueFalseBoolean( from );
if ( result != null ) {
return result;
}
@ -702,4 +708,14 @@ public class AltibaseDialect extends Dialect {
};
}
@Override
public String getDual() {
return "dual";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -218,16 +218,6 @@ public class AltibaseSqlAstTranslator<T extends JdbcOperation> extends AbstractS
emulateQueryPartTableReferenceColumnAliasing( tableReference );
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
@Override
protected boolean needsRecursiveKeywordInWithClause() {
return false;

View File

@ -515,4 +515,16 @@ public class CUBRIDDialect extends Dialect {
sqlAppender.append( diffUnit.conversionFactor( toUnit, this ) );
}
@Override
public String getDual() {
//TODO: is this really needed?
//TODO: would "from table({0})" be better?
return "db_root";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -77,16 +77,4 @@ public class CUBRIDSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}
@Override
protected String getDual() {
//TODO: is this really needed?
//TODO: would "from table({0})" be better?
return "db_root";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -68,6 +68,7 @@ import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.procedure.internal.DB2CallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.query.sqm.CastType;
import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.mutation.internal.cte.CteInsertStrategy;
@ -1134,6 +1135,16 @@ public class DB2LegacyDialect extends Dialect {
return super.extractPattern( unit );
}
@Override
public String castPattern(CastType from, CastType to) {
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
return "cast(?1 as ?2)";
}
else {
return super.castPattern( from, to );
}
}
@Override
public int getInExpressionCountLimit() {
return BIND_PARAMETERS_NUMBER_LIMIT;
@ -1201,4 +1212,14 @@ public class DB2LegacyDialect extends Dialect {
public boolean supportsFromClauseInUpdate() {
return getDB2Version().isSameOrAfter( 11 );
}
@Override
public String getDual() {
return "sysibm.dual";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -604,16 +604,6 @@ public class DB2LegacySqlAstTranslator<T extends JdbcOperation> extends Abstract
return false;
}
@Override
protected String getDual() {
return "sysibm.dual";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
@Override
protected void visitReturningColumns(List<ColumnReference> returningColumns) {
// For DB2 we use #renderReturningClause to render a wrapper around the DML statement

View File

@ -1040,6 +1040,11 @@ public class DerbyDialect extends Dialect {
return true;
}
@Override
public boolean supportsValuesList() {
return true;
}
@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {
@ -1057,4 +1062,14 @@ public class DerbyDialect extends Dialect {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}
@Override
public String getDual() {
return "(values 0)";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
}

View File

@ -1051,6 +1051,11 @@ public class DerbyLegacyDialect extends Dialect {
return getVersion().isSameOrAfter( 10, 4 );
}
@Override
public boolean supportsValuesList() {
return true;
}
@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {
@ -1062,4 +1067,14 @@ public class DerbyLegacyDialect extends Dialect {
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}
@Override
public String getDual() {
return "(values 0)";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
}

View File

@ -298,16 +298,6 @@ public class DerbyLegacySqlAstTranslator<T extends JdbcOperation> extends Abstra
return false;
}
@Override
protected String getDual() {
return "(values 0)";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
@Override
protected boolean needsRowsToSkip() {
return !supportsOffsetFetchClause();

View File

@ -298,16 +298,6 @@ public class DerbySqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
return false;
}
@Override
protected String getDual() {
return "(values 0)";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
@Override
protected boolean needsRowsToSkip() {
return !supportsOffsetFetchClause();

View File

@ -401,25 +401,33 @@ public class FirebirdDialect extends Dialect {
}
break;
case BOOLEAN:
result = BooleanDecoder.toBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "true", "false" )
: BooleanDecoder.toBoolean( from );
if ( result != null ) {
return result;
}
break;
case INTEGER_BOOLEAN:
result = BooleanDecoder.toIntegerBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "1", "0" )
: BooleanDecoder.toIntegerBoolean( from );
if ( result != null ) {
return result;
}
break;
case YN_BOOLEAN:
result = BooleanDecoder.toYesNoBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
: BooleanDecoder.toYesNoBoolean( from );
if ( result != null ) {
return result;
}
break;
case TF_BOOLEAN:
result = BooleanDecoder.toTrueFalseBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'T'", "'F'" )
: BooleanDecoder.toTrueFalseBoolean( from );
if ( result != null ) {
return result;
}
@ -1094,4 +1102,14 @@ public class FirebirdDialect extends Dialect {
}
}
@Override
public String getDual() {
return "rdb$database";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -260,16 +260,6 @@ public class FirebirdSqlAstTranslator<T extends JdbcOperation> extends AbstractS
return false;
}
@Override
protected String getDual() {
return "rdb$database";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
private boolean supportsOffsetFetchClause() {
return getDialect().getVersion().isSameOrAfter( 3 );
}

View File

@ -47,6 +47,7 @@ import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.query.sqm.CastType;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType;
import org.hibernate.dialect.NullOrdering;
@ -513,6 +514,16 @@ public class H2LegacyDialect extends Dialect {
: super.extractPattern(unit);
}
@Override
public String castPattern(CastType from, CastType to) {
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
return "cast(?1 as ?2)";
}
else {
return super.castPattern( from, to );
}
}
@Override
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
if ( intervalType != null ) {
@ -991,4 +1002,14 @@ public class H2LegacyDialect extends Dialect {
public boolean supportsCaseInsensitiveLike() {
return getVersion().isSameOrAfter( 1, 4, 194 );
}
@Override
public boolean supportsValuesList() {
return true;
}
@Override
public String getDual() {
return "dual";
}
}

View File

@ -386,11 +386,6 @@ public class H2LegacySqlAstTranslator<T extends JdbcOperation> extends AbstractS
return getClauseStack().getCurrent() != Clause.WITHIN_GROUP || getDialect().getVersion().isSameOrAfter( 2 );
}
@Override
protected String getDual() {
return "dual";
}
private boolean supportsOffsetFetchClause() {
return getDialect().getVersion().isSameOrAfter( 1, 4, 195 );
}

View File

@ -315,25 +315,33 @@ public class HSQLLegacyDialect extends Dialect {
}
break;
case BOOLEAN:
result = BooleanDecoder.toBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "true", "false" )
: BooleanDecoder.toBoolean( from );
if ( result != null ) {
return result;
}
break;
case INTEGER_BOOLEAN:
result = BooleanDecoder.toIntegerBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "1", "0" )
: BooleanDecoder.toIntegerBoolean( from );
if ( result != null ) {
return result;
}
break;
case YN_BOOLEAN:
result = BooleanDecoder.toYesNoBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
: BooleanDecoder.toYesNoBoolean( from );
if ( result != null ) {
return result;
}
break;
case TF_BOOLEAN:
result = BooleanDecoder.toTrueFalseBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'T'", "'F'" )
: BooleanDecoder.toTrueFalseBoolean( from );
if ( result != null ) {
return result;
}
@ -825,6 +833,11 @@ public class HSQLLegacyDialect extends Dialect {
return true;
}
@Override
public boolean supportsValuesList() {
return true;
}
@Override
public IdentityColumnSupport getIdentityColumnSupport() {
return identityColumnSupport;
@ -900,4 +913,9 @@ public class HSQLLegacyDialect extends Dialect {
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -339,11 +339,6 @@ public class HSQLLegacySqlAstTranslator<T extends JdbcOperation> extends Abstrac
return false;
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
private boolean supportsOffsetFetchClause() {
return getDialect().getVersion().isSameOrAfter( 2, 5 );
}

View File

@ -854,4 +854,14 @@ public class InformixDialect extends Dialect {
jdbcTypeRegistry.addDescriptor( Types.NCLOB, ClobJdbcType.DEFAULT );
typeContributions.contributeJdbcType( VarcharUUIDJdbcType.INSTANCE );
}
@Override
public String getDual() {
return "(select 0 from systables where tabid=1)";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
}

View File

@ -151,16 +151,6 @@ public class InformixSqlAstTranslator<T extends JdbcOperation> extends AbstractS
return false;
}
@Override
protected String getDual() {
return "(select 0 from systables where tabid=1)";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
@Override
protected void renderNull(Literal literal) {
if ( getParameterRenderingMode() == SqlAstNodeRenderingMode.NO_UNTYPED ) {

View File

@ -557,4 +557,15 @@ public class IngresDialect extends Dialect {
public boolean supportsFetchClause(FetchClauseType type) {
return getVersion().isSameOrAfter( 9, 3 );
}
@Override
public String getDual() {
return "(select 0)";
}
@Override
public String getFromDualForSelectOnly() {
//this is only necessary if the query has a where clause
return " from " + getDual() + " dual";
}
}

View File

@ -135,17 +135,6 @@ public class IngresSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
return false;
}
@Override
protected String getDual() {
return "(select 0)";
}
@Override
protected String getFromDualForSelectOnly() {
//this is only necessary if the query has a where clause
return " from " + getDual() + " dual";
}
@Override
protected boolean needsRowsToSkip() {
return !supportsOffsetFetchClause();

View File

@ -262,4 +262,14 @@ public class MariaDBLegacyDialect extends MySQLLegacyDialect {
return super.buildIdentifierHelper( builder, dbMetaData );
}
@Override
public String getDual() {
return "dual";
}
@Override
public String getFromDualForSelectOnly() {
return getVersion().isBefore( 10, 4 ) ? ( " from " + getDual() ) : "";
}
}

View File

@ -364,16 +364,6 @@ public class MariaDBLegacySqlAstTranslator<T extends JdbcOperation> extends Abst
return true;
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getFromDualForSelectOnly() {
return getDialect().getVersion().isBefore( 10, 4 ) ? ( " from " + getDual() ) : "";
}
@Override
public MariaDBLegacyDialect getDialect() {
return this.dialect;

View File

@ -328,4 +328,14 @@ public class MaxDBDialect extends Dialect {
public boolean supportsJdbcConnectionLobCreation(DatabaseMetaData databaseMetaData) {
return false;
}
@Override
public String getDual() {
return "dual";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -89,14 +89,4 @@ public class MaxDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -341,4 +341,9 @@ public class MimerSQLDialect extends Dialect {
public IdentityColumnSupport getIdentityColumnSupport() {
return MimerSQLIdentityColumnSupport.INSTANCE;
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -78,9 +78,4 @@ public class MimerSQLSqlAstTranslator<T extends JdbcOperation> extends AbstractS
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -1436,4 +1436,14 @@ public class MySQLLegacyDialect extends Dialect {
}
return sqlCheckConstraint;
}
@Override
public String getDual() {
return "dual";
}
@Override
public String getFromDualForSelectOnly() {
return getVersion().isSameOrAfter( 8 ) ? "" : ( " from " + getDual() );
}
}

View File

@ -387,16 +387,6 @@ public class MySQLLegacySqlAstTranslator<T extends JdbcOperation> extends Abstra
return getDialect().getVersion().isSameOrAfter( 8 );
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getFromDualForSelectOnly() {
return getDialect().getVersion().isSameOrAfter( 8 ) ? "" : ( " from " + getDual() );
}
@Override
public MySQLLegacyDialect getDialect() {
return (MySQLLegacyDialect) DialectDelegateWrapper.extractRealDialect( super.getDialect() );

View File

@ -397,20 +397,33 @@ public class OracleLegacyDialect extends Dialect {
}
break;
case INTEGER_BOOLEAN:
result = BooleanDecoder.toIntegerBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "1", "0" )
: BooleanDecoder.toIntegerBoolean( from );
if ( result != null ) {
return result;
}
break;
case YN_BOOLEAN:
result = BooleanDecoder.toYesNoBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
: BooleanDecoder.toYesNoBoolean( from );
if ( result != null ) {
return result;
}
break;
case BOOLEAN:
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "true", "false" )
: BooleanDecoder.toBoolean( from );
if ( result != null ) {
return result;
}
break;
case TF_BOOLEAN:
result = BooleanDecoder.toTrueFalseBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'T'", "'F'" )
: BooleanDecoder.toTrueFalseBoolean( from );
if ( result != null ) {
return result;
}
@ -1577,4 +1590,14 @@ public class OracleLegacyDialect extends Dialect {
}
return sqlCheckConstraint;
}
@Override
public String getDual() {
return "dual";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -672,16 +672,6 @@ public class OracleLegacySqlAstTranslator<T extends JdbcOperation> extends Abstr
return getDialect().getVersion().isSameOrAfter( 9 );
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
private boolean supportsOffsetFetchClause() {
return getDialect().supportsFetchClause( FetchClauseType.ROWS_ONLY );
}

View File

@ -58,6 +58,7 @@ import org.hibernate.procedure.internal.PostgreSQLCallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.query.SemanticException;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.CastType;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit;
@ -416,6 +417,16 @@ public class PostgreSQLLegacyDialect extends Dialect {
}
}
@Override
public String castPattern(CastType from, CastType to) {
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
return "cast(?1 as ?2)";
}
else {
return super.castPattern( from, to );
}
}
/**
* {@code microsecond} is the smallest unit for an {@code interval},
* and the highest precision for a {@code timestamp}, so we could

View File

@ -432,4 +432,14 @@ public class RDMSOS2200Dialect extends Dialect {
public String trimPattern(TrimSpec specification, boolean isWhitespace) {
return AbstractTransactSQLDialect.replaceLtrimRtrim( specification, isWhitespace );
}
@Override
public String getDual() {
return "rdms.rdms_dummy";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual() + " where key_col=1";
}
}

View File

@ -122,14 +122,4 @@ public class RDMSOS2200SqlAstTranslator<T extends JdbcOperation> extends Abstrac
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}
@Override
protected String getDual() {
return "rdms.rdms_dummy";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual() + " where key_col=1";
}
}

View File

@ -1410,4 +1410,9 @@ public class SingleStoreDialect extends Dialect {
* @settingDefault {@code false}
*/
public static final String SINGLE_STORE_FOR_UPDATE_LOCK_ENABLED = "hibernate.dialect.singlestore.for_update_lock_enabled";
@Override
public String getDual() {
return "dual";
}
}

View File

@ -435,11 +435,6 @@ public class SingleStoreSqlAstTranslator<T extends JdbcOperation> extends Abstra
return false;
}
@Override
protected String getDual() {
return "dual";
}
@Override
public SingleStoreDialect getDialect() {
return this.dialect;

View File

@ -694,4 +694,9 @@ public class SybaseASELegacyDialect extends SybaseLegacyDialect {
}
return new TopLimitHandler(false);
}
@Override
public String getDual() {
return "(select 1 c1)";
}
}

View File

@ -527,11 +527,6 @@ public class SybaseASELegacySqlAstTranslator<T extends JdbcOperation> extends Ab
return false;
}
@Override
protected String getDual() {
return "(select 1 c1)";
}
private boolean supportsTopClause() {
return getDialect().getVersion().isSameOrAfter( 12, 5 );
}

View File

@ -213,4 +213,14 @@ public class SybaseAnywhereDialect extends SybaseDialect {
return TopLimitHandler.INSTANCE;
}
@Override
public String getDual() {
return "sys.dummy";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -235,14 +235,4 @@ public class SybaseAnywhereSqlAstTranslator<T extends JdbcOperation> extends Abs
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}
@Override
protected String getDual() {
return "sys.dummy";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -58,6 +58,7 @@ import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.procedure.internal.DB2CallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.query.sqm.CastType;
import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.mutation.internal.cte.CteInsertStrategy;
@ -1215,6 +1216,16 @@ public class DB2Dialect extends Dialect {
return super.extractPattern( unit );
}
@Override
public String castPattern(CastType from, CastType to) {
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
return "cast(?1 as ?2)";
}
else {
return super.castPattern( from, to );
}
}
@Override
public int getInExpressionCountLimit() {
return BIND_PARAMETERS_NUMBER_LIMIT;
@ -1282,4 +1293,14 @@ public class DB2Dialect extends Dialect {
public boolean supportsFromClauseInUpdate() {
return getDB2Version().isSameOrAfter( 11 );
}
@Override
public String getDual() {
return "sysibm.dual";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -585,16 +585,6 @@ public class DB2SqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAst
return false;
}
@Override
protected String getDual() {
return "sysibm.dual";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
@Override
protected void visitReturningColumns(List<ColumnReference> returningColumns) {
// For DB2 we use #renderReturningClause to render a wrapper around the DML statement

View File

@ -1455,7 +1455,7 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
case INTEGER_BOOLEAN:
switch ( from ) {
case STRING:
return "case ?1 when 'T' then 1 when 'Y' then 1 when 'F' then 0 when 'N' then 0 else null end";
return buildStringToBooleanCast( "1", "0" );
case INTEGER:
case LONG:
return "abs(sign(?1))";
@ -1470,7 +1470,7 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
case YN_BOOLEAN:
switch ( from ) {
case STRING:
return "case ?1 when 'T' then 'Y' when 'Y' then 'Y' when 'F' then 'N' when 'N' then 'N' else null end";
return buildStringToBooleanCast( "'Y'", "'N'" );
case INTEGER_BOOLEAN:
return "case ?1 when 1 then 'Y' when 0 then 'N' else null end";
case INTEGER:
@ -1485,7 +1485,7 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
case TF_BOOLEAN:
switch ( from ) {
case STRING:
return "case ?1 when 'T' then 'T' when 'Y' then 'T' when 'F' then 'F' when 'N' then 'F' else null end";
return buildStringToBooleanCast( "'T'", "'F'" );
case INTEGER_BOOLEAN:
return "case ?1 when 1 then 'T' when 0 then 'F' else null end";
case INTEGER:
@ -1500,7 +1500,7 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
case BOOLEAN:
switch ( from ) {
case STRING:
return "case ?1 when 'T' then true when 'Y' then true when 'F' then false when 'N' then false else null end";
return buildStringToBooleanCast( "true", "false" );
case INTEGER_BOOLEAN:
case INTEGER:
case LONG:
@ -1515,6 +1515,153 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
return "cast(?1 as ?2)";
}
protected static final String[] TRUE_STRING_VALUES = new String[] { "t", "true", "y", "1" };
protected static final String[] FALSE_STRING_VALUES = new String[] { "f", "false", "n", "0" };
protected String buildStringToBooleanCast(String trueValue, String falseValue) {
final boolean supportsValuesList = supportsValuesList();
final StringBuilder sb = new StringBuilder();
sb.append( "(select v.x from (" );
if ( supportsValuesList ) {
sb.append( "values (" );
sb.append( trueValue );
sb.append( "),(" );
sb.append( falseValue );
sb.append( ")) v(x)" );
}
else {
sb.append( "select " );
sb.append( trueValue );
sb.append( " x");
sb.append( getFromDualForSelectOnly() );
sb.append(" union all select " );
sb.append( falseValue );
sb.append( getFromDualForSelectOnly() );
sb.append( ") v" );
}
sb.append( " left join (" );
if ( supportsValuesList ) {
sb.append( "values" );
char separator = ' ';
for ( String trueStringValue : Dialect.TRUE_STRING_VALUES ) {
sb.append( separator );
sb.append( "('" );
sb.append( trueStringValue );
sb.append( "'," );
sb.append( trueValue );
sb.append( ')' );
separator = ',';
}
for ( String falseStringValue : Dialect.FALSE_STRING_VALUES ) {
sb.append( ",('" );
sb.append( falseStringValue );
sb.append( "'," );
sb.append( falseValue );
sb.append( ')' );
}
sb.append( ") t(k,v)" );
}
else {
sb.append( "select '" );
sb.append( Dialect.TRUE_STRING_VALUES[0] );
sb.append( "' k," );
sb.append( trueValue );
sb.append( " v" );
sb.append( getFromDualForSelectOnly() );
for ( int i = 1; i < Dialect.TRUE_STRING_VALUES.length; i++ ) {
sb.append( " union all select '" );
sb.append( Dialect.TRUE_STRING_VALUES[i] );
sb.append( "'," );
sb.append( trueValue );
sb.append( getFromDualForSelectOnly() );
}
for ( String falseStringValue : Dialect.FALSE_STRING_VALUES ) {
sb.append( " union all select '" );
sb.append( falseStringValue );
sb.append( "'," );
sb.append( falseValue );
sb.append( getFromDualForSelectOnly() );
}
sb.append( ") t" );
}
sb.append( " on " );
sb.append( getLowercaseFunction() );
sb.append( "(?1)=t.k where t.v is null or v.x=t.v)" );
return sb.toString();
}
protected String buildStringToBooleanCastDecode(String trueValue, String falseValue) {
final boolean supportsValuesList = supportsValuesList();
final StringBuilder sb = new StringBuilder();
sb.append( "(select v.x from (" );
if ( supportsValuesList ) {
sb.append( "values (" );
sb.append( trueValue );
sb.append( "),(" );
sb.append( falseValue );
sb.append( ")) v(x)" );
}
else {
sb.append( "select " );
sb.append( trueValue );
sb.append( " x");
sb.append( getFromDualForSelectOnly() );
sb.append(" union all select " );
sb.append( falseValue );
sb.append( getFromDualForSelectOnly() );
sb.append( ") v" );
}
sb.append( ", (" );
if ( supportsValuesList ) {
sb.append( "values (" );
sb.append( buildStringToBooleanDecode( trueValue, falseValue ) );
sb.append( ")) t(v)" );
}
else {
sb.append( "select " );
sb.append( buildStringToBooleanDecode( trueValue, falseValue ) );
sb.append( " v");
sb.append( getFromDualForSelectOnly() );
sb.append(") t" );
}
sb.append( " where t.v is null or v.x=t.v)" );
return sb.toString();
}
protected String buildStringToBooleanDecode(String trueValue, String falseValue) {
final StringBuilder sb = new StringBuilder();
sb.append( "decode(" );
sb.append( getLowercaseFunction() );
sb.append( "(?1)" );
for ( String trueStringValue : TRUE_STRING_VALUES ) {
sb.append( ",'" );
sb.append( trueStringValue );
sb.append( "'," );
sb.append( trueValue );
}
for ( String falseStringValue : FALSE_STRING_VALUES ) {
sb.append( ",'" );
sb.append( falseStringValue );
sb.append( "'," );
sb.append( falseValue );
}
sb.append( ",null)" );
return sb.toString();
}
/**
* Returns a table expression that has one row.
*
* @return the SQL equivalent to Oracle's {@code dual}.
*/
public String getDual() {
return "(values(0))";
}
public String getFromDualForSelectOnly() {
return "";
}
/**
* Obtain a pattern for the SQL equivalent to a
* {@code trim()} function call. The resulting

View File

@ -42,6 +42,7 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.mapping.EntityMappingType;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.query.sqm.CastType;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit;
@ -442,6 +443,16 @@ public class H2Dialect extends Dialect {
: super.extractPattern(unit);
}
@Override
public String castPattern(CastType from, CastType to) {
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
return "cast(?1 as ?2)";
}
else {
return super.castPattern( from, to );
}
}
@Override @SuppressWarnings("deprecation")
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
if ( intervalType != null ) {
@ -1010,4 +1021,14 @@ public class H2Dialect extends Dialect {
return true;
}
@Override
public boolean supportsValuesList() {
return true;
}
@Override
public String getDual() {
return "dual";
}
}

View File

@ -358,11 +358,6 @@ public class H2SqlAstTranslator<T extends JdbcOperation> extends SqlAstTranslato
return true;
}
@Override
protected String getDual() {
return "dual";
}
private boolean supportsOffsetFetchClause() {
return true;
}

View File

@ -1996,4 +1996,14 @@ public class HANADialect extends Dialect {
public String getForUpdateString(LockMode lockMode) {
return super.getForUpdateString(lockMode);
}
@Override
public String getDual() {
return "sys.dummy";
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -249,16 +249,6 @@ public class HANASqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
return false;
}
@Override
protected String getDual() {
return "sys.dummy";
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
@Override
protected void renderInsertIntoNoColumns(TableInsertStandard tableInsert) {
throw new MappingException(

View File

@ -250,25 +250,33 @@ public class HSQLDialect extends Dialect {
}
break;
case BOOLEAN:
result = BooleanDecoder.toBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "true", "false" )
: BooleanDecoder.toBoolean( from );
if ( result != null ) {
return result;
}
break;
case INTEGER_BOOLEAN:
result = BooleanDecoder.toIntegerBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "1", "0" )
: BooleanDecoder.toIntegerBoolean( from );
if ( result != null ) {
return result;
}
break;
case YN_BOOLEAN:
result = BooleanDecoder.toYesNoBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
: BooleanDecoder.toYesNoBoolean( from );
if ( result != null ) {
return result;
}
break;
case TF_BOOLEAN:
result = BooleanDecoder.toTrueFalseBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'T'", "'F'" )
: BooleanDecoder.toTrueFalseBoolean( from );
if ( result != null ) {
return result;
}
@ -610,6 +618,11 @@ public class HSQLDialect extends Dialect {
return true;
}
@Override
public boolean supportsValuesList() {
return true;
}
@Override
public IdentityColumnSupport getIdentityColumnSupport() {
return identityColumnSupport;
@ -690,4 +703,9 @@ public class HSQLDialect extends Dialect {
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}
@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}

View File

@ -333,11 +333,6 @@ public class HSQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
return false;
}
@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}
private boolean supportsOffsetFetchClause() {
return true;
}

View File

@ -281,4 +281,9 @@ public class MariaDBDialect extends MySQLDialect {
return super.buildIdentifierHelper( builder, dbMetaData );
}
@Override
public String getDual() {
return "dual";
}
}

View File

@ -367,11 +367,6 @@ public class MariaDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSq
return true;
}
@Override
protected String getDual() {
return "dual";
}
@Override
public MariaDBDialect getDialect() {
return this.dialect;

View File

@ -1516,4 +1516,9 @@ public class MySQLDialect extends Dialect {
public boolean supportsBindingNullSqlTypeForSetNull() {
return true;
}
@Override
public String getDual() {
return "dual";
}
}

View File

@ -433,11 +433,6 @@ public class MySQLSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlA
return true;
}
@Override
protected String getDual() {
return "dual";
}
@Override
public MySQLDialect getDialect() {
return (MySQLDialect) DialectDelegateWrapper.extractRealDialect( super.getDialect() );

View File

@ -462,25 +462,33 @@ public class OracleDialect extends Dialect {
}
break;
case INTEGER_BOOLEAN:
result = BooleanDecoder.toIntegerBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "1", "0" )
: BooleanDecoder.toIntegerBoolean( from );
if ( result != null ) {
return result;
}
break;
case YN_BOOLEAN:
result = BooleanDecoder.toYesNoBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
: BooleanDecoder.toYesNoBoolean( from );
if ( result != null ) {
return result;
}
break;
case BOOLEAN:
result = BooleanDecoder.toBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "true", "false" )
: BooleanDecoder.toBoolean( from );
if ( result != null ) {
return result;
}
break;
case TF_BOOLEAN:
result = BooleanDecoder.toTrueFalseBoolean( from );
result = from == CastType.STRING
? buildStringToBooleanCastDecode( "'T'", "'F'" )
: BooleanDecoder.toTrueFalseBoolean( from );
if ( result != null ) {
return result;
}
@ -1696,4 +1704,14 @@ public class OracleDialect extends Dialect {
? sqlCheckConstraint + " " + checkConstraint.getOptions()
: sqlCheckConstraint;
}
@Override
public String getDual() {
return "dual";
}
@Override
public String getFromDualForSelectOnly() {
return getVersion().isSameOrAfter( 23 ) ? "" : ( " from " + getDual() );
}
}

View File

@ -615,16 +615,6 @@ public class OracleSqlAstTranslator<T extends JdbcOperation> extends SqlAstTrans
return false;
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getFromDualForSelectOnly() {
return getDialect().getVersion().isSameOrAfter( 23 ) ? "" : ( " from " + getDual() );
}
private boolean supportsOffsetFetchClause() {
return getDialect().supportsFetchClause( FetchClauseType.ROWS_ONLY );
}

View File

@ -56,6 +56,7 @@ import org.hibernate.procedure.internal.PostgreSQLCallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.query.SemanticException;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.CastType;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit;
@ -436,6 +437,16 @@ public class PostgreSQLDialect extends Dialect {
};
}
@Override
public String castPattern(CastType from, CastType to) {
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
return "cast(?1 as ?2)";
}
else {
return super.castPattern( from, to );
}
}
/**
* {@code microsecond} is the smallest unit for an {@code interval},
* and the highest precision for a {@code timestamp}, so we could

View File

@ -710,4 +710,9 @@ public class SybaseASEDialect extends SybaseDialect {
public LimitHandler getLimitHandler() {
return new TopLimitHandler(false);
}
@Override
public String getDual() {
return "(select 1 c1)";
}
}

View File

@ -506,11 +506,6 @@ public class SybaseASESqlAstTranslator<T extends JdbcOperation> extends Abstract
return false;
}
@Override
protected String getDual() {
return "(select 1 c1)";
}
private boolean supportsParameterOffsetFetchExpression() {
return false;
}

View File

@ -187,4 +187,9 @@ public class TiDBDialect extends MySQLDialect {
? "timestampadd(microsecond,?2*1e6,?3)"
: super.timestampaddPattern(unit, temporalType, intervalType);
}
@Override
public String getDual() {
return "dual";
}
}

View File

@ -330,11 +330,6 @@ public class TiDBSqlAstTranslator<T extends JdbcOperation> extends AbstractSqlAs
return false;
}
@Override
protected String getDual() {
return "dual";
}
@Override
protected String getForShare(int timeoutMillis) {
if ( timeoutMillis == LockOptions.NO_WAIT ) {

View File

@ -8579,11 +8579,11 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
* @return the SQL equivalent to Oracle's {@code dual}.
*/
protected String getDual() {
return "(values(0))";
return dialect.getDual();
}
protected String getFromDualForSelectOnly() {
return "";
return dialect.getFromDualForSelectOnly();
}
protected enum LockStrategy {

View File

@ -5,6 +5,9 @@
package org.hibernate.orm.test.query.hql;
import org.hamcrest.Matchers;
import org.hibernate.HibernateException;
import org.hibernate.JDBCException;
import org.hibernate.QueryException;
import org.hibernate.community.dialect.AltibaseDialect;
import org.hibernate.community.dialect.InformixDialect;
@ -22,6 +25,8 @@ import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.dialect.TiDBDialect;
import org.hibernate.query.sqm.produce.function.FunctionArgumentException;
import org.hibernate.sql.exec.ExecutionException;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;
@ -30,6 +35,7 @@ import org.hibernate.testing.orm.domain.gambit.EntityOfMaps;
import org.hibernate.testing.orm.domain.gambit.SimpleEntity;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
@ -63,6 +69,7 @@ import static org.hamcrest.Matchers.isOneOf;
import static org.hibernate.testing.orm.domain.gambit.EntityOfBasics.Gender.FEMALE;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
@ -1028,6 +1035,44 @@ public class FunctionTests {
);
}
@Test
@Jira("https://hibernate.atlassian.net/browse/HHH-18447")
public void testCastStringToBoolean(SessionFactoryScope scope) {
scope.inTransaction( session -> {
assertThat( session.createQuery("select cast('1' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('0' as Boolean)", Boolean.class).getSingleResult(), is(false) );
assertThat( session.createQuery("select cast('y' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('n' as Boolean)", Boolean.class).getSingleResult(), is(false) );
assertThat( session.createQuery("select cast('Y' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('N' as Boolean)", Boolean.class).getSingleResult(), is(false) );
assertThat( session.createQuery("select cast('t' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('f' as Boolean)", Boolean.class).getSingleResult(), is(false) );
assertThat( session.createQuery("select cast('T' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('F' as Boolean)", Boolean.class).getSingleResult(), is(false) );
assertThat( session.createQuery("select cast('true' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('false' as Boolean)", Boolean.class).getSingleResult(), is(false) );
assertThat( session.createQuery("select cast('TRUE' as Boolean)", Boolean.class).getSingleResult(), is(true) );
assertThat( session.createQuery("select cast('FALSE' as Boolean)", Boolean.class).getSingleResult(), is(false) );
});
}
@Test
@Jira("https://hibernate.atlassian.net/browse/HHH-18447")
public void testCastInvalidStringToBoolean(SessionFactoryScope scope) {
scope.inTransaction( session -> {
try {
session.createQuery( "select cast('bla' as Boolean)", Boolean.class ).getSingleResult();
fail("Casting invalid boolean string should fail");
}
catch ( HibernateException e ) {
// Expected
if ( !( e instanceof JDBCException || e instanceof ExecutionException ) ) {
throw e;
}
}
} );
}
@Test
@SkipForDialect(dialectClass = DB2Dialect.class, matchSubTypes = true)
@SkipForDialect(dialectClass = DerbyDialect.class)