Skip to content

Commit f12c6c3

Browse files
committed
Don't render distinct from predicate on SQL Server versions that don't support it
1 parent c0804d3 commit f12c6c3

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,28 @@ protected void renderComparison(Expression lhs, ComparisonOperator operator, Exp
492492
&& lhsExpressionType.getSingleJdbcMapping().getJdbcType().getDdlTypeCode() == SqlTypes.SQLXML ) {
493493
// In SQL Server, XMLTYPE is not "comparable", so we have to cast the two parts to varchar for this purpose
494494
switch ( operator ) {
495+
case DISTINCT_FROM:
496+
if ( !supportsDistinctFromPredicate() ) {
497+
appendSql( "not " );
498+
}
499+
case NOT_DISTINCT_FROM: {
500+
if ( !supportsDistinctFromPredicate() ) {
501+
appendSql( "exists (select cast(" );
502+
getClauseStack().push( Clause.SELECT );
503+
visitSqlSelectExpression( lhs );
504+
appendSql( " as nvarchar(max))" );
505+
appendSql( getFromDualForSelectOnly() );
506+
appendSql( " intersect select cast(" );
507+
visitSqlSelectExpression( rhs );
508+
appendSql( " as nvarchar(max))" );
509+
appendSql( getFromDualForSelectOnly() );
510+
getClauseStack().pop();
511+
appendSql( CLOSE_PARENTHESIS );
512+
return;
513+
}
514+
}
495515
case EQUAL:
496-
case NOT_DISTINCT_FROM:
497516
case NOT_EQUAL:
498-
case DISTINCT_FROM:
499517
appendSql( "cast(" );
500518
lhs.accept( this );
501519
appendSql( " as nvarchar(max))" );
@@ -509,7 +527,17 @@ protected void renderComparison(Expression lhs, ComparisonOperator operator, Exp
509527
break;
510528
}
511529
}
512-
renderComparisonEmulateIntersect( lhs, operator, rhs );
530+
if ( supportsDistinctFromPredicate() ) {
531+
renderComparisonStandard( lhs, operator, rhs );
532+
}
533+
else {
534+
renderComparisonEmulateIntersect( lhs, operator, rhs );
535+
}
536+
}
537+
538+
@Override
539+
protected boolean supportsDistinctFromPredicate() {
540+
return getDialect().getVersion().isSameOrAfter( 16 );
513541
}
514542

515543
@Override

hibernate-core/src/main/java/org/hibernate/dialect/SQLServerSqlAstTranslator.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,28 @@ protected void renderComparison(Expression lhs, ComparisonOperator operator, Exp
463463
&& lhsExpressionType.getSingleJdbcMapping().getJdbcType().getDdlTypeCode() == SqlTypes.SQLXML ) {
464464
// In SQL Server, XMLTYPE is not "comparable", so we have to cast the two parts to varchar for this purpose
465465
switch ( operator ) {
466+
case DISTINCT_FROM:
467+
if ( !supportsDistinctFromPredicate() ) {
468+
appendSql( "not " );
469+
}
470+
case NOT_DISTINCT_FROM: {
471+
if ( !supportsDistinctFromPredicate() ) {
472+
appendSql( "exists (select cast(" );
473+
getClauseStack().push( Clause.SELECT );
474+
visitSqlSelectExpression( lhs );
475+
appendSql( " as nvarchar(max))" );
476+
appendSql( getFromDualForSelectOnly() );
477+
appendSql( " intersect select cast(" );
478+
visitSqlSelectExpression( rhs );
479+
appendSql( " as nvarchar(max))" );
480+
appendSql( getFromDualForSelectOnly() );
481+
getClauseStack().pop();
482+
appendSql( CLOSE_PARENTHESIS );
483+
return;
484+
}
485+
}
466486
case EQUAL:
467-
case NOT_DISTINCT_FROM:
468487
case NOT_EQUAL:
469-
case DISTINCT_FROM:
470488
appendSql( "cast(" );
471489
lhs.accept( this );
472490
appendSql( " as nvarchar(max))" );
@@ -480,7 +498,17 @@ protected void renderComparison(Expression lhs, ComparisonOperator operator, Exp
480498
break;
481499
}
482500
}
483-
renderComparisonEmulateIntersect( lhs, operator, rhs );
501+
if ( supportsDistinctFromPredicate() ) {
502+
renderComparisonStandard( lhs, operator, rhs );
503+
}
504+
else {
505+
renderComparisonEmulateIntersect( lhs, operator, rhs );
506+
}
507+
}
508+
509+
@Override
510+
protected boolean supportsDistinctFromPredicate() {
511+
return getDialect().getVersion().isSameOrAfter( 16 );
484512
}
485513

486514
@Override

0 commit comments

Comments
 (0)