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

fix!: forbid to change information_schema #4233

Merged
merged 4 commits into from
Jul 3, 2024

Conversation

CookiePieWw
Copy link
Collaborator

@CookiePieWw CookiePieWw commented Jun 30, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

close #3653

What's changed and what's your intention?

As title. Following ops in information_schema are forbidden in this patch:

  • create, drop and truncate tables
  • drop database
  • insert into/delete from tables

Updated some sqlness tests for it.

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

    • Added checks to prevent modifications to read-only schemas and tables, specifically targeting the information_schema.
  • Bug Fixes

    • Improved error handling to provide specific errors when attempting to modify read-only schemas and tables.
  • Refactor

    • Enhanced schema and table operation checks to ensure compliance with read-only constraints.

@CookiePieWw CookiePieWw requested review from evenyag and a team as code owners June 30, 2024 16:41
@CookiePieWw CookiePieWw added the breaking-change This pull request contains breaking changes. label Jun 30, 2024
@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jun 30, 2024
Copy link
Contributor

coderabbitai bot commented Jun 30, 2024

Walkthrough

The recent changes introduce checks to ensure that operations on read-only schemas and tables, specifically within the "information_schema," result in user-friendly error messages rather than internal errors. These checks have been added across several modules, enhancing user experience by preventing unwanted modifications and providing descriptive error messages.

Changes

File Path Change Summary
src/common/catalog/src/consts.rs Added is_readonly_schema function to check if a schema is read-only.
src/operator/src/req_convert/insert/stmt_to_region.rs Added a check to prevent operations on "information_schema" and include SchemaReadOnlySnafu error.
src/operator/src/statement/ddl.rs Integrated checks to disallow operations on "information_schema" in multiple StatementExecutor functions, adding SchemaReadOnlySnafu errors.
src/query/src/datafusion.rs Implemented checks for read-only tables in the DatafusionQueryEngine, introducing TableReadOnlySnafu errors.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Application
    participant QueryEngine
    participant Catalog
    participant SchemaRegistry

    User->>Application: Perform operation on information_schema
    Application->>QueryEngine: Forward request
    QueryEngine->>Catalog: Check if schema/table is read-only
    Catalog-->>QueryEngine: Schema/table is read-only
    QueryEngine-->>Application: Return SchemaReadOnlySnafu / TableReadOnlySnafu error
    Application-->>User: Display user-friendly error message
Loading

Assessment against linked issues

Objective Addressed Explanation
Improve error message for read-only tables (3653)

Poem

In the code where data flows,
A rabbit hopped and gently shows,
Errors now more friendly made,
No more fears of updates staid.
Schema checks in place now be,
Just for you, from me, Code Bunny.🐰


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

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.

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 ddc7a80 and 1c17d83.

