Search in sources :

Example 21 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class MultiTableSqmMutationConverter method visitWhereClause.

public Predicate visitWhereClause(SqmWhereClause sqmWhereClause, Consumer<ColumnReference> restrictionColumnReferenceConsumer, SqmParameterResolutionConsumer parameterResolutionConsumer) {
    this.parameterResolutionConsumer = parameterResolutionConsumer;
    if (sqmWhereClause == null || sqmWhereClause.getPredicate() == null) {
        return null;
    }
    final SqlAstProcessingState rootProcessingState = getCurrentProcessingState();
    final SqlAstProcessingStateImpl restrictionProcessingState = new SqlAstProcessingStateImpl(rootProcessingState, this, getCurrentClauseStack()::getCurrent) {

        @Override
        public SqlExpressionResolver getSqlExpressionResolver() {
            return this;
        }

        @Override
        public Expression resolveSqlExpression(String key, Function<SqlAstProcessingState, Expression> creator) {
            final Expression expression = rootProcessingState.getSqlExpressionResolver().resolveSqlExpression(key, creator);
            if (expression instanceof ColumnReference) {
                restrictionColumnReferenceConsumer.accept((ColumnReference) expression);
            }
            return expression;
        }
    };
    pushProcessingState(restrictionProcessingState, getFromClauseIndex());
    try {
        return SqlAstHelper.combinePredicates((Predicate) sqmWhereClause.getPredicate().accept(this), discriminatorPredicate);
    } finally {
        popProcessingStateStack();
        this.parameterResolutionConsumer = null;
    }
}
Also used : SqlAstProcessingState(org.hibernate.sql.ast.spi.SqlAstProcessingState) Function(java.util.function.Function) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlAstProcessingStateImpl(org.hibernate.query.sqm.sql.internal.SqlAstProcessingStateImpl) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 22 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class MultiTableSqmMutationConverter method consumeSingleSqmParameter.

@Override
protected Expression consumeSingleSqmParameter(SqmParameter<?> sqmParameter) {
    assert parameterResolutionConsumer != null;
    final Expression expression = super.consumeSingleSqmParameter(sqmParameter);
    final List<List<JdbcParameter>> jdbcParameters = getJdbcParamsBySqmParam().get(sqmParameter);
    final MappingModelExpressible<?> mappingType = getSqmParameterMappingModelExpressibleResolutions().get(sqmParameter);
    parameterResolutionConsumer.accept(sqmParameter, mappingType, jdbcParameters.get(jdbcParameters.size() - 1));
    return expression;
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) List(java.util.List)

Example 23 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class CteInsertHandler method execute.

