Skip to content

Commit

Permalink
HHH-18447 Try using native cast for string to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov authored and sebersole committed Sep 16, 2024
1 parent 723e18d commit 9c5cf75
Show file tree
Hide file tree
Showing 61 changed files with 595 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,26 @@ public String castPattern(CastType from, CastType to) {
}
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;
}
Expand Down Expand Up @@ -704,4 +710,14 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
};
}

@Override
public String getDual() {
return "dual";
}

@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,6 @@ public void visitQueryPartTableReference(QueryPartTableReference tableReference)
emulateQueryPartTableReferenceColumnAliasing( tableReference );
}

@Override
protected String getDual() {
return "dual";
}

@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}

@Override
protected boolean needsRecursiveKeywordInWithClause() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,16 @@ private void timediff(
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,4 @@ protected boolean supportsRowValueConstructorSyntaxInInList() {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
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;
Expand Down Expand Up @@ -1141,6 +1142,16 @@ public String extractPattern(TemporalUnit unit) {
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;
Expand Down Expand Up @@ -1208,4 +1219,14 @@ public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
public boolean supportsFromClauseInUpdate() {
return getDB2Version().isSameOrAfter( 11 );
}

@Override
public String getDual() {
return "sysibm.dual";
}

@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,11 @@ public boolean supportsWindowFunctions() {
return getVersion().isSameOrAfter( 10, 4 );
}

@Override
public boolean supportsValuesList() {
return true;
}

@Override
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
throws SQLException {
Expand All @@ -1066,4 +1071,14 @@ public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, D
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}

@Override
public String getDual() {
return "(values 0)";
}

@Override
public String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}

@Override
protected String getDual() {
return "(values 0)";
}

@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual() + " dual";
}

@Override
protected boolean needsRowsToSkip() {
return !supportsOffsetFetchClause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,25 +403,33 @@ public String castPattern(CastType from, CastType to) {
}
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;
}
Expand Down Expand Up @@ -1096,4 +1104,14 @@ else if ( supportsOffset && temporalAccessor instanceof Instant ) {
}

}

@Override
public String getDual() {
return "rdb$database";
}

@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}

@Override
protected String getDual() {
return "rdb$database";
}

@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}

private boolean supportsOffsetFetchClause() {
return getDialect().getVersion().isSameOrAfter( 3 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.hibernate.internal.util.JdbcExceptionHelper;
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;
Expand Down Expand Up @@ -514,6 +515,16 @@ public String extractPattern(TemporalUnit unit) {
: 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 ) {
Expand Down Expand Up @@ -992,4 +1003,14 @@ public String getCaseInsensitiveLike() {
public boolean supportsCaseInsensitiveLike() {
return getVersion().isSameOrAfter( 1, 4, 194 );
}

@Override
public boolean supportsValuesList() {
return true;
}

@Override
public String getDual() {
return "dual";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,6 @@ protected boolean supportsNullPrecedence() {
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 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,33 @@ public String castPattern(CastType from, CastType to) {
}
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;
}
Expand Down Expand Up @@ -825,6 +833,11 @@ public boolean requiresFloatCastingOfIntegerDivision() {
return true;
}

@Override
public boolean supportsValuesList() {
return true;
}

@Override
public IdentityColumnSupport getIdentityColumnSupport() {
return identityColumnSupport;
Expand Down Expand Up @@ -900,4 +913,9 @@ public UniqueDelegate getUniqueDelegate() {
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
}

@Override
public String getFromDualForSelectOnly() {
return " from " + getDual();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
return false;
}

@Override
protected String getFromDualForSelectOnly() {
return " from " + getDual();
}

private boolean supportsOffsetFetchClause() {
return getDialect().getVersion().isSameOrAfter( 2, 5 );
}
Expand Down
Loading

0 comments on commit 9c5cf75

Please sign in to comment.