Files selected for processing (11)
  • src/operator/src/error.rs (2 hunks)
  • src/operator/src/req_convert/insert/stmt_to_region.rs (2 hunks)
  • src/operator/src/statement/ddl.rs (6 hunks)
  • src/query/src/datafusion.rs (3 hunks)
  • src/query/src/error.rs (2 hunks)
  • tests/cases/standalone/common/information_schema/region_peers.result (3 hunks)
  • tests/cases/standalone/common/information_schema/region_peers.sql (2 hunks)
  • tests/cases/standalone/common/information_schema/table_constraints.result (1 hunks)
  • tests/cases/standalone/common/information_schema/table_constraints.sql (1 hunks)
  • tests/cases/standalone/common/information_schema/tables.result (1 hunks)
  • tests/cases/standalone/common/information_schema/tables.sql (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/cases/standalone/common/information_schema/region_peers.sql
Additional comments not posted (22)
tests/cases/standalone/common/information_schema/tables.sql (1)

20-21: LGTM!

The addition of drop schema information_schema; correctly tests the new restriction on dropping the information_schema schema.

tests/cases/standalone/common/information_schema/table_constraints.sql (1)

9-10: LGTM!

The changes to switch between INFORMATION_SCHEMA and public schemas correctly test the new restrictions on operations within the information_schema schema.

Also applies to: 13-14, 19-21

tests/cases/standalone/common/information_schema/tables.result (1)

47-50: LGTM!

The addition of the expected error message for attempting to drop the information_schema schema correctly verifies the new restriction.

tests/cases/standalone/common/information_schema/region_peers.result (1)

2-2: LGTM!

The changes to switch between public and INFORMATION_SCHEMA schemas correctly test the new restrictions on operations within the information_schema schema.

Also applies to: 38-39, 50-54

tests/cases/standalone/common/information_schema/table_constraints.result (4)

28-29: Ensure correct schema usage.

Switching to the public schema after querying INFORMATION_SCHEMA is consistent with the new restrictions. Verify if this change aligns with the expected behavior.


36-37: Ensure correct schema usage.

Switching back to INFORMATION_SCHEMA for further queries is consistent with the new restrictions. Verify if this change aligns with the expected behavior.


52-57: Validate constraint query results.

Ensure that the constraints for the test table are correctly displayed after switching schemas. This aligns with the new restrictions.


63-63: Ensure proper cleanup.

Dropping the test table in the public schema is consistent with the overall schema management. Verify if this aligns with the expected behavior.

src/operator/src/req_convert/insert/stmt_to_region.rs (2)

32-32: New error variant added.

The SchemaReadOnlySnafu error variant is added to handle read-only schema errors. This aligns with the PR objectives.


68-71: Restrict operations on information_schema.

The added check ensures that operations on the information_schema schema are restricted. This aligns with the PR objectives.

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

310-316: New error variant added.

The TableReadOnly error variant is added to handle read-only table errors. This aligns with the PR objectives.


330-330: Map new error variant to status code.

The TableReadOnly error variant is mapped to the InvalidArguments status code. This aligns with the PR objectives.

src/query/src/datafusion.rs (3)

53-53: New error variant added.

The TableReadOnlySnafu error variant is added to handle read-only table errors. This aligns with the PR objectives.


177-180: Restrict delete operations on information_schema.

The added check ensures that delete operations on the information_schema schema are restricted. This aligns with the PR objectives.


225-228: Restrict insert operations on information_schema.

The added check ensures that insert operations on the information_schema schema are restricted. This aligns with the PR objectives.

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

272-277: New error variant added: SchemaReadOnly.

This new error variant will help in identifying and handling operations on read-only schemas like information_schema.


728-728: New error variant SchemaReadOnly mapped to InvalidArguments status code.

This mapping ensures that operations on read-only schemas are correctly classified as invalid arguments.

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

75-76: New error handling added: SchemaReadOnlySnafu.

This error handling will ensure that operations on read-only schemas like information_schema are correctly identified and handled.


154-159: Check added to prevent table creation in information_schema schema.

The ensure! macro is used to validate the schema name, raising a SchemaReadOnlySnafu error if the schema is information_schema. This enforces the read-only status of the schema.


655-660: Check added to prevent dropping tables in information_schema schema.

The ensure! macro is used to validate the schema name, raising a SchemaReadOnlySnafu error if the schema is information_schema. This ensures that tables in the read-only schema cannot be dropped.


711-715: Check added to prevent dropping the information_schema schema.

The ensure! macro is used to validate the schema name, raising a SchemaReadOnlySnafu error if the schema is information_schema. This ensures that the read-only schema cannot be dropped.


747-753: Check added to prevent truncating tables in information_schema schema.

The ensure! macro is used to validate the schema name, raising a SchemaReadOnlySnafu error if the schema is information_schema. This ensures that tables in the read-only schema cannot be truncated.

Copy link

codecov bot commented Jun 30, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.70%. Comparing base (a7aa556) to head (3f545b7).
Report is 19 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4233      +/-   ##
==========================================
- Coverage   84.86%   84.70%   -0.17%     
==========================================
  Files        1045     1054       +9     
  Lines      184265   187005    +2740     
==========================================
+ Hits       156384   158396    +2012     
- Misses      27881    28609     +728     

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 1c17d83 and 6973f07.

Files selected for processing (9)
  • src/catalog/src/consts.rs (1 hunks)
  • src/catalog/src/lib.rs (1 hunks)
  • src/operator/src/error.rs (2 hunks)
  • src/operator/src/req_convert/insert/stmt_to_region.rs (2 hunks)
  • src/operator/src/statement/ddl.rs (7 hunks)
  • src/query/src/datafusion.rs (4 hunks)
  • src/query/src/error.rs (2 hunks)
  • tests/cases/standalone/common/information_schema/tables.result (1 hunks)
  • tests/cases/standalone/common/information_schema/tables.sql (1 hunks)
Files not summarized due to errors (2)
  • src/query/src/datafusion.rs: Error: Server error. Please try again later.
  • src/query/src/error.rs: Error: Server error. Please try again later.
Files skipped from review due to trivial changes (2)
  • src/catalog/src/lib.rs
  • src/operator/src/error.rs
Files skipped from review as they are similar to previous changes (4)
  • src/operator/src/req_convert/insert/stmt_to_region.rs
  • src/operator/src/statement/ddl.rs
  • tests/cases/standalone/common/information_schema/tables.result
  • tests/cases/standalone/common/information_schema/tables.sql
Additional comments not posted (6)
src/catalog/src/consts.rs (1)

17-19: LGTM! Consider future extensibility.

The function correctly checks if the schema is INFORMATION_SCHEMA_NAME. If more read-only schemas are added in the future, consider extending this function to handle them.

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

311-316: LGTM! New error variant TableReadOnly is well-defined.

The new error variant TableReadOnly includes the table name and location, which aligns with the objective of providing more informative error messages.


366-366: LGTM! Appropriate status code mapping for TableReadOnly.

The status code mapping for TableReadOnly to StatusCode::Unsupported is appropriate.

src/query/src/datafusion.rs (3)

25-25: LGTM! Necessary import statement for is_readonly_schema.

The import statement for is_readonly_schema is correctly placed and necessary for the new functionality.


177-181: LGTM! Correct check for read-only schema in delete method.

The delete method correctly uses the is_readonly_schema function to ensure the schema is not read-only and raises an appropriate error if it is.


222-230: LGTM! Correct check for read-only schema in insert method.

The insert method correctly uses the is_readonly_schema function to ensure the schema is not read-only and raises an appropriate error if it is.

@CookiePieWw
Copy link
Collaborator Author

Updated with a read-only check function, error code and some sqlness tests. PTAL @killme2008

I forgot to sync the clock before commit, so it may be a little strange that f336170 is ahead of the comments QaQ.

Copy link
Contributor

@killme2008 killme2008 left a comment

Choose a reason for hiding this comment

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

Almost done, thank you.

src/catalog/src/consts.rs Outdated Show resolved Hide resolved
src/operator/src/req_convert/insert/stmt_to_region.rs Outdated Show resolved Hide resolved
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 6973f07 and 3f545b7.

Files selected for processing (4)
  • src/common/catalog/src/consts.rs (1 hunks)
  • src/operator/src/req_convert/insert/stmt_to_region.rs (2 hunks)
  • src/operator/src/statement/ddl.rs (7 hunks)
  • src/query/src/datafusion.rs (4 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/operator/src/req_convert/insert/stmt_to_region.rs
  • src/operator/src/statement/ddl.rs
  • src/query/src/datafusion.rs
Additional comments not posted (1)
src/common/catalog/src/consts.rs (1)

112-114: LGTM!

The function is_readonly_schema correctly checks if a schema name matches INFORMATION_SCHEMA_NAME using the matches! macro.

@CookiePieWw CookiePieWw requested a review from killme2008 July 2, 2024 15:31
Copy link
Contributor

@killme2008 killme2008 left a comment

Choose a reason for hiding this comment

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

LGTM

@killme2008 killme2008 requested review from fengjiachun and sunng87 July 2, 2024 18:46
Copy link
Member

@sunng87 sunng87 left a comment

Choose a reason for hiding this comment

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

This looks good for now. But eventually we need to implement this based on our permission system @MichaelScofield

@sunng87 sunng87 enabled auto-merge July 3, 2024 01:35
@sunng87 sunng87 added this pull request to the merge queue Jul 3, 2024
Merged via the queue into GreptimeTeam:main with commit c4db9e8 Jul 3, 2024
52 checks passed
@CookiePieWw CookiePieWw deleted the read-only-schema-hint branch July 4, 2024 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This pull request contains breaking changes. docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Readonly table error message should not be Internal Error
3 participants