-
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
Allow user defined SQL planners to be registered #11208
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ use datafusion_execution::runtime_env::RuntimeEnv; | |
use datafusion_execution::TaskContext; | ||
use datafusion_expr::execution_props::ExecutionProps; | ||
use datafusion_expr::expr_rewriter::FunctionRewrite; | ||
use datafusion_expr::planner::UserDefinedSQLPlanner; | ||
use datafusion_expr::registry::{FunctionRegistry, SerializerRegistry}; | ||
use datafusion_expr::simplify::SimplifyInfo; | ||
use datafusion_expr::var_provider::{is_system_variables, VarType}; | ||
|
@@ -99,6 +100,8 @@ pub struct SessionState { | |
session_id: String, | ||
/// Responsible for analyzing and rewrite a logical plan before optimization | ||
analyzer: Analyzer, | ||
/// Provides support for customising the SQL planner, e.g. to add support for custom operators like `->>` or `?` | ||
user_defined_sql_planners: Vec<Arc<dyn UserDefinedSQLPlanner>>, | ||
/// Responsible for optimizing a logical plan | ||
optimizer: Optimizer, | ||
/// Responsible for optimizing a physical execution plan | ||
|
@@ -231,6 +234,7 @@ impl SessionState { | |
let mut new_self = SessionState { | ||
session_id, | ||
analyzer: Analyzer::new(), | ||
user_defined_sql_planners: vec![], | ||
optimizer: Optimizer::new(), | ||
physical_optimizers: PhysicalOptimizer::new(), | ||
query_planner: Arc::new(DefaultQueryPlanner {}), | ||
|
@@ -947,16 +951,21 @@ impl SessionState { | |
where | ||
S: ContextProvider, | ||
{ | ||
let query = SqlToRel::new_with_options(provider, self.get_parser_options()); | ||
let mut query = SqlToRel::new_with_options(provider, self.get_parser_options()); | ||
|
||
// custom planners are registered first, so they're run first and take precedence over built-in planners | ||
for planner in self.user_defined_sql_planners.iter() { | ||
query = query.with_user_defined_planner(planner.clone()); | ||
} | ||
|
||
// register crate of array expressions (if enabled) | ||
#[cfg(feature = "array_expressions")] | ||
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. It might make sense to move the registering of these planners into the constructor of SessionState rather than here (so users could potentially more carefully control the planner content). But we can do that as a follow on PR |
||
{ | ||
let array_planner = | ||
Arc::new(functions_array::planner::ArrayFunctionPlanner::default()) as _; | ||
Arc::new(functions_array::planner::ArrayFunctionPlanner) as _; | ||
|
||
let field_access_planner = | ||
Arc::new(functions_array::planner::FieldAccessPlanner::default()) as _; | ||
Arc::new(functions_array::planner::FieldAccessPlanner) as _; | ||
|
||
query | ||
.with_user_defined_planner(array_planner) | ||
|
@@ -1176,6 +1185,15 @@ impl FunctionRegistry for SessionState { | |
self.analyzer.add_function_rewrite(rewrite); | ||
Ok(()) | ||
} | ||
|
||
fn register_user_defined_sql_planner( | ||
&mut self, | ||
user_defined_sql_planner: Arc<dyn UserDefinedSQLPlanner>, | ||
) -> datafusion_common::Result<()> { | ||
self.user_defined_sql_planners | ||
.push(user_defined_sql_planner); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl OptimizerConfig for SessionState { | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||||
// 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_array::RecordBatch; | ||||||||
use std::sync::Arc; | ||||||||
|
||||||||
use datafusion::common::{assert_batches_eq, DFSchema}; | ||||||||
use datafusion::error::Result; | ||||||||
use datafusion::execution::FunctionRegistry; | ||||||||
use datafusion::logical_expr::Operator; | ||||||||
use datafusion::prelude::*; | ||||||||
use datafusion::sql::sqlparser::ast::BinaryOperator; | ||||||||
use datafusion_expr::planner::{PlannerResult, RawBinaryExpr, UserDefinedSQLPlanner}; | ||||||||
use datafusion_expr::BinaryExpr; | ||||||||
|
||||||||
struct MyCustomPlanner; | ||||||||
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. ❤️ |
||||||||
|
||||||||
impl UserDefinedSQLPlanner for MyCustomPlanner { | ||||||||
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.
Suggested change
|
||||||||
fn plan_binary_op( | ||||||||
&self, | ||||||||
expr: RawBinaryExpr, | ||||||||
_schema: &DFSchema, | ||||||||
) -> Result<PlannerResult<RawBinaryExpr>> { | ||||||||
match &expr.op { | ||||||||
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. this is awesome |
||||||||
BinaryOperator::Arrow => { | ||||||||
Ok(PlannerResult::Planned(Expr::BinaryExpr(BinaryExpr { | ||||||||
left: Box::new(expr.left.clone()), | ||||||||
right: Box::new(expr.right.clone()), | ||||||||
op: Operator::StringConcat, | ||||||||
}))) | ||||||||
} | ||||||||
BinaryOperator::LongArrow => { | ||||||||
Ok(PlannerResult::Planned(Expr::BinaryExpr(BinaryExpr { | ||||||||
left: Box::new(expr.left.clone()), | ||||||||
right: Box::new(expr.right.clone()), | ||||||||
op: Operator::Plus, | ||||||||
}))) | ||||||||
} | ||||||||
_ => Ok(PlannerResult::Original(expr)), | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
async fn plan_and_collect(sql: &str) -> Result<Vec<RecordBatch>> { | ||||||||
let mut ctx = SessionContext::new(); | ||||||||
ctx.register_user_defined_sql_planner(Arc::new(MyCustomPlanner))?; | ||||||||
ctx.sql(sql).await?.collect().await | ||||||||
} | ||||||||
|
||||||||
#[tokio::test] | ||||||||
async fn test_custom_operators_arrow() { | ||||||||
let actual = plan_and_collect("select 'foo'->'bar';").await.unwrap(); | ||||||||
let expected = [ | ||||||||
"+----------------------------+", | ||||||||
"| Utf8(\"foo\") || Utf8(\"bar\") |", | ||||||||
"+----------------------------+", | ||||||||
"| foobar |", | ||||||||
"+----------------------------+", | ||||||||
]; | ||||||||
assert_batches_eq!(&expected, &actual); | ||||||||
} | ||||||||
|
||||||||
#[tokio::test] | ||||||||
async fn test_custom_operators_long_arrow() { | ||||||||
let actual = plan_and_collect("select 1->>2;").await.unwrap(); | ||||||||
let expected = [ | ||||||||
"+---------------------+", | ||||||||
"| Int64(1) + Int64(2) |", | ||||||||
"+---------------------+", | ||||||||
"| 3 |", | ||||||||
"+---------------------+", | ||||||||
]; | ||||||||
assert_batches_eq!(&expected, &actual); | ||||||||
} |
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.
👍