@@ -29,13 +29,18 @@ use sqlparser::ast;
29
29
30
30
use crate :: { AggregateUDF , Expr , GetFieldAccess , ScalarUDF , TableSource , WindowUDF } ;
31
31
32
- /// Provides the `SQL` query planner meta-data about tables and
33
- /// functions referenced in SQL statements, without a direct dependency on other
34
- /// DataFusion structures
32
+ /// Provides the `SQL` query planner meta-data about tables and
33
+ /// functions referenced in SQL statements, without a direct dependency on the
34
+ /// `datafusion` Catalog structures such as [`TableProvider`]
35
+ ///
36
+ /// [`TableProvider`]: https://docs.rs/datafusion/latest/datafusion/catalog/trait.TableProvider.html
35
37
pub trait ContextProvider {
36
- /// Getter for a datasource
38
+ /// Returns a table by reference, if it exists
37
39
fn get_table_source ( & self , name : TableReference ) -> Result < Arc < dyn TableSource > > ;
38
40
41
+ /// Return the type of a file based on its extension (e.g. `.parquet`)
42
+ ///
43
+ /// This is used to plan `COPY` statements
39
44
fn get_file_type ( & self , _ext : & str ) -> Result < Arc < dyn FileType > > {
40
45
not_impl_err ! ( "Registered file types are not supported" )
41
46
}
@@ -49,11 +54,20 @@ pub trait ContextProvider {
49
54
not_impl_err ! ( "Table Functions are not supported" )
50
55
}
51
56
52
- /// This provides a worktable (an intermediate table that is used to store the results of a CTE during execution)
53
- /// We don't directly implement this in the logical plan's ['SqlToRel`]
54
- /// because the sql code needs access to a table that contains execution-related types that can't be a direct dependency
55
- /// of the sql crate (namely, the `CteWorktable`).
57
+ /// Provides an intermediate table that is used to store the results of a CTE during execution
58
+ ///
59
+ /// CTE stands for "Common Table Expression"
60
+ ///
61
+ /// # Notes
62
+ /// We don't directly implement this in [`SqlToRel`] as implementing this function
63
+ /// often requires access to a table that contains
64
+ /// execution-related types that can't be a direct dependency
65
+ /// of the sql crate (for example [`CteWorkTable`]).
66
+ ///
56
67
/// The [`ContextProvider`] provides a way to "hide" this dependency.
68
+ ///
69
+ /// [`SqlToRel`]: https://docs.rs/datafusion/latest/datafusion/sql/planner/struct.SqlToRel.html
70
+ /// [`CteWorkTable`]: https://docs.rs/datafusion/latest/datafusion/datasource/cte_worktable/struct.CteWorkTable.html
57
71
fn create_cte_work_table (
58
72
& self ,
59
73
_name : & str ,
@@ -62,39 +76,44 @@ pub trait ContextProvider {
62
76
not_impl_err ! ( "Recursive CTE is not implemented" )
63
77
}
64
78
65
- /// Getter for expr planners
79
+ /// Return [`ExprPlanner`] extensions for planning expressions
66
80
fn get_expr_planners ( & self ) -> & [ Arc < dyn ExprPlanner > ] {
67
81
& [ ]
68
82
}
69
83
70
- /// Getter for the data type planner
84
+ /// Return [`TypePlanner`] extensions for planning data types
71
85
fn get_type_planner ( & self ) -> Option < Arc < dyn TypePlanner > > {
72
86
None
73
87
}
74
88
75
- /// Getter for a UDF description
89
+ /// Return the scalar function with a given name, if any
76
90
fn get_function_meta ( & self , name : & str ) -> Option < Arc < ScalarUDF > > ;
77
- /// Getter for a UDAF description
91
+
92
+ /// Return the aggregate function with a given name, if any
78
93
fn get_aggregate_meta ( & self , name : & str ) -> Option < Arc < AggregateUDF > > ;
79
- /// Getter for a UDWF
94
+
95
+ /// Return the window function with a given name, if any
80
96
fn get_window_meta ( & self , name : & str ) -> Option < Arc < WindowUDF > > ;
81
- /// Getter for system/user-defined variable type
97
+
98
+ /// Return the system/user-defined variable type, if any
99
+ ///
100
+ /// A user defined variable is typically accessed via `@var_name`
82
101
fn get_variable_type ( & self , variable_names : & [ String ] ) -> Option < DataType > ;
83
102
84
- /// Get configuration options
103
+ /// Return overall configuration options
85
104
fn options ( & self ) -> & ConfigOptions ;
86
105
87
- /// Get all user defined scalar function names
106
+ /// Return all scalar function names
88
107
fn udf_names ( & self ) -> Vec < String > ;
89
108
90
- /// Get all user defined aggregate function names
109
+ /// Return all aggregate function names
91
110
fn udaf_names ( & self ) -> Vec < String > ;
92
111
93
- /// Get all user defined window function names
112
+ /// Return all window function names
94
113
fn udwf_names ( & self ) -> Vec < String > ;
95
114
}
96
115
97
- /// This trait allows users to customize the behavior of the SQL planner
116
+ /// Customize planning of SQL AST expressions to [`Expr`]s
98
117
pub trait ExprPlanner : Debug + Send + Sync {
99
118
/// Plan the binary operation between two expressions, returns original
100
119
/// BinaryExpr if not possible
@@ -106,9 +125,9 @@ pub trait ExprPlanner: Debug + Send + Sync {
106
125
Ok ( PlannerResult :: Original ( expr) )
107
126
}
108
127
109
- /// Plan the field access expression
128
+ /// Plan the field access expression, such as `foo.bar`
110
129
///
111
- /// returns original FieldAccessExpr if not possible
130
+ /// returns original [`RawFieldAccessExpr`] if not possible
112
131
fn plan_field_access (
113
132
& self ,
114
133
expr : RawFieldAccessExpr ,
@@ -117,7 +136,7 @@ pub trait ExprPlanner: Debug + Send + Sync {
117
136
Ok ( PlannerResult :: Original ( expr) )
118
137
}
119
138
120
- /// Plan the array literal, returns OriginalArray if not possible
139
+ /// Plan an array literal, such as `[1, 2, 3]`
121
140
///
122
141
/// Returns origin expression arguments if not possible
123
142
fn plan_array_literal (
@@ -128,13 +147,14 @@ pub trait ExprPlanner: Debug + Send + Sync {
128
147
Ok ( PlannerResult :: Original ( exprs) )
129
148
}
130
149
131
- // Plan the POSITION expression, e.g., POSITION(<expr> in <expr>)
132
- // returns origin expression arguments if not possible
150
+ /// Plan a `POSITION` expression, such as `POSITION(<expr> in <expr>)`
151
+ ///
152
+ /// returns origin expression arguments if not possible
133
153
fn plan_position ( & self , args : Vec < Expr > ) -> Result < PlannerResult < Vec < Expr > > > {
134
154
Ok ( PlannerResult :: Original ( args) )
135
155
}
136
156
137
- /// Plan the dictionary literal `{ key: value, ...}`
157
+ /// Plan a dictionary literal, such as `{ key: value, ...}`
138
158
///
139
159
/// Returns origin expression arguments if not possible
140
160
fn plan_dictionary_literal (
@@ -145,27 +165,26 @@ pub trait ExprPlanner: Debug + Send + Sync {
145
165
Ok ( PlannerResult :: Original ( expr) )
146
166
}
147
167
148
- /// Plan an extract expression, e.g., `EXTRACT(month FROM foo)`
168
+ /// Plan an extract expression, such as `EXTRACT(month FROM foo)`
149
169
///
150
170
/// Returns origin expression arguments if not possible
151
171
fn plan_extract ( & self , args : Vec < Expr > ) -> Result < PlannerResult < Vec < Expr > > > {
152
172
Ok ( PlannerResult :: Original ( args) )
153
173
}
154
174
155
- /// Plan an substring expression, e.g., `SUBSTRING(<expr> [FROM <expr>] [FOR <expr>])`
175
+ /// Plan an substring expression, such as `SUBSTRING(<expr> [FROM <expr>] [FOR <expr>])`
156
176
///
157
177
/// Returns origin expression arguments if not possible
158
178
fn plan_substring ( & self , args : Vec < Expr > ) -> Result < PlannerResult < Vec < Expr > > > {
159
179
Ok ( PlannerResult :: Original ( args) )
160
180
}
161
181
162
- /// Plans a struct `struct(expression1[, ..., expression_n]) `
163
- /// literal based on the given input expressions.
164
- /// This function takes a vector of expressions and a boolean flag indicating whether
165
- /// the struct uses the optional name
182
+ /// Plans a struct literal, such as `{'field1' : expr1, 'field2' : expr2, ...} `
183
+ ///
184
+ /// This function takes a vector of expressions and a boolean flag
185
+ /// indicating whether the struct uses the optional name
166
186
///
167
- /// Returns a `PlannerResult` containing either the planned struct expressions or the original
168
- /// input expressions if planning is not possible.
187
+ /// Returns the original input expressions if planning is not possible.
169
188
fn plan_struct_literal (
170
189
& self ,
171
190
args : Vec < Expr > ,
@@ -174,26 +193,26 @@ pub trait ExprPlanner: Debug + Send + Sync {
174
193
Ok ( PlannerResult :: Original ( args) )
175
194
}
176
195
177
- /// Plans an overlay expression eg `overlay(str PLACING substr FROM pos [FOR count])`
196
+ /// Plans an overlay expression, such as `overlay(str PLACING substr FROM pos [FOR count])`
178
197
///
179
198
/// Returns origin expression arguments if not possible
180
199
fn plan_overlay ( & self , args : Vec < Expr > ) -> Result < PlannerResult < Vec < Expr > > > {
181
200
Ok ( PlannerResult :: Original ( args) )
182
201
}
183
202
184
- /// Plan a make_map expression, e.g., `make_map(key1, value1, key2, value2, ...)`
203
+ /// Plans a ` make_map` expression, such as `make_map(key1, value1, key2, value2, ...)`
185
204
///
186
205
/// Returns origin expression arguments if not possible
187
206
fn plan_make_map ( & self , args : Vec < Expr > ) -> Result < PlannerResult < Vec < Expr > > > {
188
207
Ok ( PlannerResult :: Original ( args) )
189
208
}
190
209
191
- /// Plans compound identifier eg `db.schema.table` for non-empty nested names
210
+ /// Plans compound identifier such as `db.schema.table` for non-empty nested names
192
211
///
193
- /// Note:
212
+ /// # Note:
194
213
/// Currently compound identifier for outer query schema is not supported.
195
214
///
196
- /// Returns planned expression
215
+ /// Returns original expression if not possible
197
216
fn plan_compound_identifier (
198
217
& self ,
199
218
_field : & Field ,
@@ -205,7 +224,7 @@ pub trait ExprPlanner: Debug + Send + Sync {
205
224
)
206
225
}
207
226
208
- /// Plans `ANY` expression, e.g., `expr = ANY(array_expr)`
227
+ /// Plans `ANY` expression, such as `expr = ANY(array_expr)`
209
228
///
210
229
/// Returns origin binary expression if not possible
211
230
fn plan_any ( & self , expr : RawBinaryExpr ) -> Result < PlannerResult < RawBinaryExpr > > {
@@ -256,9 +275,9 @@ pub enum PlannerResult<T> {
256
275
Original ( T ) ,
257
276
}
258
277
259
- /// This trait allows users to customize the behavior of the data type planning
278
+ /// Customize planning SQL types to DataFusion (Arrow) types.
260
279
pub trait TypePlanner : Debug + Send + Sync {
261
- /// Plan SQL type to DataFusion data type
280
+ /// Plan SQL [`ast::DataType`] to DataFusion [`DataType`]
262
281
///
263
282
/// Returns None if not possible
264
283
fn plan_type ( & self , _sql_type : & ast:: DataType ) -> Result < Option < DataType > > {
0 commit comments