From dc65d041d10198becb3e7e8337e52b20c1caadda Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Fri, 4 Feb 2022 15:14:36 +0100 Subject: [PATCH] Fix issues with min/max element/index in the select clause --- .../sqm/sql/BaseSqmToSqlAstConverter.java | 25 +++++++++++++++-- .../ast/tree/from/QueryPartTableGroup.java | 27 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 69109a1a09..e4fd9689af 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -3755,12 +3755,23 @@ public abstract class BaseSqmToSqlAstConverter extends Base 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( queryPath, null, subQuerySpec, identifierVariable, columnNames, + compatibleTableExpression, true, false, creationContext.getSessionFactory() @@ -3801,7 +3812,12 @@ public abstract class BaseSqmToSqlAstConverter extends Base } parentFromClauseAccess.registerTableGroup( lateralTableGroup.getNavigablePath(), lateralTableGroup ); if ( jdbcTypeCount == 1 ) { - return resultColumnReferences.get( 0 ); + return new BasicValuedPathInterpretation<>( + resultColumnReferences.get( 0 ), + queryPath, + (BasicValuedModelPart) modelPart, + lateralTableGroup + ); } else { return new SqlTuple( resultColumnReferences, modelPart ); @@ -3813,7 +3829,8 @@ public abstract class BaseSqmToSqlAstConverter extends Base } final QueryPartTableReference tableReference = (QueryPartTableReference) lateralTableGroup.getPrimaryTableReference(); if ( jdbcTypeCount == 1 ) { - return new ColumnReference( + return new BasicValuedPathInterpretation<>( + new ColumnReference( identifierVariable, tableReference.getColumnNames().get( 0 ), false, @@ -3821,6 +3838,10 @@ public abstract class BaseSqmToSqlAstConverter extends Base null, modelPart.getJdbcMappings().get( 0 ), creationContext.getSessionFactory() + ), + queryPath, + (BasicValuedModelPart) modelPart, + lateralTableGroup ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/QueryPartTableGroup.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/QueryPartTableGroup.java index 3009f15af6..5a947a05c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/QueryPartTableGroup.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/QueryPartTableGroup.java @@ -8,6 +8,7 @@ package org.hibernate.sql.ast.tree.from; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.function.Consumer; import org.hibernate.engine.spi.SessionFactoryImplementor; @@ -22,6 +23,7 @@ import org.hibernate.sql.ast.tree.select.QueryPart; public class QueryPartTableGroup extends AbstractTableGroup { private final QueryPartTableReference queryPartTableReference; + private final String compatibleTableExpression; public QueryPartTableGroup( NavigablePath navigablePath, @@ -32,6 +34,28 @@ public class QueryPartTableGroup extends AbstractTableGroup { boolean lateral, boolean canUseInnerJoins, SessionFactoryImplementor sessionFactory) { + this( + navigablePath, + tableGroupProducer, + queryPart, + sourceAlias, + columnNames, + null, + lateral, + canUseInnerJoins, + sessionFactory + ); + } + + public QueryPartTableGroup( + NavigablePath navigablePath, + TableGroupProducer tableGroupProducer, + QueryPart queryPart, + String sourceAlias, + List columnNames, + String compatibleTableExpression, boolean lateral, + boolean canUseInnerJoins, + SessionFactoryImplementor sessionFactory) { super( canUseInnerJoins, navigablePath, @@ -40,6 +64,7 @@ public class QueryPartTableGroup extends AbstractTableGroup { null, sessionFactory ); + this.compatibleTableExpression = compatibleTableExpression; this.queryPartTableReference = new QueryPartTableReference( queryPart, sourceAlias, @@ -60,7 +85,7 @@ public class QueryPartTableGroup extends AbstractTableGroup { String tableExpression, boolean allowFkOptimization, boolean resolve) { - if ( tableExpression == null ) { + if ( Objects.equals( tableExpression, compatibleTableExpression ) ) { return getPrimaryTableReference(); } for ( TableGroupJoin tableGroupJoin : getNestedTableGroupJoins() ) {