Repository navigation
Description
Describe the bug
#883 fixed ViewArrayAs<T>::operator[] so that it adds the array offset. The three byte views in nanoarrow.hpp still call range_.get(i) without range_.offset: ViewArrayAsBytes<N>, ViewArrayAsFixedSizeBytes and ViewBinaryViewArrayAsBytes. On a sliced array (offset > 0), v[i] returns element i of the unsliced array, while iterating with begin()/end() returns the right elements.
To reproduce
#include <string>
#include <cstdio>
#include "nanoarrow/nanoarrow.hpp"
static std::string S(ArrowStringView v) { return std::string(v.data, v.size_bytes); }
template <typename V>
static int check(const char* name, const V& v) {
std::string at0 = S(*v[0]);
std::string it0 = S(**v.begin());
std::printf("EXPECTED: %s v[0] == *begin() == \"%s\"\n", name, it0.c_str());
std::printf("ACTUAL: %s v[0] == \"%s\"\n", name, at0.c_str());
return at0 != it0;
}
int main() {
int bad = 0;
const char* vals[] = {"aaa", "bbb", "ccc"};
for (int kind = 0; kind < 3; kind++) {
nanoarrow::UniqueArray arr;
if (kind == 1) {
nanoarrow::UniqueSchema schema;
ArrowSchemaInit(schema.get());
ArrowSchemaSetTypeFixedSize(schema.get(), NANOARROW_TYPE_FIXED_SIZE_BINARY, 3);
ArrowArrayInitFromSchema(arr.get(), schema.get(), nullptr);
} else {
ArrowArrayInitFromType(arr.get(), kind == 0 ? NANOARROW_TYPE_STRING
: NANOARROW_TYPE_STRING_VIEW);
}
ArrowArrayStartAppending(arr.get());
for (auto s : vals) {
ArrowStringView sv = {s, 3};
if (kind == 1) ArrowArrayAppendBytes(arr.get(), ArrowBufferView{{s}, 3});
else ArrowArrayAppendString(arr.get(), sv);
}
ArrowArrayFinishBuildingDefault(arr.get(), nullptr);
arr->offset = 1;
arr->length -= 1;
if (kind == 0) bad |= check("ViewArrayAsBytes<32>", nanoarrow::ViewArrayAsBytes<32>(arr.get()));
if (kind == 1) bad |= check("ViewArrayAsFixedSizeBytes", nanoarrow::ViewArrayAsFixedSizeBytes(arr.get(), 3));
if (kind == 2) bad |= check("ViewBinaryViewArrayAsBytes", nanoarrow::ViewBinaryViewArrayAsBytes(arr.get()));
}
return bad;
}Output on main (ee22361):
EXPECTED: ViewArrayAsBytes<32> v[0] == *begin() == "bbb"
ACTUAL: ViewArrayAsBytes<32> v[0] == "aaa"
EXPECTED: ViewArrayAsFixedSizeBytes v[0] == *begin() == "bbb"
ACTUAL: ViewArrayAsFixedSizeBytes v[0] == "aaa"
EXPECTED: ViewBinaryViewArrayAsBytes v[0] == *begin() == "bbb"
ACTUAL: ViewBinaryViewArrayAsBytes v[0] == "aaa"
Expected behavior
v[0] should be the first element of the slice ("bbb"), the same as *v.begin(), as ViewArrayAs<T>::operator[] does since #883 (range_.get(range_.offset + i), src/nanoarrow/hpp/view.hpp:178).
Versions
- nanoarrow 0.9.0 and
main@ ee22361 (both affected) - g++ 15.2.0, Linux aarch64;
-std=c++17 -g -fsanitize=address,undefined, linked against a CMake Debug build oflibnanoarrow_static.a
Additional context
At ee22361 the three operators are at src/nanoarrow/hpp/view.hpp:236, :290 and :341; they look like they need the same range_.get(range_.offset + i) change as line 178.
I have a fix with a regression test ready and will open a pull request for it right after filing this issue; it will link back here.
Activity
fix: Correctly subscript ViewArrayAsBytes, ViewArrayAsFixedSizeBytes …