Search in sources :

Example 1 with AbstractSqmPathInterpretation

use of org.hibernate.query.sqm.sql.internal.AbstractSqmPathInterpretation in project hibernate-orm by hibernate.

the class CountFunction method canReplaceWithStar.

private boolean canReplaceWithStar(SqlAstNode arg, SqlAstTranslator<?> translator) {
    // To determine if we can replace the argument with a star, we must know if the argument is nullable
    if (arg instanceof AbstractSqmPathInterpretation<?>) {
        final AbstractSqmPathInterpretation<?> pathInterpretation = (AbstractSqmPathInterpretation<?>) arg;
        final TableGroup tableGroup = pathInterpretation.getTableGroup();
        final Expression sqlExpression = pathInterpretation.getSqlExpression();
        final JdbcMappingContainer expressionType = sqlExpression.getExpressionType();
        // The entity identifier mapping is always considered non-nullable
        final boolean isNonNullable = expressionType instanceof EntityIdentifierMapping;
        // But we also have to check if it contains joins that could alter the nullability (RIGHT or FULL)
        if (isNonNullable && tableGroup.canUseInnerJoins() && !hasJoinsAlteringNullability(tableGroup)) {
            // COUNT can only be used in query specs as query groups can only refer positionally in the order by
            final QuerySpec querySpec = (QuerySpec) translator.getCurrentQueryPart();
            // On top of this, we also have to ensure that there are no neighbouring joins that alter nullability
            for (TableGroup root : querySpec.getFromClause().getRoots()) {
                final Boolean result = hasNeighbouringJoinsAlteringNullability(root, tableGroup);
                if (result != null) {
                    return !result;
                }
            }
            return true;
        }
    }
    return false;
}
Also used : JdbcMappingContainer(org.hibernate.metamodel.mapping.JdbcMappingContainer) AbstractSqmPathInterpretation(org.hibernate.query.sqm.sql.internal.AbstractSqmPathInterpretation) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) Expression(org.hibernate.sql.ast.tree.expression.Expression) FunctionExpression(org.hibernate.sql.ast.tree.expression.FunctionExpression) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Aggregations

EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 JdbcMappingContainer (org.hibernate.metamodel.mapping.JdbcMappingContainer)1 AbstractSqmPathInterpretation (org.hibernate.query.sqm.sql.internal.AbstractSqmPathInterpretation)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 FunctionExpression (org.hibernate.sql.ast.tree.expression.FunctionExpression)1 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)1 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)1