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: Alter inverted index #5131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lyang24
Copy link
Collaborator

@lyang24 lyang24 commented Dec 10, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

!!! DO NOT LEAVE THIS BLOCK EMPTY !!!

Please explain IN DETAIL what the changes are in this PR and why they are needed:

  • Summarize your change (mandatory)
    This pr enables alter table x modify column set/ unset inverted index syntax.
  1. modify index proto has been refactored.
  2. parse support the new syntax with refactoring.
  3. metadata update with alter inverted index.
  • How does this PR work? Need a brief introduction for the changed logic (optional)
  • Describe clearly one logical change and avoid lazy messages (optional)
  • Describe any limitations of the current code (optional)

Checklist

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

Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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 Dec 10, 2024
@lyang24 lyang24 force-pushed the alter_inverted_index branch from 0313c65 to 9772667 Compare December 12, 2024 06:50
@lyang24 lyang24 changed the title feat: (wip) Alter inverted index feat: Alter inverted index Dec 12, 2024
@lyang24 lyang24 force-pushed the alter_inverted_index branch 3 times, most recently from 6bc72b7 to a9f433b Compare December 12, 2024 07:10
@lyang24 lyang24 marked this pull request as ready for review December 12, 2024 07:10
@lyang24 lyang24 requested a review from CookiePieWw December 12, 2024 07:10
Copy link

codecov bot commented Dec 12, 2024

Codecov Report

Attention: Patch coverage is 66.91042% with 181 lines in your changes missing coverage. Please review.

Project coverage is 83.88%. Comparing base (c305b2b) to head (b3c8d8b).
Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5131      +/-   ##
==========================================
- Coverage   84.14%   83.88%   -0.27%     
==========================================
  Files        1200     1181      -19     
  Lines      225414   221353    -4061     
==========================================
- Hits       189682   185676    -4006     
+ Misses      35732    35677      -55     

@waynexia waynexia requested review from zhongzc and Copilot December 12, 2024 12:59

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 16 changed files in this pull request and generated no suggestions.

Files not reviewed (10)
  • tests/cases/standalone/common/alter/change_col_inverted_index.result: Language not supported
  • tests/cases/standalone/common/alter/change_col_inverted_index.sql: Language not supported
  • src/store-api/src/metadata.rs: Evaluated as low risk
  • src/common/grpc-expr/src/alter.rs: Evaluated as low risk
  • Cargo.toml: Evaluated as low risk
  • src/table/src/requests.rs: Evaluated as low risk
  • src/table/src/metadata.rs: Evaluated as low risk
  • src/store-api/src/region_request.rs: Evaluated as low risk
  • src/common/meta/src/ddl/alter_table/region_request.rs: Evaluated as low risk
  • src/common/meta/src/ddl/alter_table/update_metadata.rs: Evaluated as low risk
Comments skipped due to low confidence (4)

src/sql/src/parsers/alter_parser.rs:277

  • The error message should clearly indicate that the parser was expecting either FULLTEXT or INVERTED INDEX. Consider changing it to 'Expected FULLTEXT or INVERTED INDEX after ALTER TABLE MODIFY COLUMN'.
self.expected(format!("{:?} OR INVERTED INDEX", Keyword::FULLTEXT).as_str(), self.parser.peek_token(),)

src/sql/src/parsers/alter_parser.rs:304

  • The error message should clearly indicate that the parser was expecting either FULLTEXT or INVERTED INDEX. Consider changing it to 'Expected FULLTEXT or INVERTED INDEX after ALTER TABLE MODIFY COLUMN'.
self.expected(format!("{:?} OR INVERTED INDEX", Keyword::FULLTEXT).as_str(), self.parser.peek_token(),)

src/operator/src/expr_factory.rs:543

  • [nitpick] The name 'SetIndex' is not very descriptive. Consider renaming it to something more specific, like 'SetColumnIndex'.
