@@ -124,6 +124,7 @@ pub async fn get_sql_query_param(
124124 single_table_name : & Option < & str > ,
125125 table_with_joins : & Option < Vec < TableWithJoins > > ,
126126 db_conn : & DBConn ,
127+ cte_columns : & std:: collections:: HashMap < String , std:: collections:: HashMap < String , TsFieldType > > ,
127128) -> Result < Option < ( TsFieldType , bool , Option < String > ) > , TsGeneratorError > {
128129 let table_name: Option < String > ;
129130
@@ -145,6 +146,15 @@ pub async fn get_sql_query_param(
145146
146147 match ( column_name, expr_placeholder, table_name) {
147148 ( Some ( column_name) , Some ( expr_placeholder) , Some ( table_name) ) => {
149+ // First check if the table is a CTE or table-valued function
150+ if let Some ( cte_table_columns) = cte_columns. get ( table_name. as_str ( ) ) {
151+ if let Some ( ts_type) = cte_table_columns. get ( column_name. as_str ( ) ) {
152+ return Ok ( Some ( ( ts_type. clone ( ) , false , Some ( expr_placeholder) ) ) ) ;
153+ }
154+ // Column not found in CTE columns — return None to allow fallback handling
155+ return Ok ( None ) ;
156+ }
157+
148158 let table_names = vec ! [ table_name. as_str( ) ] ;
149159 let columns = DB_SCHEMA
150160 . lock ( )
@@ -312,7 +322,15 @@ pub async fn translate_expr(
312322 // OPERATORS START //
313323 /////////////////////
314324 Expr :: BinaryOp { left, op : _, right } => {
315- let param = get_sql_query_param ( left, right, single_table_name, table_with_joins, db_conn) . await ?;
325+ let param = get_sql_query_param (
326+ left,
327+ right,
328+ single_table_name,
329+ table_with_joins,
330+ db_conn,
331+ & ts_query. table_valued_function_columns ,
332+ )
333+ . await ?;
316334 if let Some ( ( value, is_nullable, index) ) = param {
317335 let _ = ts_query. insert_param ( & value, & is_nullable, & index) ;
318336 Ok ( ( ) )
@@ -354,6 +372,7 @@ pub async fn translate_expr(
354372 single_table_name,
355373 table_with_joins,
356374 db_conn,
375+ & ts_query. table_valued_function_columns ,
357376 )
358377 . await ?;
359378
@@ -383,8 +402,24 @@ pub async fn translate_expr(
383402 low,
384403 high,
385404 } => {
386- let low = get_sql_query_param ( expr, low, single_table_name, table_with_joins, db_conn) . await ?;
387- let high = get_sql_query_param ( expr, high, single_table_name, table_with_joins, db_conn) . await ?;
405+ let low = get_sql_query_param (
406+ expr,
407+ low,
408+ single_table_name,
409+ table_with_joins,
410+ db_conn,
411+ & ts_query. table_valued_function_columns ,
412+ )
413+ . await ?;
414+ let high = get_sql_query_param (
415+ expr,
416+ high,
417+ single_table_name,
418+ table_with_joins,
419+ db_conn,
420+ & ts_query. table_valued_function_columns ,
421+ )
422+ . await ?;
388423 if let Some ( ( value, is_nullable, placeholder) ) = low {
389424 ts_query. insert_param ( & value, & is_nullable, & placeholder) ?;
390425 }
0 commit comments