XieJiann commented on code in PR #27627:
URL: https://github.com/apache/doris/pull/27627#discussion_r1410636110
##########
fe/fe-core/src/main/java/org/apache/doris/catalog/TableIf.java:
##########
@@ -153,6 +156,80 @@ default int getBaseColumnIdxByName(String colName) {
void write(DataOutput out) throws IOException;
+ default Map<String, Constraint> getConstraintMap() {
+ throw new RuntimeException("Not implemented constraint for table " +
this.toString());
+ }
+
+ // Note this function is not thread safe
+ default void checkConstraintNotExistence(String name, Constraint
primaryKeyConstraint,
+ Map<String, Constraint> constraintMap) {
+ if (constraintMap.containsKey(name)) {
+ throw new RuntimeException(String.format("Constraint name %s has
existed", name));
+ }
+ for (Map.Entry<String, Constraint> entry : constraintMap.entrySet()) {
+ if (entry.getValue().equals(primaryKeyConstraint)) {
+ throw new RuntimeException(String.format(
+ "Constraint %s has existed, named %s",
primaryKeyConstraint, entry.getKey()));
+ }
+ }
+ }
+
+ default void addUniqueConstraint(String name, ImmutableList<Column>
columns) {
+ writeLock();
+ try {
+ Map<String, Constraint> constraintMap = getConstraintMap();
+ UniqueConstraint uniqueConstraint = new
UniqueConstraint(ImmutableSet.copyOf(columns));
+ checkConstraintNotExistence(name, uniqueConstraint, constraintMap);
+ constraintMap.put(name, uniqueConstraint);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ default void addPrimaryKeyConstraint(String name, ImmutableList<Column>
columns) {
+ writeLock();
+ try {
+ Map<String, Constraint> constraintMap = getConstraintMap();
+ PrimaryKeyConstraint primaryKeyConstraint = new
PrimaryKeyConstraint(ImmutableSet.copyOf(columns));
+ checkConstraintNotExistence(name, primaryKeyConstraint,
constraintMap);
+ constraintMap.put(name, primaryKeyConstraint);
+ } finally {
+ writeUnlock();
+ }
+ }
+
+ default void updatePrimaryKeyForForeignKey(PrimaryKeyConstraint
requirePrimaryKey, TableIf referencedTable) {
+ referencedTable.writeLock();
+ try {
+ Optional<Constraint> primaryKeyConstraint =
referencedTable.getConstraintMap().values().stream()
+ .filter(requirePrimaryKey::equals)
+ .findFirst();
+ if (!primaryKeyConstraint.isPresent()) {
+ throw new AnalysisException("Foreign key constraint requires a
primary key constraint of"
+ + requirePrimaryKey.getColumns() + " in " +
referencedTable.getName());
+ }
+ ((PrimaryKeyConstraint)
(primaryKeyConstraint.get())).addForeignTable(this);
+ } finally {
+ referencedTable.writeUnlock();
+ }
+ }
+
+ default void addForeignConstraint(String name, ImmutableList<Column>
columns,
+ TableIf referencedTable, ImmutableList<Column> referencedColumns) {
+ writeLock();
+ try {
+ Map<String, Constraint> constraintMap = getConstraintMap();
+ ForeignKeyConstraint foreignKeyConstraint =
+ new ForeignKeyConstraint(columns, referencedTable,
referencedColumns);
+ checkConstraintNotExistence(name, foreignKeyConstraint,
constraintMap);
+ PrimaryKeyConstraint requirePrimaryKey = new
PrimaryKeyConstraint(foreignKeyConstraint.getColumns());
Review Comment:
Yeah
--
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]