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"));
}
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"));
}
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());
}
}
Aggregations