Apache DataFusion SQL Query Engine
ARROW-9757: [Rust] [DataFusion] Add prelude.rs
Andy Grove4563e3968d07
diff --git a/rust/datafusion/examples/csv_sql.rs b/rust/datafusion/examples/csv_sql.rsindex a5f3837a5f..771d99bfc3 100644--- a/rust/datafusion/examples/csv_sql.rs+++ b/rust/datafusion/examples/csv_sql.rs@@ -17,9 +17,8 @@use arrow::util::pretty;-use datafusion::datasource::csv::CsvReadOptions;use datafusion::error::Result;-use datafusion::ExecutionContext;+use datafusion::prelude::*;/// This example demonstrates executing a simple query against an Arrow data source (CSV) and/// fetching resultsdiff --git a/rust/datafusion/examples/dataframe.rs b/rust/datafusion/examples/dataframe.rsindex 4b931b6af7..5b45d11cef 100644--- a/rust/datafusion/examples/dataframe.rs+++ b/rust/datafusion/examples/dataframe.rs@@ -16,9 +16,9 @@// under the License.use arrow::util::pretty;+use datafusion::error::Result;-use datafusion::logicalplan::{col, lit};-use datafusion::ExecutionContext;+use datafusion::prelude::*;/// This example demonstrates executing a simple query against an Arrow data source (Parquet) and/// fetching results, using the DataFrame traitdiff --git a/rust/datafusion/examples/flight_server.rs b/rust/datafusion/examples/flight_server.rsindex c71a75876c..0cb28580ef 100644--- a/rust/datafusion/examples/flight_server.rs+++ b/rust/datafusion/examples/flight_server.rs@@ -23,7 +23,7 @@ use tonic::{Request, Response, Status, Streaming};use datafusion::datasource::parquet::ParquetTable;use datafusion::datasource::TableProvider;-use datafusion::execution::context::ExecutionContext;+use datafusion::prelude::*;use arrow_flight::{flight_service_server::FlightService, flight_service_server::FlightServiceServer,diff --git a/rust/datafusion/examples/memory_table_api.rs b/rust/datafusion/examples/memory_table_api.rsindex ee85785b23..da947613eb 100644--- a/rust/datafusion/examples/memory_table_api.rs+++ b/rust/datafusion/examples/memory_table_api.rs@@ -25,8 +25,7 @@ use arrow::util::pretty;use datafusion::datasource::MemTable;use datafusion::error::Result;-use datafusion::execution::context::ExecutionContext;-use datafusion::logicalplan::{col, lit};+use datafusion::prelude::*;/// This example demonstrates basic uses of the Table API on an in-memory tablefn main() -> Result<()> {diff --git a/rust/datafusion/examples/parquet_sql.rs b/rust/datafusion/examples/parquet_sql.rsindex f73a2aecff..6359023dd8 100644--- a/rust/datafusion/examples/parquet_sql.rs+++ b/rust/datafusion/examples/parquet_sql.rs@@ -16,8 +16,9 @@// under the License.use arrow::util::pretty;+use datafusion::error::Result;-use datafusion::execution::context::ExecutionContext;+use datafusion::prelude::*;/// This example demonstrates executing a simple query against an Arrow data source (Parquet) and/// fetching resultsdiff --git a/rust/datafusion/src/dataframe.rs b/rust/datafusion/src/dataframe.rsindex 75618d1c5f..f89f06cb91 100644--- a/rust/datafusion/src/dataframe.rs+++ b/rust/datafusion/src/dataframe.rs@@ -35,10 +35,8 @@ use std::sync::Arc;/// The query can be executed by calling the `collect` method.////// ```-/// # use datafusion::ExecutionContext;+/// # use datafusion::prelude::*;/// # use datafusion::error::Result;-/// # use datafusion::execution::physical_plan::csv::CsvReadOptions;-/// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -54,9 +52,8 @@ pub trait DataFrame {/// specified columns.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -69,10 +66,8 @@ pub trait DataFrame {/// Create a projection based on arbitrary expressions.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -85,10 +80,8 @@ pub trait DataFrame {/// Filter a DataFrame to only include rows that match the specified filter expression.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -101,10 +94,8 @@ pub trait DataFrame {/// Perform an aggregate query with optional grouping expressions.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -126,10 +117,8 @@ pub trait DataFrame {/// Limit the number of rows returned from this DataFrame.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -143,10 +132,8 @@ pub trait DataFrame {/// a sort expression by calling its [sort](../logicalplan/enum.Expr.html#method.sort) method.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -159,10 +146,8 @@ pub trait DataFrame {/// Executes this DataFrame and collects all results into a vector of RecordBatch.////// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;@@ -176,10 +161,8 @@ pub trait DataFrame {/// where each column has a name, data type, and nullability attribute./// ```- /// # use datafusion::ExecutionContext;+ /// # use datafusion::prelude::*;/// # use datafusion::error::Result;- /// # use datafusion::execution::physical_plan::csv::CsvReadOptions;- /// # use datafusion::logicalplan::col;/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;diff --git a/rust/datafusion/src/execution/context.rs b/rust/datafusion/src/execution/context.rsindex 8f92aae302..bafdf74a5d 100644--- a/rust/datafusion/src/execution/context.rs+++ b/rust/datafusion/src/execution/context.rs@@ -60,31 +60,34 @@ use crate::sql::{/// * Execution a SQL query////// The following example demonstrates how to use the context to execute a query against a CSV-/// data source:+/// data source using the DataFrame API:////// ```-/// use datafusion::ExecutionContext;-/// use datafusion::execution::physical_plan::csv::CsvReadOptions;-/// use datafusion::logicalplan::col;-///+/// use datafusion::prelude::*;+/// # use datafusion::error::Result;+/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();-/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new()).unwrap();-/// let df = df.filter(col("a").lt_eq(col("b"))).unwrap()-/// .aggregate(vec![col("a")], vec![df.min(col("b")).unwrap()]).unwrap()-/// .limit(100).unwrap();+/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;+/// let df = df.filter(col("a").lt_eq(col("b")))?+/// .aggregate(vec![col("a")], vec![df.min(col("b"))?])?+/// .limit(100)?;/// let results = df.collect();+/// # Ok(())+/// # }/// ```////// The following example demonstrates how to execute the same query using SQL:////// ```-/// use datafusion::ExecutionContext;-/// use datafusion::execution::physical_plan::csv::CsvReadOptions;-/// use datafusion::logicalplan::col;+/// use datafusion::prelude::*;///+/// # use datafusion::error::Result;+/// # fn main() -> Result<()> {/// let mut ctx = ExecutionContext::new();-/// ctx.register_csv("example", "tests/example.csv", CsvReadOptions::new()).unwrap();-/// let results = ctx.sql("SELECT a, MIN(b) FROM example GROUP BY a LIMIT 100").unwrap();+/// ctx.register_csv("example", "tests/example.csv", CsvReadOptions::new())?;+/// let results = ctx.sql("SELECT a, MIN(b) FROM example GROUP BY a LIMIT 100")?;+/// # Ok(())+/// # }/// ```pub struct ExecutionContext {/// Internal state for the contextdiff --git a/rust/datafusion/src/lib.rs b/rust/datafusion/src/lib.rsindex 73897eeaed..0b30f471c2 100644--- a/rust/datafusion/src/lib.rs+++ b/rust/datafusion/src/lib.rs@@ -15,17 +15,46 @@// specific language governing permissions and limitations// under the License.+#![warn(missing_docs)]+//! DataFusion is an extensible query execution framework that uses//! Apache Arrow as the memory model.//!-//! DataFusion supports both SQL and a Table/DataFrame-style API for building logical query plans+//! DataFusion supports both SQL and a DataFrame API for building logical query plans//! and also provides a query optimizer and execution engine capable of parallel execution//! against partitioned data sources (CSV and Parquet) using threads.//!//! DataFusion currently supports simple projection, selection, and aggregate queries.--#![warn(missing_docs)]-+//!+/// [ExecutionContext](../execution/context/struct.ExecutionContext.html) is the main interface+/// for executing queries with DataFusion.+///+/// The following example demonstrates how to use the context to execute a query against a CSV+/// data source using the DataFrame API:+///+/// ```+/// # use datafusion::prelude::*;+/// # use datafusion::error::Result;+/// # fn main() -> Result<()> {+/// let mut ctx = ExecutionContext::new();+/// let df = ctx.read_csv("tests/example.csv", CsvReadOptions::new())?;+/// let df = df.filter(col("a").lt_eq(col("b")))?+/// .aggregate(vec![col("a")], vec![df.min(col("b"))?])?+/// .limit(100)?;+/// let results = df.collect();+/// # Ok(())+/// # }+/// ```+///+/// The following example demonstrates how to execute the same query using SQL:+///+/// ```+/// use datafusion::prelude::*;+///+/// let mut ctx = ExecutionContext::new();+/// ctx.register_csv("example", "tests/example.csv", CsvReadOptions::new()).unwrap();+/// let results = ctx.sql("SELECT a, MIN(b) FROM example GROUP BY a LIMIT 100").unwrap();+/// ```extern crate arrow;extern crate sqlparser;@@ -35,9 +64,8 @@ pub mod error;pub mod execution;pub mod logicalplan;pub mod optimizer;+pub mod prelude;pub mod sql;-pub use execution::context::ExecutionContext;-#[cfg(test)]pub mod test;diff --git a/rust/datafusion/src/prelude.rs b/rust/datafusion/src/prelude.rsnew file mode 100644index 0000000000..658f970b32--- /dev/null+++ b/rust/datafusion/src/prelude.rs@@ -0,0 +1,31 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.pub},++//! A "prelude" for users of the datafusion crate.+//!+//! Like the standard library's prelude, this module simplifies importing of+//! common items. Unlike the standard prelude, the contents of this module must+//! be imported manually:+//!+//! ```+//! use datafusion::prelude::*;+//! ```++pub use crate::dataframe::DataFrame;+pub use crate::execution::context::{ExecutionConfig, ExecutionContext};+pub use crate::execution::physical_plan::csv::CsvReadOptions;+pub use crate::logicalplan::{col, lit};