@@ -17,7 +17,6 @@ use std::sync::Arc;
17
17
use common_ast:: ast:: Expr ;
18
18
use common_exception:: ErrorCode ;
19
19
use common_exception:: Result ;
20
- use common_exception:: Span ;
21
20
22
21
use super :: Finder ;
23
22
use crate :: binder:: split_conjunctions;
@@ -28,14 +27,11 @@ use crate::binder::ScalarBinder;
28
27
use crate :: binder:: Visibility ;
29
28
use crate :: optimizer:: SExpr ;
30
29
use crate :: planner:: semantic:: GroupingChecker ;
30
+ use crate :: plans:: walk_expr_mut;
31
31
use crate :: plans:: BoundColumnRef ;
32
- use crate :: plans:: CastExpr ;
33
32
use crate :: plans:: Filter ;
34
- use crate :: plans:: FunctionCall ;
35
- use crate :: plans:: LambdaFunc ;
36
33
use crate :: plans:: ScalarExpr ;
37
- use crate :: plans:: UDFLambdaCall ;
38
- use crate :: plans:: UDFServerCall ;
34
+ use crate :: plans:: SubqueryExpr ;
39
35
use crate :: plans:: Visitor ;
40
36
use crate :: plans:: VisitorMut ;
41
37
use crate :: BindContext ;
@@ -51,7 +47,6 @@ impl Binder {
51
47
aliases : & [ ( String , ScalarExpr ) ] ,
52
48
qualify : & Expr ,
53
49
) -> Result < ScalarExpr > {
54
- dbg ! ( "analyze window qualify" , qualify) ;
55
50
bind_context. set_expr_context ( ExprContext :: QualifyClause ) ;
56
51
let mut scalar_binder = ScalarBinder :: new (
57
52
bind_context,
@@ -73,10 +68,8 @@ impl Binder {
73
68
& mut self ,
74
69
bind_context : & mut BindContext ,
75
70
qualify : ScalarExpr ,
76
- span : Span ,
77
71
child : SExpr ,
78
72
) -> Result < SExpr > {
79
- dbg ! ( "bind qualify" , & qualify) ;
80
73
bind_context. set_expr_context ( ExprContext :: QualifyClause ) ;
81
74
82
75
let f = |scalar : & ScalarExpr | matches ! ( scalar, ScalarExpr :: AggregateFunction ( _) ) ;
@@ -89,17 +82,19 @@ impl Binder {
89
82
. set_span ( qualify. span ( ) ) ) ;
90
83
}
91
84
92
- let scalar = if bind_context. in_grouping {
93
- // If we are in grouping context, we will perform the grouping check
94
- let grouping_checker = GroupingChecker :: new ( bind_context) ;
95
- grouping_checker. resolve ( & qualify, span) ?
96
- } else {
97
- let qualify_checker = QualifyChecker :: new ( bind_context) ;
98
- qualify_checker. resolve ( & qualify) ?
85
+ let scalar = {
86
+ let mut qualify = qualify;
87
+ if bind_context. in_grouping {
88
+ // If we are in grouping context, we will perform the grouping check
89
+ let mut grouping_checker = GroupingChecker :: new ( bind_context) ;
90
+ grouping_checker. visit ( & mut qualify) ?;
91
+ } else {
92
+ let mut qualify_checker = QualifyChecker :: new ( bind_context) ;
93
+ qualify_checker. visit ( & mut qualify) ?;
94
+ }
95
+ qualify
99
96
} ;
100
97
101
- dbg ! ( & scalar) ;
102
-
103
98
let predicates = split_conjunctions ( & scalar) ;
104
99
105
100
let filter = Filter { predicates } ;
@@ -119,131 +114,71 @@ impl<'a> QualifyChecker<'a> {
119
114
pub fn new ( bind_context : & ' a BindContext ) -> Self {
120
115
Self { bind_context }
121
116
}
117
+ }
122
118
123
- pub fn resolve ( & self , scalar : & ScalarExpr ) -> Result < ScalarExpr > {
124
- match scalar {
125
- ScalarExpr :: BoundColumnRef ( _)
126
- | ScalarExpr :: ConstantExpr ( _)
127
- | ScalarExpr :: SubqueryExpr ( _) => Ok ( scalar. clone ( ) ) ,
128
- ScalarExpr :: FunctionCall ( func) => {
129
- let args = func
130
- . arguments
131
- . iter ( )
132
- . map ( |arg| self . resolve ( arg) )
133
- . collect :: < Result < Vec < ScalarExpr > > > ( ) ?;
134
- Ok ( FunctionCall {
135
- span : func. span ,
136
- params : func. params . clone ( ) ,
137
- arguments : args,
138
- func_name : func. func_name . clone ( ) ,
139
- }
140
- . into ( ) )
141
- }
142
-
143
- ScalarExpr :: LambdaFunction ( lambda_func) => {
144
- let args = lambda_func
145
- . args
146
- . iter ( )
147
- . map ( |arg| self . resolve ( arg) )
148
- . collect :: < Result < Vec < ScalarExpr > > > ( ) ?;
149
- Ok ( LambdaFunc {
150
- span : lambda_func. span ,
151
- func_name : lambda_func. func_name . clone ( ) ,
152
- args,
153
- lambda_expr : lambda_func. lambda_expr . clone ( ) ,
154
- lambda_display : lambda_func. lambda_display . clone ( ) ,
155
- return_type : lambda_func. return_type . clone ( ) ,
119
+ impl < ' a > VisitorMut < ' _ > for QualifyChecker < ' a > {
120
+ fn visit ( & mut self , expr : & mut ScalarExpr ) -> Result < ( ) > {
121
+ if let ScalarExpr :: WindowFunction ( window) = expr {
122
+ if let Some ( column) = self
123
+ . bind_context
124
+ . windows
125
+ . window_functions_map
126
+ . get ( & window. display_name )
127
+ {
128
+ // The exprs in `win` has already been rewrittern to `BoundColumnRef` in `WindowRewriter`.
129
+ // So we need to check the exprs in `bind_context.windows`
130
+ let window_info = & self . bind_context . windows . window_functions [ * column] ;
131
+
132
+ let column_binding = ColumnBindingBuilder :: new (
133
+ window. display_name . clone ( ) ,
134
+ window_info. index ,
135
+ Box :: new ( window_info. func . return_type ( ) ) ,
136
+ Visibility :: Visible ,
137
+ )
138
+ . build ( ) ;
139
+ * expr = BoundColumnRef {
140
+ span : None ,
141
+ column : column_binding,
156
142
}
157
- . into ( ) )
143
+ . into ( ) ;
144
+ return Ok ( ( ) ) ;
158
145
}
146
+ return Err ( ErrorCode :: Internal (
147
+ "Qualify check: Invalid window function" ,
148
+ ) ) ;
149
+ }
159
150
160
- ScalarExpr :: CastExpr ( cast) => Ok ( CastExpr {
161
- span : cast. span ,
162
- is_try : cast. is_try ,
163
- argument : Box :: new ( self . resolve ( & cast. argument ) ?) ,
164
- target_type : cast. target_type . clone ( ) ,
165
- }
166
- . into ( ) ) ,
167
-
168
- ScalarExpr :: WindowFunction ( win) => {
169
- if let Some ( column) = self
170
- . bind_context
171
- . windows
172
- . window_functions_map
173
- . get ( & win. display_name )
174
- {
175
- // The exprs in `win` has already been rewrittern to `BoundColumnRef` in `WindowRewriter`.
176
- // So we need to check the exprs in `bind_context.windows`
177
- let window_info = & self . bind_context . windows . window_functions [ * column] ;
178
-
179
- let column_binding = ColumnBindingBuilder :: new (
180
- win. display_name . clone ( ) ,
181
- window_info. index ,
182
- Box :: new ( window_info. func . return_type ( ) ) ,
183
- Visibility :: Visible ,
184
- )
185
- . build ( ) ;
186
- return Ok ( BoundColumnRef {
187
- span : None ,
188
- column : column_binding,
189
- }
190
- . into ( ) ) ;
151
+ if let ScalarExpr :: AggregateFunction ( agg) = expr {
152
+ if let Some ( column) = self
153
+ . bind_context
154
+ . aggregate_info
155
+ . aggregate_functions_map
156
+ . get ( & agg. display_name )
157
+ {
158
+ let agg_func = & self . bind_context . aggregate_info . aggregate_functions [ * column] ;
159
+ let column_binding = ColumnBindingBuilder :: new (
160
+ agg. display_name . clone ( ) ,
161
+ agg_func. index ,
162
+ Box :: new ( agg_func. scalar . data_type ( ) ?) ,
163
+ Visibility :: Visible ,
164
+ )
165
+ . build ( ) ;
166
+ * expr = BoundColumnRef {
167
+ span : None ,
168
+ column : column_binding,
191
169
}
192
- Err ( ErrorCode :: Internal (
193
- "Qualify check: Invalid window function" ,
194
- ) )
170
+ . into ( ) ;
171
+ return Ok ( ( ) ) ;
195
172
}
196
173
197
- ScalarExpr :: AggregateFunction ( agg) => {
198
- if let Some ( column) = self
199
- . bind_context
200
- . aggregate_info
201
- . aggregate_functions_map
202
- . get ( & agg. display_name )
203
- {
204
- let agg_func = & self . bind_context . aggregate_info . aggregate_functions [ * column] ;
205
- let column_binding = ColumnBindingBuilder :: new (
206
- agg. display_name . clone ( ) ,
207
- agg_func. index ,
208
- Box :: new ( agg_func. scalar . data_type ( ) ?) ,
209
- Visibility :: Visible ,
210
- )
211
- . build ( ) ;
212
- return Ok ( BoundColumnRef {
213
- span : None ,
214
- column : column_binding,
215
- }
216
- . into ( ) ) ;
217
- }
218
- Err ( ErrorCode :: Internal ( "Invalid aggregate function" ) )
219
- }
220
- ScalarExpr :: UDFServerCall ( udf) => {
221
- let args = udf
222
- . arguments
223
- . iter ( )
224
- . map ( |arg| self . resolve ( arg) )
225
- . collect :: < Result < Vec < ScalarExpr > > > ( ) ?;
226
- Ok ( UDFServerCall {
227
- span : udf. span ,
228
- func_name : udf. func_name . clone ( ) ,
229
- display_name : udf. display_name . clone ( ) ,
230
- server_addr : udf. server_addr . clone ( ) ,
231
- arg_types : udf. arg_types . clone ( ) ,
232
- return_type : udf. return_type . clone ( ) ,
233
- arguments : args,
234
- }
235
- . into ( ) )
236
- }
237
- ScalarExpr :: UDFLambdaCall ( udf) => {
238
- let scalar = & udf. scalar ;
239
- let new_scalar = self . resolve ( scalar) ?;
240
- Ok ( UDFLambdaCall {
241
- span : udf. span ,
242
- func_name : udf. func_name . clone ( ) ,
243
- scalar : Box :: new ( new_scalar) ,
244
- }
245
- . into ( ) )
246
- }
174
+ return Err ( ErrorCode :: Internal ( "Invalid aggregate function" ) ) ;
247
175
}
176
+
177
+ walk_expr_mut ( self , expr)
178
+ }
179
+
180
+ fn visit_subquery_expr ( & mut self , _: & mut SubqueryExpr ) -> Result < ( ) > {
181
+ // TODO(leiysky): check subquery in the future
182
+ Ok ( ( ) )
248
183
}
249
184
}
0 commit comments