Skip to content

Commit cdb2163

Browse files
committed
Return UUID column statistics to Java as bytes
A writer that wrote a UUID column failed in finish(), because the min and max of a fixed-size list of u8 had no Java conversion. These bounds now return to Java as byte arrays. Signed-off-by: Robert Kruszewski <[email protected]>
1 parent bed9e0c commit cdb2163

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎java/vortex-jni/src/test/java/dev/vortex/jni/JNIWriterTest.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.arrow.memory.BufferAllocator;
3434
import org.apache.arrow.memory.RootAllocator;
3535
import org.apache.arrow.vector.BigIntVector;
36+
import org.apache.arrow.vector.FixedSizeBinaryVector;
3637
import org.apache.arrow.vector.IntVector;
3738
import org.apache.arrow.vector.VarBinaryVector;
3839
import org.apache.arrow.vector.VarCharVector;
@@ -413,6 +414,48 @@ public void testMapRoundTrip(boolean independentBuffers) throws IOException {
413414
}
414415

415416
/** Write a single three-row batch of {@link #personSchema()} data. */
417+
@Test
418+
public void testUuidColumnStatisticsAreBytes() throws IOException {
419+
String writePath =
420+
tempDir.resolve("uuid_stats.vortex").toAbsolutePath().toUri().toString();
421+
BufferAllocator allocator = ArrowAllocation.rootAllocator();
422+
Field uuid = new Field(
423+
"id",
424+
new FieldType(
425+
false, new ArrowType.FixedSizeBinary(16), null, Map.of(ARROW_EXTENSION_NAME, "arrow.uuid")),
426+
null);
427+
Schema schema = new Schema(List.of(uuid));
428+
byte[] low = new byte[16];
429+
byte[] high = new byte[16];
430+
low[15] = 1;
431+
high[0] = 0x7F;
432+
433+
Session session = Session.create();
434+
VortexWriteSummary summary;
435+
try (VortexWriter writer =
436+
VortexWriter.builder(session, writePath, schema, allocator).build()) {
437+
try (VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator)) {
438+
FixedSizeBinaryVector ids = (FixedSizeBinaryVector) root.getVector("id");
439+
ids.allocateNew(2);
440+
ids.setSafe(0, high);
441+
ids.setSafe(1, low);
442+
root.setRowCount(2);
443+
444+
try (ArrowArray array = ArrowArray.allocateNew(allocator);
445+
ArrowSchema arrowSchema = ArrowSchema.allocateNew(allocator)) {
446+
Data.exportVectorSchemaRoot(allocator, root, null, array, arrowSchema);
447+
writer.writeBatch(array.memoryAddress(), arrowSchema.memoryAddress());
448+
}
449+
}
450+
451+
summary = writer.finish();
452+
}
453+
454+
VortexColumnStatistics id = summary.columnStatistics().get(0);
455+
assertArrayEquals(low, (byte[]) id.lowerBound().orElseThrow());
456+
assertArrayEquals(high, (byte[]) id.upperBound().orElseThrow());
457+
}
458+
416459
/**
417460
* Editions before the aggregates, such as {@code core2025.10.0}, do not permit {@code vortex.min}. The writer then
418461
* writes only the legacy file statistics, and these must still have the min, max and null count.

‎vortex-jni/src/statistics.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use vortex::array::scalar::ScalarValue;
2222
use vortex::array::stats::StatsSet;
2323
use vortex::dtype::DType;
2424
use vortex::dtype::FieldPath;
25+
use vortex::dtype::Nullability;
26+
use vortex::dtype::PType;
2527
use vortex::error::VortexResult;
2628
use vortex::error::vortex_err;
2729
use vortex::expr::stats::Precision;
@@ -122,6 +124,19 @@ fn scalar_to_java<'local>(
122124
}
123125
ScalarValue::Utf8(value) => Ok(env.new_string(value.as_str())?.into()),
124126
ScalarValue::Binary(value) => Ok(env.byte_array_from_slice(value.as_slice())?.into()),
127+
ScalarValue::Tuple(elements) if is_fixed_size_byte_list(scalar.dtype()) => {
128+
let bytes = elements
129+
.iter()
130+
.map(|element| match element {
131+
Some(ScalarValue::Primitive(PValue::U8(byte))) => Ok(*byte),
132+
_ => Err(vortex_err!(
133+
"fixed-size byte list statistic has a non-u8 element: {element:?}"
134+
)),
135+
})
136+
.collect::<VortexResult<Vec<u8>>>()?;
137+
138+
Ok(env.byte_array_from_slice(&bytes)?.into())
139+
}
125140
ScalarValue::Tuple(_) | ScalarValue::Union(_) | ScalarValue::Variant(_) => {
126141
Err(JNIError::Vortex(vortex_err!(
127142
"cannot return nested scalar statistic with dtype {} to Java",
@@ -131,6 +146,16 @@ fn scalar_to_java<'local>(
131146
}
132147
}
133148

149+
/// Returns true when `dtype` is a fixed-size list of non-null `u8`, the storage of byte-valued
150+
/// extension types such as UUID.
151+
fn is_fixed_size_byte_list(dtype: &DType) -> bool {
152+
matches!(
153+
dtype,
154+
DType::FixedSizeList(element, _, _)
155+
if matches!(element.as_ref(), DType::Primitive(PType::U8, Nullability::NonNullable))
156+
)
157+
}
158+
134159
/// The statistics of one top-level column that are returned to Java.
135160
pub(crate) struct ColumnStatistics {
136161
null_count: Precision<Scalar>,

0 commit comments

Comments
 (0)