Skip to content

Commit ecc2f7f

Browse files
author
wiedld
committed
Revert "Introduce TypePlanner for customizing type planning (apache#13294)"
This reverts commit 4e1f839.
1 parent be7616e commit ecc2f7f

File tree

6 files changed

+10
-211
lines changed

6 files changed

+10
-211
lines changed

datafusion/core/src/execution/context/mod.rs

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,24 +1789,22 @@ impl<'n, 'a> TreeNodeVisitor<'n> for BadPlanVisitor<'a> {
17891789

17901790
#[cfg(test)]
17911791
mod tests {
1792+
use std::env;
1793+
use std::path::PathBuf;
1794+
17921795
use super::{super::options::CsvReadOptions, *};
17931796
use crate::assert_batches_eq;
17941797
use crate::execution::memory_pool::MemoryConsumer;
17951798
use crate::execution::runtime_env::RuntimeEnvBuilder;
17961799
use crate::test;
17971800
use crate::test_util::{plan_and_collect, populate_csv_partitions};
1798-
use arrow_schema::{DataType, TimeUnit};
1799-
use std::env;
1800-
use std::path::PathBuf;
18011801

18021802
use datafusion_common_runtime::SpawnedTask;
18031803

18041804
use crate::catalog::SchemaProvider;
18051805
use crate::execution::session_state::SessionStateBuilder;
18061806
use crate::physical_planner::PhysicalPlanner;
18071807
use async_trait::async_trait;
1808-
use datafusion_expr::planner::TypePlanner;
1809-
use sqlparser::ast;
18101808
use tempfile::TempDir;
18111809

18121810
#[tokio::test]
@@ -2203,29 +2201,6 @@ mod tests {
22032201
Ok(())
22042202
}
22052203

2206-
#[tokio::test]
2207-
async fn custom_type_planner() -> Result<()> {
2208-
let state = SessionStateBuilder::new()
2209-
.with_default_features()
2210-
.with_type_planner(Arc::new(MyTypePlanner {}))
2211-
.build();
2212-
let ctx = SessionContext::new_with_state(state);
2213-
let result = ctx
2214-
.sql("SELECT DATETIME '2021-01-01 00:00:00'")
2215-
.await?
2216-
.collect()
2217-
.await?;
2218-
let expected = [
2219-
"+-----------------------------+",
2220-
"| Utf8(\"2021-01-01 00:00:00\") |",
2221-
"+-----------------------------+",
2222-
"| 2021-01-01T00:00:00 |",
2223-
"+-----------------------------+",
2224-
];
2225-
assert_batches_eq!(expected, &result);
2226-
Ok(())
2227-
}
2228-
22292204
struct MyPhysicalPlanner {}
22302205

22312206
#[async_trait]
@@ -2286,25 +2261,4 @@ mod tests {
22862261

22872262
Ok(ctx)
22882263
}
2289-
2290-
#[derive(Debug)]
2291-
struct MyTypePlanner {}
2292-
2293-
impl TypePlanner for MyTypePlanner {
2294-
fn plan_type(&self, sql_type: &ast::DataType) -> Result<Option<DataType>> {
2295-
match sql_type {
2296-
ast::DataType::Datetime(precision) => {
2297-
let precision = match precision {
2298-
Some(0) => TimeUnit::Second,
2299-
Some(3) => TimeUnit::Millisecond,
2300-
Some(6) => TimeUnit::Microsecond,
2301-
None | Some(9) => TimeUnit::Nanosecond,
2302-
_ => unreachable!(),
2303-
};
2304-
Ok(Some(DataType::Timestamp(precision, None)))
2305-
}
2306-
_ => Ok(None),
2307-
}
2308-
}
2309-
}
23102264
}

datafusion/core/src/execution/session_state.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use datafusion_execution::runtime_env::RuntimeEnv;
4747
use datafusion_execution::TaskContext;
4848
use datafusion_expr::execution_props::ExecutionProps;
4949
use datafusion_expr::expr_rewriter::FunctionRewrite;
50-
use datafusion_expr::planner::{ExprPlanner, TypePlanner};
50+
use datafusion_expr::planner::ExprPlanner;
5151
use datafusion_expr::registry::{FunctionRegistry, SerializerRegistry};
5252
use datafusion_expr::simplify::SimplifyInfo;
5353
use datafusion_expr::var_provider::{is_system_variables, VarType};
@@ -127,8 +127,6 @@ pub struct SessionState {
127127
analyzer: Analyzer,
128128
/// Provides support for customising the SQL planner, e.g. to add support for custom operators like `->>` or `?`
129129
expr_planners: Vec<Arc<dyn ExprPlanner>>,
130-
/// Provides support for customising the SQL type planning
131-
type_planner: Option<Arc<dyn TypePlanner>>,
132130
/// Responsible for optimizing a logical plan
133131
optimizer: Optimizer,
134132
/// Responsible for optimizing a physical execution plan
@@ -193,7 +191,6 @@ impl Debug for SessionState {
193191
.field("table_factories", &self.table_factories)
194192
.field("function_factory", &self.function_factory)
195193
.field("expr_planners", &self.expr_planners)
196-
.field("type_planner", &self.type_planner)
197194
.field("query_planners", &self.query_planner)
198195
.field("analyzer", &self.analyzer)
199196
.field("optimizer", &self.optimizer)
@@ -959,7 +956,6 @@ pub struct SessionStateBuilder {
959956
session_id: Option<String>,
960957
analyzer: Option<Analyzer>,
961958
expr_planners: Option<Vec<Arc<dyn ExprPlanner>>>,
962-
type_planner: Option<Arc<dyn TypePlanner>>,
963959
optimizer: Option<Optimizer>,
964960
physical_optimizers: Option<PhysicalOptimizer>,
965961
query_planner: Option<Arc<dyn QueryPlanner + Send + Sync>>,
@@ -989,7 +985,6 @@ impl SessionStateBuilder {
989985
session_id: None,
990986
analyzer: None,
991987
expr_planners: None,
992-
type_planner: None,
993988
optimizer: None,
994989
physical_optimizers: None,
995990
query_planner: None,
@@ -1037,7 +1032,6 @@ impl SessionStateBuilder {
10371032
session_id: None,
10381033
analyzer: Some(existing.analyzer),
10391034
expr_planners: Some(existing.expr_planners),
1040-
type_planner: existing.type_planner,
10411035
optimizer: Some(existing.optimizer),
10421036
physical_optimizers: Some(existing.physical_optimizers),
10431037
query_planner: Some(existing.query_planner),
@@ -1133,12 +1127,6 @@ impl SessionStateBuilder {
11331127
self
11341128
}
11351129

1136-
/// Set the [`TypePlanner`] used to customize the behavior of the SQL planner.
1137-
pub fn with_type_planner(mut self, type_planner: Arc<dyn TypePlanner>) -> Self {
1138-
self.type_planner = Some(type_planner);
1139-
self
1140-
}
1141-
11421130
/// Set the [`PhysicalOptimizerRule`]s used to optimize plans.
11431131
pub fn with_physical_optimizer_rules(
11441132
mut self,
@@ -1345,7 +1333,6 @@ impl SessionStateBuilder {
13451333
session_id,
13461334
analyzer,
13471335
expr_planners,
1348-
type_planner,
13491336
optimizer,
13501337
physical_optimizers,
13511338
query_planner,
@@ -1374,7 +1361,6 @@ impl SessionStateBuilder {
13741361
session_id: session_id.unwrap_or(Uuid::new_v4().to_string()),
13751362
analyzer: analyzer.unwrap_or_default(),
13761363
expr_planners: expr_planners.unwrap_or_default(),
1377-
type_planner,
13781364
optimizer: optimizer.unwrap_or_default(),
13791365
physical_optimizers: physical_optimizers.unwrap_or_default(),
13801366
query_planner: query_planner.unwrap_or(Arc::new(DefaultQueryPlanner {})),
@@ -1485,11 +1471,6 @@ impl SessionStateBuilder {
14851471
&mut self.expr_planners
14861472
}
14871473

1488-
/// Returns the current type_planner value
1489-
pub fn type_planner(&mut self) -> &mut Option<Arc<dyn TypePlanner>> {
1490-
&mut self.type_planner
1491-
}
1492-
14931474
/// Returns the current optimizer value
14941475
pub fn optimizer(&mut self) -> &mut Option<Optimizer> {
14951476
&mut self.optimizer
@@ -1612,7 +1593,6 @@ impl Debug for SessionStateBuilder {
16121593
.field("table_factories", &self.table_factories)
16131594
.field("function_factory", &self.function_factory)
16141595
.field("expr_planners", &self.expr_planners)
1615-
.field("type_planner", &self.type_planner)
16161596
.field("query_planners", &self.query_planner)
16171597
.field("analyzer_rules", &self.analyzer_rules)
16181598
.field("analyzer", &self.analyzer)
@@ -1654,14 +1634,6 @@ impl<'a> ContextProvider for SessionContextProvider<'a> {
16541634
&self.state.expr_planners
16551635
}
16561636

1657-
fn get_type_planner(&self) -> Option<Arc<dyn TypePlanner>> {
1658-
if let Some(type_planner) = &self.state.type_planner {
1659-
Some(Arc::clone(type_planner))
1660-
} else {
1661-
None
1662-
}
1663-
}
1664-
16651637
fn get_table_source(
16661638
&self,
16671639
name: TableReference,

datafusion/expr/src/planner.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use datafusion_common::{
2525
config::ConfigOptions, file_options::file_type::FileType, not_impl_err, DFSchema,
2626
Result, TableReference,
2727
};
28-
use sqlparser::ast;
2928

3029
use crate::{AggregateUDF, Expr, GetFieldAccess, ScalarUDF, TableSource, WindowUDF};
3130

@@ -67,11 +66,6 @@ pub trait ContextProvider {
6766
&[]
6867
}
6968

70-
/// Getter for the data type planner
71-
fn get_type_planner(&self) -> Option<Arc<dyn TypePlanner>> {
72-
None
73-
}
74-
7569
/// Getter for a UDF description
7670
fn get_function_meta(&self, name: &str) -> Option<Arc<ScalarUDF>>;
7771
/// Getter for a UDAF description
@@ -222,7 +216,7 @@ pub trait ExprPlanner: Debug + Send + Sync {
222216
/// custom expressions.
223217
#[derive(Debug, Clone)]
224218
pub struct RawBinaryExpr {
225-
pub op: ast::BinaryOperator,
219+
pub op: sqlparser::ast::BinaryOperator,
226220
pub left: Expr,
227221
pub right: Expr,
228222
}
@@ -255,13 +249,3 @@ pub enum PlannerResult<T> {
255249
/// The raw expression could not be planned, and is returned unmodified
256250
Original(T),
257251
}
258-
259-
/// This trait allows users to customize the behavior of the data type planning
260-
pub trait TypePlanner: Debug + Send + Sync {
261-
/// Plan SQL type to DataFusion data type
262-
///
263-
/// Returns None if not possible
264-
fn plan_type(&self, _sql_type: &ast::DataType) -> Result<Option<DataType>> {
265-
Ok(None)
266-
}
267-
}

datafusion/sql/src/planner.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
401401
}
402402

403403
pub(crate) fn convert_data_type(&self, sql_type: &SQLDataType) -> Result<DataType> {
404-
// First check if any of the registered type_planner can handle this type
405-
if let Some(type_planner) = self.context_provider.get_type_planner() {
406-
if let Some(data_type) = type_planner.plan_type(sql_type)? {
407-
return Ok(data_type);
408-
}
409-
}
410-
411-
// If no type_planner can handle this type, use the default conversion
412404
match sql_type {
413405
SQLDataType::Array(ArrayElemTypeDef::AngleBracket(inner_sql_type)) => {
414406
// Arrays may be multi-dimensional.

datafusion/sql/tests/common/mod.rs

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
use std::any::Any;
1919
#[cfg(test)]
2020
use std::collections::HashMap;
21-
use std::fmt::{Debug, Display};
21+
use std::fmt::Display;
2222
use std::{sync::Arc, vec};
2323

2424
use arrow_schema::*;
2525
use datafusion_common::config::ConfigOptions;
2626
use datafusion_common::file_options::file_type::FileType;
27-
use datafusion_common::{plan_err, DFSchema, GetExt, Result, TableReference};
28-
use datafusion_expr::planner::{ExprPlanner, PlannerResult, TypePlanner};
29-
use datafusion_expr::{AggregateUDF, Expr, ScalarUDF, TableSource, WindowUDF};
30-
use datafusion_functions_nested::expr_fn::make_array;
27+
use datafusion_common::{plan_err, GetExt, Result, TableReference};
28+
use datafusion_expr::planner::ExprPlanner;
29+
use datafusion_expr::{AggregateUDF, ScalarUDF, TableSource, WindowUDF};
3130
use datafusion_sql::planner::ContextProvider;
3231

3332
struct MockCsvType {}
@@ -55,7 +54,6 @@ pub(crate) struct MockSessionState {
5554
scalar_functions: HashMap<String, Arc<ScalarUDF>>,
5655
aggregate_functions: HashMap<String, Arc<AggregateUDF>>,
5756
expr_planners: Vec<Arc<dyn ExprPlanner>>,
58-
type_planner: Option<Arc<dyn TypePlanner>>,
5957
window_functions: HashMap<String, Arc<WindowUDF>>,
6058
pub config_options: ConfigOptions,
6159
}
@@ -66,11 +64,6 @@ impl MockSessionState {
6664
self
6765
}
6866

69-
pub fn with_type_planner(mut self, type_planner: Arc<dyn TypePlanner>) -> Self {
70-
self.type_planner = Some(type_planner);
71-
self
72-
}
73-
7467
pub fn with_scalar_function(mut self, scalar_function: Arc<ScalarUDF>) -> Self {
7568
self.scalar_functions
7669
.insert(scalar_function.name().to_string(), scalar_function);
@@ -266,14 +259,6 @@ impl ContextProvider for MockContextProvider {
266259
fn get_expr_planners(&self) -> &[Arc<dyn ExprPlanner>] {
267260
&self.state.expr_planners
268261
}
269-
270-
fn get_type_planner(&self) -> Option<Arc<dyn TypePlanner>> {
271-
if let Some(type_planner) = &self.state.type_planner {
272-
Some(Arc::clone(type_planner))
273-
} else {
274-
None
275-
}
276-
}
277262
}
278263

279264
struct EmptyTable {
@@ -295,37 +280,3 @@ impl TableSource for EmptyTable {
295280
Arc::clone(&self.table_schema)
296281
}
297282
}
298-
299-
#[derive(Debug)]
300-
pub struct CustomTypePlanner {}
301-
302-
impl TypePlanner for CustomTypePlanner {
303-
fn plan_type(&self, sql_type: &sqlparser::ast::DataType) -> Result<Option<DataType>> {
304-
match sql_type {
305-
sqlparser::ast::DataType::Datetime(precision) => {
306-
let precision = match precision {
307-
Some(0) => TimeUnit::Second,
308-
Some(3) => TimeUnit::Millisecond,
309-
Some(6) => TimeUnit::Microsecond,
310-
None | Some(9) => TimeUnit::Nanosecond,
311-
_ => unreachable!(),
312-
};
313-
Ok(Some(DataType::Timestamp(precision, None)))
314-
}
315-
_ => Ok(None),
316-
}
317-
}
318-
}
319-
320-
#[derive(Debug)]
321-
pub struct CustomExprPlanner {}
322-
323-
impl ExprPlanner for CustomExprPlanner {
324-
fn plan_array_literal(
325-
&self,
326-
exprs: Vec<Expr>,
327-
_schema: &DFSchema,
328-
) -> Result<PlannerResult<Vec<Expr>>> {
329-
Ok(PlannerResult::Planned(make_array(exprs)))
330-
}
331-
}

0 commit comments

Comments
 (0)