HHH-16945 Cast to varbinary for tuple distinct count on SQL Server to avoid collation issues

This commit is contained in:
Christian Beikov 2023-09-05 19:57:22 +02:00
parent a145db8d9b
commit ce19d9600e
3 changed files with 30 additions and 4 deletions

View File

@ -300,7 +300,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
"count_big", "count_big",
"+", "+",
"varchar(max)", "varchar(max)",
false false,
"varbinary(max)"
) )
); );

View File

@ -314,7 +314,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
"count_big", "count_big",
"+", "+",
"varchar(max)", "varchar(max)",
false true,
"varbinary(max)"
) )
); );

View File

@ -49,6 +49,7 @@ public class CountFunction extends AbstractSqmSelfRenderingFunctionDescriptor {
private final String concatOperator; private final String concatOperator;
private final String concatArgumentCastType; private final String concatArgumentCastType;
private final boolean castDistinctStringConcat; private final boolean castDistinctStringConcat;
private final String distinctArgumentCastType;
public CountFunction( public CountFunction(
Dialect dialect, Dialect dialect,
@ -79,7 +80,8 @@ public CountFunction(
"count", "count",
concatOperator, concatOperator,
concatArgumentCastType, concatArgumentCastType,
castDistinctStringConcat castDistinctStringConcat,
concatArgumentCastType
); );
} }
@ -91,6 +93,27 @@ public CountFunction(
String concatOperator, String concatOperator,
String concatArgumentCastType, String concatArgumentCastType,
boolean castDistinctStringConcat) { boolean castDistinctStringConcat) {
this(
dialect,
typeConfiguration,
defaultArgumentRenderingMode,
countFunctionName,
concatOperator,
concatArgumentCastType,
castDistinctStringConcat,
concatArgumentCastType
);
}
public CountFunction(
Dialect dialect,
TypeConfiguration typeConfiguration,
SqlAstNodeRenderingMode defaultArgumentRenderingMode,
String countFunctionName,
String concatOperator,
String concatArgumentCastType,
boolean castDistinctStringConcat,
String distinctArgumentCastType) {
super( super(
"count", "count",
FunctionKind.AGGREGATE, FunctionKind.AGGREGATE,
@ -106,6 +129,7 @@ public CountFunction(
this.concatOperator = concatOperator; this.concatOperator = concatOperator;
this.concatArgumentCastType = concatArgumentCastType; this.concatArgumentCastType = concatArgumentCastType;
this.castDistinctStringConcat = castDistinctStringConcat; this.castDistinctStringConcat = castDistinctStringConcat;
this.distinctArgumentCastType = distinctArgumentCastType;
} }
@Override @Override
@ -211,7 +235,7 @@ else if ( !dialect.supportsTupleDistinctCounts() ) {
sqlAppender.appendSql( "')" ); sqlAppender.appendSql( "')" );
if ( castDistinctStringConcat ) { if ( castDistinctStringConcat ) {
sqlAppender.appendSql( " as " ); sqlAppender.appendSql( " as " );
sqlAppender.appendSql( concatArgumentCastType ); sqlAppender.appendSql( distinctArgumentCastType );
sqlAppender.appendSql( ')' ); sqlAppender.appendSql( ')' );
} }
if ( caseWrapper ) { if ( caseWrapper ) {