Skip to content

[C++] Add possibility to extract spans/byte offsets directly for compute.extract_regex #44615

Closed

Description

@rtbs-dev

Describe the enhancement requested

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?

Thanks!

Component(s)

C++

Activity

  1. rtbs-dev commented on Nov 1, 2024

    @rtbs-dev
    Author

    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)

  2. 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
  3. pitrou commented on Dec 4, 2024

    @pitrou
    Member

    This is a good idea. This should be moderately easy for someone already knowing a bit about our compute kernels infrastructure.

  4. rtbs-dev commented on Dec 27, 2024

    @rtbs-dev
    Author

    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 🙂

  5. pitrou commented on Jan 6, 2025

    @pitrou
    Member

    @rtbs-dev If you'd like to contribute to Arrow, you can search for the good-first-issue label in the issues:
    https://github.com/apache/arrow/issues?q=is%3Aopen+is%3Aissue+label%3Agood-first-issue

    As for the extract_regex function, most of the excitement happens here:

    // ----------------------------------------------------------------------
    // Extract with regex
    #ifdef ARROW_WITH_RE2
    using ExtractRegexState = OptionsWrapper<ExtractRegexOptions>;
    // TODO cache this once per ExtractRegexOptions
    struct ExtractRegexData {
    // Use unique_ptr<> because RE2 is non-movable (for ARROW_ASSIGN_OR_RAISE)
    std::unique_ptr<RE2> regex;
    std::vector<std::string> group_names;
    static Result<ExtractRegexData> Make(const ExtractRegexOptions& options,
    bool is_utf8 = true) {
    ExtractRegexData data(options.pattern, is_utf8);
    RETURN_NOT_OK(RegexStatus(*data.regex));
    const int group_count = data.regex->NumberOfCapturingGroups();
    const auto& name_map = data.regex->CapturingGroupNames();
    data.group_names.reserve(group_count);
    for (int i = 0; i < group_count; i++) {
    auto item = name_map.find(i + 1); // re2 starts counting from 1
    if (item == name_map.end()) {
    // XXX should we instead just create fields with an empty name?
    return Status::Invalid("Regular expression contains unnamed groups");
    }
    data.group_names.emplace_back(item->second);
    }
    return data;
    }
    Result<TypeHolder> ResolveOutputType(const std::vector<TypeHolder>& types) const {
    const DataType* input_type = types[0].type;
    if (input_type == nullptr) {
    // No input type specified
    return nullptr;
    }
    // Input type is either [Large]Binary or [Large]String and is also the type
    // of each field in the output struct type.
    DCHECK(is_base_binary_like(input_type->id()));
    FieldVector fields;
    fields.reserve(group_names.size());
    std::shared_ptr<DataType> owned_type = input_type->GetSharedPtr();
    std::transform(group_names.begin(), group_names.end(), std::back_inserter(fields),
    [&](const std::string& name) { return field(name, owned_type); });
    return struct_(std::move(fields));
    }
    private:
    explicit ExtractRegexData(const std::string& pattern, bool is_utf8 = true)
    : regex(new RE2(pattern, MakeRE2Options(is_utf8))) {}
    };
    Result<TypeHolder> ResolveExtractRegexOutput(KernelContext* ctx,
    const std::vector<TypeHolder>& types) {
    ExtractRegexOptions options = ExtractRegexState::Get(ctx);
    ARROW_ASSIGN_OR_RAISE(auto data, ExtractRegexData::Make(options));
    return data.ResolveOutputType(types);
    }
    struct ExtractRegexBase {
    const ExtractRegexData& data;
    const int group_count;
    std::vector<re2::StringPiece> found_values;
    std::vector<RE2::Arg> args;
    std::vector<const RE2::Arg*> args_pointers;
    const RE2::Arg** args_pointers_start;
    const RE2::Arg* null_arg = nullptr;
    explicit ExtractRegexBase(const ExtractRegexData& data)
    : data(data),
    group_count(static_cast<int>(data.group_names.size())),
    found_values(group_count) {
    args.reserve(group_count);
    args_pointers.reserve(group_count);
    for (int i = 0; i < group_count; i++) {
    args.emplace_back(&found_values[i]);
    // Since we reserved capacity, we're guaranteed the pointer remains valid
    args_pointers.push_back(&args[i]);
    }
    // Avoid null pointer if there is no capture group
    args_pointers_start = (group_count > 0) ? args_pointers.data() : &null_arg;
    }
    bool Match(std::string_view s) {
    return RE2::PartialMatchN(ToStringPiece(s), *data.regex, args_pointers_start,
    group_count);
    }
    };
    template <typename Type>
    struct ExtractRegex : public ExtractRegexBase {
    using ArrayType = typename TypeTraits<Type>::ArrayType;
    using ScalarType = typename TypeTraits<Type>::ScalarType;
    using BuilderType = typename TypeTraits<Type>::BuilderType;
    using ExtractRegexBase::ExtractRegexBase;
    static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
    ExtractRegexOptions options = ExtractRegexState::Get(ctx);
    ARROW_ASSIGN_OR_RAISE(auto data, ExtractRegexData::Make(options, Type::is_utf8));
    return ExtractRegex{data}.Extract(ctx, batch, out);
    }
    Status Extract(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
    // TODO: why is this needed? Type resolution should already be
    // done and the output type set in the output variable
    ARROW_ASSIGN_OR_RAISE(TypeHolder out_type, data.ResolveOutputType(batch.GetTypes()));
    DCHECK_NE(out_type.type, nullptr);
    std::shared_ptr<DataType> type = out_type.GetSharedPtr();
    std::unique_ptr<ArrayBuilder> array_builder;
    RETURN_NOT_OK(MakeBuilder(ctx->memory_pool(), type, &array_builder));
    StructBuilder* struct_builder = checked_cast<StructBuilder*>(array_builder.get());
    std::vector<BuilderType*> field_builders;
    field_builders.reserve(group_count);
    for (int i = 0; i < group_count; i++) {
    field_builders.push_back(
    checked_cast<BuilderType*>(struct_builder->field_builder(i)));
    }
    auto visit_null = [&]() { return struct_builder->AppendNull(); };
    auto visit_value = [&](std::string_view s) {
    if (Match(s)) {
    for (int i = 0; i < group_count; i++) {
    RETURN_NOT_OK(field_builders[i]->Append(ToStringView(found_values[i])));
    }
    return struct_builder->Append();
    } else {
    return struct_builder->AppendNull();
    }
    };
    RETURN_NOT_OK(VisitArraySpanInline<Type>(batch[0].array, visit_value, visit_null));
    std::shared_ptr<Array> out_array;
    RETURN_NOT_OK(struct_builder->Finish(&out_array));
    out->value = std::move(out_array->data());
    return Status::OK();
    }
    };
    const FunctionDoc extract_regex_doc(
    "Extract substrings captured by a regex pattern",
    ("For each string in `strings`, match the regular expression and, if\n"
    "successful, emit a struct with field names and values coming from the\n"
    "regular expression's named capture groups. If the input is null or the\n"
    "regular expression fails matching, a null output value is emitted.\n"
    "\n"
    "Regular expression matching is done using the Google RE2 library."),
    {"strings"}, "ExtractRegexOptions", /*options_required=*/true);
    void AddAsciiStringExtractRegex(FunctionRegistry* registry) {
    auto func = std::make_shared<ScalarFunction>("extract_regex", Arity::Unary(),
    extract_regex_doc);
    OutputType out_ty(ResolveExtractRegexOutput);
    for (const auto& ty : BaseBinaryTypes()) {
    ScalarKernel kernel{{ty},
    out_ty,
    GenerateVarBinaryToVarBinary<ExtractRegex>(ty),
    ExtractRegexState::Init};
    // Null values will be computed based on regex match or not
    kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
    kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;
    DCHECK_OK(func->AddKernel(kernel));
    }
    DCHECK_OK(registry->AddFunction(std::move(func)));
    }
    #endif // ARROW_WITH_RE2

    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.

  6. andishgar commented on Feb 14, 2025

    @andishgar
    Contributor

    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.

    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.
      RETURN_NOT_OK(field_builders[i]->Append(ToStringView(found_values[i])));

    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))});

    1. 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?

    2. There is a TODO in

      Status Extract(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
      // TODO: why is this needed? Type resolution should already be
      // done and the output type set in the output variable
      ARROW_ASSIGN_OR_RAISE(TypeHolder out_type, data.ResolveOutputType(batch.GetTypes()));

      It seems it is completely right, as the type can be achieved from out Should 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.)

    // TODO cache this once per ExtractRegexOptions
    struct ExtractRegexData {

    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?

    1. 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

    {
    ExtractRegexOptions options("(?P<letter>[\\xfc])(?P<digit>\\d)");
    auto null_bitmap = std::make_shared<Buffer>("0");
    auto output = StructArray::Make(
    {this->MakeArray({"\xfc", "1"}), this->MakeArray({"\xfc", "2"})},
    {field("letter", this->type()), field("digit", this->type())}, null_bitmap);
    this->CheckUnary("extract_regex", this->MakeArray({"foo\xfc 1bar", "\x02\xfc\x40"}),
    std::static_pointer_cast<Array>(*output), &options);
    }
    }

    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

    {
    ExtractRegexOptions options("(?P<null>[\\x00])(?P<digit>\\d)");
    auto null_bitmap = std::make_shared<Buffer>("0");
    auto output = StructArray::Make(
    {this->template MakeArray<std::string>({{"\x00", 1}, {"1", 1}}),
    this->template MakeArray<std::string>({{"\x00", 1}, {"2", 1}})},
    {field("null", this->type()), field("digit", this->type())}, null_bitmap);
    this->CheckUnary(
    "extract_regex",
    this->template MakeArray<std::string>({{"foo\x00 1bar", 9}, {"\x02\x00\x40", 3}}),
    std::static_pointer_cast<Array>(*output), &options);
    }

    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);

  7. pitrou commented on Feb 17, 2025

    @pitrou
    Member

    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())}))
    });

    But I'm open to other suggestions as well. cc @jorisvandenbossche @zanmato1984 for ideas.

    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.)

    arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc

    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!

  8. pitrou commented on Feb 17, 2025

    @pitrou
    Member

    I've assigned you @arashandishgar . Thank you for looking into this!

  9. andishgar commented on Feb 17, 2025

    @andishgar
    Contributor

    the warning for include-what-you-use for scalar_string_ascii.cc is

    IWYU_SH match arrow/compute/kernels/scalar_string_ascii.cc 
    include-what-you-use 0.18 (git:abd5d2b) based on Ubuntu clang version 14.0.6
    Running IWYU on  /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
    /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc should add these lines:
    #include <re2/stringpiece.h>                               // for StringPiece
    #include <cstring>                                         // for memcpy
    #include <functional>                                      // for function
    #include <limits>                                          // for numeric_li...
    #include <map>                                             // for operator==
    #include <variant>                                         // for variant
    #include "arrow/array/array_base.h"                        // for Array
    #include "arrow/array/builder_base.h"                      // for MakeBuilder
    #include "arrow/array/util.h"                              // for MakeArrayO...
    #include "arrow/buffer_builder.h"                          // for TypedBuffe...
    #include "arrow/compute/api_scalar.h"                      // for MatchSubst...
    #include "arrow/compute/exec.h"                            // for ExecSpan
    #include "arrow/compute/kernels/common_internal.h"         // for vector
    #include "arrow/scalar.h"                                  // for BaseBinary...
    #include "arrow/util/bit_block_counter.h"                  // for `VisitBitBl...`
    #include "arrow/util/bit_util.h"                           // for GetBit
    #include "arrow/util/bitmap_writer.h"                      // for FirstTimeB...
    #include "arrow/visit_data_inline.h"                       // for VisitArray...
    namespace arrow { namespace compute { class FunctionOptions; } }
    
    /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/kernels/scalar_string_ascii.cc should remove these lines:
    - #include <cctype>  // lines 19-19
    - #include <memory>  // lines 21-21
    - #include <string>  // lines 22-22
    - #include "arrow/util/macros.h"  // lines 29-29
    - #include "arrow/util/value_parsing.h"  // lines 31-31
    

    and for api_scalar.cc is

    include-what-you-use 0.18 (git:abd5d2b) based on Ubuntu clang version 14.0.6
    Running IWYU on  /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/api_scalar.cc
    /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/api_scalar.h should add these lines:
    #include <cstdint>                           // for int64_t, int8_t, uint64_t
    #include <limits>                            // for numeric_limits
    #include <memory>                            // for shared_ptr
    #include <vector>                            // for vector, initializer_list
    #include "arrow/type.h"                      // for FieldRef, TimeUnit, Time...
    namespace arrow { class KeyValueMetadata; }
    namespace arrow { namespace compute { class ExecContext; } }
    namespace arrow { struct Scalar; }
    
    /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/api_scalar.h should remove these lines:
    - #include <utility>  // lines 25-25
    - #include "arrow/compute/type_fwd.h"  // lines 28-28
    
    /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/api_scalar.cc should add these lines:
    #include <algorithm>                          // for max
    #include <array>                              // for array
    #include <utility>                            // for move
    #include <variant>                            // for variant
    #include "arrow/scalar.h"                     // for DoubleScalar, NullScalar
    #include "arrow/util/compare.h"               // for operator==
    #include "arrow/util/reflection_internal.h"   // for DataMember, BasicEnumTr...
    namespace arrow { class KeyValueMetadata; }
    
    /media/arashandishgar/7ea4b859-cfd7-41cb-8466-dc8dbc34284b/ARROW/arrow/cpp/src/arrow/compute/api_scalar.cc should remove these lines:
    - #include <sstream>  // lines 22-22
    - #include "arrow/array/array_base.h"  // lines 25-25
    - #include "arrow/status.h"  // lines 29-29
    
  10. andishgar commented on Feb 17, 2025

    @andishgar
    Contributor

    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?)

  11. pitrou commented on Feb 17, 2025

    @pitrou
    Member

    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.

    Yes, that's a good point. @felipecrv @zanmato1984 Do you have opinions on this?

  12. 1 remaining item

  13. pitrou commented on Feb 17, 2025

    @pitrou
    Member

    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.

  14. pitrou commented on Feb 17, 2025

    @pitrou
    Member

    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.

  15. andishgar commented on Feb 17, 2025

    @andishgar
    Contributor

    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.

  16. andishgar commented on Feb 18, 2025

    @andishgar
    Contributor

    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?

  17. pitrou commented on Feb 18, 2025

    @pitrou
    Member

    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.

  18. andishgar commented on Feb 18, 2025

    @andishgar
    Contributor

    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.

  19. pitrou commented on Feb 18, 2025

    @pitrou
    Member

    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).

  20. andishgar commented on Feb 19, 2025

    @andishgar
    Contributor

    I've finished the implementation. Do I send a pull request or wait for further discussion?

  21. pitrou commented on Feb 19, 2025

    @pitrou
    Member

    @arashandishgar You can definitely send a PR, it will help us comment more precisely and accurately :)

  22. rtbs-dev commented on Feb 20, 2025

    @rtbs-dev
    Author

    Maybe I can help on the python API side? Do we want to do that in the same pr?

  23. pitrou commented on Feb 20, 2025

    @pitrou
    Member

    Maybe I can help on the python API side? Do we want to do that in the same pr?

    I think it would be better to do it as a followup PR once the C++ PR is merged.

  24. added a commit that references this issue on Mar 11, 2025
  25. added this to the 20.0.0 milestone on Mar 11, 2025
  26. pitrou commented on Mar 11, 2025

    @pitrou
    Member

    Issue resolved by pull request 45577
    #45577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions