Search in sources :

Example 1 with SqmInSubQueryPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate 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 2 with SqmInSubQueryPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate 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 3 with SqmInSubQueryPredicate

use of org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitInPredicate.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public SqmPredicate visitInPredicate(HqlParser.InPredicateContext ctx) {
    final boolean negated = ctx.getChildCount() == 4;
    final SqmExpression<?> testExpression = (SqmExpression<?>) ctx.getChild(0).accept(this);
    final HqlParser.InListContext inListContext = (HqlParser.InListContext) ctx.getChild(ctx.getChildCount() - 1);
    if (inListContext instanceof HqlParser.ExplicitTupleInListContext) {
        final HqlParser.ExplicitTupleInListContext tupleExpressionListContext = (HqlParser.ExplicitTupleInListContext) inListContext;
        final int size = tupleExpressionListContext.getChildCount();
        final int estimatedSize = size >> 1;
        final Class<?> testExpressionJavaType = testExpression.getJavaType();
        final boolean isEnum = testExpressionJavaType != null && testExpressionJavaType.isEnum();
        // Multi-valued bindings are only allowed if there is a single list item, hence size 3 (LP, RP and param)
        parameterDeclarationContextStack.push(() -> size == 3);
        try {
            final List<SqmExpression<?>> listExpressions = new ArrayList<>(estimatedSize);
            for (int i = 1; i < size; i++) {
                final ParseTree parseTree = tupleExpressionListContext.getChild(i);
                if (parseTree instanceof HqlParser.ExpressionOrPredicateContext) {
                    final ParseTree child = parseTree.getChild(0);
                    final HqlParser.ExpressionContext expressionContext;
                    final Map<Class<?>, Enum<?>> possibleEnumValues;
                    if (isEnum && child instanceof HqlParser.ExpressionContext && (possibleEnumValues = getPossibleEnumValues(expressionContext = (HqlParser.ExpressionContext) child)) != null) {
                        listExpressions.add(resolveEnumShorthandLiteral(expressionContext, possibleEnumValues, testExpressionJavaType));
                    } else {
                        listExpressions.add((SqmExpression<?>) child.accept(this));
                    }
                }
            }
            return new SqmInListPredicate(testExpression, listExpressions, negated, creationContext.getNodeBuilder());
        } finally {
            parameterDeclarationContextStack.pop();
        }
    } else if (inListContext instanceof HqlParser.ParamInListContext) {
        final HqlParser.ParamInListContext tupleExpressionListContext = (HqlParser.ParamInListContext) inListContext;
        parameterDeclarationContextStack.push(() -> true);
        try {
            return new SqmInListPredicate(testExpression, Collections.singletonList(tupleExpressionListContext.getChild(0).accept(this)), negated, creationContext.getNodeBuilder());
        } finally {
            parameterDeclarationContextStack.pop();
        }
    } else if (inListContext instanceof HqlParser.SubqueryInListContext) {
        final HqlParser.SubqueryInListContext subQueryOrParamInListContext = (HqlParser.SubqueryInListContext) inListContext;
        return new SqmInSubQueryPredicate(testExpression, visitSubquery((HqlParser.SubqueryContext) subQueryOrParamInListContext.getChild(1)), negated, creationContext.getNodeBuilder());
    } else if (inListContext instanceof HqlParser.PersistentCollectionReferenceInListContext) {
        if (getCreationOptions().useStrictJpaCompliance()) {
            throw new StrictJpaComplianceViolation(StrictJpaComplianceViolation.Type.HQL_COLLECTION_FUNCTION);
        }
        final HqlParser.PersistentCollectionReferenceInListContext collectionReferenceInListContext = (HqlParser.PersistentCollectionReferenceInListContext) inListContext;
        return new SqmInSubQueryPredicate<>(testExpression, createCollectionReferenceSubQuery((HqlParser.SimplePathContext) collectionReferenceInListContext.getChild(2), (TerminalNode) collectionReferenceInListContext.getChild(0)), negated, creationContext.getNodeBuilder());
    } else {
        throw new ParsingException("Unexpected IN predicate type [" + ctx.getClass().getSimpleName() + "] : " + ctx.getText());
    }
}
Also used : ArrayList(java.util.ArrayList) SqmInSubQueryPredicate(org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate) ParsingException(org.hibernate.query.sqm.ParsingException) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) HqlParser(org.hibernate.grammars.hql.HqlParser) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) SqmInListPredicate(org.hibernate.query.sqm.tree.predicate.SqmInListPredicate) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

SqmInSubQueryPredicate (org.hibernate.query.sqm.tree.predicate.SqmInSubQueryPredicate)3 BaseSqmUnitTest (org.hibernate.orm.test.query.sqm.BaseSqmUnitTest)2 SqmRoot (org.hibernate.query.sqm.tree.from.SqmRoot)2 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 ParsingException (org.hibernate.query.sqm.ParsingException)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 SqmSimplePath (org.hibernate.query.sqm.tree.domain.SqmSimplePath)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)1 SqmInListPredicate (org.hibernate.query.sqm.tree.predicate.SqmInListPredicate)1 SqmQuerySpec (org.hibernate.query.sqm.tree.select.SqmQuerySpec)1