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