Skip to content

Commit

Permalink
ARROW-6692: [Rust] [DataFusion] Update examples to use physical query…
Browse files Browse the repository at this point in the history
… plan

Closes #5581 from andygrove/ARROW-6692 and squashes the following commits:

52e8da5 <Andy Grove> Update examples to use physical query plan

Authored-by: Andy Grove <[email protected]>
Signed-off-by: Krisztián Szűcs <[email protected]>
  • Loading branch information
andygrove authored and kszucs committed Oct 5, 2019
1 parent af097e6 commit ed753fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
21 changes: 13 additions & 8 deletions rust/datafusion/examples/csv_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ extern crate datafusion;
use arrow::array::{BinaryArray, Float64Array};
use arrow::datatypes::{DataType, Field, Schema};

use datafusion::error::Result;
use datafusion::execution::context::ExecutionContext;

/// This example demonstrates executing a simple query against an Arrow data source (CSV) and
/// fetching results
fn main() {
fn main() -> Result<()> {
// create local execution context
let mut ctx = ExecutionContext::new();

Expand Down Expand Up @@ -59,16 +60,18 @@ fn main() {
true,
);

// simple projection and selection
let sql = "SELECT c1, MIN(c12), MAX(c12) FROM aggregate_test_100 WHERE c11 > 0.1 AND c11 < 0.9 GROUP BY c1";

// execute the query
let relation = ctx.sql(&sql, 1024 * 1024).unwrap();
// create the query plan
let plan = ctx.create_logical_plan(&sql)?;
let plan = ctx.optimize(&plan)?;
let plan = ctx.create_physical_plan(&plan, 1024 * 1024)?;

// display the relation
let mut results = relation.borrow_mut();
// execute the query
let results = ctx.collect(plan.as_ref())?;

while let Some(batch) = results.next().unwrap() {
// iterate over the results
results.iter().for_each(|batch| {
println!(
"RecordBatch has {} rows and {} columns",
batch.num_rows(),
Expand Down Expand Up @@ -98,5 +101,7 @@ fn main() {

println!("{}, Min: {}, Max: {}", c1_value, min.value(i), max.value(i),);
}
}
});

Ok(())
}
23 changes: 14 additions & 9 deletions rust/datafusion/examples/parquet_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ extern crate datafusion;

use arrow::array::{BinaryArray, Float64Array, Int32Array};

use datafusion::error::Result;
use datafusion::execution::context::ExecutionContext;

/// This example demonstrates executing a simple query against an Arrow data source (Parquet) and
/// fetching results
fn main() {
fn main() -> Result<()> {
// create local execution context
let mut ctx = ExecutionContext::new();

Expand All @@ -35,19 +36,21 @@ fn main() {
ctx.register_parquet(
"alltypes_plain",
&format!("{}/alltypes_plain.parquet", testdata),
)
.unwrap();
)?;

// simple selection
let sql = "SELECT int_col, double_col, date_string_col FROM alltypes_plain WHERE id > 1 AND tinyint_col < double_col";

// execute the query
let relation = ctx.sql(&sql, 1024 * 1024).unwrap();
// create the query plan
let plan = ctx.create_logical_plan(&sql)?;
let plan = ctx.optimize(&plan)?;
let plan = ctx.create_physical_plan(&plan, 1024 * 1024)?;

// display the relation
let mut results = relation.borrow_mut();
// execute the query
let results = ctx.collect(plan.as_ref())?;

while let Some(batch) = results.next().unwrap() {
// iterate over the results
results.iter().for_each(|batch| {
println!(
"RecordBatch has {} rows and {} columns",
batch.num_rows(),
Expand Down Expand Up @@ -82,5 +85,7 @@ fn main() {
double.value(i)
);
}
}
});

Ok(())
}

0 comments on commit ed753fd

Please sign in to comment.