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

[Refactor] Add DML Statement Analyzer to unify some dml analyze logical #53974

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

HangyuanLiu
Copy link
Contributor

@HangyuanLiu HangyuanLiu commented Dec 16, 2024

Why I'm doing:

There is some analyze logic that exists in all DML Statements. Here is a common abstracted visitor

What I'm doing:

Fixes #issue

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

@HangyuanLiu HangyuanLiu requested a review from a team as a code owner December 16, 2024 08:42
DMLStmtAnalyzer.analyze(statement, context);
return null;
}

// ------------------------------------------- Cluster Management Statement ----------------------------------------

@Override
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:
The duplicate handling of the visitInsertStatement, visitUpdateStatement, and visitDeleteStatement methods using two different analyzers (InsertAnalyzer, UpdateAnalyzer, DeleteAnalyzer vs. DMLStmtAnalyzer) might lead to inconsistent behavior or the incorrect analyzer being used due to potential conflicts.

You can modify the code like this:

// Remove duplicates:
// The old insert statement handler is outdated; ensure only one consistent handler is retained.
@Override
public Void visitSubmitTaskStatement(SubmitTaskStmt statement, ConnectContext context) {
    taskStmt = queryStatement;
} else if (statement.getInsertStmt() != null) {
    InsertStmt insertStmt = statement.getInsertStmt();
    DMLStmtAnalyzer.analyze(insertStmt, context); // Adjusted for consistency
    taskStmt = insertStmt;
}

// Keep DML Statement section using DMLStmtAnalyzer as intended.
// Remove previous overridden handlers to avoid conflicts:
// No change needed; duplicates already appear removed in current version.

return null;
}
}
}
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:
The DMLStmtAnalyzerVisitor class doesn't properly implement all methods from the AstVisitor interface, which might lead to the visit method not being called as expected for DmlStmt.

You can modify the code like this:

static class DMLStmtAnalyzerVisitor implements AstVisitor<Void, ConnectContext> {
    public void analyze(DmlStmt dmlStmt, ConnectContext context) {
        dmlStmt.getTableName().normalization(context);
        visit(dmlStmt, context);
    }

    @Override
    public Void visitInsertStatement(InsertStmt stmt, ConnectContext context) {
        InsertAnalyzer.analyze(stmt, context);
        return null;
    }

    @Override
    public Void visitUpdateStatement(UpdateStmt stmt, ConnectContext context) {
        UpdateAnalyzer.analyze(stmt, context);
        return null;
    }

    @Override
    public Void visitDeleteStatement(DeleteStmt stmt, ConnectContext context) {
        DeleteAnalyzer.analyze(stmt, context);
        return null;
    }

    @Override
    public Void visit(DmlStmt dmlStmt, ConnectContext context) {
        // Handle default visit behavior or throw an error if needed.
        throw new UnsupportedOperationException("Visit method not implemented for this statement type");
    }
}

This modification adds a generic visit method to handle cases where specific visitor methods for other DmlStmt types are missing. Adjust it according to your actual use case or specification.

Copy link

sonarcloud bot commented Dec 16, 2024

Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 20 / 21 (95.24%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/DMLStmtAnalyzer.java 12 13 92.31% [24]
🔵 com/starrocks/sql/analyzer/PipeAnalyzer.java 1 1 100.00% []
🔵 com/starrocks/sql/analyzer/Analyzer.java 7 7 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@nshangyiming nshangyiming merged commit a852995 into StarRocks:main Dec 16, 2024
67 of 69 checks passed
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.

4 participants