Repository navigation
to_java_int_array wraps values outside the int32 range for a NumPy array, and the docs say the dtype does not matter #120
Description
Level: bindings. to_java_int_array(np.array([1, 2**40])) returns the Java array [1, 0]: the values wrap to 32 bits. 231 becomes -2147483648, -231 - 1 becomes 2147483647, a uint32 4294967295 becomes -1, and a uint64 2**63 becomes 0, all without an error. The same values given as a Python list raise OverflowError: Cannot convert value to Java int. The page for the function says a NumPy array "of any integer dtype" is accepted and that "the dtype does not matter (int64, NumPy's default, converts as fast as int32), so there is no reason to cast before calling" (api/vector.md L161-L192). The documented use is the token-index side of a sparse vector, where a wrapped id silently points at another token.
Repro
The script prints to stdout. The JVM's own log lines go to stderr and are discarded by the command below.
uv venv .venv
uv pip install --python .venv/bin/python arcadedb_embedded-26.10.1.dev0-cp312-cp312-manylinux_2_34_x86_64.whl numpy
.venv/bin/python repro-13-to-java-int-array-wraps.py 2>/dev/null"""to_java_int_array wraps values outside the int32 range for a NumPy array, and raises OverflowError for the same list."""
import numpy as np
from arcadedb_embedded.jvm import start_jvm
from arcadedb_embedded.vector import to_java_int_array
start_jvm(heap_size="1g")
cases = {
"int64 [1, 2**40]": np.array([1, 2**40]),
"int64 [2**31]": np.array([2**31]),
"int64 [-2**31 - 1]": np.array([-(2**31) - 1]),
"uint32 [2**32 - 1]": np.array([2**32 - 1], dtype=np.uint32),
"uint64 [2**63]": np.array([2**63], dtype=np.uint64),
"int32 [1, 2**31 - 1]": np.array([1, 2**31 - 1], dtype=np.int32),
}
print(f"{'input':<24} {'as a NumPy array':<24} as a list of the same values")
for label, arr in cases.items():
try:
from_array = list(to_java_int_array(arr))
except Exception as e:
from_array = type(e).__name__
try:
from_list = list(to_java_int_array(arr.tolist()))
except Exception as e:
from_list = type(e).__name__
print(f"{label:<24} {str(from_array):<24} {from_list}")Output
Wheel arcadedb_embedded-26.10.1.dev0-cp312-cp312-manylinux_2_34_x86_64.whl with its bundled JRE (OpenJDK 25.0.4.1), JPype 1.7.1, and Python 3.12.13. The engine build the wheel reports through com.arcadedb.Constants is 26.10.1-SNAPSHOT (build 04bb03ebec07fce19101595d65ca1e3bdf516755), an ancestor of upstream cecb369df9. The repository is at f92ac1381c8335249a6fde644cbf04594f6d6391, and the installed Python sources are identical to bindings/python/src/arcadedb_embedded/ at that commit.
input as a NumPy array as a list of the same values
int64 [1, 2**40] [1, 0] OverflowError
int64 [2**31] [-2147483648] OverflowError
int64 [-2**31 - 1] [2147483647] OverflowError
uint32 [2**32 - 1] [-1] OverflowError
uint64 [2**63] [0] OverflowError
int32 [1, 2**31 - 1] [1, 2147483647] [1, 2147483647]
Cause
For a numpy.ndarray the function passes the array straight to JArray(JInt) (vector.py L77-L78), which copies through the buffer protocol (the function's docstring says so) and does not range-check. For a list it goes through JArray(JInt)(list) (vector.py L83), which converts element by element and raises.
Expected
Both inputs raise OverflowError (or ArcadeDBError) for a value outside the int32 range.
Proposed fix
Check the range of an integer array before the copy. A run-time monkeypatch of this check makes the repro raise OverflowError for every out-of-range array and leaves the in-range int32 array unchanged:
--- a/bindings/python/src/arcadedb_embedded/vector.py
+++ b/bindings/python/src/arcadedb_embedded/vector.py
if _np is not None and isinstance(vector, _np.ndarray):
+ if vector.dtype.kind in "iu" and vector.size:
+ info = _np.iinfo(_np.int32)
+ if vector.min() < info.min or vector.max() > info.max:
+ raise OverflowError("Cannot convert value to Java int")
return jtypes.JArray(jtypes.JInt)(vector)The same pattern (a NumPy array copied through JArray without a range check) exists in to_java_byte_array for int and float arrays: an int32 array [1, 200] becomes [1, -56], which the function's docstring allows for the unsigned byte range, and a float array [1.7, 2.2] becomes [1, 2]. I left those out of this report because they match the documented behaviour.
Related issues
Nothing found upstream. ArcadeData#9003 (open) is a float32 conversion of positional parameters over HTTP, a different path.
In this repository's tracker no issue is about this. The searches for to_java_int_array and numpy found nothing.
Related reports from the same review: #119 (numpy bool).
Activity
bindings: numpy bool, int32 arrays, lookup_by_key, append_samples, ge…
benchmarks: the instrument changes for the 26.10.1 re-pin campaign, w…