@Override
public int execute(DomainQueryExecutionContext executionContext) {
    final SqmInsertStatement<?> sqmInsertStatement = getSqmStatement();
    final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
    final EntityPersister entityDescriptor = getEntityDescriptor().getEntityPersister();
    final String explicitDmlTargetAlias;
    if (sqmInsertStatement.getTarget().getExplicitAlias() == null) {
        explicitDmlTargetAlias = "dml_target";
    } else {
        explicitDmlTargetAlias = sqmInsertStatement.getTarget().getExplicitAlias();
    }
    final MultiTableSqmMutationConverter sqmConverter = new MultiTableSqmMutationConverter(entityDescriptor, sqmInsertStatement, sqmInsertStatement.getTarget(), explicitDmlTargetAlias, domainParameterXref, executionContext.getQueryOptions(), executionContext.getSession().getLoadQueryInfluencers(), executionContext.getQueryParameterBindings(), factory);
    final TableGroup insertingTableGroup = sqmConverter.getMutatingTableGroup();
    final Map<SqmParameter<?>, List<List<JdbcParameter>>> parameterResolutions;
    if (domainParameterXref.getSqmParameterCount() == 0) {
        parameterResolutions = Collections.emptyMap();
    } else {
        parameterResolutions = new IdentityHashMap<>();
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // visit the insertion target using our special converter, collecting
    // information about the target paths
    final int size = sqmStatement.getInsertionTargetPaths().size();
    final List<Map.Entry<SqmCteTableColumn, Assignment>> targetPathColumns = new ArrayList<>(size);
    final List<SqmCteTableColumn> targetPathSqmCteColumns = new ArrayList<>(size);
    final Map<SqmParameter<?>, MappingModelExpressible<?>> paramTypeResolutions = new LinkedHashMap<>();
    final NamedTableReference entityTableReference = new NamedTableReference(cteTable.getCteName(), TemporaryTable.DEFAULT_ALIAS, true, sessionFactory);
    final InsertStatement insertStatement = new InsertStatement(entityTableReference);
    final BaseSqmToSqlAstConverter.AdditionalInsertValues additionalInsertValues = sqmConverter.visitInsertionTargetPaths((assignable, columnReferences) -> {
        // Find a matching cte table column and set that at the current index
        for (SqmCteTableColumn column : cteTable.getColumns()) {
            if (column.getType() == ((Expression) assignable).getExpressionType()) {
                insertStatement.addTargetColumnReferences(columnReferences);
                targetPathSqmCteColumns.add(column);
                targetPathColumns.add(new AbstractMap.SimpleEntry<>(column, new Assignment(assignable, (Expression) assignable)));
                return;
            }
        }
        throw new IllegalStateException("Couldn't find matching cte column for: " + ((Expression) assignable).getExpressionType());
    }, sqmInsertStatement, entityDescriptor, insertingTableGroup, (sqmParameter, mappingType, jdbcParameters) -> {
        parameterResolutions.computeIfAbsent(sqmParameter, k -> new ArrayList<>(1)).add(jdbcParameters);
        paramTypeResolutions.put(sqmParameter, mappingType);
    });
    final boolean assignsId = targetPathSqmCteColumns.contains(cteTable.getColumns().get(0));
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Create the statement that represent the source for the entity cte
    final Stack<SqlAstProcessingState> processingStateStack = sqmConverter.getProcessingStateStack();
    final SqlAstProcessingState oldState = processingStateStack.pop();
    final Statement queryStatement;
    if (sqmInsertStatement instanceof SqmInsertSelectStatement) {
        final QueryPart queryPart = sqmConverter.visitQueryPart(((SqmInsertSelectStatement<?>) sqmInsertStatement).getSelectQueryPart());
        queryPart.visitQuerySpecs(querySpec -> {
            // in which case we will fill the row_number column instead of the id column
            if (additionalInsertValues.applySelections(querySpec, sessionFactory)) {
                final SqmCteTableColumn rowNumberColumn = cteTable.getColumns().get(cteTable.getColumns().size() - 1);
                final ColumnReference columnReference = new ColumnReference((String) null, rowNumberColumn.getColumnName(), false, null, null, (JdbcMapping) rowNumberColumn.getType(), sessionFactory);
                insertStatement.getTargetColumnReferences().set(insertStatement.getTargetColumnReferences().size() - 1, columnReference);
                targetPathSqmCteColumns.set(targetPathSqmCteColumns.size() - 1, rowNumberColumn);
            }
            if (!assignsId && entityDescriptor.getIdentifierGenerator() instanceof PostInsertIdentifierGenerator) {
                final BasicType<Integer> rowNumberType = sessionFactory.getTypeConfiguration().getBasicTypeForJavaType(Integer.class);
                querySpec.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, new Over<>(new SelfRenderingFunctionSqlAstExpression("row_number", (appender, args, walker) -> appender.appendSql("row_number()"), Collections.emptyList(), rowNumberType, rowNumberType), Collections.emptyList(), Collections.emptyList())));
            }
        });
        queryStatement = new SelectStatement(queryPart);
    } else {
        final List<SqmValues> sqmValuesList = ((SqmInsertValuesStatement<?>) sqmInsertStatement).getValuesList();
        final List<Values> valuesList = new ArrayList<>(sqmValuesList.size());
        for (SqmValues sqmValues : sqmValuesList) {
            final Values values = sqmConverter.visitValues(sqmValues);
            additionalInsertValues.applyValues(values);
            valuesList.add(values);
        }
        final QuerySpec querySpec = new QuerySpec(true);
        final NavigablePath navigablePath = new NavigablePath(entityDescriptor.getRootPathName());
        final List<String> columnNames = new ArrayList<>(targetPathColumns.size());
        for (Map.Entry<SqmCteTableColumn, Assignment> entry : targetPathColumns) {
            for (ColumnReference columnReference : entry.getValue().getAssignable().getColumnReferences()) {
                columnNames.add(columnReference.getColumnExpression());
                querySpec.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, columnReference));
            }
        }
        final ValuesTableGroup valuesTableGroup = new ValuesTableGroup(navigablePath, entityDescriptor.getEntityPersister(), valuesList, insertingTableGroup.getPrimaryTableReference().getIdentificationVariable(), columnNames, true, factory);
        querySpec.getFromClause().addRoot(valuesTableGroup);
        queryStatement = new SelectStatement(querySpec);
    }
    processingStateStack.push(oldState);
    sqmConverter.pruneTableGroupJoins();
    if (!assignsId && entityDescriptor.getIdentifierGenerator() instanceof PostInsertIdentifierGenerator) {
        // Add the row number to the assignments
        final SqmCteTableColumn rowNumberColumn = cteTable.getColumns().get(cteTable.getColumns().size() - 1);
        final ColumnReference columnReference = new ColumnReference((String) null, rowNumberColumn.getColumnName(), false, null, null, (JdbcMapping) rowNumberColumn.getType(), sessionFactory);
        insertStatement.getTargetColumnReferences().add(columnReference);
        targetPathSqmCteColumns.add(rowNumberColumn);
    }
    final CteTable entityCteTable = BaseSqmToSqlAstConverter.createCteTable(getCteTable(), targetPathSqmCteColumns, factory);
    // Create the main query spec that will return the count of rows
    final QuerySpec querySpec = new QuerySpec(true, 1);
    final List<DomainResult<?>> domainResults = new ArrayList<>(1);
    final SelectStatement statement = new SelectStatement(querySpec, domainResults);
    final CteStatement entityCte;
    if (additionalInsertValues.requiresRowNumberIntermediate()) {
        final CteTable fullEntityCteTable = BaseSqmToSqlAstConverter.createCteTable(getCteTable(), factory);
        final String baseTableName = "base_" + entityCteTable.getTableExpression();
        final CteStatement baseEntityCte = new CteStatement(entityCteTable.withName(baseTableName), queryStatement, // The query cte will be reused multiple times
        CteMaterialization.MATERIALIZED);
        statement.addCteStatement(baseEntityCte);
        final CteColumn rowNumberColumn = fullEntityCteTable.getCteColumns().get(fullEntityCteTable.getCteColumns().size() - 1);
        final ColumnReference rowNumberColumnReference = new ColumnReference("e", rowNumberColumn.getColumnExpression(), false, null, null, rowNumberColumn.getJdbcMapping(), factory);
        final CteColumn idColumn = fullEntityCteTable.getCteColumns().get(0);
        final BasicValuedMapping idType = (BasicValuedMapping) idColumn.getJdbcMapping();
        final Optimizer optimizer = ((OptimizableGenerator) entityDescriptor.getIdentifierGenerator()).getOptimizer();
        final BasicValuedMapping integerType = (BasicValuedMapping) rowNumberColumn.getJdbcMapping();
        final Expression rowNumberMinusOneModuloIncrement = new BinaryArithmeticExpression(new BinaryArithmeticExpression(rowNumberColumnReference, BinaryArithmeticOperator.SUBTRACT, new QueryLiteral<>(1, (BasicValuedMapping) rowNumberColumn.getJdbcMapping()), integerType), BinaryArithmeticOperator.MODULO, new QueryLiteral<>(optimizer.getIncrementSize(), integerType), integerType);
        // Create the CTE that fetches a new sequence value for the row numbers that need it
        {
            final QuerySpec rowsWithSequenceQuery = new QuerySpec(true);
            rowsWithSequenceQuery.getFromClause().addRoot(new CteTableGroup(new NamedTableReference(baseTableName, "e", false, factory)));
            rowsWithSequenceQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, rowNumberColumnReference));
            final String fragment = ((BulkInsertionCapableIdentifierGenerator) entityDescriptor.getIdentifierGenerator()).determineBulkInsertionIdentifierGenerationSelectFragment(sessionFactory.getSqlStringGenerationContext());
            rowsWithSequenceQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(2, 1, new SelfRenderingSqlFragmentExpression(fragment)));
            rowsWithSequenceQuery.applyPredicate(new ComparisonPredicate(rowNumberMinusOneModuloIncrement, ComparisonOperator.EQUAL, new QueryLiteral<>(0, integerType)));
            final CteTable rowsWithSequenceCteTable = new CteTable(ROW_NUMBERS_WITH_SEQUENCE_VALUE, Arrays.asList(rowNumberColumn, idColumn), sessionFactory);
            final SelectStatement rowsWithSequenceStatement = new SelectStatement(rowsWithSequenceQuery);
            final CteStatement rowsWithSequenceCte = new CteStatement(rowsWithSequenceCteTable, rowsWithSequenceStatement, // The query cte will be reused multiple times
            CteMaterialization.MATERIALIZED);
            statement.addCteStatement(rowsWithSequenceCte);
        }
        // Create the CTE that represents the entity cte
        {
            final QuerySpec entityQuery = new QuerySpec(true);
            final NavigablePath navigablePath = new NavigablePath(baseTableName);
            final TableGroup baseTableGroup = new TableGroupImpl(navigablePath, null, new NamedTableReference(baseTableName, "e", false, factory), null);
            final TableGroup rowsWithSequenceTableGroup = new CteTableGroup(new NamedTableReference(ROW_NUMBERS_WITH_SEQUENCE_VALUE, "t", false, factory));
            baseTableGroup.addTableGroupJoin(new TableGroupJoin(rowsWithSequenceTableGroup.getNavigablePath(), SqlAstJoinType.LEFT, rowsWithSequenceTableGroup, new ComparisonPredicate(new BinaryArithmeticExpression(rowNumberColumnReference, BinaryArithmeticOperator.SUBTRACT, rowNumberMinusOneModuloIncrement, integerType), ComparisonOperator.EQUAL, new ColumnReference("t", rowNumberColumn.getColumnExpression(), false, null, null, rowNumberColumn.getJdbcMapping(), factory))));
            entityQuery.getFromClause().addRoot(baseTableGroup);
            entityQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, new BinaryArithmeticExpression(new ColumnReference("t", idColumn.getColumnExpression(), false, null, null, idColumn.getJdbcMapping(), factory), BinaryArithmeticOperator.ADD, new BinaryArithmeticExpression(rowNumberColumnReference, BinaryArithmeticOperator.SUBTRACT, new ColumnReference("t", rowNumberColumn.getColumnExpression(), false, null, null, rowNumberColumn.getJdbcMapping(), factory), integerType), idType)));
            final CteTable finalEntityCteTable;
            if (targetPathSqmCteColumns.contains(getCteTable().getColumns().get(0))) {
                finalEntityCteTable = entityCteTable;
            } else {
                targetPathSqmCteColumns.add(0, getCteTable().getColumns().get(0));
                finalEntityCteTable = BaseSqmToSqlAstConverter.createCteTable(getCteTable(), targetPathSqmCteColumns, factory);
            }
            final List<CteColumn> cteColumns = finalEntityCteTable.getCteColumns();
            for (int i = 1; i < cteColumns.size(); i++) {
                final CteColumn cteColumn = cteColumns.get(i);
                entityQuery.getSelectClause().addSqlSelection(new SqlSelectionImpl(i + 1, i, new ColumnReference("e", cteColumn.getColumnExpression(), false, null, null, cteColumn.getJdbcMapping(), factory)));
            }
            final SelectStatement entityStatement = new SelectStatement(entityQuery);
            entityCte = new CteStatement(finalEntityCteTable, entityStatement, // The query cte will be reused multiple times
            CteMaterialization.MATERIALIZED);
            statement.addCteStatement(entityCte);
        }
    } else if (!assignsId && entityDescriptor.getIdentifierGenerator() instanceof PostInsertIdentifierGenerator) {
        final String baseTableName = "base_" + entityCteTable.getTableExpression();
        final CteStatement baseEntityCte = new CteStatement(entityCteTable.withName(baseTableName), queryStatement, // The query cte will be reused multiple times
        CteMaterialization.MATERIALIZED);
        statement.addCteStatement(baseEntityCte);
        targetPathSqmCteColumns.add(0, cteTable.getColumns().get(0));
        final CteTable finalEntityCteTable = BaseSqmToSqlAstConverter.createCteTable(getCteTable(), targetPathSqmCteColumns, factory);
        final QuerySpec finalQuerySpec = new QuerySpec(true);
        final SelectStatement finalQueryStatement = new SelectStatement(finalQuerySpec);
        entityCte = new CteStatement(finalEntityCteTable, finalQueryStatement, // The query cte will be reused multiple times
        CteMaterialization.MATERIALIZED);
    } else {
        entityCte = new CteStatement(entityCteTable, queryStatement, // The query cte will be reused multiple times
        CteMaterialization.MATERIALIZED);
        statement.addCteStatement(entityCte);
    }
    // Add all CTEs
    final String baseInsertCte = addDmlCtes(statement, entityCte, targetPathColumns, assignsId, sqmConverter, parameterResolutions, factory);
    final Expression count = createCountStar(factory, sqmConverter);
    domainResults.add(new BasicResult(0, null, ((SqlExpressible) count).getJdbcMapping().getJavaTypeDescriptor()));
    querySpec.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, count));
    querySpec.getFromClause().addRoot(new CteTableGroup(new NamedTableReference(// We want to return the insertion count of the base table
    baseInsertCte, CTE_TABLE_IDENTIFIER, false, factory)));
    // Execute the statement
    final JdbcServices jdbcServices = factory.getJdbcServices();
    final SqlAstTranslator<JdbcSelect> translator = jdbcServices.getJdbcEnvironment().getSqlAstTranslatorFactory().buildSelectTranslator(factory, statement);
    final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, SqmUtil.generateJdbcParamsXref(domainParameterXref, sqmConverter), factory.getRuntimeMetamodels().getMappingMetamodel(), navigablePath -> sqmConverter.getMutatingTableGroup(), new SqmParameterMappingModelResolutionAccess() {

        @Override
        @SuppressWarnings("unchecked")
        public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
            return (MappingModelExpressible<T>) paramTypeResolutions.get(parameter);
        }
    }, executionContext.getSession());
    final JdbcSelect select = translator.translate(jdbcParameterBindings, executionContext.getQueryOptions());
    executionContext.getSession().autoFlushIfRequired(select.getAffectedTableNames());
    List<Object> list = jdbcServices.getJdbcSelectExecutor().list(select, jdbcParameterBindings, SqmJdbcExecutionContextAdapter.omittingLockingAndPaging(executionContext), row -> row[0], ListResultsConsumer.UniqueSemantic.NONE);
    return ((Number) list.get(0)).intValue();
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) Arrays(java.util.Arrays) EntityPersister(org.hibernate.persister.entity.EntityPersister) Statement(org.hibernate.sql.ast.tree.Statement) SqlExpressible(org.hibernate.metamodel.mapping.SqlExpressible) BasicType(org.hibernate.type.BasicType) SqmCteTable(org.hibernate.query.sqm.tree.cte.SqmCteTable) SqmInsertValuesStatement(org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) CteStatement(org.hibernate.sql.ast.tree.cte.CteStatement) Joinable(org.hibernate.persister.entity.Joinable) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) Identifier(org.hibernate.boot.model.naming.Identifier) SqmJdbcExecutionContextAdapter(org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) SqmUtil(org.hibernate.query.sqm.internal.SqmUtil) PostInsertIdentifierGenerator(org.hibernate.id.PostInsertIdentifierGenerator) Map(java.util.Map) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) TableReferenceJoin(org.hibernate.sql.ast.tree.from.TableReferenceJoin) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Assignment(org.hibernate.sql.ast.tree.update.Assignment) IdentityHashMap(java.util.IdentityHashMap) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) Optimizer(org.hibernate.id.enhanced.Optimizer) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) Expression(org.hibernate.sql.ast.tree.expression.Expression) BaseSqmToSqlAstConverter(org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqlAstProcessingState(org.hibernate.sql.ast.spi.SqlAstProcessingState) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) InsertHandler(org.hibernate.query.sqm.mutation.internal.InsertHandler) List(java.util.List) Over(org.hibernate.sql.ast.tree.expression.Over) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) InsertStatement(org.hibernate.sql.ast.tree.insert.InsertStatement) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) TableGroupImpl(org.hibernate.query.results.TableGroupImpl) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) CteContainer(org.hibernate.sql.ast.tree.cte.CteContainer) CteMaterialization(org.hibernate.sql.ast.tree.cte.CteMaterialization) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) OptimizableGenerator(org.hibernate.id.OptimizableGenerator) SemanticException(org.hibernate.query.SemanticException) SortOrder(org.hibernate.query.sqm.SortOrder) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings) SortSpecification(org.hibernate.sql.ast.tree.select.SortSpecification) ArrayList(java.util.ArrayList) TableReference(org.hibernate.sql.ast.tree.from.TableReference) LinkedHashMap(java.util.LinkedHashMap) CteTable(org.hibernate.sql.ast.tree.cte.CteTable) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) MultiTableSqmMutationConverter(org.hibernate.query.sqm.mutation.internal.MultiTableSqmMutationConverter) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) CteColumn(org.hibernate.sql.ast.tree.cte.CteColumn) UnionTableReference(org.hibernate.sql.ast.tree.from.UnionTableReference) BiConsumer(java.util.function.BiConsumer) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) BinaryArithmeticOperator(org.hibernate.query.sqm.BinaryArithmeticOperator) ValuesTableGroup(org.hibernate.sql.ast.tree.from.ValuesTableGroup) Values(org.hibernate.sql.ast.tree.insert.Values) BulkInsertionCapableIdentifierGenerator(org.hibernate.id.BulkInsertionCapableIdentifierGenerator) TemporaryTable(org.hibernate.dialect.temptable.TemporaryTable) ListResultsConsumer(org.hibernate.sql.results.spi.ListResultsConsumer) SqmStar(org.hibernate.query.sqm.tree.expression.SqmStar) CteTableGroup(org.hibernate.sql.ast.tree.cte.CteTableGroup) SqlAstTranslator(org.hibernate.sql.ast.SqlAstTranslator) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) DomainQueryExecutionContext(org.hibernate.query.spi.DomainQueryExecutionContext) SqmCteTableColumn(org.hibernate.query.sqm.tree.cte.SqmCteTableColumn) AbstractMap(java.util.AbstractMap) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) SqmInsertStatement(org.hibernate.query.sqm.tree.insert.SqmInsertStatement) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) Stack(org.hibernate.internal.util.collections.Stack) DomainParameterXref(org.hibernate.query.sqm.internal.DomainParameterXref) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) SqmValues(org.hibernate.query.sqm.tree.insert.SqmValues) CteColumn(org.hibernate.sql.ast.tree.cte.CteColumn) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) OptimizableGenerator(org.hibernate.id.OptimizableGenerator) ArrayList(java.util.ArrayList) Values(org.hibernate.sql.ast.tree.insert.Values) SqmValues(org.hibernate.query.sqm.tree.insert.SqmValues) InsertStatement(org.hibernate.sql.ast.tree.insert.InsertStatement) SqmInsertStatement(org.hibernate.query.sqm.tree.insert.SqmInsertStatement) LinkedHashMap(java.util.LinkedHashMap) ValuesTableGroup(org.hibernate.sql.ast.tree.from.ValuesTableGroup) AbstractMap(java.util.AbstractMap) Assignment(org.hibernate.sql.ast.tree.update.Assignment) CteStatement(org.hibernate.sql.ast.tree.cte.CteStatement) List(java.util.List) ArrayList(java.util.ArrayList) ValuesTableGroup(org.hibernate.sql.ast.tree.from.ValuesTableGroup) CteTableGroup(org.hibernate.sql.ast.tree.cte.CteTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqmParameterMappingModelResolutionAccess(org.hibernate.query.sqm.spi.SqmParameterMappingModelResolutionAccess) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) DomainResult(org.hibernate.sql.results.graph.DomainResult) CteTableGroup(org.hibernate.sql.ast.tree.cte.CteTableGroup) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) NavigablePath(org.hibernate.query.spi.NavigablePath) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) Optimizer(org.hibernate.id.enhanced.Optimizer) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) SqlAstProcessingState(org.hibernate.sql.ast.spi.SqlAstProcessingState) Over(org.hibernate.sql.ast.tree.expression.Over) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) SqmCteTable(org.hibernate.query.sqm.tree.cte.SqmCteTable) CteTable(org.hibernate.sql.ast.tree.cte.CteTable) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) BasicResult(org.hibernate.sql.results.graph.basic.BasicResult) SqmValues(org.hibernate.query.sqm.tree.insert.SqmValues) TableGroupImpl(org.hibernate.query.results.TableGroupImpl) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) BaseSqmToSqlAstConverter(org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) Statement(org.hibernate.sql.ast.tree.Statement) SqmInsertValuesStatement(org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement) CteStatement(org.hibernate.sql.ast.tree.cte.CteStatement) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) InsertStatement(org.hibernate.sql.ast.tree.insert.InsertStatement) SqmInsertStatement(org.hibernate.query.sqm.tree.insert.SqmInsertStatement) SqmInsertValuesStatement(org.hibernate.query.sqm.tree.insert.SqmInsertValuesStatement) PostInsertIdentifierGenerator(org.hibernate.id.PostInsertIdentifierGenerator) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) SqmCteTableColumn(org.hibernate.query.sqm.tree.cte.SqmCteTableColumn) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) MultiTableSqmMutationConverter(org.hibernate.query.sqm.mutation.internal.MultiTableSqmMutationConverter) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 24 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class InPredicateRestrictionProducer method produceRestriction.

