Skip to content

Commit dc65d04

Browse files
committed
Fix issues with min/max element/index in the select clause
1 parent e0a3528 commit dc65d04

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,12 +3755,23 @@ protected Expression createLateralJoinExpression(
37553755
creationContext
37563756
)
37573757
);
3758+
final String compatibleTableExpression;
3759+
if ( modelPart instanceof BasicValuedModelPart ) {
3760+
compatibleTableExpression = ( (BasicValuedModelPart) modelPart ).getContainingTableExpression();
3761+
}
3762+
else if ( modelPart instanceof EmbeddableValuedModelPart ) {
3763+
compatibleTableExpression = ( (EmbeddableValuedModelPart) modelPart ).getContainingTableExpression();
3764+
}
3765+
else {
3766+
compatibleTableExpression = null;
3767+
}
37583768
lateralTableGroup = new QueryPartTableGroup(
37593769
queryPath,
37603770
null,
37613771
subQuerySpec,
37623772
identifierVariable,
37633773
columnNames,
3774+
compatibleTableExpression,
37643775
true,
37653776
false,
37663777
creationContext.getSessionFactory()
@@ -3801,7 +3812,12 @@ protected Expression createLateralJoinExpression(
38013812
}
38023813
parentFromClauseAccess.registerTableGroup( lateralTableGroup.getNavigablePath(), lateralTableGroup );
38033814
if ( jdbcTypeCount == 1 ) {
3804-
return resultColumnReferences.get( 0 );
3815+
return new BasicValuedPathInterpretation<>(
3816+
resultColumnReferences.get( 0 ),
3817+
queryPath,
3818+
(BasicValuedModelPart) modelPart,
3819+
lateralTableGroup
3820+
);
38053821
}
38063822
else {
38073823
return new SqlTuple( resultColumnReferences, modelPart );
@@ -3813,14 +3829,19 @@ protected Expression createLateralJoinExpression(
38133829
}
38143830
final QueryPartTableReference tableReference = (QueryPartTableReference) lateralTableGroup.getPrimaryTableReference();
38153831
if ( jdbcTypeCount == 1 ) {
3816-
return new ColumnReference(
3832+
return new BasicValuedPathInterpretation<>(
3833+
new ColumnReference(
38173834
identifierVariable,
38183835
tableReference.getColumnNames().get( 0 ),
38193836
false,
38203837
null,
38213838
null,
38223839
modelPart.getJdbcMappings().get( 0 ),
38233840
creationContext.getSessionFactory()
3841+
),
3842+
queryPath,
3843+
(BasicValuedModelPart) modelPart,
3844+
lateralTableGroup
38243845
);
38253846
}
38263847
else {

hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/QueryPartTableGroup.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.util.Collections;
1010
import java.util.List;
11+
import java.util.Objects;
1112
import java.util.function.Consumer;
1213

1314
import org.hibernate.engine.spi.SessionFactoryImplementor;
@@ -22,6 +23,7 @@
2223
public class QueryPartTableGroup extends AbstractTableGroup {
2324

2425
private final QueryPartTableReference queryPartTableReference;
26+
private final String compatibleTableExpression;
2527

2628
public QueryPartTableGroup(
2729
NavigablePath navigablePath,
@@ -32,6 +34,28 @@ public QueryPartTableGroup(
3234
boolean lateral,
3335
boolean canUseInnerJoins,
3436
SessionFactoryImplementor sessionFactory) {
37+
this(
38+
navigablePath,
39+
tableGroupProducer,
40+
queryPart,
41+
sourceAlias,
42+
columnNames,
43+
null,
44+
lateral,
45+
canUseInnerJoins,
46+
sessionFactory
47+
);
48+
}
49+
50+
public QueryPartTableGroup(
51+
NavigablePath navigablePath,
52+
TableGroupProducer tableGroupProducer,
53+
QueryPart queryPart,
54+
String sourceAlias,
55+
List<String> columnNames,
56+
String compatibleTableExpression, boolean lateral,
57+
boolean canUseInnerJoins,
58+
SessionFactoryImplementor sessionFactory) {
3559
super(
3660
canUseInnerJoins,
3761
navigablePath,
@@ -40,6 +64,7 @@ public QueryPartTableGroup(
4064
null,
4165
sessionFactory
4266
);
67+
this.compatibleTableExpression = compatibleTableExpression;
4368
this.queryPartTableReference = new QueryPartTableReference(
4469
queryPart,
4570
sourceAlias,
@@ -60,7 +85,7 @@ protected TableReference getTableReferenceInternal(
6085
String tableExpression,
6186
boolean allowFkOptimization,
6287
boolean resolve) {
63-
if ( tableExpression == null ) {
88+
if ( Objects.equals( tableExpression, compatibleTableExpression ) ) {
6489
return getPrimaryTableReference();
6590
}
6691
for ( TableGroupJoin tableGroupJoin : getNestedTableGroupJoins() ) {

0 commit comments

Comments
 (0)