Fix issues with min/max element/index in the select clause

This commit is contained in:
Christian Beikov 2022-02-04 15:14:36 +01:00
parent e0a35287c9
commit dc65d041d1
2 changed files with 49 additions and 3 deletions

View File

@ -3755,12 +3755,23 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
creationContext creationContext
) )
); );
final String compatibleTableExpression;
if ( modelPart instanceof BasicValuedModelPart ) {
compatibleTableExpression = ( (BasicValuedModelPart) modelPart ).getContainingTableExpression();
}
else if ( modelPart instanceof EmbeddableValuedModelPart ) {
compatibleTableExpression = ( (EmbeddableValuedModelPart) modelPart ).getContainingTableExpression();
}
else {
compatibleTableExpression = null;
}
lateralTableGroup = new QueryPartTableGroup( lateralTableGroup = new QueryPartTableGroup(
queryPath, queryPath,
null, null,
subQuerySpec, subQuerySpec,
identifierVariable, identifierVariable,
columnNames, columnNames,
compatibleTableExpression,
true, true,
false, false,
creationContext.getSessionFactory() creationContext.getSessionFactory()
@ -3801,7 +3812,12 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
} }
parentFromClauseAccess.registerTableGroup( lateralTableGroup.getNavigablePath(), lateralTableGroup ); parentFromClauseAccess.registerTableGroup( lateralTableGroup.getNavigablePath(), lateralTableGroup );
if ( jdbcTypeCount == 1 ) { if ( jdbcTypeCount == 1 ) {
return resultColumnReferences.get( 0 ); return new BasicValuedPathInterpretation<>(
resultColumnReferences.get( 0 ),
queryPath,
(BasicValuedModelPart) modelPart,
lateralTableGroup
);
} }
else { else {
return new SqlTuple( resultColumnReferences, modelPart ); return new SqlTuple( resultColumnReferences, modelPart );
@ -3813,7 +3829,8 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
} }
final QueryPartTableReference tableReference = (QueryPartTableReference) lateralTableGroup.getPrimaryTableReference(); final QueryPartTableReference tableReference = (QueryPartTableReference) lateralTableGroup.getPrimaryTableReference();
if ( jdbcTypeCount == 1 ) { if ( jdbcTypeCount == 1 ) {
return new ColumnReference( return new BasicValuedPathInterpretation<>(
new ColumnReference(
identifierVariable, identifierVariable,
tableReference.getColumnNames().get( 0 ), tableReference.getColumnNames().get( 0 ),
false, false,
@ -3821,6 +3838,10 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
null, null,
modelPart.getJdbcMappings().get( 0 ), modelPart.getJdbcMappings().get( 0 ),
creationContext.getSessionFactory() creationContext.getSessionFactory()
),
queryPath,
(BasicValuedModelPart) modelPart,
lateralTableGroup
); );
} }
else { else {

View File

@ -8,6 +8,7 @@ package org.hibernate.sql.ast.tree.from;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -22,6 +23,7 @@ import org.hibernate.sql.ast.tree.select.QueryPart;
public class QueryPartTableGroup extends AbstractTableGroup { public class QueryPartTableGroup extends AbstractTableGroup {
private final QueryPartTableReference queryPartTableReference; private final QueryPartTableReference queryPartTableReference;
private final String compatibleTableExpression;
public QueryPartTableGroup( public QueryPartTableGroup(
NavigablePath navigablePath, NavigablePath navigablePath,
@ -32,6 +34,28 @@ public class QueryPartTableGroup extends AbstractTableGroup {
boolean lateral, boolean lateral,
boolean canUseInnerJoins, boolean canUseInnerJoins,
SessionFactoryImplementor sessionFactory) { SessionFactoryImplementor sessionFactory) {
this(
navigablePath,
tableGroupProducer,
queryPart,
sourceAlias,
columnNames,
null,
lateral,
canUseInnerJoins,
sessionFactory
);
}
public QueryPartTableGroup(
NavigablePath navigablePath,
TableGroupProducer tableGroupProducer,
QueryPart queryPart,
String sourceAlias,
List<String> columnNames,
String compatibleTableExpression, boolean lateral,
boolean canUseInnerJoins,
SessionFactoryImplementor sessionFactory) {
super( super(
canUseInnerJoins, canUseInnerJoins,
navigablePath, navigablePath,
@ -40,6 +64,7 @@ public class QueryPartTableGroup extends AbstractTableGroup {
null, null,
sessionFactory sessionFactory
); );
this.compatibleTableExpression = compatibleTableExpression;
this.queryPartTableReference = new QueryPartTableReference( this.queryPartTableReference = new QueryPartTableReference(
queryPart, queryPart,
sourceAlias, sourceAlias,
@ -60,7 +85,7 @@ public class QueryPartTableGroup extends AbstractTableGroup {
String tableExpression, String tableExpression,
boolean allowFkOptimization, boolean allowFkOptimization,
boolean resolve) { boolean resolve) {
if ( tableExpression == null ) { if ( Objects.equals( tableExpression, compatibleTableExpression ) ) {
return getPrimaryTableReference(); return getPrimaryTableReference();
} }
for ( TableGroupJoin tableGroupJoin : getNestedTableGroupJoins() ) { for ( TableGroupJoin tableGroupJoin : getNestedTableGroupJoins() ) {