@@ -150,23 +150,26 @@ impl<'a> Binder {
150
150
let tokens = tokenize_sql ( query. as_str ( ) ) ?;
151
151
let backtrace = Backtrace :: new ( ) ;
152
152
let ( stmt, _) = parse_sql ( & tokens, Dialect :: PostgreSQL , & backtrace) ?;
153
-
153
+ // For view, we need use a new context to bind it.
154
+ let new_bind_context =
155
+ BindContext :: with_parent ( Box :: new ( bind_context. clone ( ) ) ) ;
154
156
if let Statement :: Query ( query) = & stmt {
155
- let ( s_expr, mut bind_context ) =
156
- self . bind_query ( bind_context , query) . await ?;
157
+ let ( s_expr, mut new_bind_context ) =
158
+ self . bind_query ( & new_bind_context , query) . await ?;
157
159
if let Some ( alias) = alias {
158
160
// view maybe has alias, e.g. select v1.col1 from v as v1;
159
- bind_context. apply_table_alias ( alias, & self . name_resolution_ctx ) ?;
161
+ new_bind_context
162
+ . apply_table_alias ( alias, & self . name_resolution_ctx ) ?;
160
163
} else {
161
164
// e.g. select v0.c0 from v0;
162
- for column in bind_context . columns . iter_mut ( ) {
165
+ for column in new_bind_context . columns . iter_mut ( ) {
163
166
column. database_name = None ;
164
167
column. table_name = Some (
165
168
normalize_identifier ( table, & self . name_resolution_ctx ) . name ,
166
169
) ;
167
170
}
168
171
}
169
- Ok ( ( s_expr, bind_context ) )
172
+ Ok ( ( s_expr, new_bind_context ) )
170
173
} else {
171
174
Err ( ErrorCode :: Internal ( format ! (
172
175
"Invalid VIEW object: {}" ,
0 commit comments