Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: enable SQL query support in datafusion example #169

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
33 changes: 28 additions & 5 deletions examples/datafusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ use datafusion::{
error::{DataFusionError, Result},
execution::{RecordBatchStream, SendableRecordBatchStream, TaskContext},
physical_expr::EquivalenceProperties,
physical_plan::{DisplayAs, DisplayFormatType, ExecutionMode, ExecutionPlan, PlanProperties},
physical_plan::{
execute_stream, DisplayAs, DisplayFormatType, ExecutionMode, ExecutionPlan, PlanProperties,
},
prelude::*,
sql::parser::DFParser,
};
use fusio::{local::TokioFs, path::Path};
use futures_core::Stream;
Expand Down Expand Up @@ -225,9 +228,29 @@ 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(())
}
Loading