apache / apache/arrow

[Python] JSON parsing segment fault on long records (block_size) dependent

Open
#28,990 2 comments 0 reactions 0 assignees View on GitHub
Component: Python Type: bug
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

Hello,

 

I have a big JSON file (~300MB) with complex records (nested json, nested lists of jsons). When I try to read this with pyarrow I am getting a segmentation fault. I tried then couple of things from read options, please see the code below (I developed this code on an example file that was attached here: https://github.com/apache/arrow/issues/25674):

 
```python

from pyarrow import json
from pyarrow.json import ReadOptions
import tqdm

if __name__ == '__main__':

source = 'wiki_04.jsonl'

ro = ReadOptions(block_size=2**20)

with open(source, 'r') as file:
for i, line in tqdm.tqdm(enumerate(file)):
with open('temp_file_arrow_3.ndjson', 'a') as file2:
file2.write(line)
json.read_json('temp_file_arrow_3.ndjson', read_options=ro)
```
For both the example file and my file, this code will return the straddling object exception (or seg fault) once the file reach the block_size. Increasing the block_size will make the code fail later.

Then I tried, on my file, to put an explicit schema:
```python

from pyarrow import json
from pyarrow.json import ReadOptions
import pandas as pd

if __name__ == '__main__':

source = 'my_file.jsonl'

df = pd.read_json(source, lines=True)
table_schema = pa.Table.from_pandas(df).schema

ro = ReadOptions(explicit_schema = table_schema)
table = json.read_json(source, read_options=ro)

```
This works, which may suggest that this issue, and the issue of the linked JIRA issue, are only appearing when an explicit schema is not provided. Additionally the following code works as well:
```python

from pyarrow import json
from pyarrow.json import ReadOptions
import pandas as pd

if __name__ == '__main__':

source = 'my_file.jsonl'

ro = ReadOptions(block_size = 2**30)
table = json.read_json(source, read_options=ro)

```
The block_size is bigger than my file in this case. Is it possible that the schema is defined in the first block and then if the schema changes, I get a seg fault?

I cannot share my json file, however, I hope that someone could add some clarity on what I am seeing and maybe suggest a workaround.

Thank you,
Guido

**Reporter**: [Guido Muscioni](https://issues.apache.org/jira/browse/ARROW-13314)
#### Related issues:
- [[Python] Automatically back on larger IO block size when JSON parsing fails](https://github.com/apache/arrow/issues/25674) (is related to)

**Note**: *This issue was originally created as [ARROW-13314](https://issues.apache.org/jira/browse/ARROW-13314). Please see the [migration documentation](https://github.com/apache/arrow/issues/14542) for further details.*

Contributor guide

Open the contributing guide

Research direction

Start by running the reported Python reproducer with wiki_04.jsonl and temp_file_arrow_3.ndjson, varying ReadOptions.block_size and explicit_schema. Compare the segmentation fault or straddling-object exception with the successful 2**30 block_size and explicit-schema cases; done means the long-record input no longer crashes or raises the reported parsing failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
data-engineering
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.