Search in sources :

Example 21 with QuerySpec

use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.

the class OracleSqlAstTranslator method getFetchClauseTypeForRowNumbering.

@Override
protected FetchClauseType getFetchClauseTypeForRowNumbering(QueryPart queryPart) {
    final FetchClauseType fetchClauseType = super.getFetchClauseTypeForRowNumbering(queryPart);
    final boolean hasOffset;
    if (queryPart.isRoot() && hasLimit()) {
        hasOffset = getLimit().getFirstRow() != null;
    } else {
        hasOffset = queryPart.getOffsetClauseExpression() != null;
    }
    if (queryPart instanceof QuerySpec && !hasOffset && fetchClauseType == FetchClauseType.ROWS_ONLY) {
        // Note that we also build upon this in #visitOrderBy
        return null;
    }
    return fetchClauseType;
}
Also used : FetchClauseType(org.hibernate.query.sqm.FetchClauseType) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Example 22 with QuerySpec

use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method emulateSubQueryRelationalRestrictionPredicate.

protected <X extends Expression> void emulateSubQueryRelationalRestrictionPredicate(Predicate predicate, boolean negated, QueryPart queryPart, X lhsTuple, SubQueryRelationalRestrictionEmulationRenderer<X> renderer, ComparisonOperator tupleComparisonOperator) {
    final QuerySpec subQuery;
    if (queryPart instanceof QuerySpec && queryPart.getFetchClauseExpression() == null && queryPart.getOffsetClauseExpression() == null) {
        subQuery = (QuerySpec) queryPart;
        // We can only emulate the tuple sub query predicate as exists predicate when there are no limit/offsets
        if (negated) {
            appendSql("not ");
        }
        final QueryPart queryPartForRowNumbering = this.queryPartForRowNumbering;
        final int queryPartForRowNumberingClauseDepth = this.queryPartForRowNumberingClauseDepth;
        final boolean needsSelectAliases = this.needsSelectAliases;
        try {
            this.queryPartForRowNumbering = null;
            this.queryPartForRowNumberingClauseDepth = -1;
            this.needsSelectAliases = false;
            queryPartStack.push(subQuery);
            appendSql("exists (select 1");
            visitFromClause(subQuery.getFromClause());
            if (!subQuery.getGroupByClauseExpressions().isEmpty() || subQuery.getHavingClauseRestrictions() != null) {
                // If we have a group by or having clause, we have to move the tuple comparison emulation to the HAVING clause
                visitWhereClause(subQuery.getWhereClauseRestrictions());
                visitGroupByClause(subQuery, SelectItemReferenceStrategy.EXPRESSION);
                appendSql(" having ");
                clauseStack.push(Clause.HAVING);
                try {
                    renderer.renderComparison(subQuery.getSelectClause().getSqlSelections(), lhsTuple, tupleComparisonOperator);
                    final Predicate havingClauseRestrictions = subQuery.getHavingClauseRestrictions();
                    if (havingClauseRestrictions != null) {
                        appendSql(" and (");
                        havingClauseRestrictions.accept(this);
                        appendSql(CLOSE_PARENTHESIS);
                    }
                } finally {
                    clauseStack.pop();
                }
            } else {
                // If we have no group by or having clause, we can move the tuple comparison emulation to the WHERE clause
                appendSql(" where ");
                clauseStack.push(Clause.WHERE);
                try {
                    renderer.renderComparison(subQuery.getSelectClause().getSqlSelections(), lhsTuple, tupleComparisonOperator);
                    final Predicate whereClauseRestrictions = subQuery.getWhereClauseRestrictions();
                    if (whereClauseRestrictions != null) {
                        appendSql(" and (");
                        whereClauseRestrictions.accept(this);
                        appendSql(CLOSE_PARENTHESIS);
                    }
                } finally {
                    clauseStack.pop();
                }
            }
            appendSql(CLOSE_PARENTHESIS);
        } finally {
            queryPartStack.pop();
            this.queryPartForRowNumbering = queryPartForRowNumbering;
            this.queryPartForRowNumberingClauseDepth = queryPartForRowNumberingClauseDepth;
            this.needsSelectAliases = needsSelectAliases;
        }
    } else {
        // TODO: We could use nested queries and use row numbers to emulate this
        throw new IllegalArgumentException("Can't emulate in predicate with tuples and limit/offset or set operations: " + predicate);
    }
}
Also used : QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 23 with QuerySpec

use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method getSelectItemsToInline.

