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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.vortex.spark.VortexSparkSession;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import org.apache.spark.TaskContext;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.execution.datasources.OutputWriter;
Expand Down Expand Up @@ -64,6 +65,21 @@ public final class VortexOutputWriter extends OutputWriter {
* @param options additional write options
*/
public VortexOutputWriter(String filePath, StructType schema, VortexOptions options, NativeWritable writable) {
this(filePath, schema, options, writable, Map.of());
}

/**
* Creates a writer that also stores {@code metadata} in the file footer, for callers that write files outside
* Spark's commit protocol.
*
* @param metadata footer segments, read back verbatim by {@code NativeFiles.readMetadata}
*/
public VortexOutputWriter(
String filePath,
StructType schema,
VortexOptions options,
NativeWritable writable,
Map<String, byte[]> metadata) {
this.filePath = filePath;
this.options = options;
this.writable = writable;
Expand All @@ -90,6 +106,7 @@ public VortexOutputWriter(String filePath, StructType schema, VortexOptions opti

this.session = VortexSparkSession.get(options);
this.vortexWriter = VortexWriter.builder(session, writable, arrowSchema, allocator)
.metadata(metadata)
.build();
this.vectorSchemaRoot = VectorSchemaRoot.create(arrowSchema, allocator);
// The native writer holds several batches at once, so a batch of few rows must not take Arrow's default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

package dev.vortex.spark.write;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import dev.vortex.api.DataSource;
import dev.vortex.api.Scan;
import dev.vortex.api.ScanOptions;
import dev.vortex.jni.NativeFiles;
import dev.vortex.relocated.org.apache.arrow.memory.ArrowBuf;
import dev.vortex.relocated.org.apache.arrow.memory.BufferAllocator;
import dev.vortex.relocated.org.apache.arrow.memory.RootAllocator;
Expand All @@ -20,6 +22,7 @@
import dev.vortex.spark.VortexSparkSession;
import dev.vortex.spark.io.HadoopWritable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -238,6 +241,24 @@ void aLateReleaseDoesNotFailCloseAndIsStillFreed() {
assertEquals(1, readRows(writer.path()).size());
}

@Test
void storesFooterMetadata(@TempDir Path tempDir) {
StructType schema = new StructType().add("id", DataTypes.LongType, false);
String path = tempDir.resolve("metadata.vortex").toUri().toString();
byte[] value = "v1".getBytes(StandardCharsets.UTF_8);
VortexOutputWriter writer = new VortexOutputWriter(
path,
schema,
VortexOptions.empty(),
HadoopWritable.create(new Configuration(), path),
Map.of("app.version", value));
writer.write(new GenericInternalRow(new Object[] {7L}));
writer.close();

Map<String, byte[]> metadata = NativeFiles.readMetadata(VortexSparkSession.get(), path, Map.of());
assertArrayEquals(value, metadata.get("app.version"));
}

private VortexOutputWriter writer(String name, StructType schema, Map<String, String> values) {
String path = tempDir.resolve(name).toUri().toString();
return new VortexOutputWriter(path, schema, options(values), HadoopWritable.create(new Configuration(), path));
Expand Down
Loading