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

feat: impl drop view #4231

Merged
merged 12 commits into from
Jul 11, 2024
Merged

Conversation

killme2008
Copy link
Contributor

@killme2008 killme2008 commented Jun 28, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

#3729

What's changed and what's your intention?

Supports DROP VIEW statement:

mysql> CREATE VIEW v1 as SELECT * FROM numbers LIMIT 10;
Query OK, 0 rows affected (0.03 sec)

mysql> SELECT * FROM v1;
+--------+
| number |
+--------+
|      0 |
|      1 |
|      2 |
|      3 |
|      4 |
|      5 |
|      6 |
|      7 |
|      8 |
|      9 |
+--------+
10 rows in set (0.02 sec)

mysql> DROP VIEW v1;
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM v1;
ERROR 1815 (HY000): Failed to plan SQL: Error during planning: Table not found: greptime.public.v1

Main changes:

  • Adds DropViewProcedure to drop view.
  • Implements parser for DROP VIEW [IF EXISTS] view statement.
  • Updated the sqlness tests to use drop view.

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

Summary by CodeRabbit

  • New Features

    • Implemented the ability to drop views in the database system.
    • Enhanced error handling with new error variants and updated match patterns.
    • Added new metrics for drop view procedures.
  • Bug Fixes

    • Corrected SQL scripts and test cases to align with new view-dropping functionality.
  • Refactor

    • Updated constant values in the DropFlowProcedure struct to ensure consistency.
  • Tests

    • Added comprehensive test cases for dropping views, including scenarios for non-existent views and successful drops.

Copy link
Contributor

coderabbitai bot commented Jun 28, 2024

Walkthrough

The changes introduce functionality for dropping views in a database system. Key modifications include the addition of the DropViewProcedure struct, its associated methods, and state management for this procedure. New test cases and updated error handling are also included. Additionally, various SQL scripts and tests have been adapted to reflect these changes.

Changes

Files & Paths Change Summary
src/common/meta/src/ddl/drop_view.rs, ... Introduced DropViewProcedure with methods for managing the drop view process, including state handling and metadata management.
src/common/meta/src/ddl/tests.rs, ... Added a new test module for dropping views, including various scenarios and success cases.
src/common/meta/src/ddl_manager.rs Added logic to handle and submit DropView tasks within DdlManager and ProcedureExecutor.
src/common/meta/src/error.rs, ... Updated error handling logic to include new variants for DropView operations.
src/common/meta/src/metrics.rs Added a new metric for the drop view procedure.
src/common/meta/src/rpc/ddl.rs Expanded the DdlTask enum to support drop view operations, along with other DDL operations like create, alter, and drop for various entities.
src/frontend/src/instance.rs Added validation for the DropView statement's view_name parameter.
tests/cases/standalone/common/view/columns.result Modified SQL scripts and expected results to align with changes in drop view functionality, including removing database-level operations and focusing on view and table manipulations.
tests/cases/standalone/common/view/columns.sql, ... Revised scripts to reflect new drop view functionality, including removing database operations and focusing on view and table manipulations.
src/common/meta/src/ddl/drop_flow.rs Updated the TYPE_NAME constant value within the DropFlowProcedure struct for consistency.

Poem

In the code, new paths we weave,
Dropping views with such ease,
Errors handled, tests anew,
Metrics count what's tried and true.
SQL scripts now light and free,
Our database sings in harmony.
🌟✨🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jun 28, 2024
@killme2008 killme2008 marked this pull request as ready for review July 9, 2024 20:42
@killme2008 killme2008 requested review from MichaelScofield and a team as code owners July 9, 2024 20:42
@killme2008 killme2008 requested review from WenyXu, waynexia and sunng87 and removed request for a team and MichaelScofield July 9, 2024 20:42
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 17

Outside diff range and nitpick comments (10)
src/common/meta/src/ddl/drop_view.rs (6)

