Skip to content

Commit

Permalink
feat: support show function for DataFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-du committed Aug 22, 2021
1 parent 1dab962 commit 47d3844
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions datafusion/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ pub trait DataFrame: Send + Sync {
/// ```
async fn collect(&self) -> Result<Vec<RecordBatch>>;

/// Print results.
///
/// ```
/// # use datafusion::prelude::*;
/// # use datafusion::error::Result;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let mut ctx = ExecutionContext::new();
/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;
/// df.show().await?;
/// # Ok(())
/// # }
/// ```
async fn show(&self) -> Result<()>;

/// Executes this DataFrame and returns a stream over a single partition
///
/// ```
Expand Down
7 changes: 7 additions & 0 deletions datafusion/src/execution/dataframe_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::physical_plan::{
execute_stream, execute_stream_partitioned, ExecutionPlan, SendableRecordBatchStream,
};
use async_trait::async_trait;
use crate::arrow::util::pretty;

/// Implementation of DataFrame API
pub struct DataFrameImpl {
Expand Down Expand Up @@ -156,6 +157,12 @@ impl DataFrame for DataFrameImpl {
Ok(collect(plan).await?)
}

// Print results.
async fn show(&self) -> Result<()> {
let results = self.collect().await?;
Ok(pretty::print_batches(&results)?)
}

/// Convert the logical plan represented by this DataFrame into a physical plan and
/// execute it, returning a stream over a single partition
async fn execute_stream(&self) -> Result<SendableRecordBatchStream> {
Expand Down

0 comments on commit 47d3844

Please sign in to comment.