You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++] Add possibility to extract spans/byte offsets directly for compute.extract_regex #44615
I'm Coming from AwkwardArray and Polars use, trying to vectorize the equivalent of finding the byte offsets (or character spans) of all regex matches in an array of strings.
See this discussion for the request's solution in re2 directly. Per the solution there, it seems the information would be contained in the re2::StringPiece data, which this thread indicated is preferable anyway, due to memory duplication. I see something vaguely related brought up here, where string_view was vendored instead, though I don't see a way to access the view objects right now, via the results of extract_regex.
I do see the struct getting returned is not a plain string, but adding span locations might mess with downstream users' type definitions or API contracts. Maybe new behavior could be added as an additional option? Alternatively, a new function extract_regex_spans would already make my life much easier, even if downstream libraries like Polars and AkwardArray have add new wrapper APIs for their code to support the behavior.
Am I missing something obvious? I most importantly want to avoid having to loop twice over every string (first to find the string match and then to find the location of the previous match) because that feels wasteful when the matches are discovered via their offset locations in the first place, right?
Linking to an issue I opened with arrow-rs, which uses a rust regex implementation iirc, when I was only considering polars use-cases. But I'm now not sure how I would implement this in the re2 case, either.
changed the title [-]Add possibility to extract spans/byte offsets directly for `compute.extract_regext`[/-][+][C++] Add possibility to extract spans/byte offsets directly for `compute.extract_regex`[/+]on Nov 3, 2024
Thanks! Not really sure where to start wrt your kernel architecture, myself, but if there's anything an Arrow beginner can do in the meantime, please let me know 🙂
As you can see there's a ExtractRegexData that gets initialized from the function options (and also helps computing the output type), a base helper class ExtractRegexBase for execution, and a ExtractRegex derived class that's templated on the input string type (e.g. binary() vs. utf8()).
Finally I agree that a new function extract_regex_spans would be better than adding options to extract_regex. The new function can easily reuse/expand the facilities above, however.
I’ve already evaluated the issue, and I’m able to implement the feature (this is my first contribution). Could you assign it to me? @pitrou
By the way, I have several questions regarding implementation.
During the implementation of the extract_regex function, the writer was totally well-known about the StringPiece. However, the arrow array copies the string_view into its internal buffer.
2.My question is what you expect to return for extract_regex_span? My suggestion is a struct which includes a fixed_size_list type for each group with length of two for each element and it contains an offset as the the first value and the length as the second value of each element. for example for the regex"(?P<letter>[ab])(?P<digit>\\d)"the struct array will be auto type = struct_({field("letter_span", fixed_size_list(int64(), 2)), field("digit_span", fixed_size_list(int64(), 2))});
I think it would be nice if I added a boolean value to ExtratRegexSpanOptions (the new option class for extract_regex_span) to show the matched string value optionally. What’s your opinion?
6.When I ran include-what-you-use on scalar_string_ascii.cc,scalar_string_test.cc ,api_scalar.cc I gave several warnings. Should I pay attention to that?
Should I consider separate test cases for extract_regex_span or locate its tests on the test cases of extract_regex (since tests are identical with a change in expected output)?
8.What is the purpose of the two following tests for extract_regex? I mean, since in both cases the null bit map is set completely null, running the tests with any possible value is accepted.. I mean for
the following value is accepted auto output = StructArray::Make( {this->MakeArray({"\xfc 2", "3"}), this->MakeArray({"\xfc w", "3"})}, {field("letter", this->type()), field("digit", this->type())}, null_bitmap);
By the way, I have several questions regarding implementation.
1. During the implementation of the extract_regex function, the writer was totally well-known about the StringPiece. However, the arrow array copies the string_view into its internal buffer.
I'm not sure I understand your question, but the captured strings must indeed be copied into the result String array. This is required by the Arrow format.
2.My question is what you expect to return for extract_regex_span? My suggestion is a struct which includes a fixed_size_list type for each group with length of two for each element and it contains an offset as the the first value and the length as the second value of each element. for example for the regex"(?P<letter>[ab])(?P<digit>\\d)"the struct array will be auto type = struct_({field("letter_span", fixed_size_list(int64(), 2)), field("digit_span", fixed_size_list(int64(), 2))});
I would suggest the following if the input is a regular string array (i.e. with 32-bit offsets):
auto type = struct_({
field("letter", struct_({field("start", int32()), field("length", int32())})),
field("digit", struct_({field("start", int32()), field("length", int32())}))
});
and the following if the input is a large string array (i.e. with 64-bit offsets):
auto type = struct_({
field("letter", struct_({field("start", int64()), field("length", int64())})),
field("digit", struct_({field("start", int64()), field("length", int64())}))
});
3. I think it would be nice if I added a boolean value to ExtratRegexSpanOptions (the new option class for extract_regex_span) to show the matched string value optionally. What’s your opinion?
That doesn't sound very useful IMHO. The user can define a capturing group for the entire regex if that's desired.
4. There is a TODO in
[arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc](https://github.com/apache/arrow/blob/a931affa1766be1f98979228ca6ab124aa7f793c/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc#L2286-L2289)
It seems it is completely right, as the type can be achieved from `out` Should I apply the changes?
Yes, you can try applying them and see if that breaks anything.
5.What does the following TODO mean? (Since I’ve read the entire code related to extract_regex, maybe I could solve that.)
Ideally we would compile the regex once instead of at every kernel invocation (the new RE2 call). However, we don't have a nice way of doing this for now. In any case, this should be done as a separate PR.
6.When I ran include-what-you-use on scalar_string_ascii.cc,scalar_string_test.cc ,api_scalar.cc I gave several warnings. Should I pay attention to that?
It depends, can you paste the warnings somewhere?
7. Should I consider separate test cases for extract_regex_span or locate its tests on the test cases of extract_regex (since tests are identical with a change in expected output)?
It seems you can probably colocate the tests, though you can choose whatever is more convenient.
8.What is the purpose of the two following tests for extract_regex? I mean, since in both cases the null bit map is set completely null, running the tests with any possible value is accepted.. I mean for [...]
You're right, it seems these tests are broken, they deserve removing or fixing. Thanks for noticing this!
2.My question is what you expect to return for extract_regex_span? My suggestion is a struct which includes a fixed_size_list type for each group with length of two for each element and it contains an offset as the the first value and the length as the second value of each element. for example for the regex"(?P<letter>[ab])(?P<digit>\\d)"the struct array will be auto type = struct_({field("letter_span", fixed_size_list(int64(), 2)), field("digit_span", fixed_size_list(int64(), 2))});
I would suggest the following if the input is a regular string array (i.e. with 32-bit offsets):
auto type = struct_({
field("letter", struct_({field("start", int32()), field("length", int32())})),
field("digit", struct_({field("start", int32()), field("length", int32())}))
});
and the following if the input is a large string array (i.e. with 64-bit offsets):
auto type = struct_({
field("letter", struct_({field("start", int64()), field("length", int64())})),
field("digit", struct_({field("start", int64()), field("length", int64())}))
});
But I'm open to other suggestions as well. cc @jorisvandenbossche@zanmato1984 for ideas.
I think it is better to put both the start and length fields next to each other (consider fixed_size_list) in memory rather than in two separate memory buffers, as both should be used to deduce the result, and neither of them would be meaningful alone. (What is your opinion?)
I think it is better to put both the start and length fields next to each other (consider fixed_size_list) in memory rather than in two separate memory buffers, as both should be used to deduce the result, and neither of them would be meaningful alone.
However if the use case (sorry I didn't read through the full context so this may not be useful) can take some advantages of features like out of order elements or view-like structures, it may also worth looking at binary_view or list_view.
That's a possibility as well, but we can afford to have another regex-extraction function that returns a string-view.
the warning for include-what-you-use for scalar_string_ascii.cc is
Thanks for posting them. I don't think there's anything really important there, and some warnings seem misguided (e.g. why <map> for operator==?). So I would ignore them.
I agree that start and end are preferable to be placed together. So I'm in favor of fixed_size_list.
However if the use case (sorry I didn't read through the full context so this may not be useful) can take some advantages of features like out of order elements or view-like structures, it may also worth looking at binary_view or list_view.
I think there is no point in the extract_regex_span function (it's supposed to implement) using list_view as the length of each element is constant.(remember the problem is to return the position of the finding string). However, it can be beneficial to consider it for extract_regex. But there is a problem. As it is mentioned in #44336, there is no compute kernel for View type. Thus, the result cannot be used for other computations.
2.My question is what you expect to return for extract_regex_span? My suggestion is a struct which includes a fixed_size_list type for each group with length of two for each element and it contains an offset as the the first value and the length as the second value of each element. for example for the regex"(?P<letter>[ab])(?P<digit>\\d)"the struct array will be auto type = struct_({field("letter_span", fixed_size_list(int64(), 2)), field("digit_span", fixed_size_list(int64(), 2))});
I would suggest the following if the input is a regular string array (i.e. with 32-bit offsets):
auto type = struct_({
field("letter", struct_({field("start", int32()), field("length", int32())})),
field("digit", struct_({field("start", int32()), field("length", int32())}))
});
and the following if the input is a large string array (i.e. with 64-bit offsets):
auto type = struct_({
field("letter", struct_({field("start", int64()), field("length", int64())})),
field("digit", struct_({field("start", int64()), field("length", int64())}))
});
But I'm open to other suggestions as well. cc @jorisvandenbossche@zanmato1984 for ideas.
I have a question regarding what offset should be used for storing? According to this link the offset can be achieved by pointer arithmetic. Is it safe to store in 32-bit space for a regular string array?
I have a question regarding what offset should be used for storing? According to this link the offset can be achieved by pointer arithmetic. Is it safe to store in 32-bit space for a regular string array?
What do you mean? The "start" field above would be an offset relative to the start of string, not a pointer.
I have a question regarding what offset should be used for storing? According to this link the offset can be achieved by pointer arithmetic. Is it safe to store in 32-bit space for a regular string array?
What do you mean? The "start" field above would be an offset relative to the start of string, not a pointer.
I mean, is it safe to minus two pointers from each other and cast the result from 64-bit to 32-bit? I know that the maximum range for a string array is 2 GB. I do not assume any special case. I just ask to sure that casting from 64-bit to 32-bit is safe.
I mean, is it safe to minus two pointers from each other and cast the result from 64-bit to 32-bit? I know that the maximum range for a string array is 2 GB. I do not assume any special case. I just ask to sure that casting from 64-bit to 32-bit is safe.
Well, of course it is, since we know the result will fit in 32 bits (31, actually).
Activity
rtbs-dev commentedon Nov 1, 2024
Linking to an issue I opened with arrow-rs, which uses a rust regex implementation iirc, when I was only considering polars use-cases. But I'm now not sure how I would implement this in the re2 case, either.
apache/arrow-rs#5966 (comment)
[-]Add possibility to extract spans/byte offsets directly for `compute.extract_regext`[/-][+][C++] Add possibility to extract spans/byte offsets directly for `compute.extract_regex`[/+]pitrou commentedon Dec 4, 2024
This is a good idea. This should be moderately easy for someone already knowing a bit about our compute kernels infrastructure.
rtbs-dev commentedon Dec 27, 2024
Thanks! Not really sure where to start wrt your kernel architecture, myself, but if there's anything an Arrow beginner can do in the meantime, please let me know 🙂
pitrou commentedon Jan 6, 2025
@rtbs-dev If you'd like to contribute to Arrow, you can search for the
good-first-issuelabel in the issues:https://github.com/apache/arrow/issues?q=is%3Aopen+is%3Aissue+label%3Agood-first-issue
As for the
extract_regexfunction, most of the excitement happens here:arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Lines 2180 to 2350 in a931aff
As you can see there's a
ExtractRegexDatathat gets initialized from the function options (and also helps computing the output type), a base helper classExtractRegexBasefor execution, and aExtractRegexderived class that's templated on the input string type (e.g. binary() vs. utf8()).Finally I agree that a new function
extract_regex_spanswould be better than adding options toextract_regex. The new function can easily reuse/expand the facilities above, however.andishgar commentedon Feb 14, 2025
I’ve already evaluated the issue, and I’m able to implement the feature (this is my first contribution). Could you assign it to me? @pitrou
By the way, I have several questions regarding implementation.
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Line 2308 in a931aff
2.My question is what you expect to return for extract_regex_span? My suggestion is a struct which includes a fixed_size_list type for each group with length of two for each element and it contains an offset as the the first value and the length as the second value of each element. for example for the regex
"(?P<letter>[ab])(?P<digit>\\d)"the struct array will beauto type = struct_({field("letter_span", fixed_size_list(int64(), 2)), field("digit_span", fixed_size_list(int64(), 2))});I think it would be nice if I added a boolean value to ExtratRegexSpanOptions (the new option class for extract_regex_span) to show the matched string value optionally. What’s your opinion?
There is a TODO in
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Lines 2286 to 2289 in a931aff
It seems it is completely right, as the type can be achieved from
outShould I apply the changes?5.What does the following TODO mean? (Since I’ve read the entire code related to extract_regex, maybe I could solve that.)
arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Lines 2187 to 2188 in a931aff
6.When I ran include-what-you-use on
scalar_string_ascii.cc,scalar_string_test.cc,api_scalar.ccI gave several warnings. Should I pay attention to that?8.What is the purpose of the two following tests for extract_regex? I mean, since in both cases the null bit map is set completely null, running the tests with any possible value is accepted.. I mean for
arrow/cpp/src/arrow/compute/kernels/scalar_string_test.cc
Lines 317 to 326 in a931aff
the following value is accepted
auto output = StructArray::Make( {this->MakeArray({"\xfc 2", "3"}), this->MakeArray({"\xfc w", "3"})}, {field("letter", this->type()), field("digit", this->type())}, null_bitmap);and for the following test
arrow/cpp/src/arrow/compute/kernels/scalar_string_test.cc
Lines 373 to 384 in a931aff
The following value is accepted
auto output = StructArray::Make( {this->template MakeArray<std::string>({{"\x00", 1}, {"1", 1}}), this->template MakeArray<std::string>({{"\x00 2", 3}, {"2 2", 3}})}, {field("null", this->type()), field("digit", this->type())}, null_bitmap);pitrou commentedon Feb 17, 2025
I'm not sure I understand your question, but the captured strings must indeed be copied into the result String array. This is required by the Arrow format.
I would suggest the following if the input is a regular string array (i.e. with 32-bit offsets):
and the following if the input is a large string array (i.e. with 64-bit offsets):
But I'm open to other suggestions as well. cc @jorisvandenbossche @zanmato1984 for ideas.
That doesn't sound very useful IMHO. The user can define a capturing group for the entire regex if that's desired.
Yes, you can try applying them and see if that breaks anything.
Ideally we would compile the regex once instead of at every kernel invocation (the
new RE2call). However, we don't have a nice way of doing this for now. In any case, this should be done as a separate PR.It depends, can you paste the warnings somewhere?
It seems you can probably colocate the tests, though you can choose whatever is more convenient.
You're right, it seems these tests are broken, they deserve removing or fixing. Thanks for noticing this!
pitrou commentedon Feb 17, 2025
I've assigned you @arashandishgar . Thank you for looking into this!
andishgar commentedon Feb 17, 2025
the warning for include-what-you-use for scalar_string_ascii.cc is
and for
api_scalar.ccisandishgar commentedon Feb 17, 2025
I think it is better to put both the start and length fields next to each other (consider fixed_size_list) in memory rather than in two separate memory buffers, as both should be used to deduce the result, and neither of them would be meaningful alone. (What is your opinion?)
pitrou commentedon Feb 17, 2025
Yes, that's a good point. @felipecrv @zanmato1984 Do you have opinions on this?
1 remaining item
pitrou commentedon Feb 17, 2025
That's a possibility as well, but we can afford to have another regex-extraction function that returns a string-view.
pitrou commentedon Feb 17, 2025
Thanks for posting them. I don't think there's anything really important there, and some warnings seem misguided (e.g. why
<map>foroperator==?). So I would ignore them.andishgar commentedon Feb 17, 2025
I think there is no point in the extract_regex_span function (it's supposed to implement) using list_view as the length of each element is constant.(remember the problem is to return the position of the finding string). However, it can be beneficial to consider it for extract_regex. But there is a problem. As it is mentioned in #44336, there is no compute kernel for View type. Thus, the result cannot be used for other computations.
andishgar commentedon Feb 18, 2025
I have a question regarding what offset should be used for storing? According to this link the offset can be achieved by pointer arithmetic. Is it safe to store in 32-bit space for a regular string array?
pitrou commentedon Feb 18, 2025
What do you mean? The "start" field above would be an offset relative to the start of string, not a pointer.
andishgar commentedon Feb 18, 2025
I mean, is it safe to minus two pointers from each other and cast the result from 64-bit to 32-bit? I know that the maximum range for a string array is 2 GB. I do not assume any special case. I just ask to sure that casting from 64-bit to 32-bit is safe.
pitrou commentedon Feb 18, 2025
Well, of course it is, since we know the result will fit in 32 bits (31, actually).
andishgar commentedon Feb 19, 2025
I've finished the implementation. Do I send a pull request or wait for further discussion?
pitrou commentedon Feb 19, 2025
@arashandishgar You can definitely send a PR, it will help us comment more precisely and accurately :)
rtbs-dev commentedon Feb 20, 2025
Maybe I can help on the python API side? Do we want to do that in the same pr?
pitrou commentedon Feb 20, 2025
I think it would be better to do it as a followup PR once the C++ PR is merged.
GH-44615: [C++][Compute] Add extract_regex_span function (#45577)
pitrou commentedon Mar 11, 2025
Issue resolved by pull request 45577
#45577