11use itertools:: Itertools ;
22
33use super :: { Expr , Filter , From , OrderBy , Subquery } ;
4+ use crate :: planner:: sql_templates:: PlanSqlTemplates ;
45use crate :: planner:: VisitorContext ;
56use cubenativeutils:: CubeError ;
67use std:: rc:: Rc ;
78
89pub struct Select {
9- pub projection : Vec < Expr > ,
10- pub from : From ,
11- pub filter : Option < Filter > ,
12- pub group_by : Vec < Expr > ,
13- pub having : Option < Filter > ,
14- pub order_by : Vec < OrderBy > ,
15- pub context : Rc < VisitorContext > ,
16- pub ctes : Vec < Rc < Subquery > > ,
17- pub is_distinct : bool ,
18- pub limit : Option < usize > ,
19- pub offset : Option < usize > ,
10+ pub ( super ) projection : Vec < Expr > ,
11+ pub ( super ) from : From ,
12+ pub ( super ) filter : Option < Filter > ,
13+ pub ( super ) group_by : Vec < Expr > ,
14+ pub ( super ) having : Option < Filter > ,
15+ pub ( super ) order_by : Vec < OrderBy > ,
16+ pub ( super ) context : Rc < VisitorContext > ,
17+ pub ( super ) ctes : Vec < Rc < Subquery > > ,
18+ pub ( super ) is_distinct : bool ,
19+ pub ( super ) limit : Option < usize > ,
20+ pub ( super ) offset : Option < usize > ,
2021}
2122
2223impl Select {
23- pub fn to_sql ( & self ) -> Result < String , CubeError > {
24+ pub fn to_sql ( & self , templates : & PlanSqlTemplates ) -> Result < String , CubeError > {
2425 let projection = self
2526 . projection
2627 . iter ( )
27- . map ( |p| p. to_sql ( self . context . clone ( ) ) )
28+ . map ( |p| p. to_sql ( templates , self . context . clone ( ) ) )
2829 . collect :: < Result < Vec < _ > , _ > > ( ) ?
2930 . join ( ", " ) ;
3031 let where_condition = if let Some ( filter) = & self . filter {
@@ -56,7 +57,11 @@ impl Select {
5657 . ctes
5758 . iter ( )
5859 . map ( |cte| -> Result < _ , CubeError > {
59- Ok ( format ! ( " {} as ({})" , cte. alias( ) , cte. query( ) . to_sql( ) ?) )
60+ Ok ( format ! (
61+ " {} as ({})" ,
62+ cte. alias( ) ,
63+ cte. query( ) . to_sql( templates) ?
64+ ) )
6065 } )
6166 . collect :: < Result < Vec < _ > , _ > > ( ) ?
6267 . join ( ",\n " ) ;
@@ -78,7 +83,7 @@ impl Select {
7883 } ;
7984
8085 let distinct = if self . is_distinct { "DISTINCT " } else { "" } ;
81- let from = self . from . to_sql ( self . context . clone ( ) ) ?;
86+ let from = self . from . to_sql ( templates , self . context . clone ( ) ) ?;
8287 let limit = if let Some ( limit) = self . limit {
8388 format ! ( " LIMIT {limit}" )
8489 } else {
0 commit comments