[feature](Nereids): support drop constraint on table (#27944)
This commit is contained in:
@ -236,6 +236,36 @@ public interface TableIf {
|
||||
}
|
||||
}
|
||||
|
||||
default void dropConstraint(String name) {
|
||||
writeLock();
|
||||
try {
|
||||
Map<String, Constraint> constraintMap = getConstraintsMap();
|
||||
if (!constraintMap.containsKey(name)) {
|
||||
throw new AnalysisException(
|
||||
String.format("Unknown constraint %s on table %s.", name, this.getName()));
|
||||
}
|
||||
Constraint constraint = constraintMap.get(name);
|
||||
constraintMap.remove(name);
|
||||
if (constraint instanceof PrimaryKeyConstraint) {
|
||||
((PrimaryKeyConstraint) constraint).getForeignTables()
|
||||
.forEach(t -> t.dropFKReferringPK(this, (PrimaryKeyConstraint) constraint));
|
||||
}
|
||||
} finally {
|
||||
writeUnlock();
|
||||
}
|
||||
}
|
||||
|
||||
default void dropFKReferringPK(TableIf table, PrimaryKeyConstraint constraint) {
|
||||
writeLock();
|
||||
try {
|
||||
Map<String, Constraint> constraintMap = getConstraintsMap();
|
||||
constraintMap.entrySet().removeIf(e -> e.getValue() instanceof ForeignKeyConstraint
|
||||
&& ((ForeignKeyConstraint) e.getValue()).isReferringPK(table, constraint));
|
||||
} finally {
|
||||
writeUnlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if this kind of table need read lock when doing query plan.
|
||||
*
|
||||
|
||||
@ -69,6 +69,11 @@ public class ForeignKeyConstraint extends Constraint {
|
||||
return referencedTable.toTableIf();
|
||||
}
|
||||
|
||||
public Boolean isReferringPK(TableIf table, PrimaryKeyConstraint constraint) {
|
||||
return constraint.getPrimaryKeyNames().equals(getForeignKeyNames())
|
||||
&& getReferencedTable().equals(table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(foreignToReference, referencedTable);
|
||||
|
||||
@ -46,7 +46,7 @@ public class PrimaryKeyConstraint extends Constraint {
|
||||
foreignTables.add(new TableIdentifier(table));
|
||||
}
|
||||
|
||||
public List<TableIf> getReferenceTables() {
|
||||
public List<TableIf> getForeignTables() {
|
||||
return foreignTables.stream()
|
||||
.map(TableIdentifier::toTableIf)
|
||||
.collect(ImmutableList.toImmutableList());
|
||||
|
||||
@ -75,6 +75,7 @@ import org.apache.doris.nereids.DorisParser.Date_subContext;
|
||||
import org.apache.doris.nereids.DorisParser.DecimalLiteralContext;
|
||||
import org.apache.doris.nereids.DorisParser.DeleteContext;
|
||||
import org.apache.doris.nereids.DorisParser.DereferenceContext;
|
||||
import org.apache.doris.nereids.DorisParser.DropConstraintContext;
|
||||
import org.apache.doris.nereids.DorisParser.DropMTMVContext;
|
||||
import org.apache.doris.nereids.DorisParser.ElementAtContext;
|
||||
import org.apache.doris.nereids.DorisParser.ExistContext;
|
||||
@ -319,6 +320,7 @@ import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.DeleteCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
|
||||
@ -621,8 +623,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
|
||||
@Override
|
||||
public LogicalPlan visitAddConstraint(AddConstraintContext ctx) {
|
||||
LogicalPlan curTable = visitRelation(ctx.table);
|
||||
ImmutableList<Slot> slots = ctx.constraint().slots.stream()
|
||||
.map(RuleContext::getText)
|
||||
ImmutableList<Slot> slots = visitIdentifierList(ctx.constraint().slots).stream()
|
||||
.map(UnboundSlot::new)
|
||||
.collect(ImmutableList.toImmutableList());
|
||||
Constraint constraint;
|
||||
@ -631,19 +632,24 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
|
||||
} else if (ctx.constraint().PRIMARY() != null) {
|
||||
constraint = Constraint.newPrimaryKeyConstraint(curTable, slots);
|
||||
} else if (ctx.constraint().FOREIGN() != null) {
|
||||
ImmutableList<Slot> referenceSlots = ctx.constraint().referenceSlots.stream()
|
||||
.map(RuleContext::getText)
|
||||
ImmutableList<Slot> referencedSlots = visitIdentifierList(ctx.constraint().referencedSlots).stream()
|
||||
.map(UnboundSlot::new)
|
||||
.collect(ImmutableList.toImmutableList());
|
||||
List<String> nameParts = visitMultipartIdentifier(ctx.constraint().referenceTable);
|
||||
LogicalPlan referenceTable = new UnboundRelation(StatementScopeIdGenerator.newRelationId(), nameParts);
|
||||
constraint = Constraint.newForeignKeyConstraint(curTable, slots, referenceTable, referenceSlots);
|
||||
constraint = Constraint.newForeignKeyConstraint(curTable, slots, referenceTable, referencedSlots);
|
||||
} else {
|
||||
throw new AnalysisException("Unsupported constraint " + ctx.getText());
|
||||
}
|
||||
return new AddConstraintCommand(ctx.constraintName.getText().toLowerCase(), constraint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LogicalPlan visitDropConstraint(DropConstraintContext ctx) {
|
||||
LogicalPlan curTable = visitRelation(ctx.table);
|
||||
return new DropConstraintCommand(ctx.constraintName.getText().toLowerCase(), curTable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LogicalPlan visitUpdate(UpdateContext ctx) {
|
||||
LogicalPlan query = LogicalPlanBuilderAssistant.withCheckPolicy(new UnboundRelation(
|
||||
|
||||
@ -127,6 +127,7 @@ public enum PlanType {
|
||||
CREATE_MTMV_COMMAND,
|
||||
ALTER_MTMV_COMMAND,
|
||||
ADD_CONSTRAINT_COMMAND,
|
||||
DROP_CONSTRAINT_COMMAND,
|
||||
REFRESH_MTMV_COMMAND,
|
||||
DROP_MTMV_COMMAND
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* create multi table materialized view
|
||||
* add constraint command
|
||||
*/
|
||||
public class AddConstraintCommand extends Command implements ForwardWithSync {
|
||||
|
||||
@ -62,10 +62,10 @@ public class AddConstraintCommand extends Command implements ForwardWithSync {
|
||||
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
|
||||
Pair<ImmutableList<String>, TableIf> columnsAndTable = extractColumnsAndTable(ctx, constraint.toProject());
|
||||
if (constraint.isForeignKey()) {
|
||||
Pair<ImmutableList<String>, TableIf> foreignColumnsAndTable
|
||||
Pair<ImmutableList<String>, TableIf> referencedColumnsAndTable
|
||||
= extractColumnsAndTable(ctx, constraint.toReferenceProject());
|
||||
columnsAndTable.second.addForeignConstraint(name, columnsAndTable.first,
|
||||
foreignColumnsAndTable.second, foreignColumnsAndTable.first);
|
||||
referencedColumnsAndTable.second, referencedColumnsAndTable.first);
|
||||
} else if (constraint.isPrimaryKey()) {
|
||||
columnsAndTable.second.addPrimaryKeyConstraint(name, columnsAndTable.first);
|
||||
} else if (constraint.isUnique()) {
|
||||
|
||||
@ -0,0 +1,82 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
|
||||
package org.apache.doris.nereids.trees.plans.commands;
|
||||
|
||||
import org.apache.doris.catalog.Table;
|
||||
import org.apache.doris.catalog.TableIf;
|
||||
import org.apache.doris.nereids.NereidsPlanner;
|
||||
import org.apache.doris.nereids.exceptions.AnalysisException;
|
||||
import org.apache.doris.nereids.properties.PhysicalProperties;
|
||||
import org.apache.doris.nereids.trees.plans.Plan;
|
||||
import org.apache.doris.nereids.trees.plans.PlanType;
|
||||
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
|
||||
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
|
||||
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
|
||||
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
|
||||
import org.apache.doris.qe.ConnectContext;
|
||||
import org.apache.doris.qe.StmtExecutor;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* drop constraint command
|
||||
*/
|
||||
public class DropConstraintCommand extends Command implements ForwardWithSync {
|
||||
|
||||
public static final Logger LOG = LogManager.getLogger(DropConstraintCommand.class);
|
||||
private final String name;
|
||||
private final LogicalPlan plan;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*/
|
||||
public DropConstraintCommand(String name, LogicalPlan plan) {
|
||||
super(PlanType.DROP_CONSTRAINT_COMMAND);
|
||||
this.name = name;
|
||||
this.plan = plan;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
|
||||
TableIf table = extractTable(ctx, plan);
|
||||
table.dropConstraint(name);
|
||||
}
|
||||
|
||||
private TableIf extractTable(ConnectContext ctx, LogicalPlan plan) {
|
||||
NereidsPlanner planner = new NereidsPlanner(ctx.getStatementContext());
|
||||
Plan analyzedPlan = planner.plan(plan, PhysicalProperties.ANY, ExplainLevel.ANALYZED_PLAN);
|
||||
Set<LogicalCatalogRelation> logicalCatalogRelationSet = analyzedPlan
|
||||
.collect(LogicalCatalogRelation.class::isInstance);
|
||||
if (logicalCatalogRelationSet.size() != 1) {
|
||||
throw new AnalysisException("Can not found table when dropping constraint");
|
||||
}
|
||||
LogicalCatalogRelation catalogRelation = logicalCatalogRelationSet.iterator().next();
|
||||
Preconditions.checkArgument(catalogRelation.getTable() instanceof Table,
|
||||
"Don't support table ", catalogRelation.getTable());
|
||||
return catalogRelation.getTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
|
||||
return visitor.visitDropConstraintCommand(this, context);
|
||||
}
|
||||
}
|
||||
@ -24,6 +24,7 @@ import org.apache.doris.nereids.trees.plans.commands.CreateMTMVCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.CreatePolicyCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.DeleteCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.DropConstraintCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.DropMTMVCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.ExplainCommand;
|
||||
import org.apache.doris.nereids.trees.plans.commands.ExportCommand;
|
||||
@ -82,6 +83,10 @@ public interface CommandVisitor<R, C> {
|
||||
return visitCommand(addConstraintCommand, context);
|
||||
}
|
||||
|
||||
default R visitDropConstraintCommand(DropConstraintCommand dropConstraintCommand, C context) {
|
||||
return visitCommand(dropConstraintCommand, context);
|
||||
}
|
||||
|
||||
default R visitRefreshMTMVCommand(RefreshMTMVCommand refreshMTMVCommand, C context) {
|
||||
return visitCommand(refreshMTMVCommand, context);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user