Skip to content

Commit 150d4f8

Browse files
committed
special handling for literal null inside a cast in select clause
this caused a failure on Informix
1 parent 526af6b commit 150d4f8

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void visitValuesTableReference(ValuesTableReference tableReference) {
205205
protected boolean shouldEmulateFetchClause(QueryPart queryPart) {
206206
// Check if current query part is already row numbering to avoid infinite recursion
207207
return useOffsetFetchClause( queryPart ) && getQueryPartForRowNumbering() != queryPart
208-
&& getDialect().supportsWindowFunctions() && !isRowsOnlyFetchClauseType( queryPart );
208+
&& getDialect().supportsWindowFunctions() && !isRowsOnlyFetchClauseType( queryPart );
209209
}
210210

211211
@Override

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public void render(
7777
renderCastArrayToString( sqlAppender, arguments.get( 0 ), dialect, walker );
7878
}
7979
else {
80+
if ( source instanceof QueryLiteral<?> literal && literal.getLiteralValue() == null ) {
81+
literal.setInCast(true);
82+
}
8083
new PatternRenderer( dialect.castPattern( sourceType, targetType ) )
8184
.render( sqlAppender, arguments, walker );
8285
}

hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7431,7 +7431,12 @@ public void visitJdbcLiteral(JdbcLiteral<?> jdbcLiteral) {
74317431

74327432
@Override
74337433
public void visitQueryLiteral(QueryLiteral<?> queryLiteral) {
7434-
visitLiteral( queryLiteral );
7434+
if ( queryLiteral.getLiteralValue() == null && queryLiteral.isInCast() ) {
7435+
appendSql( SqlAppender.NULL_KEYWORD );
7436+
}
7437+
else {
7438+
visitLiteral( queryLiteral );
7439+
}
74357440
}
74367441

74377442
@Override

hibernate-core/src/main/java/org/hibernate/sql/ast/tree/expression/QueryLiteral.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public class QueryLiteral<T> implements Literal, DomainResultProducer<T> {
3131
private final T value;
3232
private final SqlExpressible expressible;
33+
private boolean inCast;
3334

3435
public QueryLiteral(T value, SqlExpressible expressible) {
3536
assert value == null || expressible.getJdbcMapping().getJdbcJavaType().isInstance( value );
@@ -100,4 +101,12 @@ public void applySqlSelections(DomainResultCreationState creationState) {
100101
creationState.getSqlAstCreationState().getCreationContext().getMappingMetamodel().getTypeConfiguration()
101102
);
102103
}
104+
105+
public void setInCast(boolean inCast) {
106+
this.inCast = inCast;
107+
}
108+
109+
public boolean isInCast() {
110+
return inCast;
111+
}
103112
}

0 commit comments

Comments
 (0)