@@ -210,26 +210,44 @@ def execute_query(self, query_string):
210210
211211 if cmd_type == 'SELECT' :
212212 limit = parsed .get ('limit' )
213- # SELECT always reads from current state
213+ condition = parsed .get ('condition' )
214+ columns = parsed .get ('columns' , '*' )
215+
216+ # Handle nested subquery in WHERE clause
217+ if condition and isinstance (condition ['value' ], str ) and condition ['value' ].startswith ('(' ) and 'SELECT' in condition ['value' ].upper ():
218+ sub_sql = condition ['value' ][1 :- 1 ].strip ()
219+ sub_res = self .execute_query (sub_sql )
220+
221+ if isinstance (sub_res , list ):
222+ # Flatten subquery result to a simple list of values
223+ if sub_res and isinstance (sub_res [0 ], dict ):
224+ flattened = []
225+ for row in sub_res :
226+ flattened .extend (row .values ())
227+ condition ['value' ] = flattened
228+ else :
229+ condition ['value' ] = sub_res
230+
231+ # Execution Logic
214232 if self .transaction .in_transaction and table_name in self .transaction .staging_area :
215233 # Read from staging area if modified in transaction
216234 staged_data = self .transaction .staging_area [table_name ]['data' ]
217- condition = parsed .get ('condition' )
218235 res = staged_data
219236 if condition :
220- # Apply WHERE filter to staged data
221237 res = [row for row in staged_data
222238 if table ._matches_condition (row , condition ['column' ],
223239 condition ['operator' ], condition ['value' ])]
224240 if limit :
225- return res [:limit ]
226- return res
241+ res = res [:limit ]
227242 else :
228243 # Read from disk
229- condition = parsed .get ('condition' )
230244 if condition :
231- return table .select_where (condition ['column' ], condition ['operator' ], condition ['value' ], limit = limit )
232- return table .select_all (limit = limit )
245+ res = table .select_where (condition ['column' ], condition ['operator' ], condition ['value' ], limit = limit )
246+ else :
247+ res = table .select_all (limit = limit )
248+
249+ # Apply column projection
250+ return table .project_columns (res , columns )
233251
234252 if cmd_type == 'DELETE' :
235253 condition = parsed ['condition' ]
0 commit comments