github-actions[bot] commented on code in PR #61176:
URL: https://github.com/apache/doris/pull/61176#discussion_r3085631514


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropConstraintCommand.java:
##########
@@ -72,8 +75,14 @@ public void run(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
                     + "falling back to name-based lookup: {}", name, 
e.getMessage());
             tableNameInfo = extractTableNameFromPlan(ctx);
         }
-        Env.getCurrentEnv().getConstraintManager().dropConstraint(
-                tableNameInfo, name, false);
+        Constraint constraint = 
Env.getCurrentEnv().getConstraintManager().getConstraint(tableNameInfo, name);
+        if (constraint == null) {
+            throw new AnalysisException(
+                    String.format("Unknown constraint %s on table %s.", name, 
tableNameInfo));
+        }
+        List<MTMV> dependentMtmvs = 
MTMVUtil.getDependentMtmvsByConstraint(tableNameInfo, constraint);

Review Comment:
   Same issue here for `DROP CONSTRAINT`: if dependent-MTMV lookup hits a stale 
relation, the DDL now fails before the constraint is removed, while replay only 
logs and continues. Cache invalidation should not be able to block the metadata 
change.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AddConstraintCommand.java:
##########
@@ -73,33 +76,43 @@ public void run(ConnectContext ctx, StmtExecutor executor) 
throws Exception {
                 table.getDatabase().getCatalog(), table.getDatabase(), table);
         ImmutableList<String> columns = columnsAndTable.first;
 
+        Pair<ImmutableList<String>, TableNameInfo> referencedColumnsAndTable = 
null;
         if (constraint.isForeignKey()) {
-            Pair<ImmutableList<String>, TableIf> refColumnsAndTable
-                    = extractColumnsAndTable(ctx, 
constraint.toReferenceProject());
+            Pair<ImmutableList<String>, TableIf> refColumnsAndTable =
+                    extractColumnsAndTable(ctx, 
constraint.toReferenceProject());
             TableIf refTable = refColumnsAndTable.second;
             TableNameInfo refTableInfo = TableNameInfoUtils.fromCatalogDb(
-                    refTable.getDatabase().getCatalog(),
-                    refTable.getDatabase(), refTable);
-            ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(
-                    name, columns, refTableInfo, refColumnsAndTable.first);
-            Env.getCurrentEnv().getConstraintManager().addConstraint(
-                    tableNameInfo, name, fkConstraint, false);
+                    refTable.getDatabase().getCatalog(), 
refTable.getDatabase(), refTable);
+            referencedColumnsAndTable = Pair.of(refColumnsAndTable.first, 
refTableInfo);
+        }
+        if (constraint.isForeignKey()) {
+            Preconditions.checkState(referencedColumnsAndTable != null);
+            addConstraintAndInvalidate(tableNameInfo,
+                    new ForeignKeyConstraint(name, columns,
+                            referencedColumnsAndTable.second, 
referencedColumnsAndTable.first));
         } else if (constraint.isPrimaryKey()) {
-            PrimaryKeyConstraint pkConstraint = new PrimaryKeyConstraint(
-                    name, ImmutableSet.copyOf(columns));
-            Env.getCurrentEnv().getConstraintManager().addConstraint(
-                    tableNameInfo, name, pkConstraint, false);
+            addConstraintAndInvalidate(
+                    tableNameInfo, new PrimaryKeyConstraint(name, 
ImmutableSet.copyOf(columns)));
         } else if (constraint.isUnique()) {
-            UniqueConstraint uniqueConstraint = new UniqueConstraint(
-                    name, ImmutableSet.copyOf(columns));
-            Env.getCurrentEnv().getConstraintManager().addConstraint(
-                    tableNameInfo, name, uniqueConstraint, false);
+            addConstraintAndInvalidate(
+                    tableNameInfo, new UniqueConstraint(name, 
ImmutableSet.copyOf(columns)));
+        } else {
+            throw new AnalysisException("Unsupported constraint type: " + 
constraint);
         }
     }
 
+    private void addConstraintAndInvalidate(
+            TableNameInfo tableNameInfo, 
org.apache.doris.catalog.constraint.Constraint constraint)
+            throws Exception {
+        List<MTMV> dependentMtmvs = 
MTMVUtil.getDependentMtmvsByConstraint(tableNameInfo, constraint);

Review Comment:
   This lookup is now on the critical path of `ALTER TABLE ... ADD CONSTRAINT`, 
but it is not guaranteed to succeed even when the constraint change itself is 
valid. `MTMVRelationManager` can legitimately hold stale relations, and this PR 
already adds a replay test proving `getDependentMtmvsByBaseTables()` can throw 
in that situation. On replay you treat that as warn-and-continue, but here the 
same failure aborts the user DDL before the constraint is added. Please make 
dependent-MTMV lookup/invalidation best-effort on the live path as well so 
master and replay stay consistent.



-- 
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]

Reply via email to