-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Switch to non-recursive on heap virtual stack when building logical plan from SQL expression #6360
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
5 commits
Select commit
Hold shift + click to select a range
b176c79
Adding stack overflow test
aprimadi 59099ea
Implement heap based non-recursive visitor
aprimadi c50b62b
Fix clippy
aprimadi e38636c
Update datafusion/sql/src/expr/mod.rs
aprimadi 6a93b69
Update datafusion/sql/src/expr/mod.rs
aprimadi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,4 +47,5 @@ sqlparser = "0.33" | |
| [dev-dependencies] | ||
| ctor = "0.2.0" | ||
| env_logger = "0.10" | ||
| paste = "^1.0" | ||
| rstest = "0.17" | ||
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 |
|---|---|---|
|
|
@@ -46,27 +46,56 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| schema: &DFSchema, | ||
| planner_context: &mut PlannerContext, | ||
| ) -> Result<Expr> { | ||
| // Workaround for https://github.com/apache/arrow-datafusion/issues/4065 | ||
| // | ||
| // Minimize stack space required in debug builds to plan | ||
| // deeply nested binary operators by keeping the stack space | ||
| // needed for sql_expr_to_logical_expr minimal for BinaryOp | ||
| // | ||
| // The reason this reduces stack size in debug builds is | ||
| // explained in the "Technical Backstory" heading of | ||
| // https://github.com/apache/arrow-datafusion/pull/1047 | ||
| // | ||
| // A likely better way to support deeply nested expressions | ||
| // would be to avoid recursion all together and use an | ||
| // iterative algorithm. | ||
| match sql { | ||
| SQLExpr::BinaryOp { left, op, right } => { | ||
| self.parse_sql_binary_op(*left, op, *right, schema, planner_context) | ||
| enum StackEntry { | ||
| SQLExpr(Box<SQLExpr>), | ||
| Operator(Operator), | ||
| } | ||
|
|
||
| // Virtual stack machine to convert SQLExpr to Expr | ||
| // This allows visiting the expr tree in a depth-first manner which | ||
| // produces expressions in postfix notations, i.e. `a + b` => `a b +`. | ||
| // See https://github.com/apache/arrow-datafusion/issues/1444 | ||
| let mut stack = vec![StackEntry::SQLExpr(Box::new(sql))]; | ||
| let mut eval_stack = vec![]; | ||
|
|
||
| while let Some(entry) = stack.pop() { | ||
| match entry { | ||
| StackEntry::SQLExpr(sql_expr) => { | ||
| match *sql_expr { | ||
| SQLExpr::BinaryOp { left, op, right } => { | ||
| // Note the order that we push the entries to the stack | ||
| // is important. We want to visit the left node first. | ||
| let op = self.parse_sql_binary_op(op)?; | ||
| stack.push(StackEntry::Operator(op)); | ||
| stack.push(StackEntry::SQLExpr(right)); | ||
| stack.push(StackEntry::SQLExpr(left)); | ||
| } | ||
| _ => { | ||
| let expr = self.sql_expr_to_logical_expr_internal( | ||
| *sql_expr, | ||
| schema, | ||
| planner_context, | ||
| )?; | ||
| eval_stack.push(expr); | ||
| } | ||
| } | ||
| } | ||
| StackEntry::Operator(op) => { | ||
| let right = eval_stack.pop().unwrap(); | ||
| let left = eval_stack.pop().unwrap(); | ||
| let expr = Expr::BinaryExpr(BinaryExpr::new( | ||
| Box::new(left), | ||
| op, | ||
| Box::new(right), | ||
| )); | ||
| eval_stack.push(expr); | ||
| } | ||
| } | ||
| // since this function requires more space per frame | ||
| // avoid calling it for binary ops | ||
| _ => self.sql_expr_to_logical_expr_internal(sql, schema, planner_context), | ||
| } | ||
|
|
||
| assert_eq!(1, eval_stack.len()); | ||
| let expr = eval_stack.pop().unwrap(); | ||
| Ok(expr) | ||
| } | ||
|
|
||
| /// Generate a relational expression from a SQL expression | ||
|
|
@@ -566,3 +595,124 @@ fn plan_indexed(expr: Expr, mut keys: Vec<SQLExpr>) -> Result<Expr> { | |
| plan_key(key)?, | ||
| ))) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use sqlparser::dialect::GenericDialect; | ||
| use sqlparser::parser::Parser; | ||
|
|
||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_expr::logical_plan::builder::LogicalTableSource; | ||
| use datafusion_expr::{AggregateUDF, ScalarUDF, TableSource}; | ||
|
|
||
| use crate::TableReference; | ||
|
|
||
| struct TestSchemaProvider { | ||
| options: ConfigOptions, | ||
| tables: HashMap<String, Arc<dyn TableSource>>, | ||
| } | ||
|
|
||
| impl TestSchemaProvider { | ||
| pub fn new() -> Self { | ||
| let mut tables = HashMap::new(); | ||
| tables.insert( | ||
| "table1".to_string(), | ||
| create_table_source(vec![Field::new( | ||
| "column1".to_string(), | ||
| DataType::Utf8, | ||
| false, | ||
| )]), | ||
| ); | ||
|
|
||
| Self { | ||
| options: Default::default(), | ||
| tables, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ContextProvider for TestSchemaProvider { | ||
| fn get_table_provider( | ||
| &self, | ||
| name: TableReference, | ||
| ) -> Result<Arc<dyn TableSource>> { | ||
| match self.tables.get(name.table()) { | ||
| Some(table) => Ok(table.clone()), | ||
| _ => Err(DataFusionError::Plan(format!( | ||
| "Table not found: {}", | ||
| name.table() | ||
| ))), | ||
| } | ||
| } | ||
|
|
||
| fn get_function_meta(&self, _name: &str) -> Option<Arc<ScalarUDF>> { | ||
| None | ||
| } | ||
|
|
||
| fn get_aggregate_meta(&self, _name: &str) -> Option<Arc<AggregateUDF>> { | ||
| None | ||
| } | ||
|
|
||
| fn get_variable_type(&self, _variable_names: &[String]) -> Option<DataType> { | ||
| None | ||
| } | ||
|
|
||
| fn options(&self) -> &ConfigOptions { | ||
| &self.options | ||
| } | ||
| } | ||
|
|
||
| fn create_table_source(fields: Vec<Field>) -> Arc<dyn TableSource> { | ||
| Arc::new(LogicalTableSource::new(Arc::new( | ||
| Schema::new_with_metadata(fields, HashMap::new()), | ||
| ))) | ||
| } | ||
|
|
||
| macro_rules! test_stack_overflow { | ||
| ($num_expr:expr) => { | ||
| paste::item! { | ||
| #[test] | ||
| fn [<test_stack_overflow_ $num_expr>]() { | ||
| let schema = DFSchema::empty(); | ||
| let mut planner_context = PlannerContext::default(); | ||
|
|
||
| let expr_str = (0..$num_expr) | ||
| .map(|i| format!("column1 = 'value{:?}'", i)) | ||
| .collect::<Vec<String>>() | ||
| .join(" OR "); | ||
|
|
||
| let dialect = GenericDialect{}; | ||
| let mut parser = Parser::new(&dialect) | ||
| .try_with_sql(expr_str.as_str()) | ||
| .unwrap(); | ||
| let sql_expr = parser.parse_expr().unwrap(); | ||
|
|
||
| let schema_provider = TestSchemaProvider::new(); | ||
| let sql_to_rel = SqlToRel::new(&schema_provider); | ||
|
|
||
| // Should not stack overflow | ||
| sql_to_rel.sql_expr_to_logical_expr( | ||
| sql_expr, | ||
| &schema, | ||
| &mut planner_context, | ||
| ).unwrap(); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| test_stack_overflow!(64); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I verified that without the code in this PR, the tests fail as expected: |
||
| test_stack_overflow!(128); | ||
| test_stack_overflow!(256); | ||
| test_stack_overflow!(512); | ||
| test_stack_overflow!(1024); | ||
| test_stack_overflow!(2048); | ||
| test_stack_overflow!(4096); | ||
| test_stack_overflow!(8192); | ||
| } | ||
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.