-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Support Column rename for unique/foreign key constraints #47851
Merged
gengjun-git
merged 6 commits into
StarRocks:main
from
gengjun-git:fix_unique_foreign_key_column_id
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,15 @@ | |
import com.google.common.base.Joiner; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.Lists; | ||
import com.starrocks.analysis.TableName; | ||
import com.starrocks.common.Pair; | ||
import com.starrocks.server.GlobalStateMgr; | ||
import com.starrocks.sql.analyzer.SemanticException; | ||
import com.starrocks.sql.common.MetaUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
@@ -35,22 +39,47 @@ | |
// a table may have multi unique constraints. | ||
public class UniqueConstraint { | ||
private static final Logger LOG = LogManager.getLogger(UniqueConstraint.class); | ||
// here id is preferred, but meta of column does not have id. | ||
// have to use name here, so column rename is not supported | ||
private final List<String> uniqueColumns; | ||
private final List<ColumnId> uniqueColumns; | ||
|
||
private final String catalogName; | ||
private final String dbName; | ||
private final String tableName; | ||
private String catalogName; | ||
private String dbName; | ||
private String tableName; | ||
|
||
public UniqueConstraint(String catalogName, String dbName, String tableName, List<String> uniqueColumns) { | ||
private Table referencedTable; | ||
|
||
public UniqueConstraint(String catalogName, String dbName, String tableName, List<ColumnId> uniqueColumns) { | ||
this.catalogName = catalogName; | ||
this.dbName = dbName; | ||
this.tableName = tableName; | ||
this.uniqueColumns = uniqueColumns; | ||
} | ||
|
||
public List<String> getUniqueColumns() { | ||
// Used for primaryKey/uniqueKey table to create default uniqueConstraints. | ||
public UniqueConstraint(Table referencedTable, List<ColumnId> uniqueColumns) { | ||
this.referencedTable = referencedTable; | ||
this.uniqueColumns = uniqueColumns; | ||
} | ||
|
||
public List<String> getUniqueColumnNames() { | ||
Table targetTable; | ||
if (referencedTable != null) { | ||
targetTable = referencedTable; | ||
} else { | ||
targetTable = MetaUtils.getTable(catalogName, dbName, tableName); | ||
} | ||
List<String> result = new ArrayList<>(uniqueColumns.size()); | ||
for (ColumnId columnId : uniqueColumns) { | ||
Column column = targetTable.getColumn(columnId); | ||
if (column == null) { | ||
LOG.warn("Can not find column by column id: {}, the column may have been dropped", columnId); | ||
continue; | ||
} | ||
result.add(column.getName()); | ||
} | ||
return result; | ||
} | ||
|
||
public List<ColumnId> getUniqueColumns() { | ||
return uniqueColumns; | ||
} | ||
|
||
|
@@ -66,7 +95,8 @@ public boolean isMatch(Table parentTable, Set<String> foreignKeys) { | |
return false; | ||
} | ||
} | ||
Set<String> uniqueColumnSet = uniqueColumns.stream().map(String::toLowerCase).collect(Collectors.toSet()); | ||
Set<String> uniqueColumnSet = getUniqueColumnNames().stream().map(String::toLowerCase) | ||
.collect(Collectors.toSet()); | ||
return uniqueColumnSet.equals(foreignKeys); | ||
} | ||
|
||
|
@@ -81,10 +111,30 @@ public String toString() { | |
if (tableName != null) { | ||
sb.append(tableName).append("."); | ||
} | ||
sb.append(Joiner.on(",").join(uniqueColumns)); | ||
sb.append(Joiner.on(",").join(getUniqueColumns())); | ||
return sb.toString(); | ||
} | ||
|
||
public static String getShowCreateTableConstraintDesc(List<UniqueConstraint> constraints) { | ||
List<String> constraintStrs = Lists.newArrayList(); | ||
for (UniqueConstraint constraint : constraints) { | ||
StringBuilder constraintSb = new StringBuilder(); | ||
if (constraint.catalogName != null) { | ||
constraintSb.append(constraint.catalogName).append("."); | ||
} | ||
if (constraint.dbName != null) { | ||
constraintSb.append(constraint.dbName).append("."); | ||
} | ||
if (constraint.tableName != null) { | ||
constraintSb.append(constraint.tableName).append("."); | ||
} | ||
constraintSb.append(Joiner.on(",").join(constraint.getUniqueColumnNames())); | ||
constraintStrs.add(constraintSb.toString()); | ||
} | ||
|
||
return Joiner.on(";").join(constraintStrs); | ||
} | ||
|
||
public String getCatalogName() { | ||
return catalogName; | ||
} | ||
|
@@ -97,8 +147,8 @@ public String getTableName() { | |
return tableName; | ||
} | ||
|
||
public static List<UniqueConstraint> parse(String catalogName, String dbName, String tableName, | ||
String constraintDescs) { | ||
public static List<UniqueConstraint> parse(String defaultCatalogName, String defaultDbName, | ||
String defaultTableName, String constraintDescs) { | ||
if (Strings.isNullOrEmpty(constraintDescs)) { | ||
return null; | ||
} | ||
|
@@ -108,17 +158,20 @@ public static List<UniqueConstraint> parse(String catalogName, String dbName, St | |
if (Strings.isNullOrEmpty(constraintDesc)) { | ||
continue; | ||
} | ||
String[] uniqueColumns = constraintDesc.split(","); | ||
List<String> columnNames = | ||
Arrays.stream(uniqueColumns).map(String::trim).collect(Collectors.toList()); | ||
parseUniqueConstraintColumns(catalogName, dbName, tableName, columnNames, uniqueConstraints); | ||
Pair<TableName, List<String>> descResult = parseUniqueConstraintDesc( | ||
defaultCatalogName, defaultDbName, defaultTableName, constraintDesc); | ||
uniqueConstraints.add(new UniqueConstraint(descResult.first.getCatalog(), | ||
descResult.first.getDb(), descResult.first.getTbl(), | ||
descResult.second.stream().map(ColumnId::create).collect(Collectors.toList()))); | ||
} | ||
return uniqueConstraints; | ||
} | ||
|
||
private static void parseUniqueConstraintColumns(String defaultCatalogName, String defaultDbName, | ||
String defaultTableName, List<String> columnNames, | ||
List<UniqueConstraint> uniqueConstraints) { | ||
// result if a pair, the fist value is TableName(catalogName, dbName, tableName), the second value is columns | ||
public static Pair<TableName, List<String>> parseUniqueConstraintDesc(String defaultCatalogName, String defaultDbName, | ||
String defaultTableName, String constraintDesc) { | ||
String[] uniqueColumns = constraintDesc.split(","); | ||
List<String> columnNames = Arrays.stream(uniqueColumns).map(String::trim).collect(Collectors.toList()); | ||
String catalogName = null; | ||
String dbName = null; | ||
String tableName = null; | ||
|
@@ -172,6 +225,6 @@ private static void parseUniqueConstraintColumns(String defaultCatalogName, Stri | |
tableName = defaultTableName; | ||
} | ||
|
||
uniqueConstraints.add(new UniqueConstraint(catalogName, dbName, tableName, uniqueConstraintColumns)); | ||
return Pair.create(new TableName(catalogName, dbName, tableName), uniqueConstraintColumns); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most risky bug in this code is: You can modify the code like this: public String toString() {
StringBuilder sb = new StringBuilder();
if (tableName != null) {
sb.append(tableName).append(".");
}
sb.append(Joiner.on(",").join(getUniqueColumnNames())); // Change from getUniqueColumns() to getUniqueColumnNames()
return sb.toString();
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most risky bug in this code is:
Potential
NullPointerException
due to missing null checks for the tables returned byMetaUtils.getTable
.You can modify the code like this: