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

Add regexp_extract func #14282

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add regexp_extract func #14282

wants to merge 3 commits into from

Conversation

SKY-ALIN
Copy link

Which issue does this PR close?

Closes #14280.

Rationale for this change

Adding more functions

What changes are included in this PR?

The implementation of regexp_extract based on Spark regexp_extract

Are these changes tested?

Yes

Are there any user-facing changes?

No

@github-actions github-actions bot added documentation Improvements or additions to documentation sqllogictest SQL Logic Tests (.slt) functions labels Jan 24, 2025
@Omega359
Copy link
Contributor

Thanks for your contribution! I've left some comments for your review.

@SKY-ALIN SKY-ALIN requested a review from Omega359 January 25, 2025 19:26
@alamb
Copy link
Contributor

alamb commented Jan 27, 2025

FYI @Omega359 noted this is quite similar to

@rluvaton
Copy link
Contributor

FYI Spark regex is not the same as Rust regex and can have different results

LargeUtf8 => LargeUtf8,
Utf8 => Utf8,
Utf8View => Utf8View,
Null => Null,
Copy link
Contributor

Choose a reason for hiding this comment

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

you say return type is null while you never returned null

Copy link
Author

@SKY-ALIN SKY-ALIN Jan 31, 2025

Choose a reason for hiding this comment

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

line 157

            (None, _, _) | (_, None, _) | (_, _, None) => {
                // If any of arguments is null, the result will be null too
                builder.append_null();
            }

UPD: It's line 181 now

Copy link
Contributor

Choose a reason for hiding this comment

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

This does not handle null array, there is a special type called NullArray

@SKY-ALIN SKY-ALIN requested review from Omega359 and rluvaton January 31, 2025 18:24
Ok(match &arg_types[0] {
LargeUtf8 => LargeUtf8,
Utf8 => Utf8,
Utf8View => Utf8View,
Copy link
Contributor

Choose a reason for hiding this comment

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

Your implementation does not return UTF8 view but only return tug8 or large utf 8 (GenericStringBuilder)

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed. To actually return Utf8View you would need to do something similar to what regexp replace does

Copy link
Contributor

@Omega359 Omega359 left a comment

Choose a reason for hiding this comment

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

This is looking good.

  • I may have missed it but I don't see tests for out of bounds indexes (negative #'s, index larger than match group count).
  • The docs do not say whether the index is 0-indexed or 1-indexed.
  • I would love to see a benchmark for this but that absolutely could be added as a followup PR
  • The returning of Utf8View should be corrected - either just return Utf8/LargeUtf8 or fix the return to actually return utf8view if input is utf8view

My primary concern is there are two PR's for essentially the same functionality. I am not in a position to decide which should be included.

Ok(match &arg_types[0] {
LargeUtf8 => LargeUtf8,
Utf8 => Utf8,
Utf8View => Utf8View,
Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed. To actually return Utf8View you would need to do something similar to what regexp replace does


#[user_doc(
doc_section(label = "Regular Expression Functions"),
description = "Extract a specific group matched by [regular expression](https://docs.rs/regex/latest/regex/#syntax). If the regex did not match, or the specified group did not match, an empty string is returned..",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
description = "Extract a specific group matched by [regular expression](https://docs.rs/regex/latest/regex/#syntax). If the regex did not match, or the specified group did not match, an empty string is returned..",
description = "Extract a specific group matched by [regular expression](https://docs.rs/regex/latest/regex/#syntax). If the regex did not match, or the specified group did not match, an empty string is returned.",

let group_idx = idx_val as usize;
if group_idx < caps.len() {
// If specified group index really exists
if let Some(m) = caps.get(group_idx) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If group_idx is 0 this would return the whole string, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation functions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

regexp_extract func from Spark
4 participants