Skip to content

Commit

Permalink
HHH-18720 Type check on select columns in union all gives SemanticExc…
Browse files Browse the repository at this point in the history
…eption when there is a null column
  • Loading branch information
dreab8 committed Oct 22, 2024
1 parent cdca0d3 commit 0843f7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private void validateQueryGroupFetchStructure(List<? extends SqmTypedNode<?>> ty
for ( int j = 0; j < firstSelectionSize; j++ ) {
final SqmTypedNode<?> firstSqmSelection = typedNodes.get( j );
final JavaType<?> firstJavaType = firstSqmSelection.getNodeJavaType();
if ( firstJavaType != selections.get( j ).getNodeJavaType() ) {
final JavaType<?> nodeJavaType = selections.get( j ).getNodeJavaType();
if ( nodeJavaType != null && firstJavaType != null && firstJavaType != nodeJavaType ) {
throw new SemanticException(
"Select items of the same index must have the same java type across all query parts"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.results.ResultSetMapping;
import org.hibernate.query.results.ResultSetMappingImpl;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.select.QueryGroup;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducer;
import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducerProvider;
import org.hibernate.type.descriptor.jdbc.NullJdbcType;

import java.util.List;

/**
* Standard JdbcValuesMappingProducerProvider implementation
Expand All @@ -28,10 +34,20 @@ public class JdbcValuesMappingProducerProviderStandard implements JdbcValuesMapp
public JdbcValuesMappingProducer buildMappingProducer(
SelectStatement sqlAst,
SessionFactoryImplementor sessionFactory) {
return new JdbcValuesMappingProducerStandard(
sqlAst.getQuerySpec().getSelectClause().getSqlSelections(),
sqlAst.getDomainResultDescriptors()
);
return new JdbcValuesMappingProducerStandard( getSelections( sqlAst ), sqlAst.getDomainResultDescriptors() );
}

private static List<SqlSelection> getSelections(SelectStatement selectStatement) {
if ( selectStatement.getQueryPart() instanceof QueryGroup ) {
final QueryGroup queryGroup = (QueryGroup) selectStatement.getQueryPart();
for ( QueryPart queryPart : queryGroup.getQueryParts() ) {
if ( !(queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections()
.get( 0 ).getExpressionType().getSingleJdbcMapping().getJdbcType() instanceof NullJdbcType) ) {
return queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections();
}
}
}
return selectStatement.getQuerySpec().getSelectClause().getSqlSelections();
}

@Override
Expand Down

0 comments on commit 0843f7d

Please sign in to comment.