Skip to content

Commit

Permalink
feat: enable SQL query support in datafusion example
Browse files Browse the repository at this point in the history
Added support for executing SQL queries using DataFusion's parser and physical plan execution. This enhancement allows querying the "music" table with SQL statements, improving flexibility and functionality.
  • Loading branch information
loloxwg committed Sep 27, 2024
1 parent 837cc3f commit dd97345
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions examples/datafusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use datafusion::{
prelude::*,
};
use fusio::{local::TokioFs, path::Path};
use datafusion::physical_plan::execute_stream;
use datafusion::sql::parser::DFParser;
use futures_core::Stream;
use futures_util::StreamExt;
use tokio::fs;
Expand Down Expand Up @@ -225,9 +227,26 @@ async fn main() -> Result<()> {
let provider = MusicProvider { db: Arc::new(db) };
ctx.register_table("music", Arc::new(provider))?;

let df = ctx.table("music").await?;
let df = df.select(vec![col("name")])?;
let batches = df.collect().await?;
pretty::print_batches(&batches).unwrap();
{
let df = ctx.table("music").await?;
let df = df.select(vec![col("name")])?;
let batches = df.collect().await?;
pretty::print_batches(&batches).unwrap();
}

{
// support sql query for tonbo
let statements = DFParser::parse_sql("select * from music")?;
let plan = ctx.state().statement_to_plan(statements.front().cloned().unwrap()).await?;
ctx.execute_logical_plan(plan).await?;
let df = ctx.table("music").await?;
let physical_plan = df.create_physical_plan().await?;
let mut stream = execute_stream(physical_plan, ctx.task_ctx())?;
while let Some(maybe_batch) = stream.next().await {
let batch = maybe_batch?;
pretty::print_batches(&[batch]).unwrap();
}
}

Ok(())
}

0 comments on commit dd97345

Please # to comment.