Skip to content
Open
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 @@ -83,7 +83,9 @@ public InternalRow next() {
Blob blob;
long offset = fileMeta.blobOffset(currentPosition) + 4;
long length = fileMeta.blobLength(currentPosition) - 16;
if (in != null) {
if (length == 0) {
blob = null;
} else if (in != null) {
blob = Blob.fromData(readInlineBlob(in, offset, length));
} else {
blob = Blob.fromFile(fileIO, filePath.toString(), offset, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ public BlobFormatWriter(PositionOutputStream out) {
@Override
public void addElement(InternalRow element) throws IOException {
checkArgument(element.getFieldCount() == 1, "BlobFormatWriter only support one field.");
checkArgument(!element.isNullAt(0), "BlobFormatWriter only support non-null blob.");
Blob blob = element.getBlob(0);

long previousPos = out.getPos();
crc32.reset();

write(MAGIC_NUMBER_BYTES);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to write magic number for null blob?

try (SeekableInputStream in = blob.newInputStream()) {
int bytesRead = in.read(tmpBuffer);
while (bytesRead >= 0) {
write(tmpBuffer, bytesRead);
bytesRead = in.read(tmpBuffer);

if (!element.isNullAt(0)) {
Blob blob = element.getBlob(0);
try (SeekableInputStream in = blob.newInputStream()) {
int bytesRead = in.read(tmpBuffer);
while (bytesRead >= 0) {
write(tmpBuffer, bytesRead);
bytesRead = in.read(tmpBuffer);
}
}
}

crc32.reset();
long binLength = out.getPos() - previousPos + 12;
lengths.add(binLength);
byte[] lenBytes = longToLittleEndian(binLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ private void innerTest(boolean blobAsDescriptor) throws IOException {

// write
FormatWriterFactory writerFactory = format.createWriterFactory(rowType);
List<byte[]> blobs = Arrays.asList("hello".getBytes(), "world".getBytes());
List<byte[]> blobs =
Arrays.asList(
"hello".getBytes(), "world".getBytes(), null, "asldfjasldkfjas".getBytes());
try (PositionOutputStream out = fileIO.newOutputStream(file, false)) {
FormatWriter formatWriter = writerFactory.create(out, null);
for (byte[] bytes : blobs) {
formatWriter.addElement(GenericRow.of(new BlobData(bytes)));
formatWriter.addElement(GenericRow.of(bytes == null ? null : new BlobData(bytes)));
}
formatWriter.close();
}
Expand All @@ -96,13 +98,17 @@ private void innerTest(boolean blobAsDescriptor) throws IOException {
.createReader(context)
.forEachRemaining(
row -> {
Blob blob = row.getBlob(0);
if (blobAsDescriptor) {
assertThat(blob).isInstanceOf(BlobRef.class);
if (row.isNullAt(0)) {
result.add(null);
} else {
assertThat(blob).isInstanceOf(BlobData.class);
Blob blob = row.getBlob(0);
if (blobAsDescriptor) {
assertThat(blob).isInstanceOf(BlobRef.class);
} else {
assertThat(blob).isInstanceOf(BlobData.class);
}
result.add(blob.toData());
}
result.add(blob.toData());
});

// assert
Expand Down