Search in sources :

Example 1 with DirectRowSet

use of org.apache.drill.test.rowSet.DirectRowSet in project drill by apache.

the class QueryBuilder method rowSet.

/**
   * Run the query and return the first result set as a
   * {@link DirectRowSet} object that can be inspected directly
   * by the code using a {@link RowSetReader}.
   * <p>
   * An enhancement is to provide a way to read a series of result
   * batches as row sets.
   * @return a row set that represents the first batch returned from
   * the query
   * @throws RpcException if anything goes wrong
   */
public DirectRowSet rowSet() throws RpcException {
    // Ignore all but the first non-empty batch.
    QueryDataBatch dataBatch = null;
    for (QueryDataBatch batch : results()) {
        if (dataBatch == null && batch.getHeader().getRowCount() != 0) {
            dataBatch = batch;
        } else {
            batch.release();
        }
    }
    if (dataBatch == null) {
        return null;
    }
    // Unload the batch and convert to a row set.
    final RecordBatchLoader loader = new RecordBatchLoader(client.allocator());
    try {
        loader.load(dataBatch.getHeader().getDef(), dataBatch.getData());
        dataBatch.release();
        VectorContainer container = loader.getContainer();
        container.setRecordCount(loader.getRecordCount());
        return new DirectRowSet(client.allocator(), container);
    } catch (SchemaChangeException e) {
        throw new IllegalStateException(e);
    }
}
Also used : SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) QueryDataBatch(org.apache.drill.exec.rpc.user.QueryDataBatch) RecordBatchLoader(org.apache.drill.exec.record.RecordBatchLoader) DirectRowSet(org.apache.drill.test.rowSet.DirectRowSet) VectorContainer(org.apache.drill.exec.record.VectorContainer)

Aggregations

SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)1 RecordBatchLoader (org.apache.drill.exec.record.RecordBatchLoader)1 VectorContainer (org.apache.drill.exec.record.VectorContainer)1 QueryDataBatch (org.apache.drill.exec.rpc.user.QueryDataBatch)1 DirectRowSet (org.apache.drill.test.rowSet.DirectRowSet)1