Skip to content
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

Conversation

gengjun-git
Copy link
Contributor

@gengjun-git gengjun-git commented Jul 4, 2024

Why I'm doing:

Store the ColumnId in ForeignKeyConstraint and UniqueConstraint.

What I'm doing:

Fixes #42783 #42782

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@gengjun-git gengjun-git requested review from a team as code owners July 4, 2024 09:11
for (Pair<String, String> pair : columnRefPairs) {
List<Pair<ColumnId, ColumnId>> columnRefPairs = Streams.zip(childColumnIds.stream(),
parentColumnIds.stream(), Pair::create).collect(Collectors.toList());
for (Pair<ColumnId, ColumnId> pair : columnRefPairs) {
Column childColumn = childTable.getColumn(pair.first);
Column parentColumn = parentTable.getColumn(pair.second);
if (!childColumn.getType().equals(parentColumn.getType())) {
Copy link

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:
Inconsistent unique constraint materialized view table name retrieval, which may cause NullPointerException.

You can modify the code like this:

                 if (table.isMaterializedView()) {
                     Table uniqueConstraintTable = GlobalStateMgr.getCurrentState().getMetadataMgr().getTable(tableName.getCatalog(),
                            tableName.getDb(), tableName.getTbl());
                     if (uniqueConstraintTable == null) {
                         throw new SemanticException(
                                 String.format("table: %s does not exist", tableName));
                     }
                     List<ColumnId> columnIds = MetaUtils.getColumnIdsByColumnNames(uniqueConstraintTable, columnNames);
                     analyzedUniqueConstraints.add(new UniqueConstraint(tableName.getCatalog(), tableName.getDb(), tableName.getTbl(), columnIds));
                 } else {
                     List<ColumnId> columnIds = MetaUtils.getColumnIdsByColumnNames(table, columnNames);
                     analyzedUniqueConstraints.add(new UniqueConstraint(table.getCatalogName(), db.getFullName(), table.getName(), columnIds));
                 }

@@ -172,6 +212,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);
}
}
Copy link

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:
Using getUniqueColumns() instead of getUniqueColumnNames() in the toString method.

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();
}

constraintSb.append(parentColumns);
constraintSb.append(")");
constraintStrs.add(constraintSb.toString());
}

sb.append(Joiner.on(";").join(constraintStrs));
return sb.toString();
return Joiner.on(";").join(constraintStrs);
}
}
Copy link

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 by MetaUtils.getTable.

You can modify the code like this:

private Table getParentTable() {
    Table table;
    if (parentTableInfo.isInternalCatalog()) {
        table = MetaUtils.getTable(parentTableInfo.getDbId(), parentTableInfo.getTableId());
    } else {
        table = MetaUtils.getTable(parentTableInfo.getCatalogName(), parentTableInfo.getDbName(),
                parentTableInfo.getTableName());
    }
    if (table == null) {
        LOG.warn("Parent table {} not found", parentTableInfo);
        throw new IllegalStateException("Parent table not found");
    }
    return table;
}

private Table getChildTable() {
    Table table;
    if (childTableInfo.isInternalCatalog()) {
        table = MetaUtils.getTable(childTableInfo.getDbId(), childTableInfo.getTableId());
    } else {
        table = MetaUtils.getTable(childTableInfo.getCatalogName(), childTableInfo.getDbName(),
                childTableInfo.getTableName());
    }
    if (table == null) {
        LOG.warn("Child table {} not found", childTableInfo);
        throw new IllegalStateException("Child table not found");
    }
    return table;
}

Signed-off-by: gengjun-git <[email protected]>
Signed-off-by: gengjun-git <[email protected]>
@gengjun-git gengjun-git force-pushed the fix_unique_foreign_key_column_id branch from 983a2a7 to b668508 Compare July 5, 2024 03:06
Signed-off-by: gengjun-git <[email protected]>
Signed-off-by: gengjun-git <[email protected]>
Signed-off-by: gengjun-git <[email protected]>
Signed-off-by: gengjun-git <[email protected]>
Copy link

sonarcloud bot commented Jul 5, 2024

Copy link

github-actions bot commented Jul 5, 2024

[FE Incremental Coverage Report]

pass : 130 / 147 (88.44%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/optimizer/dump/DesensitizedSQLBuilder.java 0 4 00.00% [583, 585, 729, 731]
🔵 com/starrocks/sql/common/MetaUtils.java 0 2 00.00% [359, 360]
🔵 com/starrocks/alter/SchemaChangeHandler.java 0 4 00.00% [2556, 2557, 2576, 2577]
🔵 com/starrocks/catalog/ForeignKeyConstraint.java 30 34 88.24% [91, 93, 97, 99]
🔵 com/starrocks/catalog/UniqueConstraint.java 38 40 95.00% [74, 75]
🔵 com/starrocks/common/util/PropertyAnalyzer.java 27 28 96.43% [1063]
🔵 com/starrocks/catalog/MaterializedView.java 1 1 100.00% []
🔵 com/starrocks/sql/optimizer/rule/transformation/pruner/UniquenessBasedTablePruneRule.java 1 1 100.00% []
🔵 com/starrocks/sql/optimizer/rule/transformation/materialization/MaterializedViewRewriter.java 13 13 100.00% []
🔵 com/starrocks/catalog/OlapTable.java 5 5 100.00% []
🔵 com/starrocks/sql/optimizer/rule/transformation/pruner/CboTablePruneRule.java 1 1 100.00% []
🔵 com/starrocks/sql/optimizer/UKFKConstraintsCollector.java 5 5 100.00% []
🔵 com/starrocks/sql/optimizer/rule/transformation/pruner/CPBiRel.java 9 9 100.00% []

Copy link

github-actions bot commented Jul 5, 2024

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@gengjun-git gengjun-git enabled auto-merge (squash) July 12, 2024 07:03
@gengjun-git gengjun-git merged commit ebcd369 into StarRocks:main Jul 12, 2024
47 checks passed
@gengjun-git
Copy link
Contributor Author

@Mergifyio backport branch-3.3

Copy link
Contributor

mergify bot commented Jul 12, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jul 12, 2024
…47851)

Store the ColumnId in ForeignKeyConstraint and UniqueConstraint.

Signed-off-by: gengjun-git <[email protected]>
(cherry picked from commit ebcd369)
wanpengfei-git pushed a commit that referenced this pull request Jul 12, 2024
@gengjun-git
Copy link
Contributor Author

ignore backport check: 3.2.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[rename column]rename column, the column name of foreign_key_constraints is not changed
3 participants