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 @@ -20,6 +20,7 @@
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Locale;
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 @@ -96,6 +97,21 @@ enum CapacityReset {
* @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 @@ -120,6 +136,7 @@ public VortexOutputWriter(String filePath, StructType schema, VortexOptions opti
this.session = VortexSparkSession.get(options);
this.vortexWriter = VortexWriter.builder(session, writable, arrowSchema, allocator)
.listLayout(listLayout(options))
.metadata(metadata)
.build();

logger.debug("Initialized VortexOutputWriter for {}", filePath);
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 @@ -21,6 +23,7 @@
import dev.vortex.spark.io.HadoopWritable;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -640,6 +643,25 @@ void decimalsRoundTrip() {
assertEquals(expected, readRows(writer.path()));
}

@Test
void storesFooterMetadata() throws IOException {
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"));
assertEquals(List.of("7"), readRows(path));
}

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