borinquenkid commented on code in PR #16733: URL: https://github.com/apache/grails-core/pull/16733#discussion_r4363559418
########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/QueryPathContext.java: ########## @@ -0,0 +1,49 @@ +/* + * Copyright 2024-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.grails.orm.hibernate.query; + +import jakarta.persistence.criteria.Expression; +import jakarta.persistence.criteria.From; +import jakarta.persistence.criteria.Path; + +/** + * What a criterion or order handler sees of the query it converts for: the root of the query and the resolution + * of property paths the way the built-in criteria resolve them. {@code alias.property}, for an alias created with + * {@code createAlias}, or {@code association.property} becomes a path on the join GORM created for that + * association, so a custom criterion and a built-in one on the same association restrict the same row rather + * than each joining the association again. + * + * @since 8.1 + */ +public interface QueryPathContext { + + /** + * @return the root of the query or subquery the handler converts for + */ + From<?, ?> getRoot(); + + /** + * @param path a property path: {@code property}, {@code association.property} or {@code alias.property} + * @return the path, or {@code null} when the property path resolves to an expression that is not a path + */ + Path<?> getFullyQualifiedPath(String path); + + /** + * @param path a property path, or the alias of a projection Review Comment: Reverted in 3bdd02449a, on jdaugherty's analysis in his reply to your second comment: `getFullyQualifiedExpression` resolves as the built-in predicates do, so a projection alias is not folded into it. With `width * 10 as height`, resolving the alias there would make `eq('height', 9)` restrict the projection instead of the column, which SQL does not do (a select alias is visible to `order by`, not to `where`) and neither does Hibernate 5. Your first request, to expose a selection lookup and leave predicate resolution alone, is what `getSelectionAlias` is. An order handler asks it first and `getFullyQualifiedExpression` second, and then orders exactly as `order(alias)` does, for `scaled` (no such property) and for `height` (a property). The javadoc and the guide say so, and `HibernateCriteriaBuilderSqlProjectionSpec` pins it, including that `eq('height', 9)` restricts the property. ########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaQueryContext.java: ########## @@ -173,6 +175,7 @@ public Expression<?> getAliasedExpression(String alias) { return expr; } + @Override public Expression<?> getFullyQualifiedExpression(String path) { Review Comment: Reverted in 3bdd02449a, on jdaugherty's analysis in his reply to your second comment: `getFullyQualifiedExpression` resolves as the built-in predicates do, so a projection alias is not folded into it. With `width * 10 as height`, resolving the alias there would make `eq('height', 9)` restrict the projection instead of the column, which SQL does not do (a select alias is visible to `order by`, not to `where`) and neither does Hibernate 5. Your first request, to expose a selection lookup and leave predicate resolution alone, is what `getSelectionAlias` is. An order handler asks it first and `getFullyQualifiedExpression` second, and then orders exactly as `order(alias)` does, for `scaled` (no such property) and for `height` (a property). The javadoc and the guide say so, and `HibernateCriteriaBuilderSqlProjectionSpec` pins it, including that `eq('height', 9)` restricts the property. ########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaQueryContext.java: ########## @@ -173,6 +175,7 @@ public Expression<?> getAliasedExpression(String alias) { return expr; } + @Override public Expression<?> getFullyQualifiedExpression(String path) { Review Comment: Done in 3bdd02449a: the resolver is back to what it was, and `QueryPathContext.getFullyQualifiedExpression` documents which aliases it resolves and points to `getSelectionAlias` for the column alias of a `sqlProjection` or `sqlGroupProjection`. The guide's order-handler paragraph says to ask `getSelectionAlias` first and `getFullyQualifiedExpression` second, and that a criterion handler asks the second alone. The new feature in `HibernateCriteriaBuilderSqlProjectionSpec` runs your two cases (`scaled`, which no property has, and `height`, which one has) and the `eq('height', 9)` that must restrict the column. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
