nooneuse commented on code in PR #66307:
URL: https://github.com/apache/doris/pull/66307#discussion_r3796047816
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AddConstraintCommand.java:
##########
@@ -113,13 +139,60 @@ private void checkAlterPriv(ConnectContext ctx,
TableNameInfo tableNameInfo)
}
}
- private void addConstraintAndInvalidate(
- TableNameInfo tableNameInfo,
org.apache.doris.catalog.constraint.Constraint constraint)
+ private void addConstraintWithLocks(TableNameInfo tableNameInfo,
+ List<TableNameInfo> affectedTableInfos,
+ org.apache.doris.catalog.constraint.Constraint constraint,
+ TableIf analyzedTable, TableIf analyzedReferencedTable,
+ ConstraintCommandUtils.ExternalCatalogSnapshots
externalCatalogSnapshots)
throws Exception {
- List<MTMV> dependentMtmvs =
MTMVUtil.getDependentMtmvsByConstraint(tableNameInfo, constraint);
-
Env.getCurrentEnv().getConstraintManager().addConstraint(tableNameInfo, name,
constraint, false);
+ List<TableIf> analyzedTables = new ArrayList<>();
+ analyzedTables.add(analyzedTable);
+ if (analyzedReferencedTable != null) {
+ analyzedTables.add(analyzedReferencedTable);
+ }
+ List<MTMV> dependentMtmvs;
+ try (ConstraintCommandUtils.LockedDatabases lockedDatabases =
+ ConstraintCommandUtils.lockCurrentDatabases(
+ affectedTableInfos, externalCatalogSnapshots,
analyzedTables);
+ ConstraintCommandUtils.LockedTables lockedTables =
+ ConstraintCommandUtils.lockCurrentTables(
+ lockedDatabases, affectedTableInfos)) {
+ lockedTables.requireSame(tableNameInfo, analyzedTable);
+ TableIf currentTable = lockedTables.get(tableNameInfo);
+ if (constraint instanceof DistributionMappingConstraint) {
+ Preconditions.checkState(currentTable instanceof OlapTable,
+ "distribution mapping constraint requires an OLAP
table");
+ ((OlapTable) currentTable).checkNormalStateForAlter();
+ }
+ TableIf referencedTable = null;
+ if (constraint instanceof ForeignKeyConstraint) {
+ TableNameInfo referencedTableInfo =
+ ((ForeignKeyConstraint)
constraint).getReferencedTableName();
+ Preconditions.checkNotNull(referencedTableInfo);
+ referencedTable = lockedTables.get(referencedTableInfo);
+ lockedTables.requireSame(referencedTableInfo,
analyzedReferencedTable);
+ }
+ dependentMtmvs =
MTMVUtil.getDependentMtmvsByConstraint(tableNameInfo, constraint);
+ Env.getCurrentEnv().getConstraintManager()
+ .addConstraintWithResolvedTables(
Review Comment:
Okay.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]