@@ -697,14 +697,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
697697 }
698698 }
699699
700- /// Generate a logic plan from an SQL select
701- fn select_to_plan (
700+ fn plan_selection (
702701 & self ,
703702 select : & Select ,
704- ctes : & mut HashMap < String , LogicalPlan > ,
705- alias : Option < String > ,
703+ plans : Vec < LogicalPlan > ,
706704 ) -> Result < LogicalPlan > {
707- let plans = self . plan_from_tables ( & select. from , ctes) ?;
708705 let plan = match & select. selection {
709706 Some ( predicate_expr) => {
710707 // build join schema
@@ -822,9 +819,23 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
822819 }
823820 }
824821 } ;
825- let plan = plan?;
822+ plan
823+ }
826824
827- // The SELECT expressions, with wildcards expanded.
825+ /// Generate a logic plan from an SQL select
826+ fn select_to_plan (
827+ & self ,
828+ select : & Select ,
829+ ctes : & mut HashMap < String , LogicalPlan > ,
830+ alias : Option < String > ,
831+ ) -> Result < LogicalPlan > {
832+ // process `from` clause
833+ let plans = self . plan_from_tables ( & select. from , ctes) ?;
834+
835+ // process `where` clause
836+ let plan = self . plan_selection ( select, plans) ?;
837+
838+ // process the SELECT expressions, with wildcards expanded.
828839 let select_exprs = self . prepare_select_exprs ( & plan, select) ?;
829840
830841 // having and group by clause may reference aliases defined in select projection
@@ -873,6 +884,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
873884 // All of the aggregate expressions (deduplicated).
874885 let aggr_exprs = find_aggregate_exprs ( & aggr_expr_haystack) ;
875886
887+ // All of the group by expressions
876888 let group_by_exprs = select
877889 . group_by
878890 . iter ( )
@@ -891,6 +903,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
891903 } )
892904 . collect :: < Result < Vec < Expr > > > ( ) ?;
893905
906+ // process group by, aggregation or having
894907 let ( plan, select_exprs_post_aggr, having_expr_post_aggr_opt) = if !group_by_exprs
895908 . is_empty ( )
896909 || !aggr_exprs. is_empty ( )
@@ -931,7 +944,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
931944 plan
932945 } ;
933946
934- // window function
947+ // process window function
935948 let window_func_exprs = find_window_exprs ( & select_exprs_post_aggr) ;
936949
937950 let plan = if window_func_exprs. is_empty ( ) {
@@ -940,13 +953,16 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
940953 LogicalPlanBuilder :: window_plan ( plan, window_func_exprs) ?
941954 } ;
942955
956+ // process distinct clause
943957 let plan = if select. distinct {
944958 return LogicalPlanBuilder :: from ( plan)
945959 . aggregate ( select_exprs_post_aggr, iter:: empty :: < Expr > ( ) ) ?
946960 . build ( ) ;
947961 } else {
948962 plan
949963 } ;
964+
965+ // generate the final projection plan
950966 project_with_alias ( plan, select_exprs_post_aggr, alias)
951967 }
952968
0 commit comments