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
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/how-to/databases-and-queries.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ A query vector for `vectorNeighbors` can be a numpy array or a list. This sectio

* `first()` returns the first row, or `None` when there is none, so `if db.query(...).first():` tests whether anything matched.
* `to_list()` materialises the whole result as a list of dicts, one Python dict per row. Use it for a small result, or when you need the whole result in memory; iterate when you stop early. For a large result, see <<reading-large-results>>.
* `to_json_list(batch_size=10_000)` returns the rows as a list of JSON-native dicts, the same on both builds. A `DATE` or `DATETIME` is a number of epoch milliseconds, a record id is its text, and every `DECIMAL` is a `float` whatever its scale, so `Decimal("3")` is `3.0` and one column has one Python type. A `float` keeps about 17 significant digits, so a `DECIMAL` with more digits is rounded: `to_list()` returns the `Decimal` with every digit.
* `iter_json_batches(batch_size=10_000)` yields the rows as lists of JSON-native dicts, `batch_size` rows per list (the last may be shorter), with the same values as `to_json_list()`. On the native build `batch_size` bounds memory: the result set holds one reply of the cursor (2048 rows) and one batch at a time, and reads the next reply only when the next batch is asked for, so a large result is never held whole. On the JVM build a batch is serialised on the Java side one at a time. `to_list()`, `to_json_list()`, `to_dataframe()`, `to_columns()`, `to_arrow()` and `to_polars()` still hold the whole result, because they return it whole.
+
One laptop study on the native build, 1,000,000 rows read with `batch_size=10_000`, 10 clean pairs of this change against the previous behaviour; each figure is the ratio of the new value to the old one, so below 1 is lower. Peak RSS: 0.286 (95% interval 0.284 to 0.289). Time to the first batch: 0.010 (95% interval 0.009 to 0.010). Wall time of the whole read: 0.958 (95% interval 0.952 to 0.966). This is one machine and one result shape, not a speed promise.
Expand Down
6 changes: 6 additions & 0 deletions src/arcadedb/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,12 @@ def to_json_list(self, batch_size: int = 10_000) -> List[Dict[str, Any]]:
floats. Use ``to_list()`` when you want ``date``, ``datetime`` and
``Decimal`` values: it costs about the same.

A DECIMAL is a ``float`` whatever its scale, on both builds, so one
column has one Python type: ``Decimal("3")`` arrives as ``3.0``, not
``3``. A float keeps about 17 significant digits, so a DECIMAL with more
digits is rounded (``Decimal("12345678901234567890")`` arrives as
``1.2345678901234567e+19``); ``to_list()`` keeps every digit.

A record id (a ``LINK`` value, ``@rid``) arrives as its text
(``"#10:5"``), at the top level and inside a list or a map, and a map key
that is not a string arrives as text (``"1"``, ``"true"``, ``"null"``),
Expand Down
18 changes: 13 additions & 5 deletions src/java/com/arcadedb/python/RowBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
* Compiled into arcadedb-python-bridge.jar during the wheel build
* (scripts/Dockerfile.build and scripts/build-native.sh) and consumed by
* ResultSet.to_json_list() in the Python bindings. Values carry JSON-native
* types (DATE and DATETIME as epoch-millisecond integers), as documented on
* the Python side.
* types (DATE and DATETIME as epoch-millisecond integers, every DECIMAL as a
* double), as documented on the Python side.
*/
package com.arcadedb.python;

