-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Support parsing SQL strings to Exprs #10995
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7d71c7
Draft parse_sql
xinlifoobar 4406358
Allow stirng pass
xinlifoobar ea21023
Complete sql to expr support
xinlifoobar 82961c4
Add examples
xinlifoobar 8cddc4a
Add unit tests
xinlifoobar ab14454
Fix format
xinlifoobar 40b6a95
Remove async for trival operation and add parquet demo
xinlifoobar 0650fc7
Fix comments
xinlifoobar 2de72d9
fix comments
xinlifoobar 6035703
fix comments
xinlifoobar ef4d3b0
Fix doc link
xinlifoobar 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 hidden or 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 hidden or 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,157 @@ | ||
| // 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 arrow::datatypes::{DataType, Field, Schema}; | ||
| use datafusion::{ | ||
| assert_batches_eq, | ||
| error::Result, | ||
| prelude::{ParquetReadOptions, SessionContext}, | ||
| }; | ||
| use datafusion_common::DFSchema; | ||
| use datafusion_expr::{col, lit}; | ||
| use datafusion_sql::unparser::Unparser; | ||
|
|
||
| /// This example demonstrates the programmatic parsing of SQL expressions using | ||
xinlifoobar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// the DataFusion [`SessionContext::parse_sql_expr`] API or the [`DataFrame::parse_sql_expr`] API. | ||
| /// | ||
| /// | ||
| /// The code in this example shows how to: | ||
| /// | ||
| /// 1. [`simple_session_context_parse_sql_expr_demo`]: Parse a simple SQL text into a logical | ||
| /// expression using a schema at [`SessionContext`]. | ||
| /// | ||
| /// 2. [`simple_dataframe_parse_sql_expr_demo`]: Parse a simple SQL text into a logical expression | ||
| /// using a schema at [`DataFrame`]. | ||
| /// | ||
| /// 3. [`query_parquet_demo`]: Query a parquet file using the parsed_sql_expr from a DataFrame. | ||
| /// | ||
| /// 4. [`round_trip_parse_sql_expr_demo`]: Parse a SQL text and convert it back to SQL using [`Unparser`]. | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<()> { | ||
| // See how to evaluate expressions | ||
| simple_session_context_parse_sql_expr_demo()?; | ||
| simple_dataframe_parse_sql_expr_demo().await?; | ||
| query_parquet_demo().await?; | ||
| round_trip_parse_sql_expr_demo().await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// DataFusion can parse a SQL text to a logical expression against a schema at [`SessionContext`]. | ||
| fn simple_session_context_parse_sql_expr_demo() -> Result<()> { | ||
| let sql = "a < 5 OR a = 8"; | ||
| let expr = col("a").lt(lit(5_i64)).or(col("a").eq(lit(8_i64))); | ||
|
|
||
| // provide type information that `a` is an Int32 | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]); | ||
| let df_schema = DFSchema::try_from(schema).unwrap(); | ||
| let ctx = SessionContext::new(); | ||
|
|
||
| let parsed_expr = ctx.parse_sql_expr(sql, &df_schema)?; | ||
|
|
||
| assert_eq!(parsed_expr, expr); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// DataFusion can parse a SQL text to an logical expression using schema at [`DataFrame`]. | ||
| async fn simple_dataframe_parse_sql_expr_demo() -> Result<()> { | ||
| let sql = "int_col < 5 OR double_col = 8.0"; | ||
| let expr = col("int_col") | ||
| .lt(lit(5_i64)) | ||
| .or(col("double_col").eq(lit(8.0_f64))); | ||
|
|
||
| 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?; | ||
|
|
||
| let parsed_expr = df.parse_sql_expr(sql)?; | ||
|
|
||
| assert_eq!(parsed_expr, expr); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn query_parquet_demo() -> Result<()> { | ||
| 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?; | ||
|
|
||
| let df = df | ||
| .clone() | ||
| .select(vec![ | ||
| df.parse_sql_expr("int_col")?, | ||
| df.parse_sql_expr("double_col")?, | ||
| ])? | ||
| .filter(df.parse_sql_expr("int_col < 5 OR double_col = 8.0")?)? | ||
| .aggregate( | ||
| vec![df.parse_sql_expr("double_col")?], | ||
| vec![df.parse_sql_expr("SUM(int_col) as sum_int_col")?], | ||
| )? | ||
| // Directly parsing the SQL text into a sort expression is not supported yet, so | ||
| // construct it programatically | ||
| .sort(vec![col("double_col").sort(false, false)])? | ||
| .limit(0, Some(1))?; | ||
|
|
||
| let result = df.collect().await?; | ||
|
|
||
| assert_batches_eq!( | ||
| &[ | ||
| "+------------+----------------------+", | ||
| "| double_col | sum(?table?.int_col) |", | ||
| "+------------+----------------------+", | ||
| "| 10.1 | 4 |", | ||
| "+------------+----------------------+", | ||
| ], | ||
| &result | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// DataFusion can parse a SQL text and convert it back to SQL using [`Unparser`]. | ||
| async fn round_trip_parse_sql_expr_demo() -> Result<()> { | ||
| let sql = "((int_col < 5) OR (double_col = 8))"; | ||
|
|
||
| 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?; | ||
|
|
||
| let parsed_expr = df.parse_sql_expr(sql)?; | ||
|
|
||
| let unparser = Unparser::default(); | ||
| let round_trip_sql = unparser.expr_to_sql(&parsed_expr)?.to_string(); | ||
|
|
||
| assert_eq!(sql, round_trip_sql); | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.