diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 index 488c8b82a0..4519e9eac5 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 @@ -164,6 +164,7 @@ CONNECTION: 'CONNECTION'; CONNECTION_ID: 'CONNECTION_ID'; CONSISTENT: 'CONSISTENT'; CONSTRAINT: 'CONSTRAINT'; +CONSTRAINTS: 'CONSTRAINTS'; CONVERT: 'CONVERT'; COPY: 'COPY'; COUNT: 'COUNT'; diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 5ce755eb39..709ee61f54 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -102,6 +102,7 @@ statement constraint #addConstraint | ALTER TABLE table=relation DROP CONSTRAINT constraintName=errorCapturingIdentifier #dropConstraint + | SHOW CONSTRAINTS FROM table=relation #showConstraint | CALL functionName=identifier LEFT_PAREN (expression (COMMA expression)*)? RIGHT_PAREN #callProcedure ; diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java index 4abf490f90..d0df5bad71 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java @@ -341,7 +341,7 @@ public abstract class Table extends MetaObject implements Writable, TableIf { } @Override - public Map getConstraintsMap() { + public Map getConstraintsMapUnsafe() { return constraintsMap; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/TableIf.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/TableIf.java index 50a32e3d00..e6feb6b4a5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/TableIf.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/TableIf.java @@ -33,6 +33,7 @@ import org.apache.doris.statistics.TableStatsMeta; import org.apache.doris.thrift.TTableDescriptor; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -162,7 +163,7 @@ public interface TableIf { void write(DataOutput out) throws IOException; // Don't use it outside due to its thread-unsafe, use get specific constraints instead. - default Map getConstraintsMap() { + default Map getConstraintsMapUnsafe() { throw new RuntimeException(String.format("Not implemented constraint for table %s. " + "And the function can't be called outside, consider get specific function " + "like getForeignKeyConstraints/getPrimaryKeyConstraints/getUniqueConstraints.", this)); @@ -171,7 +172,7 @@ public interface TableIf { default Set getForeignKeyConstraints() { readLock(); try { - return getConstraintsMap().values().stream() + return getConstraintsMapUnsafe().values().stream() .filter(ForeignKeyConstraint.class::isInstance) .map(ForeignKeyConstraint.class::cast) .collect(ImmutableSet.toImmutableSet()); @@ -182,10 +183,21 @@ public interface TableIf { } } + default Map getConstraintMap() { + readLock(); + try { + return ImmutableMap.copyOf(getConstraintsMapUnsafe()); + } catch (Exception ignored) { + return ImmutableMap.of(); + } finally { + readUnlock(); + } + } + default Set getPrimaryKeyConstraints() { readLock(); try { - return getConstraintsMap().values().stream() + return getConstraintsMapUnsafe().values().stream() .filter(PrimaryKeyConstraint.class::isInstance) .map(PrimaryKeyConstraint.class::cast) .collect(ImmutableSet.toImmutableSet()); @@ -199,7 +211,7 @@ public interface TableIf { default Set getUniqueConstraints() { readLock(); try { - return getConstraintsMap().values().stream() + return getConstraintsMapUnsafe().values().stream() .filter(UniqueConstraint.class::isInstance) .map(UniqueConstraint.class::cast) .collect(ImmutableSet.toImmutableSet()); @@ -227,7 +239,7 @@ public interface TableIf { default void addUniqueConstraint(String name, ImmutableList columns) { writeLock(); try { - Map constraintMap = getConstraintsMap(); + Map constraintMap = getConstraintsMapUnsafe(); UniqueConstraint uniqueConstraint = new UniqueConstraint(name, ImmutableSet.copyOf(columns)); checkConstraintNotExistence(name, uniqueConstraint, constraintMap); constraintMap.put(name, uniqueConstraint); @@ -239,7 +251,7 @@ public interface TableIf { default void addPrimaryKeyConstraint(String name, ImmutableList columns) { writeLock(); try { - Map constraintMap = getConstraintsMap(); + Map constraintMap = getConstraintsMapUnsafe(); PrimaryKeyConstraint primaryKeyConstraint = new PrimaryKeyConstraint(name, ImmutableSet.copyOf(columns)); checkConstraintNotExistence(name, primaryKeyConstraint, constraintMap); constraintMap.put(name, primaryKeyConstraint); @@ -251,7 +263,7 @@ public interface TableIf { default void updatePrimaryKeyForForeignKey(PrimaryKeyConstraint requirePrimaryKey, TableIf referencedTable) { referencedTable.writeLock(); try { - Optional primaryKeyConstraint = referencedTable.getConstraintsMap().values().stream() + Optional primaryKeyConstraint = referencedTable.getConstraintsMapUnsafe().values().stream() .filter(requirePrimaryKey::equals) .findFirst(); if (!primaryKeyConstraint.isPresent()) { @@ -269,7 +281,7 @@ public interface TableIf { TableIf referencedTable, ImmutableList referencedColumns) { writeLock(); try { - Map constraintMap = getConstraintsMap(); + Map constraintMap = getConstraintsMapUnsafe(); ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint(name, columns, referencedTable, referencedColumns); checkConstraintNotExistence(name, foreignKeyConstraint, constraintMap); @@ -285,7 +297,7 @@ public interface TableIf { default void dropConstraint(String name) { writeLock(); try { - Map constraintMap = getConstraintsMap(); + Map constraintMap = getConstraintsMapUnsafe(); if (!constraintMap.containsKey(name)) { throw new AnalysisException( String.format("Unknown constraint %s on table %s.", name, this.getName())); @@ -304,7 +316,7 @@ public interface TableIf { default void dropFKReferringPK(TableIf table, PrimaryKeyConstraint constraint) { writeLock(); try { - Map constraintMap = getConstraintsMap(); + Map constraintMap = getConstraintsMapUnsafe(); constraintMap.entrySet().removeIf(e -> e.getValue() instanceof ForeignKeyConstraint && ((ForeignKeyConstraint) e.getValue()).isReferringPK(table, constraint)); } finally { diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/Constraint.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/Constraint.java index e733337274..9faa80f8c1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/Constraint.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/Constraint.java @@ -19,9 +19,19 @@ package org.apache.doris.catalog.constraint; public abstract class Constraint { public enum ConstraintType { - FOREIGN_KEY, - PRIMARY_KEY, - UNIQUE + FOREIGN_KEY("FOREIGN KEY"), + PRIMARY_KEY("PRIMARY KEY"), + UNIQUE("UNIQUE"); + + private final String name; + + private ConstraintType(String stringValue) { + this.name = stringValue; + } + + public String getName() { + return name; + } } private final String name; @@ -36,4 +46,8 @@ public abstract class Constraint { public String getName() { return name; } + + public ConstraintType getType() { + return type; + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/ForeignKeyConstraint.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/ForeignKeyConstraint.java index 3284c9e3ab..01dc00052b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/ForeignKeyConstraint.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/ForeignKeyConstraint.java @@ -104,4 +104,15 @@ public class ForeignKeyConstraint extends Constraint { return Objects.equals(foreignToReference, other.foreignToReference) && Objects.equals(referencedTable, other.referencedTable); } + + @Override + public String toString() { + String foreignKeys = "(" + String.join(", ", foreignToReference.keySet()) + ")"; + String primaryKeys = "(" + String.join(", ", foreignToReference.values()) + ")"; + return String.format("FOREIGN KEY %s REFERENCES %s %s", foreignKeys, referencedTable, primaryKeys); + } + + public String getTypeName() { + return "FOREIGN KEY"; + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java index fd894c498c..c868d3a84f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java @@ -57,6 +57,11 @@ public class PrimaryKeyConstraint extends Constraint { .collect(ImmutableList.toImmutableList()); } + @Override + public String toString() { + return "PRIMARY KEY (" + String.join(", ", columns) + ")"; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/TableIdentifier.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/TableIdentifier.java index 4d5061a08f..c4db65c14e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/TableIdentifier.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/TableIdentifier.java @@ -65,4 +65,10 @@ class TableIdentifier { public int hashCode() { return Objects.hash(databaseId, tableId); } + + @Override + public String toString() { + TableIf tableIf = this.toTableIf(); + return String.format("%s.%s", tableIf.getDatabase().getFullName(), tableIf.getName()); + } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/UniqueConstraint.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/UniqueConstraint.java index 2fc7fbb261..3e8042dc8e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/UniqueConstraint.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/UniqueConstraint.java @@ -53,6 +53,11 @@ public class UniqueConstraint extends Constraint { return columns.equals(that.columns); } + @Override + public String toString() { + return "UNIQUE (" + String.join(", ", columns) + ")"; + } + @Override public int hashCode() { return Objects.hashCode(columns); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 0d4c6ea4bf..cef3cd0a10 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -156,6 +156,7 @@ import org.apache.doris.nereids.DorisParser.SelectClauseContext; import org.apache.doris.nereids.DorisParser.SelectColumnClauseContext; import org.apache.doris.nereids.DorisParser.SelectHintContext; import org.apache.doris.nereids.DorisParser.SetOperationContext; +import org.apache.doris.nereids.DorisParser.ShowConstraintContext; import org.apache.doris.nereids.DorisParser.SimpleColumnDefContext; import org.apache.doris.nereids.DorisParser.SimpleColumnDefsContext; import org.apache.doris.nereids.DorisParser.SingleStatementContext; @@ -347,6 +348,7 @@ import org.apache.doris.nereids.trees.plans.commands.LoadCommand; import org.apache.doris.nereids.trees.plans.commands.PauseMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.RefreshMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.ResumeMTMVCommand; +import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand; import org.apache.doris.nereids.trees.plans.commands.UpdateCommand; import org.apache.doris.nereids.trees.plans.commands.info.AlterMTMVInfo; import org.apache.doris.nereids.trees.plans.commands.info.AlterMTMVPropertyInfo; @@ -429,6 +431,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -746,6 +749,13 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { return new AlterMTMVCommand(alterMTMVInfo); } + @Override + public LogicalPlan visitShowConstraint(ShowConstraintContext ctx) { + Set unboundRelation = visitRelation(ctx.table) + .collect(UnboundRelation.class::isInstance); + return new ShowConstraintsCommand(unboundRelation.iterator().next().getNameParts()); + } + @Override public LogicalPlan visitAddConstraint(AddConstraintContext ctx) { LogicalPlan curTable = visitRelation(ctx.table); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java index f1eae21663..530003b791 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java @@ -133,6 +133,7 @@ public enum PlanType { ALTER_MTMV_COMMAND, ADD_CONSTRAINT_COMMAND, DROP_CONSTRAINT_COMMAND, + SHOW_CONSTRAINTS_COMMAND, REFRESH_MTMV_COMMAND, DROP_MTMV_COMMAND, PAUSE_MTMV_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowConstraintsCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowConstraintsCommand.java new file mode 100644 index 0000000000..32950308e0 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowConstraintsCommand.java @@ -0,0 +1,66 @@ +// 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.TableIf; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.nereids.util.RelationUtil; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; + +import org.apache.hadoop.util.Lists; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * add constraint command + */ +public class ShowConstraintsCommand extends Command implements NoForward { + + public static final Logger LOG = LogManager.getLogger(ShowConstraintsCommand.class); + private final List nameParts; + + /** + * constructor + */ + public ShowConstraintsCommand(List nameParts) { + super(PlanType.SHOW_CONSTRAINTS_COMMAND); + this.nameParts = nameParts; + } + + @Override + public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + TableIf tableIf = RelationUtil.getDbAndTable( + RelationUtil.getQualifierName(ctx, nameParts), ctx.getEnv()).value(); + List> res = tableIf.getConstraintMap().entrySet().stream() + .map(e -> Lists.newArrayList(e.getKey(), + e.getValue().getType().getName(), + e.getValue().toString())) + .collect(Collectors.toList()); + executor.handleShowConstraintStmt(res); + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitShowConstraintsCommand(this, context); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java index 297e80114d..db14074c06 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java @@ -38,6 +38,7 @@ import org.apache.doris.nereids.trees.plans.commands.LoadCommand; import org.apache.doris.nereids.trees.plans.commands.PauseMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.RefreshMTMVCommand; import org.apache.doris.nereids.trees.plans.commands.ResumeMTMVCommand; +import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand; import org.apache.doris.nereids.trees.plans.commands.UpdateCommand; /** CommandVisitor. */ @@ -108,6 +109,10 @@ public interface CommandVisitor { return visitCommand(dropConstraintCommand, context); } + default R visitShowConstraintsCommand(ShowConstraintsCommand showConstraintsCommand, C context) { + return visitCommand(showConstraintsCommand, context); + } + default R visitRefreshMTMVCommand(RefreshMTMVCommand refreshMTMVCommand, C context) { return visitCommand(refreshMTMVCommand, context); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index 59909e59b4..9738cde764 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -2367,6 +2367,16 @@ public class StmtExecutor { private void handleLockTablesStmt() { } + public void handleShowConstraintStmt(List> result) throws IOException { + ShowResultSetMetaData metaData = ShowResultSetMetaData.builder() + .addColumn(new Column("Name", ScalarType.createVarchar(20))) + .addColumn(new Column("Type", ScalarType.createVarchar(20))) + .addColumn(new Column("Definition", ScalarType.createVarchar(20))) + .build(); + ResultSet resultSet = new ShowResultSet(metaData, result); + sendResultSet(resultSet); + } + public void handleExplainStmt(String result, boolean isNereids) throws IOException { ShowResultSetMetaData metaData = ShowResultSetMetaData.builder() .addColumn(new Column("Explain String" + (isNereids ? "(Nereids Planner)" : "(Old Planner)"), diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ConstraintTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ConstraintTest.java index 9084415f48..6d5600909f 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ConstraintTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ConstraintTest.java @@ -81,7 +81,7 @@ class ConstraintTest extends TestWithFeService implements PlanPatternMatchSuppor "alter table t1 drop constraint pk"); dropCommand.run(connectContext, null); PlanChecker.from(connectContext).parse("select * from t1").analyze().matches( - logicalOlapScan().when(o -> o.getTable().getConstraintsMap().isEmpty())); + logicalOlapScan().when(o -> o.getTable().getConstraintsMapUnsafe().isEmpty())); } @Test @@ -102,7 +102,7 @@ class ConstraintTest extends TestWithFeService implements PlanPatternMatchSuppor "alter table t1 drop constraint un"); dropCommand.run(connectContext, null); PlanChecker.from(connectContext).parse("select * from t1").analyze().matches( - logicalOlapScan().when(o -> o.getTable().getConstraintsMap().isEmpty())); + logicalOlapScan().when(o -> o.getTable().getConstraintsMapUnsafe().isEmpty())); } @Test @@ -149,7 +149,7 @@ class ConstraintTest extends TestWithFeService implements PlanPatternMatchSuppor "alter table t1 drop constraint fk"); dropCommand.run(connectContext, null); PlanChecker.from(connectContext).parse("select * from t1").analyze().matches( - logicalOlapScan().when(o -> o.getTable().getConstraintsMap().isEmpty())); + logicalOlapScan().when(o -> o.getTable().getConstraintsMapUnsafe().isEmpty())); // drop pk and fk referenced it also should be dropped ((AddConstraintCommand) new NereidsParser().parseSingle( "alter table t1 add constraint fk foreign key (k1, k2) references t2(k1, k2)")).run(connectContext, @@ -158,8 +158,8 @@ class ConstraintTest extends TestWithFeService implements PlanPatternMatchSuppor .run(connectContext, null); PlanChecker.from(connectContext).parse("select * from t1").analyze().matches( - logicalOlapScan().when(o -> o.getTable().getConstraintsMap().isEmpty())); + logicalOlapScan().when(o -> o.getTable().getConstraintsMapUnsafe().isEmpty())); PlanChecker.from(connectContext).parse("select * from t2").analyze().matches( - logicalOlapScan().when(o -> o.getTable().getConstraintsMap().isEmpty())); + logicalOlapScan().when(o -> o.getTable().getConstraintsMapUnsafe().isEmpty())); } } diff --git a/regression-test/data/nereids_syntax_p0/constraint.out b/regression-test/data/nereids_syntax_p0/constraint.out new file mode 100644 index 0000000000..6dbdd84db6 --- /dev/null +++ b/regression-test/data/nereids_syntax_p0/constraint.out @@ -0,0 +1,10 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !show_constrait -- +uk UNIQUE UNIQUE (id) +pk PRIMARY KEY PRIMARY KEY (id) +fk2 FOREIGN KEY FOREIGN KEY (id) REFERENCES regression_test_nereids_syntax_p0.t2 (id) +fk1 FOREIGN KEY FOREIGN KEY (id) REFERENCES regression_test_nereids_syntax_p0.t1 (id) + +-- !show_constrait -- +pk PRIMARY KEY PRIMARY KEY (id) + diff --git a/regression-test/suites/nereids_syntax_p0/constraint.groovy b/regression-test/suites/nereids_syntax_p0/constraint.groovy new file mode 100644 index 0000000000..c01f55c0f1 --- /dev/null +++ b/regression-test/suites/nereids_syntax_p0/constraint.groovy @@ -0,0 +1,81 @@ +// 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. + +suite("show_constraint") { + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + + sql """ DROP TABLE IF EXISTS `t1` """ + sql """ DROP TABLE IF EXISTS `t2` """ + sql """ + CREATE TABLE `t1` ( + `id` varchar(64) NULL, + `name` varchar(64) NULL, + `age` int NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`,`name`) + COMMENT 'OLAP' + DISTRIBUTED BY HASH(`id`,`name`) BUCKETS 4 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2", + "disable_auto_compaction" = "false" + ); + """ + sql """ + CREATE TABLE `t2` ( + `id` varchar(64) NULL, + `name` varchar(64) NULL, + `age` int NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`,`name`) + COMMENT 'OLAP' + DISTRIBUTED BY HASH(`id`,`name`) BUCKETS 4 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2", + "disable_auto_compaction" = "false" + ); + """ + + sql """ + alter table t1 add constraint pk primary key (id) + """ + sql """ + alter table t2 add constraint pk primary key (id) + """ + + sql """ + alter table t1 add constraint uk unique (id) + """ + + sql """ + alter table t1 add constraint fk1 foreign key (id) references t1(id) + """ + sql """ + alter table t1 add constraint fk2 foreign key (id) references t2(id) + """ + + qt_show_constrait """ + show constraints from t1; + """ + qt_show_constrait """ + show constraints from t2; + """ +}