@@ -29,11 +29,13 @@ use std::{
29
29
use arrow_schema:: DataType ;
30
30
use chrono:: { DateTime , NaiveDate } ;
31
31
use datafusion:: execution:: context:: SessionState ;
32
+ use datafusion:: execution:: session_state:: SessionStateBuilder ;
32
33
use datafusion:: execution:: FunctionRegistry ;
33
34
use datafusion_common:: Result as DFResult ;
34
35
use datafusion_common:: { config:: ConfigOptions , DFSchema , Result , ScalarValue , TableReference } ;
35
36
use datafusion_expr:: {
36
- expr:: InList , AggregateUDF , Between , BinaryExpr , Cast , Expr , Like , TableSource ,
37
+ expr:: InList , planner:: ExprPlanner , AggregateUDF , Between , BinaryExpr , Cast , Expr , Like ,
38
+ TableSource ,
37
39
} ;
38
40
use datafusion_sql:: planner:: { ContextProvider , SqlToRel } ;
39
41
use datafusion_sql:: sqlparser:: ast:: escape_quoted_string;
@@ -46,14 +48,44 @@ use crate::{DeltaResult, DeltaTableError};
46
48
use super :: DeltaParserOptions ;
47
49
48
50
pub ( crate ) struct DeltaContextProvider < ' a > {
49
- state : & ' a SessionState ,
51
+ state : SessionState ,
52
+ /// Keeping this around just to make use of the 'a lifetime
53
+ _original : & ' a SessionState ,
54
+ planners : Vec < Arc < dyn ExprPlanner > > ,
55
+ }
56
+
57
+ impl < ' a > DeltaContextProvider < ' a > {
58
+ fn new ( state : & ' a SessionState ) -> Self {
59
+ let planners = state. expr_planners ( ) ;
60
+ DeltaContextProvider {
61
+ planners,
62
+ // Creating a new session state with overridden scalar_functions since
63
+ // the get_field() UDF was dropped from the default scalar functions upstream in
64
+ // `36660fe10d9c0cdff62e0da0b94bee28422d3419`
65
+ state : SessionStateBuilder :: new_from_existing ( state. clone ( ) )
66
+ . with_scalar_functions (
67
+ state
68
+ . scalar_functions ( )
69
+ . values ( )
70
+ . cloned ( )
71
+ . chain ( std:: iter:: once ( datafusion:: functions:: core:: get_field ( ) ) )
72
+ . collect ( ) ,
73
+ )
74
+ . build ( ) ,
75
+ _original : state,
76
+ }
77
+ }
50
78
}
51
79
52
80
impl < ' a > ContextProvider for DeltaContextProvider < ' a > {
53
81
fn get_table_source ( & self , _name : TableReference ) -> DFResult < Arc < dyn TableSource > > {
54
82
unimplemented ! ( )
55
83
}
56
84
85
+ fn get_expr_planners ( & self ) -> & [ Arc < dyn ExprPlanner > ] {
86
+ self . planners . as_slice ( )
87
+ }
88
+
57
89
fn get_function_meta ( & self , name : & str ) -> Option < Arc < datafusion_expr:: ScalarUDF > > {
58
90
self . state . scalar_functions ( ) . get ( name) . cloned ( )
59
91
}
@@ -75,15 +107,15 @@ impl<'a> ContextProvider for DeltaContextProvider<'a> {
75
107
}
76
108
77
109
fn udf_names ( & self ) -> Vec < String > {
78
- unimplemented ! ( )
110
+ self . state . scalar_functions ( ) . keys ( ) . cloned ( ) . collect ( )
79
111
}
80
112
81
113
fn udaf_names ( & self ) -> Vec < String > {
82
- unimplemented ! ( )
114
+ self . state . aggregate_functions ( ) . keys ( ) . cloned ( ) . collect ( )
83
115
}
84
116
85
117
fn udwf_names ( & self ) -> Vec < String > {
86
- unimplemented ! ( )
118
+ self . state . window_functions ( ) . keys ( ) . cloned ( ) . collect ( )
87
119
}
88
120
}
89
121
@@ -107,16 +139,10 @@ pub(crate) fn parse_predicate_expression(
107
139
source : Box :: new ( err) ,
108
140
} ) ?;
109
141
110
- let context_provider = DeltaContextProvider { state : df_state } ;
111
- let mut sql_to_rel =
142
+ let context_provider = DeltaContextProvider :: new ( df_state) ;
143
+ let sql_to_rel =
112
144
SqlToRel :: new_with_options ( & context_provider, DeltaParserOptions :: default ( ) . into ( ) ) ;
113
145
114
- // NOTE: This can be probably removed with Datafusion 41 once
115
- // <https://github.com/apache/datafusion/pull/11485> is released
116
- for planner in context_provider. state . expr_planners ( ) {
117
- sql_to_rel = sql_to_rel. with_user_defined_planner ( planner. clone ( ) ) ;
118
- }
119
-
120
146
Ok ( sql_to_rel. sql_to_expr ( sql, schema, & mut Default :: default ( ) ) ?)
121
147
}
122
148
@@ -401,6 +427,8 @@ impl<'a> fmt::Display for ScalarValueFormat<'a> {
401
427
#[ cfg( test) ]
402
428
mod test {
403
429
use arrow_schema:: DataType as ArrowDataType ;
430
+ use datafusion:: functions_array:: expr_fn:: cardinality;
431
+ use datafusion:: functions_nested:: expr_ext:: { IndexAccessor , SliceAccessor } ;
404
432
use datafusion:: prelude:: SessionContext ;
405
433
use datafusion_common:: { Column , ScalarValue , ToDFSchema } ;
406
434
use datafusion_expr:: expr:: ScalarFunction ;
@@ -409,8 +437,6 @@ mod test {
409
437
use datafusion_functions:: core:: expr_ext:: FieldAccessor ;
410
438
use datafusion_functions:: encoding:: expr_fn:: decode;
411
439
use datafusion_functions:: expr_fn:: substring;
412
- use datafusion_functions_array:: expr_ext:: { IndexAccessor , SliceAccessor } ;
413
- use datafusion_functions_array:: expr_fn:: cardinality;
414
440
415
441
use crate :: delta_datafusion:: { DataFusionMixins , DeltaSessionContext } ;
416
442
use crate :: kernel:: { ArrayType , DataType , PrimitiveType , StructField , StructType } ;
0 commit comments