nooneuse commented on code in PR #66307:
URL: https://github.com/apache/doris/pull/66307#discussion_r3796042632
##########
fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/ConstraintManager.java:
##########
@@ -234,10 +305,115 @@ public ImmutableList<UniqueConstraint>
getUniqueConstraints(
UniqueConstraint.class);
}
+ /** Returns all distribution mapping constraints for the given table. */
+ public ImmutableList<DistributionMappingConstraint>
getDistributionMappingConstraints(
+ TableNameInfo tableNameInfo) {
+ return getConstraintsByType(toKey(tableNameInfo),
DistributionMappingConstraint.class);
+ }
+
/**
- * Remove all constraints for a table and clean up bidirectional
references.
- * Called when a table is dropped.
+ * Returns mappings owned by the concrete table object.
+ *
+ * <p>The table-local copy follows the table through recycle, backup,
restore, and rename lifecycles.
+ * Optimizer code must use this overload so a stale qualified-name entry
can never bind to another table.</p>
*/
+ @SuppressWarnings("deprecation")
+ public ImmutableList<DistributionMappingConstraint>
getDistributionMappingConstraints(TableIf table) {
+ if (!(table instanceof Table)) {
+ return ImmutableList.of();
+ }
+ readLock();
+ try {
+ return ((Table)
table).getTableAttributes().getConstraintsMap().values().stream()
+ .filter(DistributionMappingConstraint.class::isInstance)
+ .map(DistributionMappingConstraint.class::cast)
+ .collect(ImmutableList.toImmutableList());
+ } finally {
+ readUnlock();
+ }
+ }
+
+ /** Return the table-owned mapping that uses the given column, if any. */
+ @SuppressWarnings("deprecation")
+ public String findDistributionMappingConstraintWithColumn(TableIf table,
String columnName) {
+ if (!(table instanceof Table)) {
+ return null;
+ }
+ readLock();
+ try {
+ return ((Table)
table).getTableAttributes().getConstraintsMap().entrySet().stream()
+ .filter(entry -> entry.getValue() instanceof
DistributionMappingConstraint)
+ .filter(entry -> {
+ DistributionMappingConstraint mapping =
+ (DistributionMappingConstraint)
entry.getValue();
+ return
containsIgnoreCase(mapping.getDeterminantColumnNames(), columnName)
+ ||
containsIgnoreCase(mapping.getDistributionColumnNames(), columnName);
+ })
+ .map(Entry::getKey)
+ .findFirst()
+ .orElse(null);
+ } finally {
+ readUnlock();
+ }
+ }
+
+ /** Rebuild the qualified-name index from constraints persisted with a
recovered or restored table. */
+ @SuppressWarnings("deprecation")
+ public void restoreTableConstraints(TableNameInfo tableNameInfo, TableIf
table) {
+ if (!(table instanceof Table)) {
+ return;
+ }
+ Map<String, Constraint> tableLocalConstraints =
+ ((Table) table).getTableAttributes().getConstraintsMap();
+ String key = toKey(tableNameInfo);
+ writeLock();
+ try {
+ Map<String, Constraint> indexedConstraints =
constraintsMap.get(key);
+ if (indexedConstraints != null) {
+ indexedConstraints.entrySet().removeIf(
+ entry -> entry.getValue() instanceof
DistributionMappingConstraint);
+ }
+ for (Entry<String, Constraint> entry :
tableLocalConstraints.entrySet()) {
+ Constraint constraint = entry.getValue();
+ if (constraint instanceof DistributionMappingConstraint) {
+ if (indexedConstraints == null) {
+ indexedConstraints = new HashMap<>();
+ }
+ indexedConstraints.put(entry.getKey(), constraint);
+ }
+ }
+ if (indexedConstraints == null || indexedConstraints.isEmpty()) {
+ constraintsMap.remove(key);
+ } else {
+ constraintsMap.put(key, indexedConstraints);
+ }
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ /** Populate table-local mapping metadata after loading an image created
before table-local ownership. */
+ public void syncDistributionMappingsToTables() {
+ Map<String, Map<String, Constraint>> snapshot;
+ readLock();
+ try {
+ snapshot = constraintsMap.entrySet().stream()
+ .collect(Collectors.toMap(
+ Entry::getKey,
+ entry -> ImmutableMap.copyOf(entry.getValue())));
+ } finally {
+ readUnlock();
+ }
+ snapshot.forEach((tableKey, constraints) -> {
+ TableIf table = resolveTableIfPresent(new TableNameInfo(tableKey));
Review Comment:
alright. fixed
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/DropConstraintCommand.java:
##########
@@ -67,38 +74,103 @@ 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.
+ tableNameInfo = unresolvedTableName;
Review Comment:
right.
--
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]