@@ -143,22 +143,22 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
143143 } )
144144 }
145145 Expr :: Exists { subquery, negated } => {
146- let ( sub_query, column) = self . bind_subquery ( subquery) ?;
146+ let ( sub_query, column) = self . bind_subquery ( None , subquery) ?;
147147 let ( _, sub_query) = if !self . context . is_step ( & QueryBindStep :: Where ) {
148148 self . bind_temp_table ( column, sub_query) ?
149149 } else {
150- ( ScalarExpression :: ColumnRef ( column) , sub_query)
150+ ( column, sub_query)
151151 } ;
152152 self . context
153153 . sub_query ( SubQueryType :: ExistsSubQuery ( * negated, sub_query) ) ;
154154 Ok ( ScalarExpression :: Constant ( DataValue :: Boolean ( true ) ) )
155155 }
156156 Expr :: Subquery ( subquery) => {
157- let ( sub_query, column) = self . bind_subquery ( subquery) ?;
157+ let ( sub_query, column) = self . bind_subquery ( None , subquery) ?;
158158 let ( expr, sub_query) = if !self . context . is_step ( & QueryBindStep :: Where ) {
159159 self . bind_temp_table ( column, sub_query) ?
160160 } else {
161- ( ScalarExpression :: ColumnRef ( column) , sub_query)
161+ ( column, sub_query)
162162 } ;
163163 self . context . sub_query ( SubQueryType :: SubQuery ( sub_query) ) ;
164164 Ok ( expr)
@@ -169,7 +169,8 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
169169 negated,
170170 } => {
171171 let left_expr = Box :: new ( self . bind_expr ( expr) ?) ;
172- let ( sub_query, column) = self . bind_subquery ( subquery) ?;
172+ let ( sub_query, column) =
173+ self . bind_subquery ( Some ( left_expr. return_type ( ) ) , subquery) ?;
173174
174175 if !self . context . is_step ( & QueryBindStep :: Where ) {
175176 return Err ( DatabaseError :: UnsupportedStmt (
@@ -250,14 +251,14 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
250251
251252 fn bind_temp_table (
252253 & mut self ,
253- column : ColumnRef ,
254+ expr : ScalarExpression ,
254255 sub_query : LogicalPlan ,
255256 ) -> Result < ( ScalarExpression , LogicalPlan ) , DatabaseError > {
256- let mut alias_column = ColumnCatalog :: clone ( & column ) ;
257+ let mut alias_column = ColumnCatalog :: clone ( & expr . output_column ( ) ) ;
257258 alias_column. set_ref_table ( self . context . temp_table ( ) , ColumnId :: new ( ) , true ) ;
258259
259260 let alias_expr = ScalarExpression :: Alias {
260- expr : Box :: new ( ScalarExpression :: ColumnRef ( column ) ) ,
261+ expr : Box :: new ( expr ) ,
261262 alias : AliasType :: Expr ( Box :: new ( ScalarExpression :: ColumnRef ( ColumnRef :: from (
262263 alias_column,
263264 ) ) ) ) ,
@@ -268,8 +269,9 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
268269
269270 fn bind_subquery (
270271 & mut self ,
272+ in_ty : Option < LogicalType > ,
271273 subquery : & Query ,
272- ) -> Result < ( LogicalPlan , ColumnRef ) , DatabaseError > {
274+ ) -> Result < ( LogicalPlan , ScalarExpression ) , DatabaseError > {
273275 let BinderContext {
274276 table_cache,
275277 view_cache,
@@ -294,14 +296,30 @@ impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T
294296 let mut sub_query = binder. bind_query ( subquery) ?;
295297 let sub_query_schema = sub_query. output_schema ( ) ;
296298
297- if sub_query_schema. len ( ) != 1 {
298- return Err ( DatabaseError :: MisMatch (
299- "expects only one expression to be returned" ,
300- "the expression returned by the subquery" ,
301- ) ) ;
302- }
303- let column = sub_query_schema[ 0 ] . clone ( ) ;
304- Ok ( ( sub_query, column) )
299+ let fn_check = |len : usize | {
300+ if sub_query_schema. len ( ) != len {
301+ return Err ( DatabaseError :: MisMatch (
302+ "expects only one expression to be returned" ,
303+ "the expression returned by the subquery" ,
304+ ) ) ;
305+ }
306+ Ok ( ( ) )
307+ } ;
308+
309+ let expr = if let Some ( LogicalType :: Tuple ( tys) ) = in_ty {
310+ fn_check ( tys. len ( ) ) ?;
311+
312+ let columns = sub_query_schema
313+ . iter ( )
314+ . map ( |column| ScalarExpression :: ColumnRef ( column. clone ( ) ) )
315+ . collect :: < Vec < _ > > ( ) ;
316+ ScalarExpression :: Tuple ( columns)
317+ } else {
318+ fn_check ( 1 ) ?;
319+
320+ ScalarExpression :: ColumnRef ( sub_query_schema[ 0 ] . clone ( ) )
321+ } ;
322+ Ok ( ( sub_query, expr) )
305323 }
306324
307325 pub fn bind_like (
0 commit comments