private BitSet getSelectItemsToInline() {
    final QuerySpec querySpec = (QuerySpec) getQueryPartStack().getCurrent();
    final List<SqlSelection> sqlSelections = querySpec.getSelectClause().getSqlSelections();
    final BitSet bitSet = new BitSet(sqlSelections.size());
    for (Expression groupByClauseExpression : querySpec.getGroupByClauseExpressions()) {
        final SqlSelectionExpression selectItemReference = getSelectItemReference(groupByClauseExpression);
        if (selectItemReference != null) {
            bitSet.set(sqlSelections.indexOf(selectItemReference.getSelection()));
        }
    }
    return bitSet;
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) BitSet(java.util.BitSet) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)

Example 24 with QuerySpec

use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.

the class AggregateWindowEmulationQueryTransformer method transform.

@Override
public QuerySpec transform(CteContainer cteContainer, QuerySpec querySpec, SqmToSqlAstConverter converter) {
    final SessionFactoryImplementor factory = converter.getCreationContext().getSessionFactory();
    final QuerySpec outerQuerySpec = new QuerySpec(querySpec.isRoot());
    final String identifierVariable = "hhh_";
    final NavigablePath navigablePath = new NavigablePath(identifierVariable, identifierVariable);
    final SelectClause selectClause = outerQuerySpec.getSelectClause();
    final QuerySpec subQuerySpec = querySpec.asSubQuery();
    final SelectClause subSelectClause = subQuerySpec.getSelectClause();
    final List<SqlSelection> subSelections = subSelectClause.getSqlSelections();
    final List<String> columnNames = new ArrayList<>(subSelections.size());
    // A map to find the select item position for an expression
    // which is needed to decide if we need to introduce synthetic select items
    // for group by items, since these group by items are migrated to the outer query
    final Map<Expression, Integer> selectionMapping = new HashMap<>(subSelections.size());
    // for the QueryPartTableGroup within which the sub query spec is embedded
    for (int i = 0; i < subSelections.size(); i++) {
        final BasicValuedMapping mapping = (BasicValuedMapping) subSelections.get(i).getExpressionType();
        final String columnName = "col" + i;
        final ColumnReference columnReference = new ColumnReference(identifierVariable, columnName, false, null, null, mapping.getJdbcMapping(), factory);
        final Expression expression = subSelections.get(i).getExpression();
        final Expression finalExpression;
        if (expression == windowFunction) {
            finalExpression = new SelfRenderingAggregateFunctionSqlAstExpression("min", (sqlAppender, sqlAstArguments, walker1) -> {
                sqlAppender.appendSql("min(");
                sqlAstArguments.get(0).accept(walker1);
                sqlAppender.append(')');
            }, Collections.singletonList(columnReference), null, (ReturnableType<?>) mapping.getMappedType(), expression.getExpressionType());
        } else {
            finalExpression = columnReference;
            selectionMapping.put(expression, i);
        }
        columnNames.add(columnName);
        selectClause.addSqlSelection(new ResolvedSqlSelection(i + 1, i, finalExpression, (BasicType<Object>) mapping.getJdbcMapping()));
    }
    // Migrate the group by clause to the outer query
    // and push group by expressions into the partition by clause of the window function
    final List<Expression> groupByExpressions = new ArrayList<>(subQuerySpec.getGroupByClauseExpressions().size());
    for (Expression groupByClauseExpression : subQuerySpec.getGroupByClauseExpressions()) {
        final Expression realExpression;
        final Expression outerGroupByExpression;
        if (groupByClauseExpression instanceof SqlSelectionExpression) {
            final SqlSelection selection = ((SqlSelectionExpression) groupByClauseExpression).getSelection();
            outerGroupByExpression = new SqlSelectionExpression(selectClause.getSqlSelections().get(selection.getValuesArrayPosition()));
            realExpression = selection.getExpression();
        } else {
            if (groupByClauseExpression instanceof SqmPathInterpretation<?>) {
                realExpression = ((SqmPathInterpretation<?>) groupByClauseExpression).getSqlExpression();
            } else {
                realExpression = groupByClauseExpression;
            }
            final Integer position = selectionMapping.get(realExpression);
            if (position == null) {
                // Group by something that has no corresponding selection item,
                // so we need to introduce an intermediate selection item
                final int valuesPosition = selectClause.getSqlSelections().size();
                final String columnName = "col" + valuesPosition;
                final JdbcMapping jdbcMapping = realExpression.getExpressionType().getJdbcMappings().get(0);
                final ColumnReference columnReference = new ColumnReference(identifierVariable, columnName, false, null, null, jdbcMapping, factory);
                final int subValuesPosition = subSelectClause.getSqlSelections().size();
                final SqlSelection subSelection = new ResolvedSqlSelection(subValuesPosition + 1, subValuesPosition, realExpression, (BasicType<Object>) jdbcMapping);
                columnNames.add(columnName);
                subSelectClause.addSqlSelection(subSelection);
                outerGroupByExpression = columnReference;
                selectionMapping.put(realExpression, subValuesPosition);
            } else {
                outerGroupByExpression = new SqlSelectionExpression(selectClause.getSqlSelections().get(position));
            }
        }
        windowFunction.getPartitions().add(realExpression);
        groupByExpressions.add(outerGroupByExpression);
    }
    outerQuerySpec.setGroupByClauseExpressions(groupByExpressions);
    subQuerySpec.setGroupByClauseExpressions(null);
    // Migrate the having clause to the outer query
    if (subQuerySpec.getHavingClauseRestrictions() != null) {
        final Predicate predicate = new ExpressionReplacementWalker() {

            @Override
            protected <X extends SqlAstNode> X replaceExpression(X expression) {
                if (expression instanceof Literal || expression instanceof JdbcParameter) {
                    return expression;
                }
                final Expression outerExpression;
                if (expression instanceof SqlSelectionExpression) {
                    final SqlSelection selection = ((SqlSelectionExpression) expression).getSelection();
                    outerExpression = selectClause.getSqlSelections().get(selection.getValuesArrayPosition()).getExpression();
                } else {
                    final Expression realExpression;
                    if (expression instanceof SqmPathInterpretation<?>) {
                        realExpression = ((SqmPathInterpretation<?>) expression).getSqlExpression();
                    } else {
                        realExpression = (Expression) expression;
                    }
                    final Integer position = selectionMapping.get(realExpression);
                    if (position == null) {
                        // An expression that has no corresponding selection item,
                        // so we need to introduce an intermediate selection item
                        final int valuesPosition = selectClause.getSqlSelections().size();
                        final String columnName = "col" + valuesPosition;
                        final JdbcMapping jdbcMapping = realExpression.getExpressionType().getJdbcMappings().get(0);
                        final ColumnReference columnReference = new ColumnReference(identifierVariable, columnName, false, null, null, jdbcMapping, factory);
                        final int subValuesPosition = subSelectClause.getSqlSelections().size();
                        final SqlSelection subSelection = new ResolvedSqlSelection(subValuesPosition + 1, subValuesPosition, realExpression, (BasicType<Object>) jdbcMapping);
                        columnNames.add(columnName);
                        subSelectClause.addSqlSelection(subSelection);
                        outerExpression = columnReference;
                        selectionMapping.put(realExpression, subValuesPosition);
                    } else {
                        outerExpression = selectClause.getSqlSelections().get(position).getExpression();
                    }
                }
                return (X) outerExpression;
            }
        }.replaceExpressions(subQuerySpec.getHavingClauseRestrictions());
        outerQuerySpec.setHavingClauseRestrictions(predicate);
        subQuerySpec.setHavingClauseRestrictions(null);
    }
    // Migrate the order by clause to the outer query
    if (subQuerySpec.hasSortSpecifications()) {
        for (SortSpecification sortSpecification : subQuerySpec.getSortSpecifications()) {
            final Expression sortExpression = sortSpecification.getSortExpression();
            final Expression outerSortExpression;
            if (sortExpression instanceof SqlSelectionExpression) {
                final SqlSelection selection = ((SqlSelectionExpression) sortExpression).getSelection();
                outerSortExpression = new SqlSelectionExpression(selectClause.getSqlSelections().get(selection.getValuesArrayPosition()));
            } else {
                final Expression realExpression;
                if (sortExpression instanceof SqmPathInterpretation<?>) {
                    realExpression = ((SqmPathInterpretation<?>) sortExpression).getSqlExpression();
                } else {
                    realExpression = sortExpression;
                }
                final Integer position = selectionMapping.get(realExpression);
                if (position == null) {
                    // Group by something that has no corresponding selection item,
                    // so we need to introduce an intermediate selection item
                    final int valuesPosition = selectClause.getSqlSelections().size();
                    final String columnName = "col" + valuesPosition;
                    final JdbcMapping jdbcMapping = realExpression.getExpressionType().getJdbcMappings().get(0);
                    final ColumnReference columnReference = new ColumnReference(identifierVariable, columnName, false, null, null, jdbcMapping, factory);
                    final int subValuesPosition = subSelectClause.getSqlSelections().size();
                    final SqlSelection subSelection = new ResolvedSqlSelection(subValuesPosition + 1, subValuesPosition, realExpression, (BasicType<Object>) jdbcMapping);
                    columnNames.add(columnName);
                    subSelectClause.addSqlSelection(subSelection);
                    outerSortExpression = columnReference;
                    selectionMapping.put(realExpression, subValuesPosition);
                } else {
                    outerSortExpression = new SqlSelectionExpression(selectClause.getSqlSelections().get(position));
                }
            }
            outerQuerySpec.addSortSpecification(new SortSpecification(outerSortExpression, sortSpecification.getSortOrder(), sortSpecification.getNullPrecedence()));
        }
        subQuerySpec.getSortSpecifications().clear();
    }
    // We need to add selection items for the expressions we order by to the sub query spec.
    final int selectionOffset = columnNames.size();
    // Collect the sorting column references so we can apply the filter later
    final List<ColumnReference> sortingColumns = new ArrayList<>(withinGroup.size());
    for (int i = 0; i < withinGroup.size(); i++) {
        final int valueIndex = selectionOffset + i;
        final Expression sortExpression = withinGroup.get(i).getSortExpression();
        final BasicValuedMapping mapping = (BasicValuedMapping) sortExpression.getExpressionType();
        final String columnName = "col" + valueIndex;
        final int oldValueIndex = subSelectClause.getSqlSelections().size();
        columnNames.add(columnName);
        subSelectClause.addSqlSelection(new ResolvedSqlSelection(oldValueIndex + 1, oldValueIndex, sortExpression, (BasicType<Object>) mapping.getJdbcMapping()));
        sortingColumns.add(new ColumnReference(identifierVariable, columnName, false, null, null, mapping.getJdbcMapping(), factory));
    }
    if (arguments != null) {
        // So we add a filter to the outer query so we can extract the rank
        switch(arguments.size()) {
            case 0:
                break;
            case 1:
                outerQuerySpec.applyPredicate(new ComparisonPredicate(sortingColumns.get(0), ComparisonOperator.EQUAL, (Expression) arguments.get(0)));
                break;
            default:
                outerQuerySpec.applyPredicate(new ComparisonPredicate(new SqlTuple(sortingColumns, null), ComparisonOperator.EQUAL, new SqlTuple((List<? extends Expression>) (List<?>) arguments, null)));
        }
    }
    final QueryPartTableGroup queryPartTableGroup = new QueryPartTableGroup(navigablePath, null, subQuerySpec, identifierVariable, columnNames, false, true, factory);
    outerQuerySpec.getFromClause().addRoot(queryPartTableGroup);
    // Migrate the offset/fetch clause
    outerQuerySpec.setOffsetClauseExpression(subQuerySpec.getOffsetClauseExpression());
    outerQuerySpec.setFetchClauseExpression(subQuerySpec.getFetchClauseExpression(), subQuerySpec.getFetchClauseType());
    subQuerySpec.setOffsetClauseExpression(null);
    subQuerySpec.setFetchClauseExpression(null, null);
    return outerQuerySpec;
}
Also used : JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicType(org.hibernate.type.BasicType) CteContainer(org.hibernate.sql.ast.tree.cte.CteContainer) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) HashMap(java.util.HashMap) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) ArrayList(java.util.ArrayList) ExpressionReplacementWalker(org.hibernate.sql.ast.spi.ExpressionReplacementWalker) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) Map(java.util.Map) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) ReturnableType(org.hibernate.query.ReturnableType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) ResolvedSqlSelection(org.hibernate.sql.results.internal.ResolvedSqlSelection) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) NavigablePath(org.hibernate.spi.NavigablePath) QueryTransformer(org.hibernate.sql.ast.tree.expression.QueryTransformer) SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) List(java.util.List) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) Literal(org.hibernate.sql.ast.tree.expression.Literal) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) Over(org.hibernate.sql.ast.tree.expression.Over) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) Collections(java.util.Collections) SqmPathInterpretation(org.hibernate.query.sqm.sql.internal.SqmPathInterpretation) SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) NavigablePath(org.hibernate.spi.NavigablePath) BasicType(org.hibernate.type.BasicType) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) ResolvedSqlSelection(org.hibernate.sql.results.internal.ResolvedSqlSelection) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) ReturnableType(org.hibernate.query.ReturnableType) ExpressionReplacementWalker(org.hibernate.sql.ast.spi.ExpressionReplacementWalker) Literal(org.hibernate.sql.ast.tree.expression.Literal) ResolvedSqlSelection(org.hibernate.sql.results.internal.ResolvedSqlSelection) SqmPathInterpretation(org.hibernate.query.sqm.sql.internal.SqmPathInterpretation) ArrayList(java.util.ArrayList) List(java.util.List) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 25 with QuerySpec

use of org.hibernate.sql.ast.tree.select.QuerySpec 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

QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)55 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)33 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)28 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)24 Expression (org.hibernate.sql.ast.tree.expression.Expression)24 NavigablePath (org.hibernate.spi.NavigablePath)23 ArrayList (java.util.ArrayList)22 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)19 TableReference (org.hibernate.sql.ast.tree.from.TableReference)19 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)19 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)19 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)18 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)16 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)16 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)16 List (java.util.List)15 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)14 TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)14 Map (java.util.Map)13 Collections (java.util.Collections)12