@Override
public InListPredicate produceRestriction(List<?> matchingIdValues, EntityMappingType entityDescriptor, int valueIndex, ModelPart valueModelPart, TableReference mutatingTableReference, Supplier<Consumer<SelectableConsumer>> columnsToMatchVisitationSupplier, ExecutionContext executionContext) {
    assert matchingIdValues != null;
    assert !matchingIdValues.isEmpty();
    final SessionFactoryImplementor sessionFactory = executionContext.getSession().getFactory();
    final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
    final int idColumnCount = identifierMapping.getJdbcTypeCount();
    assert idColumnCount > 0;
    final InListPredicate predicate;
    if (idColumnCount == 1) {
        final BasicValuedModelPart basicIdMapping = (BasicValuedModelPart) identifierMapping;
        final String idColumn = basicIdMapping.getSelectionExpression();
        final Expression inFixture = new ColumnReference(mutatingTableReference, idColumn, // id columns cannot be formulas and cannot have custom read and write expressions
        false, null, null, basicIdMapping.getJdbcMapping(), sessionFactory);
        predicate = new InListPredicate(inFixture);
        matchingIdValues.forEach(matchingId -> predicate.addExpression(new JdbcLiteral<>(matchingId, basicIdMapping.getJdbcMapping())));
    } else {
        final List<ColumnReference> columnReferences = new ArrayList<>(idColumnCount);
        final List<JdbcMapping> jdbcMappings = new ArrayList<>(idColumnCount);
        identifierMapping.forEachSelectable((columnIndex, selection) -> {
            columnReferences.add(new ColumnReference(mutatingTableReference, selection, sessionFactory));
            jdbcMappings.add(selection.getJdbcMapping());
        });
        final Expression inFixture = new SqlTuple(columnReferences, identifierMapping);
        predicate = new InListPredicate(inFixture);
        matchingIdValues.forEach(matchingId -> {
            assert matchingId instanceof Object[];
            final Object[] matchingIdParts = (Object[]) matchingId;
            final List<JdbcLiteral<?>> tupleParts = new ArrayList<>(idColumnCount);
            for (int p = 0; p < matchingIdParts.length; p++) {
                tupleParts.add(new JdbcLiteral<>(matchingIdParts[p], jdbcMappings.get(p)));
            }
            predicate.addExpression(new SqlTuple(tupleParts, identifierMapping));
        });
    }
    return predicate;
}
Also used : BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ArrayList(java.util.ArrayList) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Expression(org.hibernate.sql.ast.tree.expression.Expression) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JdbcLiteral(org.hibernate.sql.ast.tree.expression.JdbcLiteral)

