Repository navigation
to_columns, to_dataframe, and to_arrow drop every property that the first row lacks, and export_to_csv raises after writing part of the file #113
Description
Level: bindings (Java bridge and Python). ArcadeDB documents are schemaless, so a property that is missing from the first row of a result set is legal and common (an optional field). ResultSet.to_columns, to_dataframe, and to_arrow take the column set from the first row only, so a property that the first row lacks is dropped from the result without an error, while to_list and to_json_list return it. Reversing the order of the same rows brings the column back. Database.export_to_csv takes its header from the first row too, so it raises ArcadeDBError on the second row, after the header and the first row are already in the file.
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 pandas pyarrow
.venv/bin/python repro-6-columnar-drops-columns.py 2>/dev/null"""to_columns, to_dataframe and to_arrow take the column set from the FIRST row, so a property the first row lacks is dropped."""
import os
import tempfile
import arcadedb_embedded as arcade
from arcadedb_embedded.jvm import start_jvm
start_jvm(heap_size="2g")
db = arcade.create_database(os.path.join(tempfile.mkdtemp(prefix="hb6-"), "db"))
db.command("sql", "CREATE DOCUMENT TYPE Event")
with db.transaction():
db.command("sql", "INSERT INTO Event SET n = 1, kind = 'start'")
db.command("sql", "INSERT INTO Event SET n = 2, kind = 'stop', reason = 'timeout'")
db.command("sql", "INSERT INTO Event SET n = 3, kind = 'stop', reason = 'user'")
Q = "SELECT FROM Event ORDER BY n"
print("to_list :", db.query("sql", Q).to_list())
print("to_json_list :", db.query("sql", Q).to_json_list())
print("to_columns :", {k: (v.tolist() if hasattr(v, "tolist") else v) for k, v in db.query("sql", Q).to_columns().items()})
print("to_dataframe :", db.query("sql", Q).to_dataframe().to_dict("list"))
print("to_arrow :", db.query("sql", Q).to_arrow().to_pydict())
print()
print("the same rows in the other order (first row has every property):")
Q2 = "SELECT FROM Event ORDER BY n DESC"
print("to_dataframe :", db.query("sql", Q2).to_dataframe().to_dict("list"))
db.close()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.
to_list : [{'n': 1, 'kind': 'start'}, {'n': 2, 'kind': 'stop', 'reason': 'timeout'}, {'n': 3, 'kind': 'stop', 'reason': 'user'}]
to_json_list : [{'n': 1, 'kind': 'start'}, {'n': 2, 'kind': 'stop', 'reason': 'timeout'}, {'n': 3, 'kind': 'stop', 'reason': 'user'}]
to_columns : {'n': [1, 2, 3], 'kind': ['start', 'stop', 'stop']}
to_dataframe : {'n': [1, 2, 3], 'kind': ['start', 'stop', 'stop']}
to_arrow : {'n': [1, 2, 3], 'kind': ['start', 'stop', 'stop']}
the same rows in the other order (first row has every property):
to_dataframe : {'n': [3, 2, 1], 'kind': ['stop', 'stop', 'start'], 'reason': ['user', 'timeout', nan]}
In the output, to_columns, to_dataframe, and to_arrow have lost reason, which two of the three rows carry. The same rows in the opposite order keep it, with nan for the row that lacks it (last line). export_to_csv on the same three rows raises CSV export failed: dict contains fields not in fieldnames: 'reason' after it wrote the header n,kind and the first row 1,start to the file.
Cause
ColumnBatcher.nextColumnBatch derives the column set from the first row of the first batch when it is given no spec (ColumnBatcher.java L61), and reads only those columns for every row after it. The Python side then pins that set for all later batches (first_names at results.py L481 and joined = json.dumps(first_names) at results.py L506; the same in to_arrow, L628 and L650). export_to_csv uses list(batch[0].keys()) (exporter.py L226).
The docs for to_dataframe (api/results.md L167-L177) say only that it converts the results to a DataFrame, and do not say that the columns come from the first row.
Expected
Every property that any row has appears as a column, with a null for the rows that lack it, as to_list() and to_json_list() show it.
Proposed fix
Compute the column set as the union of the property names of every row, in order of first appearance, instead of the first row's names. Within one batch that is a change to nextColumnBatch (a LinkedHashSet over results). Across batches the Python side must stop pinning the first batch's set: let each batch report its own columns and fill the missing ones with nulls when the batches are merged. export_to_csv needs the same union for its header; failing loudly on a key that first appears after the header was written is better than dropping it. This is a design sketch; I did not implement or test it.
Related issues
ArcadeData#6758 (closed) is the same pinned column spec, fixed only for names that contain a quote, a backslash, or a semicolon.
In this repository's tracker no issue is about this. The searches for to_columns, to_arrow, to_dataframe, dataframe, pyarrow, Arrow, and export_to_csv found only #38 (closed), a docs sweep that mentions the to_columns speed ratio.
Related reports from the same review: #114 (Arrow type follows batch size), #115 (DECIMAL columns).
Activity
fix(#113, #114, #115): columnar readers keep every column, a stable A…
docs: mutations_before_rebuild is the floor of the rebuild threshold (#…