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 drop partitions with common expressions #53118

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LiShuMing
Copy link
Contributor

@LiShuMing LiShuMing commented Nov 22, 2024

Why I'm doing:

What I'm doing:

Fixes #53117

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.4
    • 3.3
    • 3.2
    • 3.1
    • 3.0

@LiShuMing LiShuMing requested review from a team as code owners November 22, 2024 03:17
} catch (Exception e) {
throw new SemanticException("Failed to prune partitions with where expression: " + e.getMessage());
}
}
}
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:

In the putValueMapItem method, there is a risk of a concurrent modification exception because the partitionIdSet, once retrieved from the map, can be modified outside of the synchronization context before it is put back.

You can modify the code like this:

private static void putValueMapItem(ConcurrentNavigableMap<LiteralExpr, Set<Long>> partitionValueToIds,
                                    Long partitionId,
                                    LiteralExpr value) {
    partitionValueToIds.compute(value, (k, partitionIdSet) -> {
        if (partitionIdSet == null) {
            partitionIdSet = new HashSet<>();
        }
        partitionIdSet.add(partitionId);
        return partitionIdSet;
    });
}

This change uses compute which safely adds partitionId to the set associated with value without risking concurrent modification issues.

}

public Expr getDropWhereExpr() {
return dropWhereExpr;
}

public List<String> getResolvedPartitionNames() {
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 null pointer dereference due to assumptions about partition lists.

You can modify the code like this:

// Before accessing any fields that may be null, check for null references.
public DropPartitionClause(boolean ifExists, Expr whereExpr, boolean isTempPartition,
                           boolean forceDrop, NodePosition pos) {
    super(AlterOpType.DROP_PARTITION, pos);
    this.ifExists = ifExists;
    this.partitionName = null;
    this.isTempPartition = isTempPartition;
    this.forceDrop = forceDrop;
    this.multiRangePartitionDesc = null;
    this.partitionNames = new ArrayList<>(); // Ensure partitionNames is initialized to prevent null access.
    this.dropWhereExpr = whereExpr;
}

Make sure the partitionNames or any potentially nullable field is either checked before use or given a safe default (like an empty list). This helps avoid null pointer exceptions when the object is used elsewhere in the system.

.getDb(context.getCurrentCatalog(), context.getDatabase());
TableName tableName = new TableName(db.getFullName(), table.getName());
List<String> dropPartitionNames = ListPartitionPruner.getPartitionNamesByExpr(olapTable, tableName, expr, context);
clause.setResolvedPartitionNames(dropPartitionNames);
}

if (table instanceof OlapTable) {
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:
Incorrect conditions in if-else statements that can lead to semantic exceptions for unsupported table types.

You can modify the code like this:

@Override
public Void visitDropPartitionClause(DropPartitionClause clause, ConnectContext context) {
    if (clause.getMultiRangePartitionDesc() != null) {
        if (!(table instanceof OlapTable)) {
            throw new SemanticException("Can't drop partitions with multi-range since its table type is not olap");
        }
        MultiRangePartitionDesc multiRangePartitionDesc = clause.getMultiRangePartitionDesc();
        PartitionDescAnalyzer.analyze(multiRangePartitionDesc);
        
        OlapTable olapTable = (OlapTable) table;
        PartitionDescAnalyzer.analyzePartitionDescWithExistsTable(multiRangePartitionDesc, olapTable);

        PartitionInfo partitionInfo = olapTable.getPartitionInfo();
        List<String> dropPartitionNames = multiRangePartitionDesc.convertToSingleRangePartitions(partitionInfo);
        clause.setResolvedPartitionNames(dropPartitionNames);
    } else if (clause.getPartitionNames() != null) {
        clause.setResolvedPartitionNames(clause.getPartitionNames());
    } else if (clause.getDropWhereExpr() != null) {
        if (!(table instanceof OlapTable)) {
            throw new SemanticException("Can't drop partitions with where expression since its table type is not olap");
        }
        OlapTable olapTable = (OlapTable) table;
        Expr expr = clause.getDropWhereExpr();
        Database db = context.getGlobalStateMgr().getMetadataMgr()
                .getDb(context.getCurrentCatalog(), context.getDatabase());
        TableName tableName = new TableName(db.getFullName(), table.getName());
        List<String> dropPartitionNames = ListPartitionPruner.getPartitionNamesByExpr(olapTable, tableName, expr, context);
        clause.setResolvedPartitionNames(dropPartitionNames);
    }

    if (table instanceof OlapTable) {
        // any additional logic for OlapTable
    }
}

Explanation:

  1. Placement and order of MultiRangePartitionDesc multiRangePartitionDesc initialization: In the original code, there's a risk because initialization should happen only after confirming the table type as OlapTable.
  2. Ensuring the if conditions align logically with their use case to prevent invalid operations on non-Olap tables.
  3. It ensures that both types of partition dropping verifications are correctly placed and don't leak erroneous behaviors on different table types.

Signed-off-by: shuming.li <[email protected]>
@LiShuMing LiShuMing force-pushed the feat/main/support_drop_partition_expression branch from a0e136e to 3ade038 Compare November 22, 2024 04:52
Copy link

sonarcloud bot commented Nov 22, 2024

Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 141 / 148 (95.27%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/AlterTableClauseAnalyzer.java 11 13 84.62% [1235, 1261]
🔵 com/starrocks/sql/optimizer/rule/transformation/ListPartitionPruner.java 114 119 95.80% [809, 824, 848, 888, 933]
🔵 com/starrocks/sql/ast/DropPartitionClause.java 14 14 100.00% []
🔵 com/starrocks/sql/optimizer/rewrite/OptOlapPartitionPruner.java 2 2 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

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

Successfully merging this pull request may close these issues.

[Feature] Support TTL for List Partition Tables and Materialized Views
1 participant