Skip to content
2 changes: 2 additions & 0 deletions docs/modules/ROOT/pages/how-to/bulk-loads.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Because the two packages have different defaults, the same program can keep diff

On both packages, `insert_many` keeps rows with `datetime`, `date`, `Decimal`, `bytes`, numpy scalar, or pandas `NA` and `NaT` values on the bulk path. On the `arcadedb` (native) package, a row with a value the bulk text cannot carry (NaN or infinity, an integer beyond 64 bits, a date-time or `Decimal` inside a list or dict, or a `datetime.time`) goes in with its own statement, in the order given.

On the `arcadedb-jvm` package, a chunk of rows (at most 10,000, at least 64) crosses as typed columns, with no JSON text, when every row is a `dict` with the same keys in the same order and each column holds `None` and values of one type: `int` within 64 bits, `float` (NaN and infinity included), `bool`, `str`, `date`, or a `datetime` without a time zone. The stored values are the same as the JSON text stores: a whole number is an `Integer` when it fits an int and a `Long` beyond, and `None` is a null property. A row with a NaN, an infinity, or a subnormal `float` stores its whole numbers as `Long`, which is what the per-row path stores for such a row. Any other chunk keeps the JSON text, as before: a row that lacks a key or lists the keys in another order, a column that mixes types (an `int` beside a `float`), a `Decimal`, `bytes`, numpy or pandas value, a `datetime` with a time zone, a list, tuple or dict, and a float vector for a property declared `ARRAY_OF_FLOATS`. A chunk is judged as a whole, so one such row sends its 10,000 rows down the JSON route. A chunk of fewer than 64 rows keeps the JSON text too (a call with a few rows, or the last rows of a load): the typed columns have a fixed cost per chunk, and below about 64 rows the text is the faster of the two. The `arcadedb` (native) package already sends the rows of one shape through its columnar entry.

`insert_many` and `create_vertices` check their rows while they write the JSON text, in one pass over the rows, not in a loop before it. The same rows fail the check as before, and they take the same fallback path as before. The check raises no error to your code.

