forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'apache/main' into alamb/PR_flow
- Loading branch information
Showing
148 changed files
with
5,486 additions
and
2,463 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// 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. | ||
|
||
use datafusion::error::Result; | ||
|
||
use datafusion::prelude::*; | ||
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. | ||
/// | ||
/// | ||
/// 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`] | ||
#[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?; | ||
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); | ||
assert_eq!(sql, r#"(("a" < 5) OR ("a" = 8))"#); | ||
Ok(()) | ||
} | ||
|
||
/// DataFusion can convert expressions to SQL without escaping column names using | ||
/// using a custom dialect and an explicit unparser | ||
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); | ||
assert_eq!(sql, r#"((a < 5) OR (a = 8))"#); | ||
Ok(()) | ||
} | ||
|
||
/// DataFusion can convert expressions to SQL without escaping column names using | ||
/// using a custom dialect and an explicit unparser | ||
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); | ||
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 | ||
let ctx = SessionContext::new(); | ||
|
||
let testdata = datafusion::test_util::parquet_test_data(); | ||
let df = ctx | ||
.read_parquet( | ||
&format!("{testdata}/alltypes_plain.parquet"), | ||
ParquetReadOptions::default(), | ||
) | ||
.await? | ||
.select_columns(&["id", "int_col", "double_col", "date_string_col"])?; | ||
|
||
let ast = plan_to_sql(df.logical_plan())?; | ||
|
||
let sql = format!("{}", ast); | ||
|
||
assert_eq!( | ||
sql, | ||
r#"SELECT "?table?"."id", "?table?"."int_col", "?table?"."double_col", "?table?"."date_string_col" FROM "?table?""# | ||
); | ||
|
||
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 | ||
let ctx = SessionContext::new(); | ||
|
||
let testdata = datafusion::test_util::parquet_test_data(); | ||
|
||
// register parquet file with the execution context | ||
ctx.register_parquet( | ||
"alltypes_plain", | ||
&format!("{testdata}/alltypes_plain.parquet"), | ||
ParquetReadOptions::default(), | ||
) | ||
.await?; | ||
|
||
// create a logical plan from a SQL string and then programmatically add new filters | ||
let df = ctx | ||
.sql( | ||
"SELECT int_col, double_col, CAST(date_string_col as VARCHAR) \ | ||
FROM alltypes_plain", | ||
) | ||
.await? | ||
.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); | ||
|
||
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"))"# | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.