38-43: Add visibility modifier for DropViewProcedure fields.

The fields context and data are marked as pub(crate). Consider making them private and using getters if needed.

-    pub(crate) context: DdlContext,
-    pub(crate) data: DropViewData,
+    context: DdlContext,
+    data: DropViewData,

45-57: Add documentation for DropViewProcedure methods.

The methods in the DropViewProcedure struct lack documentation. Adding doc comments will improve code readability and maintainability.

/// Creates a new `DropViewProcedure`.

65-68: Remove unnecessary test-specific code from production code.

The state method is only used in tests. Consider using #[cfg(test)] to conditionally compile this method only for tests.

#[cfg(test)]
impl DropViewProcedure {
    pub(crate) fn state(&self) -> DropViewState {
        self.data.state
    }
}

139-150: Add logging for on_delete_metadata.

Adding logging will help in debugging and monitoring the deletion process.

info!("Deleting metadata for view {view_id}");

210-226: Add documentation for DropViewData methods.

The methods in the DropViewData struct lack documentation. Adding doc comments will improve code readability and maintainability.

/// Returns a reference to the table.

228-237: Add documentation for DropViewState variants.

The variants in the DropViewState enum lack documentation. Adding doc comments will improve code readability and maintainability.

/// Prepares to drop the view.
src/sql/src/statements/drop.rs (2)

140-147: Add documentation for DropView struct and fields.

The DropView struct and its fields lack documentation. Adding doc comments will improve code readability and maintainability.

/// Represents a `DROP VIEW` statement.

Line range hint 293-340: Add more test cases for DropView.

Consider adding more test cases to cover additional edge cases, such as invalid view names and missing keywords.

#[test]
pub fn test_drop_view_invalid_name() {
    let sql = "DROP VIEW ";
    let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
    assert!(result.is_err());
}

#[test]
pub fn test_drop_view_missing_keyword() {
    let sql = "DROP foo";
    let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
    assert!(result.is_err());
}
src/common/meta/src/ddl/tests/create_view.rs (1)

Line range hint 47-278: Add more test cases for CreateViewProcedure.

Consider adding more test cases to cover additional edge cases, such as invalid view names and missing keywords.

#[tokio::test]
async fn test_create_view_invalid_name() {
    let node_manager = Arc::new(MockDatanodeManager::new(()));
    let ddl_context = new_ddl_context(node_manager);
    let cluster_id = 1;
    let task = test_create_view_task("");
    let mut procedure = CreateViewProcedure::new(cluster_id, task, ddl_context);
    let err = procedure.on_prepare().await.unwrap_err();
    assert_matches!(err, Error::InvalidViewName { .. });
}

#[tokio::test]
async fn test_create_view_missing_keyword() {
    let node_manager = Arc::new(MockDatanodeManager::new(()));
    let ddl_context = new_ddl_context(node_manager);
    let cluster_id = 1;
    let task = test_create_view_task("foo");
    let mut procedure = CreateViewProcedure::new(cluster_id, task, ddl_context);
    let err = procedure.on_prepare().await.unwrap_err();
    assert_matches!(err, Error::MissingKeyword { .. });
}
src/sql/src/parsers/drop_parser.rs (1)

293-340: Add more test cases for parse_drop_view.

Consider adding more test cases to cover additional edge cases, such as invalid view names and missing keywords.

#[test]
pub fn test_parse_drop_view_invalid_name() {
    let sql = "DROP VIEW ";
    let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
    assert!(result.is_err());
}

