-
Notifications
You must be signed in to change notification settings - Fork 498
Simplify regexp replace handling #32053
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
Merged
antiguru
merged 4 commits into
MaterializeInc:main
from
antiguru:regexpr_replace_lit_error
Apr 7, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1229,17 +1229,32 @@ impl MirScalarExpr { | |
| .map_or("", |expr| expr.as_literal_str().unwrap()); | ||
| let (limit, flags) = regexp_replace_parse_flags(flags); | ||
|
|
||
| // Defer errors until evaluation instead of eagerly returning them here | ||
| // to match the error behavior of the dynamic function (part of database-issues#4972). | ||
| let regex = match func::build_regex(pattern, &flags) { | ||
| Ok(regex) => Ok((regex, limit)), | ||
| Err(err) => Err(err), | ||
| // The behavior of `regexp_replace` is that if the data is `NULL`, the | ||
| // function returns `NULL`, independently of whether the pattern or | ||
| // flags are correct. We need to check for this case and introduce an | ||
| // if-then-else on the error path to only surface the error if the first | ||
| // input is non-NULL. | ||
| *e = match func::build_regex(pattern, &flags) { | ||
| Ok(regex) => { | ||
| let mut exprs = mem::take(exprs); | ||
| let replacement = exprs.swap_remove(2); | ||
| let source = exprs.swap_remove(0); | ||
| source.call_binary( | ||
| replacement, | ||
| BinaryFunc::RegexpReplace { regex, limit }, | ||
| ) | ||
| } | ||
| Err(err) => { | ||
| let mut exprs = mem::take(exprs); | ||
| let source = exprs.swap_remove(0); | ||
| let scalar_type = e.typ(column_types).scalar_type; | ||
| // We need to return `NULL` on `NULL` input, and error otherwise. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe extend the comment a bit. Something like: |
||
| source.call_is_null().if_then_else( | ||
| MirScalarExpr::literal_null(scalar_type.clone()), | ||
| MirScalarExpr::literal(Err(err), scalar_type), | ||
| ) | ||
| } | ||
| }; | ||
| let mut exprs = mem::take(exprs); | ||
| let replacement = exprs.swap_remove(2); | ||
| let source = exprs.swap_remove(0); | ||
| *e = source | ||
| .call_binary(replacement, BinaryFunc::RegexpReplace { regex }); | ||
| } else if *func == VariadicFunc::RegexpSplitToArray | ||
| && exprs[1].is_literal() | ||
| && exprs.get(2).map_or(true, |e| e.is_literal()) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the above comment. Or you could just remove that comment, since there is also a comment below in the error case.