RajaBabu15 commented on issue #55833:
URL: https://github.com/apache/arrow/issues/55833#issuecomment-7256437115

   I reproduced this on macOS arm64 with PyArrow 26.0.1 and with a debug build 
of main. It also crashes in the other order, when a number or string comes 
first. That path fails in the converters.
   
   ### Environment
   
   - macOS 26.5, arm64
   - PyArrow 26.0.1, Python 3.14.8
   - main at `6f040e8`: Debug, ASan and UBSan, Apple clang 21.0.0, CMake 4.5.1
   
   ### Reproduce on 26.0.1
   
   ```python
   import io
   import pyarrow.json as pj
   
   def read(first, second):
       options = pj.ReadOptions(block_size=len(first) + 1, use_threads=False)
       data = io.BytesIO(f"{first}\n{second}\n".encode())
       return pj.read_json(data, read_options=options).to_pylist()
   ```
   
   | Call | Observed |
   | --- | --- |
   | `read('{"c":1}', '{"c":{"x":1}}')` | segfault in 18 of 20 script runs; the 
other 2 raised `ArrowInvalid: Resize capacity must be positive` with a negative 
size |
   | `read('{"c":[1]}', '{"c":1}')` | segfault (20 of 20) |
   | `read('{"c":{"x":1}}', '{"c":1}')` | no error, returns `[{'c': {'x': 1}}, 
{'c': {'x': None}}]` (20 of 20 script runs) |
   
   Expected: `ArrowInvalid ... Column(/c) changed from ... to ...`. A 
single-block read already does this for object then number, and `open_json` 
does it for object and number in both orders.
   
   ### All kind pairs on main
   
   One field, one line per block, `use_threads=false`. Row is block 1, column 
is block 2.
   
   | | null | bool | number | string | list | object |
   | --- | --- | --- | --- | --- | --- | --- |
   | **null** | ok | ok | ok | ok | N | N |
   | **bool** | ok | ok | error | error | error | error |
   | **number** | ok | A | ok | error | A | A |
   | **string** | ok | A | ok | ok | A | A |
   | **list** | ok | B | B | B | ok | B |
   | **object** | ok | C | C | C | C | ok |
   
   - A: abort, `converter.cc:51`, `Check failed: (in->type_id()) == 
(Type::DICTIONARY)`
   - B: abort, `chunked_builder.cc:224`, `Check failed: 
(unconverted->type_id()) == (Type::LIST)`
   - C: abort, uncaught `std::bad_cast`
   - N: `NotImplemented: JSON conversion to ... is not supported`. This looks 
like #20253, a separate path.
   - error: returns `Invalid`
   
   <details>
   <summary>Stack: number then object on main (A)</summary>
   
   Frames shortened.
   
   ```text
   converter.cc:51        GetDictionaryArray
   converter.cc:123       NumericConverter<Int64Type>::Convert
   chunked_builder.cc:138 InferringChunkedArrayBuilder::TryConvertChunk
   chunked_builder.cc:120 InferringChunkedArrayBuilder::Insert
   chunked_builder.cc:419 ChunkedStructArrayBuilder::InsertChildren
   reader.cc:290          TableReaderImpl::ParseAndInsert
   reader.cc:269          TableReaderImpl::Read
   ```
   </details>
   
   <details>
   <summary>Stack: object then number on main (C)</summary>
   
   ```text
   checked_cast.h:38      checked_cast<const StructArray&>
                          ChunkedStructArrayBuilder::Insert
   chunked_builder.cc:419 ChunkedStructArrayBuilder::InsertChildren
                          ChunkedStructArrayBuilder::Insert
   reader.cc:290          TableReaderImpl::ParseAndInsert
   reader.cc:269          TableReaderImpl::Read
   ```
   </details>
   
   <details>
   <summary>macOS crash report: list then number on 26.0.1</summary>
   
   Argument lists and some namespaces shortened.
   
   ```text
   EXC_BAD_ACCESS (SIGSEGV), KERN_INVALID_ADDRESS at 0x0000000000000008
   0 libarrow.2600.dylib  
arrow::json::InferringChunkedArrayBuilder::Insert(...) + 708
   1 libarrow.2600.dylib  arrow::json::ChunkedListArrayBuilder::Insert(...) + 
676
   2 libarrow.2600.dylib  arrow::json::ChunkedStructArrayBuilder::Insert(...) + 
1124
   3 libarrow.2600.dylib  TableReaderImpl::Read() lambda invoke + 200
   4 libarrow.2600.dylib  SerialTaskGroup::AppendReal(...) + 132
   5 libarrow.2600.dylib  arrow::json::TableReaderImpl::Read() + 596
   6 _json.cpython-314-darwin.so  pyarrow._json.read_json + 2588
   ```
   </details>
   
   <details>
   <summary>C++ reproducer and build commands</summary>
   
   ```cpp
   #include <iostream>
   #include <string>
   
   #include "arrow/api.h"
   #include "arrow/io/memory.h"
   #include "arrow/json/api.h"
   
   arrow::Status Run(const std::string& first, const std::string& second) {
     auto input = std::make_shared<arrow::io::BufferReader>(
         arrow::Buffer::FromString(first + "\n" + second + "\n"));
     auto read = arrow::json::ReadOptions::Defaults();
     read.use_threads = false;
     read.block_size = static_cast<int32_t>(first.size() + 1);
     ARROW_ASSIGN_OR_RAISE(auto reader, arrow::json::TableReader::Make(
                                            arrow::default_memory_pool(), 
input, read,
                                            
arrow::json::ParseOptions::Defaults()));
     ARROW_ASSIGN_OR_RAISE(auto table, reader->Read());
     ARROW_RETURN_NOT_OK(table->ValidateFull());
     std::cout << "returned " << table->num_rows() << " rows: "
               << table->schema()->ToString() << std::endl;
     return arrow::Status::OK();
   }
   
   int main(int, char** argv) {
     arrow::Status status = Run(argv[1], argv[2]);
     if (!status.ok()) std::cout << status.ToString() << std::endl;
   }
   ```
   
   From the repository root:
   
   ```bash
   cmake -S cpp -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug \
     -DARROW_DEPENDENCY_SOURCE=BUNDLED -DARROW_JSON=ON -DARROW_BUILD_TESTS=ON \
     -DARROW_BUILD_STATIC=OFF -DARROW_USE_ASAN=ON -DARROW_USE_UBSAN=ON
   cmake --build build --target arrow-json-test
   c++ -std=c++20 -g -fsanitize=address,undefined -Icpp/src -Ibuild/src 
repro.cc \
     -Lbuild/debug -larrow -Wl,-rpath,$PWD/build/debug -o repro
   ./repro '{"c":1}' '{"c":{"x":1}}'
   ```
   </details>
   
   ### Notes for a fix
   
   - Both traces above go through `InsertChildren`, the place the report points 
to. When the number comes first, the failure is further down: 
`GetDictionaryArray` only has a `DCHECK`. `BooleanConverter` already returns 
`Invalid` for a wrong input type.
   - A check in `InsertChildren` alone would miss values inside lists. 
`{"c":[1]}` then `{"c":[{"x":1}]}` also aborts at `converter.cc:51`.
   - The 246 tests in `arrow-json-test` pass on this build.
   - Not checked: Linux, x86, nested explicit schemas.
   
   Is anyone working on a fix? If not, I would like to take it.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to