#[test]
pub fn test_parse_drop_view_missing_keyword() {
    let sql = "DROP foo";
    let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
    assert!(result.is_err());
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 33ed745 and 75d3e58.

Files selected for processing (24)
  • src/common/meta/src/ddl.rs (1 hunks)
  • src/common/meta/src/ddl/drop_view.rs (1 hunks)
  • src/common/meta/src/ddl/tests.rs (1 hunks)
  • src/common/meta/src/ddl/tests/create_view.rs (1 hunks)
  • src/common/meta/src/ddl/tests/drop_view.rs (1 hunks)
  • src/common/meta/src/ddl_manager.rs (7 hunks)
  • src/common/meta/src/error.rs (2 hunks)
  • src/common/meta/src/metrics.rs (1 hunks)
  • src/common/meta/src/rpc/ddl.rs (11 hunks)
  • src/frontend/src/instance.rs (1 hunks)
  • src/operator/src/error.rs (2 hunks)
  • src/operator/src/statement.rs (1 hunks)
  • src/operator/src/statement/ddl.rs (2 hunks)
  • src/sql/src/parsers/drop_parser.rs (5 hunks)
  • src/sql/src/statements/drop.rs (1 hunks)
  • src/sql/src/statements/statement.rs (3 hunks)
  • tests/cases/standalone/common/view/columns.result (2 hunks)
  • tests/cases/standalone/common/view/columns.sql (2 hunks)
  • tests/cases/standalone/common/view/create.result (5 hunks)
  • tests/cases/standalone/common/view/create.sql (2 hunks)
  • tests/cases/standalone/common/view/show_create.result (2 hunks)
  • tests/cases/standalone/common/view/show_create.sql (2 hunks)
  • tests/cases/standalone/common/view/view.result (2 hunks)
  • tests/cases/standalone/common/view/view.sql (2 hunks)
Files skipped from review due to trivial changes (4)
  • src/common/meta/src/ddl.rs
  • src/common/meta/src/ddl/tests.rs
  • tests/cases/standalone/common/view/columns.result
  • tests/cases/standalone/common/view/columns.sql
Additional comments not posted (42)
tests/cases/standalone/common/view/show_create.sql (1)

41-41: Clarify the purpose of this statement.

The SHOW CREATE VIEW v1; statement is used to display the SQL statement that creates the view v1. However, this view was dropped in line 37. Ensure that this is the intended behavior or add comments to clarify the purpose.

tests/cases/standalone/common/view/view.sql (1)

35-35: Clarify the purpose of this statement.

The SELECT * FROM v1; statement is used to retrieve data from the view v1. However, this view was dropped in line 33. Ensure that this is the intended behavior or add comments to clarify the purpose.

tests/cases/standalone/common/view/create.sql (1)

49-49: Clarify the purpose of this statement.

The SELECT * FROM test_view LIMIT 10; statement is used to retrieve data from the view test_view. However, this view was dropped in line 45. Ensure that this is the intended behavior or add comments to clarify the purpose.

tests/cases/standalone/common/view/view.result (1)

59-61: Clarify the purpose of this statement.

The SELECT * FROM v1; statement is used to retrieve data from the view v1. However, this view was dropped in line 55. Ensure that this is the intended behavior or add comments to clarify the purpose.

tests/cases/standalone/common/view/show_create.result (3)

121-122: Ensure correctness of the DROP VIEW statement.

The DROP VIEW v1; statement should drop the view v1. Verify that the view v1 is correctly dropped and that no errors occur.


125-126: Ensure correctness of the DROP TABLE statement.

The DROP TABLE t1; statement should drop the table t1. Verify that the table t1 is correctly dropped and that no errors occur.


129-132: Verify error handling for SHOW CREATE VIEW after drop.

The SHOW CREATE VIEW v1; statement should result in an error since the view v1 was dropped. Ensure that the error message is correct and that it indicates the view was not found.

src/common/meta/src/metrics.rs (1)

58-63: Add a new histogram metric for DROP VIEW procedure.

The new histogram metric METRIC_META_PROCEDURE_DROP_VIEW should follow the same pattern as the existing metrics. Ensure it is correctly defined and registered.

src/sql/src/statements/statement.rs (2)

28-28: Add DropView variant to Statement enum.

The DropView variant should be correctly added to the Statement enum following the existing pattern.


64-67: Add DropView variant to Display implementation.

The DropView variant should be correctly handled in the Display implementation for the Statement enum.

src/common/meta/src/ddl/tests/drop_view.rs (5)

30-38: Add helper function new_drop_view_task.

The new_drop_view_task helper function should correctly create a DropViewTask with the provided parameters.


40-66: Add test test_on_prepare_view_not_exists_err.

The test_on_prepare_view_not_exists_err test function should correctly handle the case where the view does not exist and return the appropriate error.


68-92: Add test test_on_prepare_not_view_err.

The test_on_prepare_not_view_err test function should correctly handle the case where the object is not a view and return the appropriate error.


94-127: Add test test_on_prepare_success.

The test_on_prepare_success test function should correctly handle the successful preparation of the DropViewProcedure.


129-180: Add test test_drop_view_success.

The test_drop_view_success test function should correctly handle the successful execution of the DropViewProcedure and verify that the view is dropped.

src/common/meta/src/ddl/tests/create_view.rs (1)

Line range hint 35-41: LGTM!

The test_table_names function is straightforward and does not require changes.

tests/cases/standalone/common/view/create.result (10)

17-17: Ensure the error message for existing table is accurate.

The error message indicates that the table already exists. Verify that this is the expected behavior for the CREATE VIEW statement.


22-22: Verify error message for CREATE VIEW IF NOT EXISTS.

The error message should correctly indicate that the table already exists even when using IF NOT EXISTS.


27-27: Check error message for CREATE OR REPLACE VIEW.

The error message indicates that the table already exists. Confirm that this is the expected behavior for the CREATE OR REPLACE VIEW statement.


36-36: Validate error message for existing view.

The error message should accurately reflect that the view already exists.


51-64: Review SHOW FULL TABLES results.

The SHOW FULL TABLES command should list all tables and their types, including views. Ensure that the results are accurate.


70-105: Validate INFORMATION_SCHEMA.TABLES output.

The output should correctly display the details of tables, including views, in the INFORMATION_SCHEMA.TABLES table.


110-114: Check INFORMATION_SCHEMA.TABLES for views.

Ensure that the output accurately lists views in the INFORMATION_SCHEMA.TABLES table.


148-148: Verify successful DROP VIEW operation.

The DROP VIEW command should execute successfully and the affected rows should be 0.


152-152: Confirm successful DROP TABLE operation.

The DROP TABLE command should execute successfully and the affected rows should be 0.


158-158: Validate error message for querying dropped view.

The error message should correctly indicate that the view does not exist after being dropped.

src/operator/src/statement.rs (1)

195-209: Validate DropView statement handling.

The DropView statement handling includes breaking down the view name into catalog, schema, and view name components and invoking the drop_view method. Ensure that:

  • The table_idents_to_full_name function is correctly used.
  • Proper error handling is in place.
  • The drop_view method is correctly called with the appropriate parameters.
src/common/meta/src/error.rs (1)

708-708: Verify status code for InvalidViewInfo error.

Ensure that the InvalidViewInfo error is correctly categorized under StatusCode::InvalidArguments.

src/frontend/src/instance.rs (1)

494-496: Validate DropView statement validation.

Ensure that the validate_param function is correctly used to validate the view name in the DropView statement.

src/operator/src/error.rs (2)

158-161: New error variant InvalidView added.

The addition of the InvalidView error variant is correct and consistent with other error variants.


786-786: Integration of InvalidView error variant into status_code method.

The integration of the InvalidView error variant into the status_code method is appropriate and consistent with other similar error variants.

src/common/meta/src/ddl_manager.rs (5)

36-36: Import DropViewProcedure added.

The import of DropViewProcedure is correct and necessary for the changes.


135-136: Register DropViewProcedure loader.

The registration of the DropViewProcedure loader in the register_loaders method is correct and consistent with other similar procedures.


325-337: New method submit_drop_view_task added.

The submit_drop_view_task method is implemented correctly and follows the pattern of similar methods.


618-638: New function handle_drop_view_task added.

The handle_drop_view_task function is implemented correctly and follows the pattern of similar functions.


791-792: Integration of DropView task handling into submit_ddl_task method.

The integration of the DropView task handling into the submit_ddl_task method is correct and consistent with other similar task handling.

src/common/meta/src/rpc/ddl.rs (2)

77-80: LGTM! New function to create DdlTask for dropping a view.

The implementation follows the pattern used by other new_* functions in this file.


443-450: LGTM! Function to return TableReference of the view.

The implementation is straightforward and correct.

src/operator/src/statement/ddl.rs (4)

648-697: Ensure proper error handling for view not found scenario.

The method handles the scenario where the view is not found and drop_if_exists is false. Ensure that this scenario is tested.


675-682: Validate view type before dropping.

The method ensures that the entity being dropped is indeed a view. This is a good practice to prevent accidental deletion of other table types.


694-696: LGTM! Ensure the procedure executor handles the task correctly.

The drop view task is submitted to the procedure executor. Ensure that the procedure executor correctly handles this task.


699-714: Ensure proper error handling in the drop view procedure.

The method submits the drop view task to the procedure executor. Ensure that potential errors are correctly handled and logged.

tests/cases/standalone/common/view/view.sql Show resolved Hide resolved
tests/cases/standalone/common/view/view.sql Show resolved Hide resolved
tests/cases/standalone/common/view/view.sql Show resolved Hide resolved
src/common/meta/src/ddl/drop_view.rs Outdated Show resolved Hide resolved
src/common/meta/src/ddl/drop_view.rs Show resolved Hide resolved
src/common/meta/src/ddl/drop_view.rs Show resolved Hide resolved
src/sql/src/statements/drop.rs Outdated Show resolved Hide resolved
src/sql/src/parsers/drop_parser.rs Show resolved Hide resolved
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

Attention: Patch coverage is 68.29268% with 91 lines in your changes missing coverage. Please review.

Project coverage is 84.89%. Comparing base (7ad248d) to head (ae4d9a0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4231      +/-   ##
==========================================
- Coverage   85.19%   84.89%   -0.30%     
==========================================
  Files        1060     1061       +1     
  Lines      189165   189447     +282     
==========================================
- Hits       161151   160834     -317     
- Misses      28014    28613     +599     

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 75d3e58 and fe48696.

Files selected for processing (2)
  • src/common/meta/src/ddl/drop_view.rs (1 hunks)
  • src/sql/src/statements/drop.rs (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/common/meta/src/ddl/drop_view.rs
  • src/sql/src/statements/drop.rs

Copy link
Member

@WenyXu WenyXu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@fengjiachun fengjiachun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

src/common/meta/src/ddl/drop_view.rs Outdated Show resolved Hide resolved
src/common/meta/src/rpc/ddl.rs Outdated Show resolved Hide resolved
src/common/meta/src/rpc/ddl.rs Outdated Show resolved Hide resolved
killme2008 and others added 3 commits July 11, 2024 11:22
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fe48696 and f617b05.

Files selected for processing (2)
  • src/common/meta/src/ddl/drop_view.rs (1 hunks)
  • src/common/meta/src/rpc/ddl.rs (11 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/common/meta/src/ddl/drop_view.rs
  • src/common/meta/src/rpc/ddl.rs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f617b05 and ae4d9a0.

Files selected for processing (1)
  • src/common/meta/src/ddl/drop_flow.rs (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/common/meta/src/ddl/drop_flow.rs

@killme2008 killme2008 added this pull request to the merge queue Jul 11, 2024
Merged via the queue into GreptimeTeam:main with commit ab22bba Jul 11, 2024
55 checks passed
@killme2008 killme2008 deleted the feature/drop-view branch July 11, 2024 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants