Skip to content

Commit 9c5cf75

Browse files
beikovsebersole
authored andcommitted
HHH-18447 Try using native cast for string to boolean
1 parent 723e18d commit 9c5cf75

File tree

61 files changed

+595
-256
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+595
-256
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/AltibaseDialect.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,26 @@ public String castPattern(CastType from, CastType to) {
395395
}
396396
break;
397397
case INTEGER_BOOLEAN:
398-
result = BooleanDecoder.toIntegerBoolean( from );
398+
result = from == CastType.STRING
399+
? buildStringToBooleanCastDecode( "1", "0" )
400+
: BooleanDecoder.toIntegerBoolean( from );
399401
if ( result != null ) {
400402
return result;
401403
}
402404
break;
403405
case YN_BOOLEAN:
404-
result = BooleanDecoder.toYesNoBoolean( from );
406+
result = from == CastType.STRING
407+
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
408+
: BooleanDecoder.toYesNoBoolean( from );
405409
if ( result != null ) {
406410
return result;
407411
}
408412
break;
409413
case BOOLEAN:
410414
case TF_BOOLEAN:
411-
result = BooleanDecoder.toTrueFalseBoolean( from );
415+
result = from == CastType.STRING
416+
? buildStringToBooleanCastDecode( "'T'", "'F'" )
417+
: BooleanDecoder.toTrueFalseBoolean( from );
412418
if ( result != null ) {
413419
return result;
414420
}
@@ -704,4 +710,14 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
704710
};
705711
}
706712

713+
@Override
714+
public String getDual() {
715+
return "dual";
716+
}
717+
718+
@Override
719+
public String getFromDualForSelectOnly() {
720+
return " from " + getDual();
721+
}
722+
707723
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/AltibaseSqlAstTranslator.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,6 @@ public void visitQueryPartTableReference(QueryPartTableReference tableReference)
221221
emulateQueryPartTableReferenceColumnAliasing( tableReference );
222222
}
223223

224-
@Override
225-
protected String getDual() {
226-
return "dual";
227-
}
228-
229-
@Override
230-
protected String getFromDualForSelectOnly() {
231-
return " from " + getDual();
232-
}
233-
234224
@Override
235225
protected boolean needsRecursiveKeywordInWithClause() {
236226
return false;

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CUBRIDDialect.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,16 @@ private void timediff(
517517
sqlAppender.append( diffUnit.conversionFactor( toUnit, this ) );
518518
}
519519

520+
@Override
521+
public String getDual() {
522+
//TODO: is this really needed?
523+
//TODO: would "from table({0})" be better?
524+
return "db_root";
525+
}
526+
527+
@Override
528+
public String getFromDualForSelectOnly() {
529+
return " from " + getDual();
530+
}
531+
520532
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CUBRIDSqlAstTranslator.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,4 @@ protected boolean supportsRowValueConstructorSyntaxInInList() {
8080
protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
8181
return false;
8282
}
83-
84-
@Override
85-
protected String getDual() {
86-
//TODO: is this really needed?
87-
//TODO: would "from table({0})" be better?
88-
return "db_root";
89-
}
90-
91-
@Override
92-
protected String getFromDualForSelectOnly() {
93-
return " from " + getDual();
94-
}
9583
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
7171
import org.hibernate.procedure.internal.DB2CallableStatementSupport;
7272
import org.hibernate.procedure.spi.CallableStatementSupport;
73+
import org.hibernate.query.sqm.CastType;
7374
import org.hibernate.query.sqm.IntervalType;
7475
import org.hibernate.query.sqm.TemporalUnit;
7576
import org.hibernate.query.sqm.mutation.internal.cte.CteInsertStrategy;
@@ -1141,6 +1142,16 @@ public String extractPattern(TemporalUnit unit) {
11411142
return super.extractPattern( unit );
11421143
}
11431144

1145+
@Override
1146+
public String castPattern(CastType from, CastType to) {
1147+
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
1148+
return "cast(?1 as ?2)";
1149+
}
1150+
else {
1151+
return super.castPattern( from, to );
1152+
}
1153+
}
1154+
11441155
@Override
11451156
public int getInExpressionCountLimit() {
11461157
return BIND_PARAMETERS_NUMBER_LIMIT;
@@ -1208,4 +1219,14 @@ public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
12081219
public boolean supportsFromClauseInUpdate() {
12091220
return getDB2Version().isSameOrAfter( 11 );
12101221
}
1222+
1223+
@Override
1224+
public String getDual() {
1225+
return "sysibm.dual";
1226+
}
1227+
1228+
@Override
1229+
public String getFromDualForSelectOnly() {
1230+
return " from " + getDual();
1231+
}
12111232
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacySqlAstTranslator.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -606,16 +606,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
606606
return false;
607607
}
608608

