Skip to content

Commit

Permalink
Fix: do not increment field index for nested lists (#1303)
Browse files Browse the repository at this point in the history
* Fix: do not increment field index for nested lists

* Test: check that fix works!

* Refactor: use simpler form keys

* Test: check case for multiple lists

* Test: make test more complex

* Fix: correctly reset index when wrapping

* Revert "Fix: correctly reset index when wrapping"

This reverts commit b27e480.

* Fix: correctly wrap index
  • Loading branch information
agoose77 authored Feb 22, 2022
1 parent 8ff15cb commit 118305b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/libawkward/layoutbuilder/RecordArrayBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,15 @@ namespace awkward {
template <typename T, typename I>
int64_t
RecordArrayBuilder<T, I>::field_index() {
return (field_index_ < contents_size_ - 1) ?
field_index_++ : (field_index_ = 0);
if (!list_field_index_.empty())
{
return field_index_;
}
auto index = field_index_;
field_index_ = (++field_index_ < contents_size_)
? field_index_
: field_index_ % contents_size_;
return index;
}

template <typename T, typename I>
Expand Down
89 changes: 89 additions & 0 deletions tests/test_1302-layout-builder-nested-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import pytest # noqa: F401
import awkward as ak # noqa: F401


def test():
builder = ak.layout.LayoutBuilder64(
"""
{
"form_key": "root",
"class": "RecordArray",
"contents": {
"u": {
"form_key": "u",
"class": "ListOffsetArray64",
"offsets": "i64",
"content": {
"form_key": "u-content",
"class": "RecordArray",
"contents": {
"i": {
"class": "NumpyArray",
"primitive": "int64",
"form_key": "i"
},
"j": {
"class": "ListOffsetArray64",
"offsets": "i64",
"content": "int64",
"form_key": "j"
}
}
}
},
"v": {
"class": "NumpyArray",
"primitive": "int64",
"form_key": "v"
},
"w": {
"class": "NumpyArray",
"primitive": "int64",
"form_key": "w"
},
"x": {
"class": "NumpyArray",
"primitive": "int64",
"form_key": "x"
}
}
}
"""
)

builder.begin_list() # u

builder.int64(1) # i
builder.begin_list() # j
builder.int64(9)
builder.int64(8)
builder.int64(7)
builder.end_list() # j

builder.end_list() # u

builder.int64(2) # v
builder.int64(3) # w
builder.int64(4) # x

builder.begin_list() # u

builder.int64(1) # i
builder.begin_list() # j
builder.int64(9)
builder.int64(8)
builder.int64(7)
builder.end_list() # j

builder.end_list() # u

builder.int64(2) # v
builder.int64(3) # w
builder.int64(4) # x

assert ak.to_list(builder.snapshot()) == [
{"u": [{"i": 1, "j": [9, 8, 7]}], "v": 2, "w": 3, "x": 4},
{"u": [{"i": 1, "j": [9, 8, 7]}], "v": 2, "w": 3, "x": 4},
]

0 comments on commit 118305b

Please sign in to comment.