Search in sources :

Example 1 with SqmSelection

use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitSubquery.

@Override
public SqmSubQuery<?> visitSubquery(HqlParser.SubqueryContext ctx) {
    final HqlParser.QueryExpressionContext queryExpressionContext = (HqlParser.QueryExpressionContext) ctx.getChild(0);
    final SqmSubQuery<?> subQuery = new SqmSubQuery<>(processingStateStack.getCurrent().getProcessingQuery(), creationContext.getNodeBuilder());
    processingStateStack.push(new SqmQuerySpecCreationProcessingStateStandardImpl(processingStateStack.getCurrent(), subQuery, this));
    try {
        queryExpressionContext.accept(this);
        final List<SqmSelection<?>> selections = subQuery.getQuerySpec().getSelectClause().getSelections();
        if (selections.size() == 1) {
            subQuery.applyInferableType(selections.get(0).getNodeType());
        }
        return subQuery;
    } finally {
        processingStateStack.pop();
    }
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmQuerySpecCreationProcessingStateStandardImpl(org.hibernate.query.sqm.internal.SqmQuerySpecCreationProcessingStateStandardImpl)

Example 2 with SqmSelection

use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.

the class AliasCollisionTest method testSameIdentificationVariablesInSubquery.

@Test
public void testSameIdentificationVariablesInSubquery() {
    final String query = "select a from SimpleEntity a where a.someString in (select a.someString from SimpleEntity a where a.someInteger = 5)";
    final SqmSelectStatement<?> sqm = interpretSelect(query);
    final SqmQuerySpec<?> querySpec = sqm.getQuerySpec();
    final List<SqmSelection<?>> selections = querySpec.getSelectClause().getSelections();
    assertThat(selections, hasSize(1));
    assertThat(selections.get(0).getAlias(), nullValue());
    final List<SqmRoot<?>> roots = querySpec.getFromClause().getRoots();
    assertThat(roots, hasSize(1));
    assertThat(roots.get(0).getJoins(), isEmpty());
    assertThat(roots.get(0).getExplicitAlias(), is("a"));
    assertThat(querySpec.getWhereClause().getPredicate(), instanceOf(SqmInSubQueryPredicate.class));
    final SqmInSubQueryPredicate predicate = (SqmInSubQueryPredicate) querySpec.getWhereClause().getPredicate();
    final SqmRoot<?> subQueryRoot = predicate.getSubQueryExpression().getQuerySpec().getFromClause().getRoots().get(0);
    assertThat(subQueryRoot.getExplicitAlias(), is("a"));
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmRoot(org.hibernate.query.sqm.tree.from.SqmRoot) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with SqmSelection

use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.

the class AliasCollisionTest method testSubqueryUsingIdentificationVariableDefinedInRootQuery.

@Test
public void testSubqueryUsingIdentificationVariableDefinedInRootQuery() {
    final String query = "select a from SimpleEntity a where a.someString in " + "( select b.someString from SimpleEntity b where a.someLong = b.someLong )";
    final SqmSelectStatement<?> sqm = interpretSelect(query);
    final SqmQuerySpec<?> querySpec = sqm.getQuerySpec();
    final List<SqmSelection<?>> selections = querySpec.getSelectClause().getSelections();
    assertThat(selections, hasSize(1));
    assertThat(selections.get(0).getAlias(), nullValue());
    final List<SqmRoot<?>> roots = querySpec.getFromClause().getRoots();
    assertThat(roots, hasSize(1));
    assertThat(roots.get(0).getJoins(), isEmpty());
    assertThat(roots.get(0).getExplicitAlias(), is("a"));
    assertThat(querySpec.getWhereClause().getPredicate(), instanceOf(SqmInSubQueryPredicate.class));
    final SqmInSubQueryPredicate predicate = (SqmInSubQueryPredicate) querySpec.getWhereClause().getPredicate();
    final SqmQuerySpec subQuerySpec = predicate.getSubQueryExpression().getQuerySpec();
    assertThat(subQuerySpec.getFromClause().getRoots().get(0).getExplicitAlias(), is("b"));
    final SqmComparisonPredicate correlation = (SqmComparisonPredicate) subQuerySpec.getWhereClause().getPredicate();
    final SqmSimplePath leftHandExpression = (SqmSimplePath) correlation.getLeftHandExpression();
    assertThat(leftHandExpression.getLhs().getExplicitAlias(), is("a"));
    final SqmSimplePath rightHandExpression = (SqmSimplePath) correlation.getRightHandExpression();
    assertThat(rightHandExpression.getLhs().getExplicitAlias(), is("b"));
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSimplePath(org.hibernate.query.sqm.tree.domain.SqmSimplePath) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) SqmRoot(org.hibernate.query.sqm.tree.from.SqmRoot) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) BaseSqmUnitTest(org.hibernate.orm.test.query.sqm.BaseSqmUnitTest) Test(org.junit.jupiter.api.Test)

Example 4 with SqmSelection

use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method buildInferredSelectClause.

protected SqmSelectClause buildInferredSelectClause(SqmFromClause fromClause) {
    // for now, this is slightly different than the legacy behavior where
    // the root and each non-fetched-join was selected.  For now, here, we simply
    // select the root
    final SqmSelectClause selectClause = new SqmSelectClause(false, fromClause.getNumberOfRoots(), creationContext.getNodeBuilder());
    fromClause.visitRoots(sqmRoot -> selectClause.addSelection(new SqmSelection<>(sqmRoot, sqmRoot.getAlias(), creationContext.getNodeBuilder())));
    return selectClause;
}
Also used : SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSelectClause(org.hibernate.query.sqm.tree.select.SqmSelectClause)

Example 5 with SqmSelection

use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method determineValueMapping.

private MappingModelExpressible<?> determineValueMapping(SqmExpression<?> sqmExpression, FromClauseIndex fromClauseIndex) {
    if (sqmExpression instanceof SqmParameter) {
        return determineValueMapping((SqmParameter<?>) sqmExpression);
    } else if (sqmExpression instanceof SqmPath) {
        log.debugf("Determining mapping-model type for SqmPath : %s ", sqmExpression);
        final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
        return SqmMappingModelHelper.resolveMappingModelExpressible(sqmExpression, domainModel, fromClauseIndex::findTableGroup);
    } else // The model type of an enum literal is always inferred
    if (sqmExpression instanceof SqmEnumLiteral<?>) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    } else if (sqmExpression instanceof SqmSubQuery<?>) {
        final SqmSubQuery<?> subQuery = (SqmSubQuery<?>) sqmExpression;
        final SqmSelectClause selectClause = subQuery.getQuerySpec().getSelectClause();
        if (selectClause.getSelections().size() == 1) {
            final SqmSelection<?> subQuerySelection = selectClause.getSelections().get(0);
            final SqmSelectableNode<?> selectableNode = subQuerySelection.getSelectableNode();
            if (selectableNode instanceof SqmExpression<?>) {
                return determineValueMapping((SqmExpression<?>) selectableNode, fromClauseIndex);
            }
            final SqmExpressible<?> selectionNodeType = subQuerySelection.getNodeType();
            if (selectionNodeType != null) {
                final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
                final MappingModelExpressible<?> expressible = domainModel.resolveMappingExpressible(selectionNodeType, this::findTableGroupByPath);
                if (expressible != null) {
                    return expressible;
                }
                try {
                    final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
                    if (mappingModelExpressible != null) {
                        return mappingModelExpressible;
                    }
                } catch (Exception ignore) {
                    return null;
                }
            }
        }
    }
    log.debugf("Determining mapping-model type for generalized SqmExpression : %s", sqmExpression);
    final SqmExpressible<?> nodeType = sqmExpression.getNodeType();
    if (nodeType == null) {
        // We can't determine the type of the expression
        return null;
    }
    final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final MappingModelExpressible<?> valueMapping = domainModel.resolveMappingExpressible(nodeType, fromClauseIndex::getTableGroup);
    if (valueMapping == null) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    }
    if (valueMapping == null) {
        // For literals it is totally possible that we can't figure out a mapping type
        if (sqmExpression instanceof SqmLiteral<?>) {
            return null;
        }
        throw new ConversionException("Could not determine ValueMapping for SqmExpression: " + sqmExpression);
    }
    return valueMapping;
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) SqmSelectableNode(org.hibernate.query.sqm.tree.select.SqmSelectableNode) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSelectClause(org.hibernate.query.sqm.tree.select.SqmSelectClause) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) SqmEnumLiteral(org.hibernate.query.sqm.tree.expression.SqmEnumLiteral) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) SemanticException(org.hibernate.query.SemanticException) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) InterpretationException(org.hibernate.query.sqm.InterpretationException) SQLException(java.sql.SQLException) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Aggregations

SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)9 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)4 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)3 SqmSelectClause (org.hibernate.query.sqm.tree.select.SqmSelectClause)3 SqmSubQuery (org.hibernate.query.sqm.tree.select.SqmSubQuery)3 SQLException (java.sql.SQLException)2 HibernateException (org.hibernate.HibernateException)2 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)2 QueryException (org.hibernate.QueryException)2 MultipleBagFetchException (org.hibernate.loader.MultipleBagFetchException)2 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)2 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)2 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)2 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)2 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)2 SemanticException (org.hibernate.query.SemanticException)2 InterpretationException (org.hibernate.query.sqm.InterpretationException)2 SqmExpressible (org.hibernate.query.sqm.SqmExpressible)2 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)2 SqmEntityValuedSimplePath (org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath)2