[[columnar-loads]]
Expand Down
206 changes: 201 additions & 5 deletions src/arcadedb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import itertools
import operator
from collections.abc import Mapping
from datetime import date
from datetime import date, datetime
from decimal import Decimal
from os import PathLike
from typing import TYPE_CHECKING, Any, Callable, List, Optional
Expand Down Expand Up @@ -36,6 +36,8 @@
from .results import ResultSet
from .transactions import TransactionContext
from .type_conversion import (
_INT64_MAX,
_INT64_MIN,
_is_numpy_bool,
bulk_row_ok,
convert_python_to_java,
Expand Down Expand Up @@ -336,6 +338,156 @@ def _column_to_java(name, values):
return jpype.JArray(jpype.JObject)(converted), len(converted)


# insert_many: the kinds of column a chunk can cross as typed columns (see _typed_chunk_columns). The exact types only: a subclass
# (pandas.Timestamp, numpy.float64) and every other type (Decimal, bytes, list, dict, tuple) keep the JSON text.
_TYPED_KINDS = {
int: "int",
float: "float",
bool: "bool",
str: "str",
date: "date",
datetime: "datetime",
}
_DICT_ONLY = frozenset((dict,))
# The types a value of a typed chunk may have: when the first row of a chunk holds another one (a Decimal, bytes, a list), the
# chunk is refused there, before any column is read (see _typed_chunk_columns).
_TYPED_TYPES = frozenset(_TYPED_KINDS) | {_NONE_TYPE}
# A chunk of fewer rows keeps the JSON text. The typed route builds at least one bridge array per column, a fixed cost per call
# that the JSON text does not have: in the first timed A/B (ArcadeData/arcadedb-python#392, 2026-11-22) a call of one row took
# 1.9 to 2.5 times as long on the typed route. A probe of both routes at 1 to 128 rows put the crossover between 16 and 64 rows
# for four row shapes (it depends on the kinds of the columns); at 64 rows the typed route was the faster one for all four.
_TYPED_MIN_ROWS = 64
_STR_ONLY = frozenset((str,))
_NO_ZONE = frozenset((None,))
_EPOCH_ORDINAL = date(1970, 1, 1).toordinal()
_EPOCH_DATE = date(1970, 1, 1)
_EPOCH_DATETIME = datetime(1970, 1, 1)


def _typed_kind(values):
"""``(kind, nullable)`` of one column of an insert_many chunk when it can cross as a typed column, else None.

The column holds ``None`` and values of ONE exact type of ``_TYPED_KINDS`` (``"null"`` when there is nothing but ``None``).
A mixed column (an int beside a float, a bool beside an int) would store each value as its own type on the JSON route, which one
typed array cannot do; an int beyond 64 bits, a date-time with a zone and a type the table does not list keep the JSON route.
"""
kinds = set(map(type, values))
nullable = _NONE_TYPE in kinds
kinds.discard(_NONE_TYPE)
if not kinds:
return "null", True
if len(kinds) != 1:
return None
kind = _TYPED_KINDS.get(next(iter(kinds)))
if kind == "int":
present = [v for v in values if v is not None] if nullable else values
if min(present) < _INT64_MIN or max(present) > _INT64_MAX:
return None
elif kind == "datetime":
present = [v for v in values if v is not None] if nullable else values
if set(map(operator.attrgetter("tzinfo"), present)) != _NO_ZONE:
return None
return (kind, nullable) if kind else None


def _typed_java_column(name, values, kind, nullable):
"""One column of an insert_many chunk as the Java column ``DocumentBatcher.insertManyColumns`` takes, or None.

A whole column of int, float or bool without a null is one numpy array and one buffer copy (``long[]``, ``double[]``,
``boolean[]``); a column of str and None is one ``StringColumn``; date and naive date-time columns are one ``TemporalColumn``
(a ``long[]`` of epoch days or microseconds, and one ``boolean[]`` when there is a null); a numeric column with a null converts
per element into an ``Object[]``, as ``insert_columns`` does, and a column of nothing but None is an ``Object[]`` of nulls.
"""
n = len(values)
if kind == "null":
return jpype.JArray(jpype.JObject)([None] * n)
if kind == "str":
return _string_column(values)
if kind == "date" or kind == "datetime":
mask = None
if nullable:
mask = jpype.JArray(jpype.JBoolean)(
_np.fromiter(
map(operator.is_, values, itertools.repeat(None)),
dtype=_np.bool_,
count=n,
)
)
filler = _EPOCH_DATE if kind == "date" else _EPOCH_DATETIME
values = [filler if v is None else v for v in values]
temporal = _java_class("com.arcadedb.python.TemporalColumn")
if kind == "date":
ticks = (
_np.fromiter(map(date.toordinal, values), dtype=_np.int64, count=n)
- _EPOCH_ORDINAL
)
return temporal.dates(jpype.JArray(jpype.JLong)(ticks), 1, mask)
ticks = _np.array(values, dtype="datetime64[us]").astype(_np.int64)
return temporal.dateTimes(jpype.JArray(jpype.JLong)(ticks), 1_000_000, mask)
if nullable:
return _column_to_java(name, values)[0]
dtype = {"int": _np.int64, "float": _np.float64, "bool": _np.bool_}[kind]
try:
array = _np.fromiter(values, dtype=dtype, count=n)
except (OverflowError, ValueError, TypeError):
return None
return _column_to_java(name, array)[0]


def _typed_chunk_columns(chunk):
"""``(names, columns)`` for one chunk of insert_many rows that can cross as typed columns, else None (the chunk keeps the JSON text).

The chunk qualifies when it has at least ``_TYPED_MIN_ROWS`` rows (below that the bridge arrays cost more than the text they
replace), every row is a ``dict`` with the same str keys in the same order (a row that lacks a key, has another
one, or lists them in another order is a different shape, and a record keeps its properties in the order they were set), and every
column holds one kind of value (``_typed_kind``). Whether a column qualifies is decided from the types alone before any bridge
array is built, so a chunk that cannot cross this way costs the census and nothing else, and a chunk whose first row holds a
value of a type no typed column carries (a ``Decimal``, ``bytes``, a list) costs one look at that row: such a column is refused
before any column is read, so a load of such rows does not pay for the census chunk after chunk. The result is stored as the JSON text
stores it: ``DocumentBatcher.insertManyColumns`` stores a whole number as an Integer when it fits an int, as the text parser
does, and ``None`` is a null property, never a missing one. A row with a NaN, an infinity or a subnormal float is the exception: the
text cannot carry it, so the JSON route inserts that row on the per-row path, which stores its whole numbers as Long, and the
bridge stores them as Long for that row too.
"""
if _np is None or not chunk or len(chunk) < _TYPED_MIN_ROWS:
return None
first = chunk[0]
if type(first) is not dict or not _TYPED_TYPES.issuperset(
map(type, first.values())
):
return None
if set(map(type, chunk)) != _DICT_ONLY:
return None
names = tuple(first)
if not names or set(map(type, names)) != _STR_ONLY:
return None
# Every row has the keys of the first one, in its order. The number of keys first: one pass that builds nothing per row, and
# it finds the row with a key more or a key less. Then the keys themselves, which stops at the first row that lists them
# otherwise. One set of the key tuples of all rows did both at once, and a chunk that keeps the JSON text paid a tuple and a
# hash per row for it: 1.2 to 2.5 percent of the whole call in the timed A/B of 2026-11-22 (rows of nine keys, the last row
# of each chunk with a key more).
if set(map(len, chunk)) != {len(names)} or not all(
map(names.__eq__, map(tuple, chunk))
):
return None
lists = []
shapes = []
for name in names:
values = list(map(operator.itemgetter(name), chunk))
shape = _typed_kind(values)
if shape is None:
return None
lists.append(values)
shapes.append(shape)
columns = []
for name, values, (kind, nullable) in zip(names, lists, shapes):
column = _typed_java_column(name, values, kind, nullable)
if column is None:
return None
columns.append(column)
return names, columns


def _wrap_java_record(java_record, database=None):
"""Wrap a Java record in the matching Python class (Vertex/Edge/Document)."""
if java_record is None:
Expand Down Expand Up @@ -765,11 +917,18 @@ def insert_many(
) -> int:
"""Bulk-insert documents with one FFI crossing per chunk of rows.

The iterable is read 10,000 rows at a time; each chunk is serialized
to one JSON string and looped Java-side (``DocumentBatcher``),
avoiding the per-row JNI cost that caps ``new_document``-loop ingest.
The iterable is read 10,000 rows at a time; each chunk crosses once and
is looped Java-side (``DocumentBatcher``), avoiding the per-row JNI cost
that caps ``new_document``-loop ingest. A chunk of at least 64 rows whose rows are dicts with
the same keys in the same order, and whose columns each hold ``None`` and
values of one type (``int`` within 64 bits, ``float``, ``bool``, ``str``,
``date``, or a ``datetime`` without a zone), crosses as typed columns, with
no text (``DocumentBatcher.insertManyColumns``); a whole number is stored
as an Integer when it fits an int and as a Long beyond, which is what the
JSON text stored (a row with a NaN, an infinity or a subnormal ``float``
stores its whole numbers as Long, as the per-row path always did). Any other chunk is serialized to one JSON string.
Memory stays bounded by the chunk whatever the row count, so a
generator of any length can be loaded (#294). A value that sits directly
generator of any length can be loaded (#294). On the JSON route, a value that sits directly
in a row may be JSON-representable (str/int/float/bool/None, nested
lists/dicts), a ``datetime`` (also ``pandas.Timestamp`` and
``numpy.datetime64``), ``date``, ``Decimal``, ``bytes``/``bytearray``,
Expand Down Expand Up @@ -944,6 +1103,12 @@ def _insert_many_chunk(self, type_name, chunk):
still go in bulk, and every row is inserted in the order given. Dates, date-times, Decimals, bytes, numpy scalars and
pandas NA and NaT sitting directly in a row cross inside the text as tagged values (see json_bulk_dumps_tagged).
"""
# A chunk of at least _TYPED_MIN_ROWS rows with the same keys whose columns each hold one kind of value crosses as typed columns, with no text
# (see _typed_chunk_columns); every other chunk keeps the JSON text below, as it was.
typed = _typed_chunk_columns(chunk)
if typed is not None:
self._insert_many_typed(type_name, typed, len(chunk))
return
# Float vectors for ARRAY_OF_FLOATS properties cross as one binary matrix
# beside the JSON, not as text (see split_float_vectors).
split = split_float_vectors(self._java_db, type_name, chunk)
Expand All @@ -954,6 +1119,21 @@ def _insert_many_chunk(self, type_name, chunk):
return
self._insert_many_bulk(type_name, payload, split)

def _insert_many_typed(self, type_name, typed, n):
"""One chunk of insert_many as typed columns inside the open transaction (commitEvery 0: the transaction stays with insert_many)."""
names, columns = typed
try:
_java_class("com.arcadedb.python.DocumentBatcher").insertManyColumns(
self._java_db,
type_name,
jpype.JArray(jpype.JString)(names),
jpype.JArray(jpype.JObject)(columns),
n,
0,
)
except Exception as e:
raise ArcadeDBError(f"Failed to bulk-insert into '{type_name}': {e}") from e

def _insert_many_runs(self, type_name, chunk):
"""A chunk with a row the JSON text cannot carry: runs of carriable rows go in bulk, the other rows one by one, in order."""
skip = declared_float_vectors(self._java_db, type_name)
Expand Down Expand Up @@ -1013,6 +1193,22 @@ def _insert_many_parallel(self, type_name, first, rows):
chunk = first
try:
while chunk:
typed = _typed_chunk_columns(chunk)
if typed is not None:
try:
failures = batcher.insertManyColumnsParallel(
self._java_db,
type_name,
jpype.JArray(jpype.JString)(typed[0]),
jpype.JArray(jpype.JObject)(typed[1]),
len(chunk),
)
except Exception as e:
raise self._parallel_load_failed(type_name, e) from e
chunk_failures.append(failures)
n += len(chunk)
chunk = list(itertools.islice(rows, _INSERT_MANY_CHUNK))
continue
bad = []
split = split_float_vectors(self._java_db, type_name, chunk)
try:
Expand Down
Loading
Loading