Example 25 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class AbstractCteMutationHandler method execute.

@Override
public int execute(DomainQueryExecutionContext executionContext) {
    final SqmDeleteOrUpdateStatement sqmMutationStatement = getSqmDeleteOrUpdateStatement();
    final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
    final EntityMappingType entityDescriptor = getEntityDescriptor();
    final String explicitDmlTargetAlias;
    // We need an alias because we try to acquire a WRITE lock for these rows in the CTE
    if (sqmMutationStatement.getTarget().getExplicitAlias() == null) {
        explicitDmlTargetAlias = "dml_target";
    } else {
        explicitDmlTargetAlias = sqmMutationStatement.getTarget().getExplicitAlias();
    }
    final MultiTableSqmMutationConverter sqmConverter = new MultiTableSqmMutationConverter(entityDescriptor, sqmMutationStatement, sqmMutationStatement.getTarget(), explicitDmlTargetAlias, domainParameterXref, executionContext.getQueryOptions(), executionContext.getSession().getLoadQueryInfluencers(), executionContext.getQueryParameterBindings(), factory);
    final Map<SqmParameter, List<JdbcParameter>> parameterResolutions;
    if (domainParameterXref.getSqmParameterCount() == 0) {
        parameterResolutions = Collections.emptyMap();
    } else {
        parameterResolutions = new IdentityHashMap<>();
    }
    final Map<SqmParameter, MappingModelExpressible> paramTypeResolutions = new LinkedHashMap<>();
    final Predicate restriction = sqmConverter.visitWhereClause(sqmMutationStatement.getWhereClause(), columnReference -> {
    }, (sqmParam, mappingType, jdbcParameters) -> paramTypeResolutions.put(sqmParam, mappingType));
    sqmConverter.pruneTableGroupJoins();
    final CteStatement idSelectCte = new CteStatement(BaseSqmToSqlAstConverter.createCteTable(getCteTable(), factory), MatchingIdSelectionHelper.generateMatchingIdSelectStatement(entityDescriptor, sqmMutationStatement, false, restriction, sqmConverter, executionContext, factory), // The id-select cte will be reused multiple times
    CteMaterialization.MATERIALIZED);
    // Create the main query spec that will return the count of
    final QuerySpec querySpec = new QuerySpec(true, 1);
    final List<DomainResult<?>> domainResults = new ArrayList<>(1);
    final SelectStatement statement = new SelectStatement(querySpec, domainResults);
    final JdbcServices jdbcServices = factory.getJdbcServices();
    final SqlAstTranslator<JdbcSelect> translator = jdbcServices.getJdbcEnvironment().getSqlAstTranslatorFactory().buildSelectTranslator(factory, statement);
    final Expression count = createCountStar(factory, sqmConverter);
    domainResults.add(new BasicResult<>(0, null, ((SqlExpressible) count).getJdbcMapping().getJavaTypeDescriptor()));
    querySpec.getSelectClause().addSqlSelection(new SqlSelectionImpl(1, 0, count));
    querySpec.getFromClause().addRoot(new CteTableGroup(new NamedTableReference(idSelectCte.getCteTable().getTableExpression(), CTE_TABLE_IDENTIFIER, false, factory)));
    // Add all CTEs
    statement.addCteStatement(idSelectCte);
    addDmlCtes(statement, idSelectCte, sqmConverter, parameterResolutions, factory);
    final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, SqmUtil.generateJdbcParamsXref(domainParameterXref, sqmConverter), factory.getRuntimeMetamodels().getMappingMetamodel(), navigablePath -> sqmConverter.getMutatingTableGroup(), paramTypeResolutions::get, executionContext.getSession());
    final LockOptions lockOptions = executionContext.getQueryOptions().getLockOptions();
    final LockMode lockMode = lockOptions.getAliasSpecificLockMode(explicitDmlTargetAlias);
    // Acquire a WRITE lock for the rows that are about to be modified
    lockOptions.setAliasSpecificLockMode(explicitDmlTargetAlias, LockMode.WRITE);
    final JdbcSelect select = translator.translate(jdbcParameterBindings, executionContext.getQueryOptions());
    lockOptions.setAliasSpecificLockMode(explicitDmlTargetAlias, lockMode);
    executionContext.getSession().autoFlushIfRequired(select.getAffectedTableNames());
    List<Object> list = jdbcServices.getJdbcSelectExecutor().list(select, jdbcParameterBindings, SqmJdbcExecutionContextAdapter.omittingLockingAndPaging(executionContext), row -> row[0], ListResultsConsumer.UniqueSemantic.NONE);
    return ((Number) list.get(0)).intValue();
}
Also used : LockOptions(org.hibernate.LockOptions) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) ArrayList(java.util.ArrayList) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) LinkedHashMap(java.util.LinkedHashMap) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) CteStatement(org.hibernate.sql.ast.tree.cte.CteStatement) List(java.util.List) ArrayList(java.util.ArrayList) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqmDeleteOrUpdateStatement(org.hibernate.query.sqm.tree.SqmDeleteOrUpdateStatement) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) DomainResult(org.hibernate.sql.results.graph.DomainResult) LockMode(org.hibernate.LockMode) CteTableGroup(org.hibernate.sql.ast.tree.cte.CteTableGroup) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) MultiTableSqmMutationConverter(org.hibernate.query.sqm.mutation.internal.MultiTableSqmMutationConverter) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

Expression (org.hibernate.sql.ast.tree.expression.Expression)130 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)68 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)67 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)62 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)57 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)54 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)54 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)42 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)37 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)35 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)34 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)33 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)32 ArrayList (java.util.ArrayList)31 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)31 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)28 TableReference (org.hibernate.sql.ast.tree.from.TableReference)25 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)23 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)23 NavigablePath (org.hibernate.query.spi.NavigablePath)21