609-
@Override
610-
protected String getDual() {
611-
return "sysibm.dual";
612-
}
613-
614-
@Override
615-
protected String getFromDualForSelectOnly() {
616-
return " from " + getDual();
617-
}
618-
619609
@Override
620610
protected void visitReturningColumns(List<ColumnReference> returningColumns) {
621611
// For DB2 we use #renderReturningClause to render a wrapper around the DML statement

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacyDialect.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,11 @@ public boolean supportsWindowFunctions() {
10551055
return getVersion().isSameOrAfter( 10, 4 );
10561056
}
10571057

1058+
@Override
1059+
public boolean supportsValuesList() {
1060+
return true;
1061+
}
1062+
10581063
@Override
10591064
public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, DatabaseMetaData dbMetaData)
10601065
throws SQLException {
@@ -1066,4 +1071,14 @@ public IdentifierHelper buildIdentifierHelper(IdentifierHelperBuilder builder, D
10661071
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
10671072
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
10681073
}
1074+
1075+
@Override
1076+
public String getDual() {
1077+
return "(values 0)";
1078+
}
1079+
1080+
@Override
1081+
public String getFromDualForSelectOnly() {
1082+
return " from " + getDual() + " dual";
1083+
}
10691084
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DerbyLegacySqlAstTranslator.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
302302
return false;
303303
}
304304

305-
@Override
306-
protected String getDual() {
307-
return "(values 0)";
308-
}
309-
310-
@Override
311-
protected String getFromDualForSelectOnly() {
312-
return " from " + getDual() + " dual";
313-
}
314-
315305
@Override
316306
protected boolean needsRowsToSkip() {
317307
return !supportsOffsetFetchClause();

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/FirebirdDialect.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,33 @@ public String castPattern(CastType from, CastType to) {
403403
}
404404
break;
405405
case BOOLEAN:
406-
result = BooleanDecoder.toBoolean( from );
406+
result = from == CastType.STRING
407+
? buildStringToBooleanCastDecode( "true", "false" )
408+
: BooleanDecoder.toBoolean( from );
407409
if ( result != null ) {
408410
return result;
409411
}
410412
break;
411413
case INTEGER_BOOLEAN:
412-
result = BooleanDecoder.toIntegerBoolean( from );
414+
result = from == CastType.STRING
415+
? buildStringToBooleanCastDecode( "1", "0" )
416+
: BooleanDecoder.toIntegerBoolean( from );
413417
if ( result != null ) {
414418
return result;
415419
}
416420
break;
417421
case YN_BOOLEAN:
418-
result = BooleanDecoder.toYesNoBoolean( from );
422+
result = from == CastType.STRING
423+
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
424+
: BooleanDecoder.toYesNoBoolean( from );
419425
if ( result != null ) {
420426
return result;
421427
}
422428
break;
423429
case TF_BOOLEAN:
424-
result = BooleanDecoder.toTrueFalseBoolean( from );
430+
result = from == CastType.STRING
431+
? buildStringToBooleanCastDecode( "'T'", "'F'" )
432+
: BooleanDecoder.toTrueFalseBoolean( from );
425433
if ( result != null ) {
426434
return result;
427435
}
@@ -1096,4 +1104,14 @@ else if ( supportsOffset && temporalAccessor instanceof Instant ) {
10961104
}
10971105

10981106
}
1107+
1108+
@Override
1109+
public String getDual() {
1110+
return "rdb$database";
1111+
}
1112+
1113+
@Override
1114+
public String getFromDualForSelectOnly() {
1115+
return " from " + getDual();
1116+
}
10991117
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/FirebirdSqlAstTranslator.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
262262
return false;
263263
}
264264

