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

copr: support more regexp functions #13480

Merged
merged 16 commits into from Sep 16, 2022
Merged

Conversation

gengliqi
Copy link
Member

@gengliqi gengliqi commented Sep 15, 2022

Signed-off-by: gengliqi gengliqiii@gmail.com

What is changed and how it works?

Issue Number: Close #13483

What's Changed:

Support `REGEXP_INSTR()`,`REGEXP_LIKE()`,`REGEXP_REPLACE()`,`REGEXP_SUBSTR()`.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test

Side effects

  • Performance regression
    • Consumes more CPU
    • Consumes more MEM
  • Breaking backward compatibility

Release note

Support more regular expression functions.

Signed-off-by: gengliqi <gengliqiii@gmail.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Sep 15, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • breezewish
  • sticnarf

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
}

fn get_match_type<C: Collator>(match_type: &[u8]) -> Result<String> {
let match_type = String::from_utf8(match_type.to_vec())?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to iterate the &[u8] directly and match each byte. Then, we can avoid this allocation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I use str::from_utf8 instead.

components/tidb_query_expr/src/impl_regexp.rs Outdated Show resolved Hide resolved
components/tidb_query_expr/src/impl_regexp.rs Outdated Show resolved Hide resolved
components/tidb_query_expr/src/impl_regexp.rs Outdated Show resolved Hide resolved
Comment on lines 159 to 169
let count = expr.chars().count() as i64;
if (pos < 1 || pos > count) && !(count == 0 && pos == 1) {
return Err(box_err!("invalid regex pos: {}, count: {}", pos, count));
}
let mut new_expr = String::new();
for (i, c) in expr.chars().enumerate() {
if i as i64 >= pos - 1 {
new_expr += &c.to_string();
}
}
expr = new_expr;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use str::char_indices to get the byte index of the start. Then, use str::get_unchecked to get a sub-str.

Then, we don't iterate the string twice and don't create a new string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! Addressed.

Comment on lines 184 to 185
for (i, m) in regex.find_iter(&expr).enumerate() {
if i as i64 == occurrence - 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about just .skip(occurrence - 1).next()

Copy link
Member Author

Choose a reason for hiding this comment

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

I change to use nth.

@sticnarf
Copy link
Contributor

The suggestions above also apply to the other functions (instr/replace).

Signed-off-by: gengliqi <gengliqiii@gmail.com>
None => return Ok(None),
};

let count = expr.chars().count() as i64;
Copy link
Contributor

Choose a reason for hiding this comment

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

chars().count() also iterates over the whole string. I mean we can check pos >= 1 first, then if expr.char_indices().nth((pos - 1) as usize) returns None, we can also return error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
Signed-off-by: gengliqi <gengliqiii@gmail.com>
@ti-chi-bot ti-chi-bot added the status/LGT1 Status: PR - There is already 1 approval label Sep 16, 2022
Copy link
Member

@breezewish breezewish left a comment

Choose a reason for hiding this comment

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

Looks fine according to the test case.

@ti-chi-bot ti-chi-bot added status/LGT2 Status: PR - There are already 2 approvals and removed status/LGT1 Status: PR - There is already 1 approval labels Sep 16, 2022
Signed-off-by: gengliqi <gengliqiii@gmail.com>
@gengliqi
Copy link
Member Author

/merge

@ti-chi-bot
Copy link
Member

@gengliqi: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: a9d9062

@ti-chi-bot ti-chi-bot added the status/can-merge Status: Can merge to base branch label Sep 16, 2022
@ti-chi-bot ti-chi-bot merged commit bcfbd56 into tikv:master Sep 16, 2022
@ti-chi-bot ti-chi-bot added this to the Pool milestone Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note size/XXL status/can-merge Status: Can merge to base branch status/LGT2 Status: PR - There are already 2 approvals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support regular expression functions
4 participants