AlterTableOperation::SetIndex { options } => AlterTableKind::SetIndex(match options {

src/operator/src/expr_factory.rs:564

  • [nitpick] The name 'UnsetIndex' is not very descriptive. Consider renaming it to something more specific, like 'UnsetColumnIndex'.
AlterTableOperation::UnsetIndex { options } => AlterTableKind::UnsetIndex(match options {
Copy link
Collaborator

@CookiePieWw CookiePieWw left a comment

Choose a reason for hiding this comment

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

Thanks for your nice work! Left some nit comments.

I wonder if the behaviour of changing inverted index is the same as fulltext index: disabling it would only stop constructing new indexes, and the previously contructed indexes will still take effect? @zhongzc

src/sql/src/parsers/alter_parser.rs Outdated Show resolved Hide resolved
src/common/grpc-expr/src/alter.rs Outdated Show resolved Hide resolved
src/common/grpc-expr/src/alter.rs Outdated Show resolved Hide resolved
src/store-api/src/region_request.rs Outdated Show resolved Hide resolved
src/store-api/src/region_request.rs Show resolved Hide resolved
src/store-api/src/region_request.rs Show resolved Hide resolved
src/datatypes/src/schema/column_schema.rs Outdated Show resolved Hide resolved
@waynexia
Copy link
Member

FYI: #5178

This issue is related to the PR, in case anyone might be interested in it.

@lyang24 lyang24 force-pushed the alter_inverted_index branch 2 times, most recently from 24a3461 to 32c4d3b Compare January 7, 2025 05:26
@lyang24 lyang24 requested a review from CookiePieWw January 7, 2025 05:27
src/table/src/metadata.rs Outdated Show resolved Hide resolved
}
);

let mut columns = Vec::with_capacity(table_schema.column_schemas().len());
Copy link
Contributor

Choose a reason for hiding this comment

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

Compatibility needs to be considered. If all column schemas do not have an inverted key set, that is, schemas.all(!has_inverted_index_key), then columns in the primary key will be inverted indexed.

See:

// Default to use primary key columns as inverted index columns.
let pk_as_inverted_index = !self
.column_metadatas
.iter()
.any(|c| c.column_schema.has_inverted_index_key());
let mut inverted_index: HashSet<_> = if pk_as_inverted_index {
self.primary_key_columns().map(|c| c.column_id).collect()
} else {
self.column_metadatas
.iter()
.filter(|column| column.column_schema.is_inverted_indexed())
.map(|column| column.column_id)
.collect()
};

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thank you, I am trying to see what would go wrong here.

Are we worried about the case when remove invert index and accidentally build inverted index on primary keys? when we alter table remove inverted index it will set the inverted index value to false in column metadata - i think that could prevent the case as is_inverted_indexed check if the value is true.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me try to explain this.

When the condition schemas.all(!has_inverted_index_key) is met, the columns in the primary key implicitly have a shadow attribute: is_inverted_index. However, this attribute is not observable from the column schema.

Once SET | UNSET INVERTED INDEX disrupts the condition schemas.all(!has_inverted_index_key), we need to materialize the shadow attribute.

Copy link
Contributor

Choose a reason for hiding this comment

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

My explanation might have become more abstract.

In practice, when we set an inverted index for a schema that previously had no columns with an inverted index set, we need to reset inverted_indexed=true for the columns in the primary key.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see the primary key will be inverted index if there are no inverted index on the table, so when we add a new inverted index we need to set the inverted index = true for primary keys as well.

@zhongzc
Copy link
Contributor

zhongzc commented Jan 7, 2025

Thanks for your nice work! Left some nit comments.

I wonder if the behaviour of changing inverted index is the same as fulltext index: disabling it would only stop constructing new indexes, and the previously contructed indexes will still take effect? @zhongzc

Yes, we can think that alter index only modifies the column schema, leaving other things to the engine.

@lyang24 lyang24 force-pushed the alter_inverted_index branch from 32c4d3b to 61abba3 Compare January 8, 2025 06:07
@lyang24 lyang24 force-pushed the alter_inverted_index branch from 61abba3 to 184b0d2 Compare January 9, 2025 02:19

Affected Rows: 0

SHOW INDEX FROM test_pk;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not sure if this is the result we are expecting, nothing changes when we set/ unset inverted index on primary keys that comes with inverted index.

Copy link
Collaborator

@CookiePieWw CookiePieWw Jan 9, 2025

Choose a reason for hiding this comment

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

The table has two primary keys with no explicit inverted index, so is_inverted_indexed and has_inverted_index_key for primary keys will return false. So that SHOW INDEX finds no inverted index in the table and add primary keys into the inverted index sets, see:

let pk_as_inverted_index = !schema
.column_schemas()
.iter()
.any(|c| c.has_inverted_index_key());

Seems we need to set inverted_indexed=true for column bar when we unset inverted index for column foo to get the expected result. When it comes to unset inverted index in all columns, seems it's impossible? cc @zhongzc

Copy link
Collaborator Author

@lyang24 lyang24 Jan 9, 2025

Choose a reason for hiding this comment

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

Seems we need to set inverted_indexed=true for column bar when we unset inverted index for column foo to get the expected result.

thanks for pointing out the logic, i had a follow up when we remove foo in the next iteration there will be no inverted index on the table - do we expecting primary key added as inverted index again?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, we expect all primary key shows no inverted index in the query, ref #5131 (comment):

CREATE TABLE test_pk (..., foo STRING, bar INT, PRIMARY KEY (foo, bar));
SHOW INDEX; // inverted indexes: [foo, bar]

UNSET foo;
SHOW INDEX; // inverted indexes: [bar]

UNSET bar;
SHOW INDEX; // inverted indexes: []

SET foo;
SHOW INDEX; // inverted indexes: [foo]

There're two cases:

  1. All primary key are marked non-inverted index but we implicitly build inverted index for primary keys. We should show them as inverted index columns.
  2. All primary key are marked non-inverted index, and it's explicitly unset by users. We don't need to show them as inverted index columns.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks i missed the expected result from previous comment. I added logic to make the results converge but the logic is not straight forward :(

@lyang24 lyang24 force-pushed the alter_inverted_index branch from 184b0d2 to b3c8d8b Compare January 10, 2025 05:00
columns.push(new_column_schema);
} else {
let mut new_column_schema = column_schema.clone();
new_column_schema.update_inverted_index(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: replace set_inverted_index and update_inverted_index with with_inverted_index(mut self, value) -> Self, and remove the key when value is false.

Then, use insert_inverted_index_placeholder instead here:

column_schema = column_schema.set_inverted_index(false);

@zhongzc
Copy link
Contributor

zhongzc commented Jan 10, 2025

great job!

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.

4 participants