265-
@Override
266-
protected String getDual() {
267-
return "rdb$database";
268-
}
269-
270-
@Override
271-
protected String getFromDualForSelectOnly() {
272-
return " from " + getDual();
273-
}
274-
275265
private boolean supportsOffsetFetchClause() {
276266
return getDialect().getVersion().isSameOrAfter( 3 );
277267
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacyDialect.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.hibernate.internal.util.JdbcExceptionHelper;
4949
import org.hibernate.metamodel.mapping.EntityMappingType;
5050
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
51+
import org.hibernate.query.sqm.CastType;
5152
import org.hibernate.query.sqm.FetchClauseType;
5253
import org.hibernate.query.sqm.IntervalType;
5354
import org.hibernate.dialect.NullOrdering;
@@ -514,6 +515,16 @@ public String extractPattern(TemporalUnit unit) {
514515
: super.extractPattern(unit);
515516
}
516517

518+
@Override
519+
public String castPattern(CastType from, CastType to) {
520+
if ( from == CastType.STRING && to == CastType.BOOLEAN ) {
521+
return "cast(?1 as ?2)";
522+
}
523+
else {
524+
return super.castPattern( from, to );
525+
}
526+
}
527+
517528
@Override
518529
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
519530
if ( intervalType != null ) {
@@ -992,4 +1003,14 @@ public String getCaseInsensitiveLike() {
9921003
public boolean supportsCaseInsensitiveLike() {
9931004
return getVersion().isSameOrAfter( 1, 4, 194 );
9941005
}
1006+
1007+
@Override
1008+
public boolean supportsValuesList() {
1009+
return true;
1010+
}
1011+
1012+
@Override
1013+
public String getDual() {
1014+
return "dual";
1015+
}
9951016
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacySqlAstTranslator.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,6 @@ protected boolean supportsNullPrecedence() {
388388
return getClauseStack().getCurrent() != Clause.WITHIN_GROUP || getDialect().getVersion().isSameOrAfter( 2 );
389389
}
390390

391-
@Override
392-
protected String getDual() {
393-
return "dual";
394-
}
395-
396391
private boolean supportsOffsetFetchClause() {
397392
return getDialect().getVersion().isSameOrAfter( 1, 4, 195 );
398393
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacyDialect.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,25 +315,33 @@ public String castPattern(CastType from, CastType to) {
315315
}
316316
break;
317317
case BOOLEAN:
318-
result = BooleanDecoder.toBoolean( from );
318+
result = from == CastType.STRING
319+
? buildStringToBooleanCastDecode( "true", "false" )
320+
: BooleanDecoder.toBoolean( from );
319321
if ( result != null ) {
320322
return result;
321323
}
322324
break;
323325
case INTEGER_BOOLEAN:
324-
result = BooleanDecoder.toIntegerBoolean( from );
326+
result = from == CastType.STRING
327+
? buildStringToBooleanCastDecode( "1", "0" )
328+
: BooleanDecoder.toIntegerBoolean( from );
325329
if ( result != null ) {
326330
return result;
327331
}
328332
break;
329333
case YN_BOOLEAN:
330-
result = BooleanDecoder.toYesNoBoolean( from );
334+
result = from == CastType.STRING
335+
? buildStringToBooleanCastDecode( "'Y'", "'N'" )
336+
: BooleanDecoder.toYesNoBoolean( from );
331337
if ( result != null ) {
332338
return result;
333339
}
334340
break;
335341
case TF_BOOLEAN:
336-
result = BooleanDecoder.toTrueFalseBoolean( from );
342+
result = from == CastType.STRING
343+
? buildStringToBooleanCastDecode( "'T'", "'F'" )
344+
: BooleanDecoder.toTrueFalseBoolean( from );
337345
if ( result != null ) {
338346
return result;
339347
}
@@ -825,6 +833,11 @@ public boolean requiresFloatCastingOfIntegerDivision() {
825833
return true;
826834
}
827835

836+
@Override
837+
public boolean supportsValuesList() {
838+
return true;
839+
}
840+
828841
@Override
829842
public IdentityColumnSupport getIdentityColumnSupport() {
830843
return identityColumnSupport;
@@ -900,4 +913,9 @@ public UniqueDelegate getUniqueDelegate() {
900913
public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
901914
return DmlTargetColumnQualifierSupport.TABLE_ALIAS;
902915
}
916+
917+
@Override
918+
public String getFromDualForSelectOnly() {
919+
return " from " + getDual();
920+
}
903921
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacySqlAstTranslator.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,6 @@ protected boolean supportsRowValueConstructorSyntaxInQuantifiedPredicates() {
341341
return false;
342342
}
343343

344-
@Override
345-
protected String getFromDualForSelectOnly() {
346-
return " from " + getDual();
347-
}
348-
349344
private boolean supportsOffsetFetchClause() {
350345
return getDialect().getVersion().isSameOrAfter( 2, 5 );
351346
}

0 commit comments

Comments
 (0)