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 */
2329package org .humemai .nativelib ;
2430
31+ import com .arcadedb .database .RID ;
2532import com .arcadedb .query .sql .executor .Result ;
2633import 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 ('[' );
0 commit comments