-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for simple Expr --> SQL conversion #10528
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bb4d39d
Example for simple conversion
edmondop a6e2f70
Fixing example
edmondop ee48ccd
Updating README
edmondop 3f2da5c
Merge branch 'main' into issue-10524
edmondop 7570052
Update plan_to_sql.rs
edmondop c976128
Fixing formatting
edmondop 01a8267
Update README.md
edmondop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,79 @@ | ||
// 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::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()?; | ||
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(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems an additional
-
?