Search in sources :

Example 6 with SqmToSqlAstConverter

use of org.hibernate.query.sqm.sql.SqmToSqlAstConverter in project hibernate-orm by hibernate.

the class HypotheticalSetWindowEmulation method generateSqmOrderedSetAggregateFunctionExpression.

@Override
public <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(List<? extends SqmTypedNode<?>> arguments, SqmPredicate filter, SqmOrderByClause withinGroupClause, ReturnableType<T> impliedResultType, QueryEngine queryEngine, TypeConfiguration typeConfiguration) {
    return new SelfRenderingSqmOrderedSetAggregateFunction<>(this, this, arguments, filter, withinGroupClause, impliedResultType, getArgumentsValidator(), getReturnTypeResolver(), queryEngine.getCriteriaBuilder(), getName()) {

        @Override
        public Expression convertToSqlAst(SqmToSqlAstConverter walker) {
            final Clause currentClause = walker.getCurrentClauseStack().getCurrent();
            if (currentClause == Clause.OVER) {
                return super.convertToSqlAst(walker);
            } else if (currentClause != Clause.SELECT) {
                throw new IllegalArgumentException("Can't emulate [" + getName() + "] in clause " + currentClause + ". Only the SELECT clause is supported!");
            }
            final ReturnableType<?> resultType = resolveResultType(walker.getCreationContext().getMappingMetamodel().getTypeConfiguration());
            List<SqlAstNode> arguments = resolveSqlAstArguments(getArguments(), walker);
            ArgumentsValidator argumentsValidator = getArgumentsValidator();
            if (argumentsValidator != null) {
                argumentsValidator.validateSqlTypes(arguments, getFunctionName());
            }
            List<SortSpecification> withinGroup;
            if (this.getWithinGroup() == null) {
                withinGroup = Collections.emptyList();
            } else {
                walker.getCurrentClauseStack().push(Clause.ORDER);
                try {
                    final List<SqmSortSpecification> sortSpecifications = this.getWithinGroup().getSortSpecifications();
                    withinGroup = new ArrayList<>(sortSpecifications.size());
                    for (SqmSortSpecification sortSpecification : sortSpecifications) {
                        final SortSpecification specification = (SortSpecification) walker.visitSortSpecification(sortSpecification);
                        if (specification != null) {
                            withinGroup.add(specification);
                        }
                    }
                } finally {
                    walker.getCurrentClauseStack().pop();
                }
            }
            final SelfRenderingFunctionSqlAstExpression function = new SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(getFunctionName(), getRenderingSupport(), Collections.emptyList(), getFilter() == null ? null : (Predicate) getFilter().accept(walker), Collections.emptyList(), resultType, getMappingModelExpressible(walker, resultType));
            final Over<Object> windowFunction = new Over<>(function, new ArrayList<>(), withinGroup);
            walker.registerQueryTransformer(new AggregateWindowEmulationQueryTransformer(windowFunction, withinGroup, arguments));
            return windowFunction;
        }
    };
}
Also used : SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SqmPredicate(org.hibernate.query.sqm.tree.predicate.SqmPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Over(org.hibernate.sql.ast.tree.expression.Over) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) SqmSortSpecification(org.hibernate.query.sqm.tree.select.SqmSortSpecification) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingOrderedSetAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression) Clause(org.hibernate.sql.ast.Clause) SqmOrderByClause(org.hibernate.query.sqm.tree.select.SqmOrderByClause) ArgumentsValidator(org.hibernate.query.sqm.produce.function.ArgumentsValidator) SelfRenderingSqmOrderedSetAggregateFunction(org.hibernate.query.sqm.function.SelfRenderingSqmOrderedSetAggregateFunction)

Example 7 with SqmToSqlAstConverter

use of org.hibernate.query.sqm.sql.SqmToSqlAstConverter in project hibernate-orm by hibernate.

the class NonAggregatedIdentifierMappingImpl method toSqlExpression.

@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
    if (hasContainingClass()) {
        final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
        final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
        final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
        final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
        int offset = 0;
        for (AttributeMapping attributeMapping : identifierValueMapper.getAttributeMappings()) {
            offset += attributeMapping.forEachSelectable(offset, (columnIndex, selection) -> {
                final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
                final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
                columnReferences.add((ColumnReference) columnReference);
            });
        }
        return new SqlTuple(columnReferences, this);
    }
    return super.toSqlExpression(tableGroup, clause, walker, sqlAstCreationState);
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) RootClass(org.hibernate.mapping.RootClass) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) Clause(org.hibernate.sql.ast.Clause) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) BiConsumer(java.util.function.BiConsumer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) NavigablePath(org.hibernate.query.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) EntityKey(org.hibernate.engine.spi.EntityKey) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) List(java.util.List) AbstractCompositeIdentifierMapping(org.hibernate.metamodel.internal.AbstractCompositeIdentifierMapping) Component(org.hibernate.mapping.Component) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) NavigablePath(org.hibernate.query.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Aggregations

SqmToSqlAstConverter (org.hibernate.query.sqm.sql.SqmToSqlAstConverter)7 Expression (org.hibernate.sql.ast.tree.expression.Expression)5 List (java.util.List)4 NavigablePath (org.hibernate.query.spi.NavigablePath)4 Clause (org.hibernate.sql.ast.Clause)4 SqlAstNode (org.hibernate.sql.ast.tree.SqlAstNode)4 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)4 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)4 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)4 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)4 ArrayList (java.util.ArrayList)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)3 ModelPart (org.hibernate.metamodel.mapping.ModelPart)3 SqlAstCreationState (org.hibernate.sql.ast.spi.SqlAstCreationState)3 SqlExpressionResolver (org.hibernate.sql.ast.spi.SqlExpressionResolver)3 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)3 Over (org.hibernate.sql.ast.tree.expression.Over)3 TableReference (org.hibernate.sql.ast.tree.from.TableReference)3 SortSpecification (org.hibernate.sql.ast.tree.select.SortSpecification)3