Skip to content

Commit

Permalink
Merge commit '513d8d1d2afd450416e8dd77cf9be8d634f6550b' into chunchun…
Browse files Browse the repository at this point in the history
…/update-df-may-week-4
  • Loading branch information
appletreeisyellow committed May 28, 2024
2 parents fc3e9ec + 513d8d1 commit c712a5c
Show file tree
Hide file tree
Showing 20 changed files with 1,133 additions and 589 deletions.
62 changes: 33 additions & 29 deletions datafusion-examples/examples/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,45 @@ use datafusion::sql::unparser::expr_to_sql;
use datafusion_sql::unparser::dialect::CustomDialect;
use datafusion_sql::unparser::{plan_to_sql, Unparser};

/// This example demonstrates the programmatic construction of
/// SQL using the DataFusion Expr [`Expr`] and LogicalPlan [`LogicalPlan`] API.
/// This example demonstrates the programmatic construction of SQL strings using
/// the DataFusion Expr [`Expr`] and LogicalPlan [`LogicalPlan`] API.
///
///
/// The code in this example shows how to:
/// 1. Create SQL from a variety of Expr and LogicalPlan: [`main`]`
/// 2. Create a simple expression [`Exprs`] with fluent API
/// and convert to sql: [`simple_expr_to_sql_demo`]
/// 3. Create a simple expression [`Exprs`] with fluent API
/// and convert to sql without escaping column names: [`simple_expr_to_sql_demo_no_escape`]
/// 4. Create a simple expression [`Exprs`] with fluent API
/// and convert to sql escaping column names a MySQL style: [`simple_expr_to_sql_demo_escape_mysql_style`]
///
/// 1. [`simple_expr_to_sql_demo`]: Create a simple expression [`Exprs`] with
/// fluent API and convert to sql suitable for passing to another database
///
/// 2. [`simple_expr_to_sql_demo_no_escape`] Create a simple expression
/// [`Exprs`] with fluent API and convert to sql without escaping column names
/// more suitable for displaying to humans.
///
/// 3. [`simple_expr_to_sql_demo_escape_mysql_style`]" Create a simple
/// expression [`Exprs`] with fluent API and convert to sql escaping column
/// names in MySQL style.
///
/// 4. [`simple_plan_to_sql_demo`]: Create a simple logical plan using the
/// DataFrames API and convert to sql string.
///
/// 5. [`round_trip_plan_to_sql_demo`]: Create a logical plan from a SQL string, modify it using the
/// DataFrames API and convert it back to a sql string.
#[tokio::main]
async fn main() -> Result<()> {
// See how to evaluate expressions
simple_expr_to_sql_demo()?;
simple_expr_to_sql_demo_no_escape()?;
simple_expr_to_sql_demo_escape_mysql_style()?;
simple_plan_to_sql_parquest_dataframe_demo().await?;
round_trip_plan_to_sql_parquest_dataframe_demo().await?;
simple_plan_to_sql_demo().await?;
round_trip_plan_to_sql_demo().await?;
Ok(())
}

/// DataFusion can convert expressions to SQL, using column name escaping
/// PostgreSQL style.
fn simple_expr_to_sql_demo() -> Result<()> {
let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
let ast = expr_to_sql(&expr)?;
let sql = format!("{}", ast);
let sql = expr_to_sql(&expr)?.to_string();
assert_eq!(sql, r#"(("a" < 5) OR ("a" = 8))"#);
Ok(())
}
Expand All @@ -62,8 +71,7 @@ fn simple_expr_to_sql_demo_no_escape() -> Result<()> {
let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
let dialect = CustomDialect::new(None);
let unparser = Unparser::new(&dialect);
let ast = unparser.expr_to_sql(&expr)?;
let sql = format!("{}", ast);
let sql = unparser.expr_to_sql(&expr)?.to_string();
assert_eq!(sql, r#"((a < 5) OR (a = 8))"#);
Ok(())
}
Expand All @@ -74,16 +82,14 @@ fn simple_expr_to_sql_demo_escape_mysql_style() -> Result<()> {
let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
let dialect = CustomDialect::new(Some('`'));
let unparser = Unparser::new(&dialect);
let ast = unparser.expr_to_sql(&expr)?;
let sql = format!("{}", ast);
let sql = unparser.expr_to_sql(&expr)?.to_string();
assert_eq!(sql, r#"((`a` < 5) OR (`a` = 8))"#);
Ok(())
}

/// DataFusion can convert a logic plan created using the DataFrames API to read from a parquet file
/// to SQL, using column name escaping PostgreSQL style.
async fn simple_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
// create local execution context
async fn simple_plan_to_sql_demo() -> Result<()> {
let ctx = SessionContext::new();

let testdata = datafusion::test_util::parquet_test_data();
Expand All @@ -95,9 +101,8 @@ async fn simple_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
.await?
.select_columns(&["id", "int_col", "double_col", "date_string_col"])?;

let ast = plan_to_sql(df.logical_plan())?;

let sql = format!("{}", ast);
// Convert the data frame to a SQL string
let sql = plan_to_sql(df.logical_plan())?.to_string();

assert_eq!(
sql,
Expand All @@ -107,9 +112,9 @@ async fn simple_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
Ok(())
}

// DataFusion could parse a SQL into a DataFrame, adding a Filter, and converting that back to sql.
async fn round_trip_plan_to_sql_parquest_dataframe_demo() -> Result<()> {
// create local execution context
/// DataFusion can also be used to parse SQL, programmatically modify the query
/// (in this case adding a filter) and then and converting back to SQL.
async fn round_trip_plan_to_sql_demo() -> Result<()> {
let ctx = SessionContext::new();

let testdata = datafusion::test_util::parquet_test_data();
Expand All @@ -124,21 +129,20 @@ async fn round_trip_plan_to_sql_parquest_dataframe_demo() -> Result<()> {

// create a logical plan from a SQL string and then programmatically add new filters
let df = ctx
// Use SQL to read some data from the parquet file
.sql(
"SELECT int_col, double_col, CAST(date_string_col as VARCHAR) \
FROM alltypes_plain",
)
.await?
// Add id > 1 and tinyint_col < double_col filter
.filter(
col("id")
.gt(lit(1))
.and(col("tinyint_col").lt(col("double_col"))),
)?;

let ast = plan_to_sql(df.logical_plan())?;

let sql = format!("{}", ast);

let sql = plan_to_sql(df.logical_plan())?.to_string();
assert_eq!(
sql,
r#"SELECT "alltypes_plain"."int_col", "alltypes_plain"."double_col", CAST("alltypes_plain"."date_string_col" AS VARCHAR) FROM "alltypes_plain" WHERE (("alltypes_plain"."id" > 1) AND ("alltypes_plain"."tinyint_col" < "alltypes_plain"."double_col"))"#
Expand Down
9 changes: 9 additions & 0 deletions datafusion/core/tests/core_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ mod macro_hygiene;
/// Run all tests that are found in the `expr_api` directory
mod expr_api;

/// Run all tests that are found in the `fifo` directory
mod fifo;

/// Run all tests that are found in the `memory_limit` directory
mod memory_limit;

/// Run all tests that are found in the `custom_sources_cases` directory
mod custom_sources_cases;

#[cfg(test)]
#[ctor::ctor]
fn init() {
Expand Down
Loading

0 comments on commit c712a5c

Please sign in to comment.