nooneuse commented on code in PR #66307:
URL: https://github.com/apache/doris/pull/66307#discussion_r3842165709


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropConstraintCommand.java:
##########
@@ -67,38 +75,123 @@ public DropConstraintCommand(String name, LogicalPlan 
plan) {
     @Override
     public void run(ConnectContext ctx, StmtExecutor executor) throws 
Exception {
         TableNameInfo tableNameInfo;
-        try {
-            TableIf table = extractTable(ctx, plan);
-            tableNameInfo = TableNameInfoUtils.fromCatalogDb(
-                    table.getDatabase().getCatalog(), table.getDatabase(), 
table);
-        } catch (Exception e) {
-            // Table may no longer exist (e.g., external table deleted by 
another system).
-            // Fall back to extracting the table name from the unresolved plan.
-            LOG.warn("Table resolution failed for dropping constraint {}, "
-                    + "falling back to name-based lookup: {}", name, 
e.getMessage());
-            tableNameInfo = extractTableNameFromPlan(ctx);
+        TableNameInfo unresolvedTableName = plan instanceof UnboundRelation
+                ? extractTableNameFromPlan(ctx) : null;
+        CatalogIf<?> unresolvedCatalog = unresolvedTableName == null ? null
+                : 
Env.getCurrentEnv().getCatalogMgr().getCatalog(unresolvedTableName.getCtl());
+        if (unresolvedCatalog instanceof ExternalCatalog) {
+            // External PK/FK/UK constraints are authoritative in 
ConstraintManager. Avoid connector
+            // schema loading so DROP works with cache disabled or session 
cache bypass.
+            ExternalCatalog externalCatalog = (ExternalCatalog) 
unresolvedCatalog;
+            unresolvedTableName = new TableNameInfo(
+                    externalCatalog.getName(), unresolvedTableName.getDb(), 
unresolvedTableName.getTbl());
+            boolean lowerCaseMetaNames =
+                    
Boolean.parseBoolean(externalCatalog.getLowerCaseMetaNames());
+            tableNameInfo = !lowerCaseMetaNames
+                    && externalCatalog.getLowerCaseTableNames() == 0
+                    && externalCatalog.getLowerCaseDatabaseNames() == 0
+                    ? unresolvedTableName
+                    : Env.getCurrentEnv().getConstraintManager()
+                            .canonicalizeExternalTableName(
+                                    unresolvedTableName,
+                                    name,
+                                    lowerCaseMetaNames
+                                            || 
externalCatalog.getLowerCaseDatabaseNames() != 0,
+                                    lowerCaseMetaNames
+                                            || 
externalCatalog.getLowerCaseTableNames() != 0);
+        } else {
+            try {
+                TableIf table = extractTable(ctx, plan);
+                tableNameInfo = TableNameInfoUtils.fromCatalogDb(
+                        table.getDatabase().getCatalog(), table.getDatabase(), 
table);
+            } catch (Exception e) {
+                // Table may no longer exist (e.g., external table deleted by 
another system).
+                // Fall back to extracting the table name from the unresolved 
plan.
+                LOG.warn("Table resolution failed for dropping constraint {}, "
+                        + "falling back to name-based lookup: {}", name, 
e.getMessage());
+                if (unresolvedTableName == null) {
+                    throw e;
+                }
+                tableNameInfo = unresolvedTableName;
+            }
         }
         // must be checked on both paths above: table resolution failing 
(which includes an
         // authorization failure) falls back to a name-only lookup that binds 
nothing.
         checkAlterPriv(ctx, tableNameInfo);
+        Constraint initialConstraint = getConstraintOrThrow(tableNameInfo);
+        List<TableNameInfo> initialCascadeDropTables = Env.getCurrentEnv()
+                
.getConstraintManager().getCascadeDropTables(initialConstraint);
+        List<TableNameInfo> affectedTableInfos = new ArrayList<>();
+        affectedTableInfos.add(tableNameInfo);
+        affectedTableInfos.addAll(initialCascadeDropTables);
+        ConstraintCommandUtils.ExternalCatalogSnapshots 
externalCatalogSnapshots =
+                
ConstraintCommandUtils.snapshotExternalCatalogs(affectedTableInfos);
+
+        Constraint constraint;
+        List<MTMV> dependentMtmvs;
+        EditLog.EditLogItem logItem;
+        try (ConstraintCommandUtils.LockedDatabases lockedDatabases =
+                ConstraintCommandUtils.lockCurrentDatabases(
+                        affectedTableInfos, externalCatalogSnapshots, 
List.of());
+                ConstraintCommandUtils.LockedTables lockedTables =
+                        ConstraintCommandUtils.lockCurrentTablesIfPresent(
+                                lockedDatabases, affectedTableInfos)) {
+            TableIf currentTable = lockedTables.get(tableNameInfo);
+            constraint = getConstraintOrThrow(tableNameInfo);
+            if (constraint instanceof DistributionMappingConstraint
+                    && !(currentTable instanceof OlapTable)) {
+                throw new AnalysisException(
+                        "Distribution mapping constraint requires an OLAP 
table");
+            }
+            List<TableNameInfo> cascadeDropTables = Env.getCurrentEnv()
+                    .getConstraintManager().getCascadeDropTables(constraint);
+            if (!ConstraintCommandUtils.sameTables(
+                    initialCascadeDropTables, cascadeDropTables)) {
+                throw new AnalysisException(
+                        "Foreign key references changed while dropping 
constraint "
+                                + name + " on " + tableNameInfo + ", retry the 
statement");
+            }
+            for (TableNameInfo fkTableInfo : cascadeDropTables) {
+                checkAlterPriv(ctx, fkTableInfo);
+            }
+            dependentMtmvs = getDependentMtmvs(
+                    tableNameInfo, constraint, cascadeDropTables);
+            logItem = Env.getCurrentEnv().getConstraintManager()
+                    .dropConstraintAndSubmit(tableNameInfo, name, 
cascadeDropTables);
+            if (constraint instanceof DistributionMappingConstraint) {
+                Env.getCurrentEnv().getSqlCacheManager()
+                        .invalidateAboutTableAndFencePublication(currentTable);
+            }
+        }
+        if (logItem != null) {

Review Comment:
   ok,will fix



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