|
25 | 25 | import org.hibernate.query.sqm.tree.domain.SqmBasicValuedSimplePath;
|
26 | 26 | import org.hibernate.query.sqm.tree.domain.SqmPath;
|
27 | 27 | import org.hibernate.query.sqm.tree.domain.SqmTreatedPath;
|
| 28 | +import org.hibernate.query.sqm.tree.select.SqmOrderByClause; |
| 29 | +import org.hibernate.query.sqm.tree.select.SqmQuerySpec; |
| 30 | +import org.hibernate.query.sqm.tree.select.SqmSelectClause; |
28 | 31 | import org.hibernate.spi.NavigablePath;
|
29 | 32 | import org.hibernate.sql.ast.SqlAstWalker;
|
30 | 33 | import org.hibernate.sql.ast.tree.expression.ColumnReference;
|
|
35 | 38 | import org.hibernate.sql.ast.tree.update.Assignable;
|
36 | 39 |
|
37 | 40 | import static org.hibernate.internal.util.NullnessUtil.castNonNull;
|
| 41 | +import static org.hibernate.query.sqm.internal.SqmUtil.isFkOptimizationAllowed; |
38 | 42 | import static org.hibernate.query.sqm.internal.SqmUtil.needsTargetTableMapping;
|
39 | 43 |
|
40 | 44 | /**
|
41 | 45 | * @author Steve Ebersole
|
| 46 | + * @author Yanming Zhou |
42 | 47 | */
|
43 | 48 | public class BasicValuedPathInterpretation<T> extends AbstractSqmPathInterpretation<T> implements Assignable, DomainResultProducer<T> {
|
44 | 49 | /**
|
@@ -83,7 +88,7 @@ public static <T> BasicValuedPathInterpretation<T> from(
|
83 | 88 | }
|
84 | 89 |
|
85 | 90 | final ModelPart modelPart;
|
86 |
| - if ( needsTargetTableMapping( sqmPath, modelPartContainer ) ) { |
| 91 | + if ( !isFkOptimizationAllowedForState( sqmPath.getLhs(), sqlAstCreationState ) && needsTargetTableMapping( sqmPath, modelPartContainer ) ) { |
87 | 92 | // We have to make sure we render the column of the target table
|
88 | 93 | modelPart = ( (ManagedMappingType) modelPartContainer.getPartMappingType() ).findSubPart(
|
89 | 94 | sqmPath.getReferencedPathSource().getPathName(),
|
@@ -140,6 +145,29 @@ else if ( expression instanceof SqlSelectionExpression ) {
|
140 | 145 | return new BasicValuedPathInterpretation<>( columnReference, sqmPath.getNavigablePath(), mapping, tableGroup );
|
141 | 146 | }
|
142 | 147 |
|
| 148 | + private static boolean isFkOptimizationAllowedForState(SqmPath<?> sqmPath, SqmToSqlAstConverter sqlAstCreationState) { |
| 149 | + boolean isFkOptimizationAllowed = isFkOptimizationAllowed( sqmPath ); |
| 150 | + if ( isFkOptimizationAllowed ) { |
| 151 | + if ( sqlAstCreationState.getCurrentSqmQueryPart() instanceof SqmQuerySpec<?> ) { |
| 152 | + final SqmQuerySpec<?> spec = (SqmQuerySpec<?>) sqlAstCreationState.getCurrentSqmQueryPart(); |
| 153 | + final SqmOrderByClause orderByClause = spec.getOrderByClause(); |
| 154 | + if ( orderByClause != null && !orderByClause.getSortSpecifications().isEmpty() ) { |
| 155 | + final SqmSelectClause selectClause = spec.getSelectClause(); |
| 156 | + if ( selectClause != null && selectClause.isDistinct() ) { |
| 157 | + // DISTINCT query requires sorted column in SELECT list |
| 158 | + isFkOptimizationAllowed = false; |
| 159 | + } |
| 160 | + if ( !spec.getGroupByClauseExpressions().isEmpty() ) { |
| 161 | + // PostgreSQL requires sorted column appear in the GROUP BY clause or be used in an aggregate function |
| 162 | + isFkOptimizationAllowed = false; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + } |
| 168 | + return isFkOptimizationAllowed; |
| 169 | + } |
| 170 | + |
143 | 171 | private final ColumnReference columnReference;
|
144 | 172 |
|
145 | 173 | public BasicValuedPathInterpretation(
|
|
0 commit comments