Skip to content

Commit ce19d96

Browse files
committed
HHH-16945 Cast to varbinary for tuple distinct count on SQL Server to avoid collation issues
1 parent a145db8 commit ce19d96

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
300300
"count_big",
301301
"+",
302302
"varchar(max)",
303-
false
303+
false,
304+
"varbinary(max)"
304305
)
305306
);
306307

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
314314
"count_big",
315315
"+",
316316
"varchar(max)",
317-
false
317+
true,
318+
"varbinary(max)"
318319
)
319320
);
320321

hibernate-core/src/main/java/org/hibernate/dialect/function/CountFunction.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class CountFunction extends AbstractSqmSelfRenderingFunctionDescriptor {
4949
private final String concatOperator;
5050
private final String concatArgumentCastType;
5151
private final boolean castDistinctStringConcat;
52+
private final String distinctArgumentCastType;
5253

5354
public CountFunction(
5455
Dialect dialect,
@@ -79,7 +80,8 @@ public CountFunction(
7980
"count",
8081
concatOperator,
8182
concatArgumentCastType,
82-
castDistinctStringConcat
83+
castDistinctStringConcat,
84+
concatArgumentCastType
8385
);
8486
}
8587

@@ -91,6 +93,27 @@ public CountFunction(
9193
String concatOperator,
9294
String concatArgumentCastType,
9395
boolean castDistinctStringConcat) {
96+
this(
97+
dialect,
98+
typeConfiguration,
99+
defaultArgumentRenderingMode,
100+
countFunctionName,
101+
concatOperator,
102+
concatArgumentCastType,
103+
castDistinctStringConcat,
104+
concatArgumentCastType
105+
);
106+
}
107+
108+
public CountFunction(
109+
Dialect dialect,
110+
TypeConfiguration typeConfiguration,
111+
SqlAstNodeRenderingMode defaultArgumentRenderingMode,
112+
String countFunctionName,
113+
String concatOperator,
114+
String concatArgumentCastType,
115+
boolean castDistinctStringConcat,
116+
String distinctArgumentCastType) {
94117
super(
95118
"count",
96119
FunctionKind.AGGREGATE,
@@ -106,6 +129,7 @@ public CountFunction(
106129
this.concatOperator = concatOperator;
107130
this.concatArgumentCastType = concatArgumentCastType;
108131
this.castDistinctStringConcat = castDistinctStringConcat;
132+
this.distinctArgumentCastType = distinctArgumentCastType;
109133
}
110134

111135
@Override
@@ -211,7 +235,7 @@ else if ( !dialect.supportsTupleDistinctCounts() ) {
211235
sqlAppender.appendSql( "')" );
212236
if ( castDistinctStringConcat ) {
213237
sqlAppender.appendSql( " as " );
214-
sqlAppender.appendSql( concatArgumentCastType );
238+
sqlAppender.appendSql( distinctArgumentCastType );
215239
sqlAppender.appendSql( ')' );
216240
}
217241
if ( caseWrapper ) {

0 commit comments

Comments
 (0)