Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/arcadedb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,9 @@ def insert_dataframe(
Raises:
ImportError: If pyarrow is not installed.
ValueError: If the frame has no columns, duplicate or non-string
column names, ``batch_size`` is not a positive integer, or
``parallel`` is used with a vertex type.
column names, ``batch_size`` is not a positive integer,
``parallel`` is used with a vertex type, or a ``uint64`` column
holds a value above 2**63 - 1. Raised before anything is written.
TypeError: If a column has a type with no ArcadeDB mapping (time of
day, duration, map), or one that does not load into the property
type ``type_name`` declares for it (a float column into an
Expand Down
34 changes: 25 additions & 9 deletions src/arcadedb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,6 @@ def _column_values(
): # uint64 keeps the range check below
return chunk
if kind in ("integer", "floating", "bool"):
if native and types.is_uint64(chunk.type) and len(chunk) > chunk.null_count:
import pyarrow.compute as pc

if (
pc.max(chunk).as_py() > 2**63 - 1
): # insert_columns refuses it on the JPype path, with this text
raise ValueError(
f"column {name!r} holds a value beyond the 64-bit signed range"
)
if chunk.null_count == 0:
values = chunk.to_numpy(zero_copy_only=False)
if values.dtype.kind == "f" and values.dtype.itemsize < 8:
Expand All @@ -448,6 +439,30 @@ def _column_values(
return chunk.to_pylist()


def _check_uint64_range(pa, table) -> None:
"""Raise ``ValueError`` when a uint64 column of ``table`` (a dictionary-encoded one included) holds a value beyond the 64-bit signed range.

The engine stores integers as signed 64-bit longs. The whole table is checked before the first batch is written: a check
made as each batch is converted would leave the batches before the bad value committed, and a batch with nulls reaches the
JPype build as Python ints, where the value raised ``OverflowError`` instead.
"""
import pyarrow.compute as pc

for field, column in zip(table.schema, table.columns):
value_type = (
field.type.value_type if pa.types.is_dictionary(field.type) else field.type
)
if not pa.types.is_uint64(value_type):
continue
if value_type is not field.type:
column = column.cast(value_type)
largest = pc.max(column).as_py() # None when every value is null
if largest is not None and largest > 2**63 - 1:
raise ValueError(
f"column {field.name!r} holds a value beyond the 64-bit signed range"
)


def insert_table(
db,
type_name: str,
Expand Down Expand Up @@ -491,6 +506,7 @@ def insert_table(
)
if table.num_rows == 0:
return 0
_check_uint64_range(pa, table)

native = _is_native(db)
written = 0
Expand Down
42 changes: 42 additions & 0 deletions tests/test_dataframe_columns_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,48 @@ def test_a_uint64_value_past_the_signed_range_is_refused(temp_db):
assert _count(temp_db, "Big") == 0


@pytest.mark.parametrize(
"values",
[[1, 2, 3, 2**63 + 5], [1, 2, None, 2**63 + 5]],
ids=["no_null", "with_null"],
)
def test_a_uint64_past_the_signed_range_in_a_late_batch_is_refused_before_any_row_is_written(
temp_db, values
):
"""The range check read each batch as it was written, so the batches before the bad value were
committed when the ValueError came (and on the JVM build a column with nulls raised OverflowError).
"""
temp_db.command("sql", "CREATE DOCUMENT TYPE Big")
table = pa.table({"v": pa.array(values, pa.uint64())})
with pytest.raises(ValueError, match="64-bit signed"):
temp_db.insert_arrow("Big", table, batch_size=2)
assert _count(temp_db, "Big") == 0


def test_a_uint64_dictionary_column_past_the_signed_range_is_refused(temp_db):
temp_db.command("sql", "CREATE DOCUMENT TYPE Big")
column = pa.array([1, 2**63, 1], pa.uint64()).dictionary_encode()
with pytest.raises(ValueError, match="64-bit signed"):
temp_db.insert_arrow("Big", pa.table({"v": column}), batch_size=2)
assert _count(temp_db, "Big") == 0


def test_a_uint64_column_at_the_signed_limit_loads(temp_db):
temp_db.command("sql", "CREATE DOCUMENT TYPE Big")
values = [1, None, 2**63 - 1]
assert (
temp_db.insert_arrow(
"Big", pa.table({"v": pa.array(values, pa.uint64())}), batch_size=2
)
== 3
)
rows = temp_db.query("sql", "SELECT v FROM Big").to_list()
assert sorted(r.get("v") for r in rows if r.get("v") is not None) == [
1,
2**63 - 1,
]


def test_a_type_with_no_mapping_is_refused_before_a_row_is_written(temp_db):
temp_db.command("sql", "CREATE DOCUMENT TYPE NoMap")
with pytest.raises(TypeError, match="no ArcadeDB mapping"):
Expand Down
Loading