Expand All @@ -35,6 +35,7 @@
import com.arcadedb.serializer.json.JSONArray;
import com.arcadedb.serializer.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -54,9 +55,10 @@ private RowBatcher() {
* NaN, Infinity and -Infinity (a Double or Float, or an element of a float[], a double[], a Collection or a Map, at any
* depth) replaced by the tag {@code {"\u0001": ["f", "NaN"]}} (the text is {@code Double.toString}), which Python turns
* back into a float. A Float, and each element of a float[], is also widened to a double (a float[] becomes a double[]),
* so the JSON text is the exact double and Python reads the same float that to_list() returns (#314). A value that is
* neither a Float nor a float[] and has nothing non-finite is returned as it is, with no copy, so the common row costs
* one type test per value.
* so the JSON text is the exact double and Python reads the same float that to_list() returns (#314). A BigDecimal is
* turned into the double nearest to it, whatever its scale, so a DECIMAL is a Python float on both builds (#452); a value
* that is too large for a double is the tagged infinity. A value that is none of these and has nothing non-finite is
* returned as it is, with no copy, so the common row costs a few type tests per value.
*/
static Object keepNonFinite(final Object value) {
if (value instanceof Double) {
Expand All @@ -69,6 +71,12 @@ static Object keepNonFinite(final Object value) {
final double d = ((Float) value).doubleValue();
return Double.isFinite(d) ? (Object) d : tag(d);
}
if (value instanceof BigDecimal decimal) {
// the JSON writer prints a BigDecimal with its own digits, so a scale of 0 has no fraction ("3"), which Python reads
// as an int; the native build returns float(Decimal) for every DECIMAL, and so does this
final double d = decimal.doubleValue();
return Double.isFinite(d) ? (Object) d : tag(d);
}
if (value instanceof float[] floats) {
final double[] wide = new double[floats.length];
boolean finite = true;
Expand Down
41 changes: 27 additions & 14 deletions tests/test_cross_wheel_golden.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
widening on the native build (``0.10000000149011612``); a DECIMAL keeps its scale on the JVM build (``1.50``) and is a float text on
the native build (``1.5``); in a projection row a DATE is epoch milliseconds and a DATETIME is text on the JVM build, days and
milliseconds on the native build; the keys of a record row come in a different order.
- ``to_json_list()`` returns a DECIMAL of scale 0 as an ``int`` on the JVM build and as a ``float`` on the native build.
- The distance of a vector search can differ in the last bits of the float32 between the builds, and between two calls on the JVM
build.
"""
Expand Down Expand Up @@ -328,18 +327,21 @@ def test_to_json_of_a_record_row_golden(golden):
"n": 0,
"whole": Decimal("3"),
"scaled": Decimal("2.00"),
"huge": Decimal("12345678901234567890"),
"fl": [0.5, 0.25, -2.75],
"fd": [0.1, 1 / 3],
},
{
"n": 1,
"whole": Decimal("-7"),
"scaled": Decimal("-4.0"),
"huge": Decimal("-12345678901234567890"),
"fl": [1.0, 2.0, 4.0],
"fd": [0.2, 0.7],
},
]
MORE_SELECT = "SELECT n, whole, scaled, fl, fd FROM GoldenMore ORDER BY n"
DECIMAL_SELECT = "SELECT n, whole, scaled, huge FROM GoldenMore ORDER BY n"


@pytest.fixture
Expand All @@ -350,6 +352,7 @@ def golden_more(temp_db):
("n", "INTEGER"),
("whole", "DECIMAL"),
("scaled", "DECIMAL"),
("huge", "DECIMAL"),
("fl", "LIST"),
("fd", "LIST"),
]:
Expand Down Expand Up @@ -380,23 +383,33 @@ def test_to_arrow_list_of_doubles_column_golden(golden_more, name):


def test_to_json_list_integral_decimal(golden_more):
"""An integral DECIMAL in ``to_json_list()``: the number is equal on both builds, its Python type is not yet (#314, open).
"""Every DECIMAL in ``to_json_list()`` is a float on both builds, whatever its scale (#452).

``to_json_list()`` documents a DECIMAL as a float. For ``Decimal("3")`` the native build returned the float ``3.0`` and the
JVM build returned the int ``3``, on engine 26.11.1 and on 26.12.1-SNAPSHOT: a DECIMAL of scale 0 has no fraction digit in the
JSON of the JVM build. ``3 == 3.0`` holds for both, so the first assertion is the part that is equal, and the type is asserted
as one of the two until the builds agree. With a fraction digit (``Decimal("2.00")``) both builds return a float. ``to_list()``
returns the ``Decimal`` on both.
``to_json_list()`` documents a DECIMAL as a float. The JVM build used to return the int ``3`` for ``Decimal("3")`` (a DECIMAL
of scale 0 has no fraction digit in its JSON) and the int ``12345678901234567890`` for the 20-digit value, where the native
build returned the floats ``3.0`` and ``1.2345678901234567e+19``, so a column held two Python types and the numbers differed.
Both builds return the float of the value now: ``3.0``, ``-7.0``, the scaled ``2.0`` (a float on both before) and the
20-digit values, rounded to the nearest double. ``to_list()`` returns the ``Decimal`` with every digit on both.
"""
rows = golden_more.query("sql", MORE_SELECT).to_json_list()
assert [row["whole"] for row in rows] == [3, -7]
assert [float(row["whole"]) for row in rows] == [3.0, -7.0]
assert all(type(row["whole"]) in (int, float) for row in rows)
rows = golden_more.query("sql", DECIMAL_SELECT).to_json_list()
assert [row["whole"] for row in rows] == [3.0, -7.0]
assert [row["scaled"] for row in rows] == [2.0, -4.0]
assert all(type(row["scaled"]) is float for row in rows)
as_list = golden_more.query("sql", MORE_SELECT).to_list()
assert [row["huge"] for row in rows] == [
1.2345678901234567e19,
-1.2345678901234567e19,
]
for name in ("whole", "scaled", "huge"):
assert all(type(row[name]) is float for row in rows), name
batches = golden_more.query("sql", DECIMAL_SELECT).iter_json_batches(batch_size=1)
assert [row for batch in batches for row in batch] == rows
as_list = golden_more.query("sql", DECIMAL_SELECT).to_list()
assert [row["whole"] for row in as_list] == [Decimal("3"), Decimal("-7")]
assert all(type(row["whole"]) is Decimal for row in as_list)
assert [row["huge"] for row in as_list] == [
Decimal("12345678901234567890"),
Decimal("-12345678901234567890"),
]
for name in ("whole", "scaled", "huge"):
assert all(type(row[name]) is Decimal for row in as_list), name


# ---- vector distances ----
Expand Down
Loading