XieJiann commented on code in PR #27627:
URL: https://github.com/apache/doris/pull/27627#discussion_r1410137399


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AddConstraintCommand.java:
##########
@@ -0,0 +1,102 @@
+// 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.Column;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.Pair;
+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.expressions.SlotReference;
+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 com.google.common.collect.ImmutableList;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.Set;
+
+/**
+ * create multi table materialized view
+ */
+public class AddConstraintCommand extends Command implements ForwardWithSync {
+
+    public static final Logger LOG = 
LogManager.getLogger(AddConstraintCommand.class);
+    private final String name;
+    private final Constraint constraint;
+
+    /**
+     * constructor
+     */
+    public AddConstraintCommand(String name, Constraint constraint) {
+        super(PlanType.ADD_CONSTRAINT_COMMAND);
+        this.constraint = constraint;
+        this.name = name;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws 
Exception {
+        Pair<ImmutableList<Column>, TableIf> columnsAndTable = 
extractColumnsAndTable(ctx, constraint.toProject());
+        if (constraint.isForeignKey()) {
+            Pair<ImmutableList<Column>, TableIf> foreignColumnsAndTable
+                    = extractColumnsAndTable(ctx, 
constraint.toReferenceProject());
+            columnsAndTable.second.addForeignConstraint(name, 
columnsAndTable.first,
+                    foreignColumnsAndTable.second, 
foreignColumnsAndTable.first);
+        } else if (constraint.isPrimaryKey()) {
+            columnsAndTable.second.addPrimaryKeyConstraint(name, 
columnsAndTable.first);
+        } else if (constraint.isUnique()) {
+            columnsAndTable.second.addUniqueConstraint(name, 
columnsAndTable.first);
+        }
+    }
+
+    private Pair<ImmutableList<Column>, TableIf> 
extractColumnsAndTable(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 in constraint" + 
constraint.toString());
+        }
+        LogicalCatalogRelation catalogRelation = 
logicalCatalogRelationSet.iterator().next();
+        Preconditions.checkArgument(catalogRelation.getTable() instanceof 
Table,
+                "We only support table now but we meet ", 
catalogRelation.getTable());
+        ImmutableList<Column> columns = analyzedPlan.getOutput().stream()
+                .map(s -> {
+                    Preconditions.checkArgument(s instanceof SlotReference
+                                    && ((SlotReference) 
s).getColumn().isPresent(),
+                            "Constraint only supports simple slot but meets ", 
s);

Review Comment:
   fixed



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