Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException reading data with struct type #43

Merged
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 @@ -48,6 +48,8 @@ public class NativeScanner implements Scanner {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock writeLock = lock.writeLock();
private final Lock readLock = lock.readLock();

private Schema schema = null;
private boolean closed = false;

public NativeScanner(NativeContext context, ScanOptions options, long scannerId) {
Expand Down Expand Up @@ -92,7 +94,7 @@ public boolean hasNext() {
}
peek = UnsafeRecordBatchSerializer.deserializeUnsafe(context.getAllocator(), bytes);
if (options.getColumns() != null) {
Preconditions.checkState(peek.getNodes().size() == options.getColumns().length);
Preconditions.checkState(schema().getFields().size() == options.getColumns().length);
}
return true;
}
Expand Down Expand Up @@ -122,12 +124,19 @@ public Iterable<? extends NativeScanTask> scan() {

@Override
public Schema schema() {
if (schema != null) {
return schema;
}
readLock.lock();
try {
if (schema != null) {
return schema;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

dup code on 132-134 vs 127-129?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry Yuan I missed the comment before merging.

The codes applies double-checked locking to balance between performance and atomicity. Although may look tedious as we don't usually have concurrent call to this method.

For example: schema can happen to be assigned by another thread before acquiring the lock. So we have to check again inside the lock.

if (closed) {
throw new NativeInstanceReleasedException();
}
return SchemaUtility.deserialize(JniWrapper.get().getSchemaFromScanner(scannerId), context.getAllocator());
schema = SchemaUtility.deserialize(JniWrapper.get().getSchemaFromScanner(scannerId), context.getAllocator());
return schema;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,68 @@ public void testCsvReadTab() throws Exception {
AutoCloseables.close(vsr, allocator);
}


@Test
public void testStructTypeRead() throws Exception {
RootAllocator allocator = new RootAllocator(Long.MAX_VALUE);
FileSystemDatasetFactory factory = new FileSystemDatasetFactory(allocator,
NativeMemoryPool.getDefault(), ParquetFileFormat.createDefault(), "file://" + resourcePath("data/struct_example.parquet"));
ScanOptions options = new ScanOptions(new String[] {"_1"}, Filter.EMPTY, 100);
Schema schema = factory.inspect();
NativeDataset dataset = factory.finish(schema);
NativeScanner nativeScanner = dataset.newScan(options);
List<? extends ScanTask> scanTasks = collect(nativeScanner.scan());
Assert.assertEquals(1, scanTasks.size());
ScanTask scanTask = scanTasks.get(0);
ScanTask.BatchIterator itr = scanTask.execute();

VectorSchemaRoot vsr = VectorSchemaRoot.create(schema, allocator);
VectorLoader loader = new VectorLoader(vsr);
int rowCount = 0;
while (itr.hasNext()) {
try (ArrowRecordBatch next = itr.next()) {
loader.load(next);
}
rowCount += vsr.getRowCount();

}
Assert.assertEquals(50, rowCount);
assertEquals(1, schema.getFields().size());
assertEquals("_1", schema.getFields().get(0).getName());
AutoCloseables.close(vsr, allocator);
}

@Test
public void testStructTypeReadWithEmptyProjector() throws Exception {
RootAllocator allocator = new RootAllocator(Long.MAX_VALUE);
FileSystemDatasetFactory factory = new FileSystemDatasetFactory(allocator,
NativeMemoryPool.getDefault(), ParquetFileFormat.createDefault(), "file://" + resourcePath("data/struct_example.parquet"));
ScanOptions options = new ScanOptions(new String[0], Filter.EMPTY, 100);
Schema schema = factory.inspect();
NativeDataset dataset = factory.finish(schema);
NativeScanner nativeScanner = dataset.newScan(options);
List<? extends ScanTask> scanTasks = collect(nativeScanner.scan());
Assert.assertEquals(1, scanTasks.size());
ScanTask scanTask = scanTasks.get(0);
ScanTask.BatchIterator itr = scanTask.execute();
Schema scannerSchema = nativeScanner.schema();
VectorSchemaRoot vsr = VectorSchemaRoot.create(scannerSchema, allocator);
VectorLoader loader = new VectorLoader(vsr);
int rowCount = 0;
while (itr.hasNext()) {
try (ArrowRecordBatch next = itr.next()) {
loader.load(next);
}
rowCount += vsr.getRowCount();

}
Assert.assertEquals(50, rowCount);
assertEquals(1, schema.getFields().size());
assertEquals("_1", schema.getFields().get(0).getName());
assertEquals(0, scannerSchema.getFields().size());
AutoCloseables.close(vsr, allocator);
}

@Test
public void testReadPartialFile() throws Exception {
ParquetWriteSupport writeSupport = ParquetWriteSupport.writeTempFile(AVRO_SCHEMA_USER, TMP.newFolder(), 1, "a");
Expand Down
Binary file not shown.