Skip to content

Commit ee95e6b

Browse files
committed
Native: ColumnFrames writes rid, li8 and lf8 columns (refs #388, #315)
The native library wrote a column of record links and a column of lists as one tagged JSON array (json), parsed per cell in Python. ColumnFrames now writes rid for a column of valid RIDs (two int64 per row, a null row is 0, 0) and li8 or lf8 for a column of lists of integers or of floats (int32 offsets, a bitmap of null elements, one flat int64 or float64 array, a null row is an empty list under the row's null bit). Maps, nested lists, lists of strings, booleans or RIDs, lists that mix ints and floats, a list of nothing but nulls, an invalid RID (negative bucket or position), a column that mixes kinds and a column with more than 2**27 list elements in one frame stay json. The decoder and the JVM writer are the change this one is stacked on. The test now expects the new kinds from the native wheel as it does from the JVM wheel (KINDS).
1 parent f1ec4a3 commit ee95e6b

2 files changed

Lines changed: 130 additions & 11 deletions

File tree

‎native/src/main/java/org/humemai/nativelib/ColumnFrames.java‎

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
* b1 one byte per row dt int64 epoch milliseconds, dtu: epoch microseconds (see micros)
1414
* f4v dim float32 LE per row (float[]) f8v dim float64 LE per row (double[])
1515
* str int32 LE offsets (n + 1) then UTF-8 bytes dec a BigDecimal as text in the str layout
16+
* rid two int64 LE per row: the bucket and the position of a record link (the text "#bucket:position")
17+
* li8 a list of int64 per row (Long, Integer, Short, Byte or null elements) and lf8 a list of float64 (Double, Float or null
18+
* elements; NaN and the infinities are values): int32 offsets (n + 1), a bitmap of the null elements (one bit per element
19+
* of the flat array), then the elements of every list one after the other. A null row is an empty list under the row's
20+
* null bit; an empty list and a null element are what they were in the json form
1621
* json one JSON array of the column in the tagged encoding of NativeCodec: what no fixed layout carries without changing a value
17-
* (nested lists and maps, records and RIDs, BigInteger, a column that mixes kinds, a DATE or a time-zone-aware temporal next to
18-
* datetimes with micros, a ragged vector, a Long beyond 2**53 next to a floating value)
22+
* (maps, nested lists, lists of strings or of RIDs, lists that mix ints and floats, a list of nothing but nulls, embedded
23+
* records, other record values, a RID that is not valid, BigInteger, a column that mixes kinds, a DATE or a time-zone-aware
24+
* temporal next to datetimes with micros, a ragged vector, a Long beyond 2**53 next to a floating value)
1925
*
2026
* A frame holds at most {@code max} rows and stops once the rows read add up to about FRAME_BYTES, so the working set of a read is a frame
2127
* and not the table.
2228
*/
2329
package org.humemai.nativelib;
2430

31+
import com.arcadedb.database.RID;
2532
import com.arcadedb.query.sql.executor.Result;
2633
import com.arcadedb.query.sql.executor.ResultSet;
2734

@@ -96,6 +103,55 @@ private static boolean yearFits(final LocalDateTime ldt) {
96103
return ldt.getYear() >= 1 && ldt.getYear() <= 9999;
97104
}
98105

106+
/**
107+
* The column kind of one list: "li8" (every element a Long, Integer, Short or Byte, or null), "lf8" (every element a Double or Float, or
108+
* null), "l0" (no element that is not null: it takes the kind of the other lists of the column) or "json" (a string, a boolean, a
109+
* nested value, ints next to floats: what no fixed layout carries without changing a value).
110+
*/
111+
private static String listKind(final List<?> list) {
112+
String kind = "l0";
113+
for (final Object e : list) {
114+
if (e == null)
115+
continue;
116+
final String k;
117+
if (e instanceof Long || e instanceof Integer || e instanceof Short || e instanceof Byte)
118+
k = "li8";
119+
else if (e instanceof Double || e instanceof Float)
120+
k = "lf8";
121+
else
122+
return "json";
123+
if (kind.equals("l0"))
124+
kind = k;
125+
else if (!kind.equals(k))
126+
return "json";
127+
}
128+
return kind;
129+
}
130+
131+
private static boolean isListKind(final String kind) {
132+
return kind.equals("li8") || kind.equals("lf8") || kind.equals("l0");
133+
}
134+
135+
/** The kind of a column that holds lists of kind {@code a} and of kind {@code b}, or null when no list layout carries both. */
136+
private static String mergeListKinds(final String a, final String b) {
137+
if (!isListKind(a) || !isListKind(b))
138+
return null;
139+
if (a.equals(b) || b.equals("l0"))
140+
return a;
141+
if (a.equals("l0"))
142+
return b;
143+
return null; // ints next to floats: each element keeps its own type in a json column
144+
}
145+
146+
/** The elements of the lists of a column of list kind (the size of the flat buffer), or -1 when the buffer would not fit an int32 offset. */
147+
private static long listElements(final List<Object[]> rows, final int c, final int count) {
148+
long total = 0;
149+
for (int r = 0; r < count; r++)
150+
if (rows.get(r)[c] instanceof List<?> l)
151+
total += l.size();
152+
return total <= (1 << 27) ? total : -1;
153+
}
154+
99155
/** About how many bytes the values of a row take in a frame (a stop rule, not an exact size). */
100156
private static long estimate(final Result row) {
101157
long n = 0;
@@ -218,12 +274,20 @@ else if (v instanceof float[])
218274
t = "f4v";
219275
else if (v instanceof double[])
220276
t = "f8v";
277+
else if (v instanceof RID rid)
278+
// a record link: bucket and position, which write the text of the RID ("#b:p") for a valid RID
279+
t = rid.getBucketId() >= 0 && rid.getPosition() >= 0 ? "rid" : "json";
280+
else if (v instanceof List<?> list)
281+
t = listKind(list);
221282
else
222283
t = "json";
223284
if (type == null)
224285
type = t;
225286
else if (!type.equals(t)) {
226-
if ((type.equals("i8") && t.equals("f8")) || (type.equals("f8") && t.equals("i8")))
287+
final String merged = mergeListKinds(type, t);
288+
if (merged != null)
289+
type = merged;
290+
else if ((type.equals("i8") && t.equals("f8")) || (type.equals("f8") && t.equals("i8")))
227291
type = "f8";
228292
else {
229293
type = "json";
@@ -233,6 +297,8 @@ else if (!type.equals(t)) {
233297
}
234298
if (type == null)
235299
type = "str"; // an all-null column
300+
if (type.equals("l0"))
301+
type = "json"; // lists with no element that is not null say nothing about the element type: Arrow types them list<null>
236302
// an integer beyond 2**53 next to a floating value has no exact double: the values stay what they are, in a json column
237303
if (type.equals("f8") && sawInt && sawFloating && bigInt)
238304
type = "json";
@@ -255,6 +321,13 @@ else if (vdim == -1)
255321
vdim = 0;
256322
}
257323

324+
long listTotal = 0;
325+
if (type.equals("li8") || type.equals("lf8")) {
326+
listTotal = listElements(rows, c, count);
327+
if (listTotal < 0)
328+
type = "json";
329+
}
330+
258331
final byte[] colBuf;
259332
if (type.equals("i8") || type.equals("dt") || type.equals("dtu")) {
260333
final ByteBuffer bb = ByteBuffer.allocate(count * 8).order(ByteOrder.LITTLE_ENDIAN);
@@ -319,6 +392,55 @@ else if (type.equals("dtu"))
319392
bb.putDouble(x);
320393
}
321394
colBuf = bb.array();
395+
} else if (type.equals("rid")) {
396+
// bucket and position as two int64 per row; a null row is 0, 0
397+
final ByteBuffer bb = ByteBuffer.allocate(count * 16).order(ByteOrder.LITTLE_ENDIAN);
398+
for (int r = 0; r < count; r++) {
399+
final Object v = rows.get(r)[c];
400+
if (v == null) {
401+
nulls[r >> 3] |= (1 << (r & 7));
402+
bb.putLong(0);
403+
bb.putLong(0);
404+
} else {
405+
bb.putLong(((RID) v).getBucketId());
406+
bb.putLong(((RID) v).getPosition());
407+
}
408+
}
409+
colBuf = bb.array();
410+
} else if (type.equals("li8") || type.equals("lf8")) {
411+
// int32 offsets (count + 1), a bitmap of the null elements (bit j of byte j/8 set for a null element, one bit per element of
412+
// the flat array), then the elements of every list one after the other, int64 or float64 LE. A null row is an empty list.
413+
final boolean ints = type.equals("li8");
414+
final ByteBuffer offs = ByteBuffer.allocate((count + 1) * 4).order(ByteOrder.LITTLE_ENDIAN);
415+
final ByteBuffer vals = ByteBuffer.allocate((int) listTotal * 8).order(ByteOrder.LITTLE_ENDIAN);
416+
final byte[] elemNulls = new byte[(int) ((listTotal + 7) / 8)];
417+
int pos = 0;
418+
offs.putInt(0);
419+
for (int r = 0; r < count; r++) {
420+
final Object v = rows.get(r)[c];
421+
if (v == null)
422+
nulls[r >> 3] |= (1 << (r & 7));
423+
else
424+
for (final Object e : (List<?>) v) {
425+
if (e == null) {
426+
elemNulls[pos >> 3] |= (1 << (pos & 7));
427+
if (ints)
428+
vals.putLong(0);
429+
else
430+
vals.putDouble(Double.NaN);
431+
} else if (ints)
432+
vals.putLong(((Number) e).longValue());
433+
else
434+
vals.putDouble(((Number) e).doubleValue());
435+
pos++;
436+
}
437+
offs.putInt(pos);
438+
}
439+
final ByteBuffer bb = ByteBuffer.allocate(offs.capacity() + elemNulls.length + vals.capacity());
440+
bb.put(offs.array());
441+
bb.put(elemNulls);
442+
bb.put(vals.array());
443+
colBuf = bb.array();
322444
} else if (type.equals("json")) {
323445
final StringBuilder arr = new StringBuilder();
324446
arr.append('[');

‎tests/test_frame_rid_list_kinds.py‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
writer that knows the new kinds writes ``rid`` (bucket and position, two int64 per row) and ``li8`` / ``lf8`` (a list of int64 or of
55
float64 per row: offsets, a bitmap of null elements, one flat array) for those columns, and keeps ``json`` for every other shape: maps,
66
nested lists, lists of strings, of booleans or of RIDs, lists that mix ints and floats, a list of nothing but nulls. The JVM bridge
7-
(``ColumnBatcher``) writes them in this change; the writer of the native library (``ColumnFrames``) is a separate change stacked on this
8-
one and still writes ``json`` here. ``KINDS`` below says what each writer writes, so the tests of each change pass alone; the decoder
9-
reads both forms, and every other test here holds for either.
7+
(``ColumnBatcher``) and the native library (``ColumnFrames``) both write them. ``KINDS`` below says what each writer writes (the
8+
two writers came in two changes, and the tests of each passed alone); the decoder reads both forms, and every other test here holds for
9+
either.
1010
1111
Three kinds of check, all on the wheel that runs the tests (no branch on ``arcadedb.__backend__`` except ``KINDS`` and the stored output,
1212
and where a wheel's frames differ in how they start, noted below):
@@ -86,9 +86,7 @@ def _g4(i):
8686
"g3": "SELECT n, lin, lfn FROM g3 ORDER BY n",
8787
"g4": "SELECT n, lfx FROM g4 ORDER BY n",
8888
}
89-
# The kind of each column in the frames of a read (the shapes in the table above). ``_BINARY`` is what a writer that knows the new kinds
90-
# writes, ``_JSON`` what a writer that does not know them writes. The JVM bridge writes the new kinds in this change; the native library
91-
# writes ``json`` here, and the change that teaches its writer the new kinds sets the ``native`` entry of ``KINDS`` to ``_BINARY``.
89+
# The kind of each column in the frames of a read (the shapes in the table above), per writer: both know the new kinds.
9290
_BINARY = {
9391
"g1": {
9492
"li": "li8",
@@ -106,8 +104,7 @@ def _g4(i):
106104
"g3": {"lin": "li8", "lfn": "lf8"},
107105
"g4": {"lfx": "lf8"},
108106
}
109-
_JSON = {g: {c: "json" for c in cols} for g, cols in _BINARY.items()}
110-
KINDS = {"jvm": _BINARY, "native": _JSON}
107+
KINDS = {"jvm": _BINARY, "native": _BINARY}
111108

112109

113110
def build(db, group):

0 commit comments

Comments
 (0)