@@ -4,6 +4,8 @@ use super::{
44 security_context:: { NativeSecurityContext , SecurityContext } ,
55 sql_utils:: { NativeSqlUtils , SqlUtils } ,
66} ;
7+ use crate :: planner:: sql_evaluator:: { FilterGroupItem , FilterParamsItem , SecutityContextProps } ;
8+ use crate :: utils:: UniqueVector ;
79use cubenativeutils:: wrappers:: make_proxy;
810use cubenativeutils:: wrappers:: object:: { NativeFunction , NativeStruct , NativeType } ;
911use cubenativeutils:: wrappers:: serializer:: {
@@ -18,53 +20,61 @@ use std::collections::hash_map::HashMap;
1820use std:: rc:: Rc ;
1921use std:: { any:: Any , cell:: RefCell , rc:: Weak } ;
2022
21- #[ derive( Clone , Debug ) ]
22- pub struct TemplatedSql {
23- pub args : HashMap < Vec < String > , usize > ,
24- pub template : String ,
23+ // Extension trait для дедупликации элементов в Vec
24+ trait VecDedup < T > {
25+ fn insert_or_get_index ( & mut self , item : T ) -> usize ;
2526}
2627
27- #[ derive( Clone , Debug , Serialize , Deserialize ) ]
28- struct FilterParamsItem {
29- pub cube_name : String ,
30- pub name : String ,
31- pub column : String ,
28+ impl < T : PartialEq > VecDedup < T > for Vec < T > {
29+ fn insert_or_get_index ( & mut self , item : T ) -> usize {
30+ if let Some ( ( index, _) ) = self . iter ( ) . enumerate ( ) . find ( |( _, itm) | * itm == & item) {
31+ index
32+ } else {
33+ let index = self . len ( ) ;
34+ self . push ( item) ;
35+ index
36+ }
37+ }
3238}
3339
3440#[ derive( Default , Clone , Debug ) ]
35- struct FilterGroupItem {
41+ pub struct SqlTemplateArgs {
42+ pub symbol_paths : Vec < Vec < String > > ,
3643 pub filter_params : Vec < FilterParamsItem > ,
44+ pub filter_groups : Vec < FilterGroupItem > ,
45+ pub security_context : SecutityContextProps ,
3746}
3847
39- # [ derive ( Default , Clone , Debug ) ]
40- struct SecutityContextProps {
41- pub values : Vec < String > ,
42- }
48+ impl SqlTemplateArgs {
49+ pub fn insert_symbol_path ( & mut self , path : Vec < String > ) -> usize {
50+ self . symbol_paths . unique_insert ( path )
51+ }
4352
44- #[ derive( Default , Clone , Debug ) ]
45- struct ProxyStateInner {
46- pub args : HashMap < Vec < String > , usize > ,
47- pub filter_params : Vec < ( FilterParamsItem , usize ) > ,
48- pub filter_groups : Vec < ( FilterGroupItem , usize ) > ,
49- pub security_context : SecutityContextProps ,
53+ pub fn insert_filter_params ( & mut self , params : FilterParamsItem ) -> usize {
54+ self . filter_params . unique_insert ( params)
55+ }
56+
57+ pub fn insert_filter_group ( & mut self , group : FilterGroupItem ) -> usize {
58+ self . filter_groups . unique_insert ( group)
59+ }
60+
61+ pub fn insert_security_context_value ( & mut self , value : String ) -> usize {
62+ self . security_context . values . unique_insert ( value)
63+ }
5064}
5165
5266struct ProxyState {
53- state : RefCell < ProxyStateInner > ,
67+ state : RefCell < SqlTemplateArgs > ,
5468}
5569
5670impl ProxyState {
5771 fn new ( ) -> Rc < Self > {
5872 Rc :: new ( Self {
59- state : RefCell :: new ( ProxyStateInner :: default ( ) ) ,
73+ state : RefCell :: new ( SqlTemplateArgs :: default ( ) ) ,
6074 } )
6175 }
6276
63- fn get_args ( self : & Rc < Self > ) -> Result < HashMap < Vec < String > , usize > , CubeError > {
64- self . with_state ( |state| state. args . clone ( ) )
65- }
66-
67- fn get_state ( self : & Rc < Self > ) -> Result < ProxyStateInner , CubeError > {
77+ fn get_args ( self : & Rc < Self > ) -> Result < SqlTemplateArgs , CubeError > {
6878 self . with_state ( |state| state. clone ( ) )
6979 }
7080
@@ -76,7 +86,7 @@ impl ProxyState {
7686
7787 fn with_state < T , F > ( & self , f : F ) -> Result < T , CubeError >
7888 where
79- F : FnOnce ( & ProxyStateInner ) -> T ,
89+ F : FnOnce ( & SqlTemplateArgs ) -> T ,
8090 {
8191 let state = self
8292 . state
@@ -86,7 +96,7 @@ impl ProxyState {
8696 }
8797 fn with_state_mut < T , F > ( & self , f : F ) -> Result < T , CubeError >
8898 where
89- F : FnOnce ( & mut ProxyStateInner ) -> T ,
99+ F : FnOnce ( & mut SqlTemplateArgs ) -> T ,
90100 {
91101 let mut state = self
92102 . state
@@ -102,36 +112,18 @@ struct ProxyStateWeak {
102112}
103113
104114impl ProxyStateWeak {
105- fn with_state < T , F > ( & self , f : F ) -> Result < T , CubeError >
106- where
107- F : FnOnce ( & ProxyStateInner ) -> T ,
108- {
109- let state = self . state . upgrade ( ) . ok_or ( CubeError :: internal ( format ! (
110- "Cant upgrade dependency parsing state"
111- ) ) ) ?;
112- state. with_state ( f)
113- }
114-
115115 fn with_state_mut < T , F > ( & self , f : F ) -> Result < T , CubeError >
116116 where
117- F : FnOnce ( & mut ProxyStateInner ) -> T ,
117+ F : FnOnce ( & mut SqlTemplateArgs ) -> T ,
118118 {
119119 let state = self . state . upgrade ( ) . ok_or ( CubeError :: internal ( format ! (
120120 "Cant upgrade dependency parsing state"
121121 ) ) ) ?;
122122 state. with_state_mut ( f)
123123 }
124124
125- fn add_arg ( & self , path : & Vec < String > ) -> Result < usize , CubeError > {
126- self . with_state_mut ( |state| {
127- if let Some ( ind) = state. args . get ( path) {
128- ind. clone ( )
129- } else {
130- let ind = state. args . len ( ) ;
131- state. args . insert ( path. clone ( ) , ind) ;
132- ind
133- }
134- } )
125+ fn insert_symbol_path ( & self , path : & Vec < String > ) -> Result < usize , CubeError > {
126+ self . with_state_mut ( |state| state. insert_symbol_path ( path. clone ( ) ) )
135127 }
136128}
137129
@@ -160,10 +152,10 @@ pub trait MemberSql {
160152 fn args_names ( & self ) -> & Vec < String > ;
161153 fn need_deps_resolve ( & self ) -> bool ;
162154 fn as_any ( self : Rc < Self > ) -> Rc < dyn Any > ;
163- fn into_template_sql (
155+ fn compile_template_sql (
164156 & self ,
165157 security_context : Rc < dyn SecurityContext > ,
166- ) -> Result < TemplatedSql , CubeError > ;
158+ ) -> Result < ( String , SqlTemplateArgs ) , CubeError > ;
167159}
168160
169161pub struct NativeMemberSql < IT : InnerTypes > {
@@ -188,7 +180,7 @@ impl<IT: InnerTypes> NativeSerialize<IT> for MemberSqlStruct {
188180 }
189181 if let Some ( sql_fn) = & self . sql_fn {
190182 res. set_field (
191- "sql " ,
183+ "__sql_fn " ,
192184 NativeObjectHandle :: new ( context. to_string_fn ( sql_fn. clone ( ) ) ?. into_object ( ) ) ,
193185 ) ?;
194186 }
@@ -253,15 +245,15 @@ impl<IT: InnerTypes> NativeMemberSql<IT> {
253245 if prop == "sql" {
254246 let mut path_with_sql = path. clone ( ) ;
255247 path_with_sql. push ( "sql" . to_string ( ) ) ;
256- let index = proxy_state. add_arg ( & path_with_sql) ?;
257- let str = format ! ( "{{prop :{}}}" , index) ;
248+ let index = proxy_state. insert_symbol_path ( & path_with_sql) ?;
249+ let str = format ! ( "{{arg :{}}}" , index) ;
258250 let result = inner_context. to_string_fn ( str) ?;
259251 let result = NativeObjectHandle :: new ( result. into_object ( ) ) ;
260252 return Ok ( Some ( result) ) ;
261253 }
262254 if prop == "toString" || prop == "valueOf" {
263- let index = proxy_state. add_arg ( & path) ?;
264- let str = format ! ( "{{prop :{}}}" , index) ;
255+ let index = proxy_state. insert_symbol_path ( & path) ?;
256+ let str = format ! ( "{{arg :{}}}" , index) ;
265257 let result = inner_context. to_string_fn ( str) ?;
266258 let result = NativeObjectHandle :: new ( result. into_object ( ) ) ;
267259 return Ok ( Some ( result) ) ;
@@ -392,7 +384,6 @@ impl<IT: InnerTypes> NativeMemberSql<IT> {
392384 ) -> Result < NativeObjectHandle < CIT > , CubeError > {
393385 context_holder. make_proxy ( Some ( base_object) , move |inner_context, target, prop| {
394386 if & prop == "filter" {
395- println ! ( "!!!! AAAAAAA" ) ;
396387 return Ok ( Some ( Self :: security_context_filter_fn (
397388 inner_context. clone ( ) ,
398389 target. clone ( ) ,
@@ -424,7 +415,6 @@ impl<IT: InnerTypes> NativeMemberSql<IT> {
424415 ) ?) ) ;
425416 }
426417
427- println ! ( "!!!! EEEEEE" ) ;
428418 let result = inner_context. empty_struct ( ) ?;
429419 result. set_field (
430420 "filter" ,
@@ -449,45 +439,9 @@ impl<IT: InnerTypes> NativeMemberSql<IT> {
449439 Self :: security_context_unsafe_value_fn ( inner_context, target. clone ( ) ) ?,
450440 ) ?;
451441 let result = NativeObjectHandle :: new ( result. into_object ( ) ) ;
452- println ! ( "!!!! BBBBBBB" ) ;
453442 Ok ( Some ( result) )
454443 } )
455444 }
456- /*
457- public static contextSymbolsProxyFrom(symbols: object, allocateParam: (param: unknown) => unknown): object {
458- return new Proxy(symbols, {
459- get: (target, name) => {
460- const propValue = target[name];
461- const methods = (paramValue) => ({
462- filter: (column) => {
463- if (paramValue) {
464- const value = Array.isArray(paramValue) ?
465- paramValue.map(allocateParam) :
466- allocateParam(paramValue);
467- if (typeof column === 'function') {
468- return column(value);
469- } else {
470- return `${column} = ${value}`;
471- }
472- } else {
473- return '1 = 1';
474- }
475- },
476- requiredFilter: (column) => {
477- if (!paramValue) {
478- throw new UserError(`Filter for ${column} is required`);
479- }
480- return methods(paramValue).filter(column);
481- },
482- unsafeValue: () => paramValue
483- });
484- return methods(target)[name] ||
485- typeof propValue === 'object' && propValue !== null && CubeSymbols.contextSymbolsProxyFrom(propValue, allocateParam) ||
486- methods(propValue);
487- }
488- });
489- }
490- */
491445
492446 fn filter_goup_fn < CIT : InnerTypes > (
493447 context_holder : NativeContextHolder < CIT > ,
@@ -508,11 +462,8 @@ impl<IT: InnerTypes> NativeMemberSql<IT> {
508462 )
509463 } ) ?;
510464 let filter_group = FilterGroupItem { filter_params } ;
511- let index = proxy_state. with_state_mut ( |state| {
512- let i = state. filter_groups . len ( ) ;
513- state. filter_groups . push ( ( filter_group, i) ) ;
514- i
515- } ) ?;
465+ let index =
466+ proxy_state. with_state_mut ( |state| state. insert_filter_group ( filter_group) ) ?;
516467
517468 let str = format ! ( "{{fg:{}}}" , index) ;
518469 Ok ( str)
@@ -534,11 +485,8 @@ impl<IT: InnerTypes> NativeMemberSql<IT> {
534485 } ) ;
535486 let item_native = item. to_native ( context_holder. clone ( ) ) ?;
536487 let to_string_fn = context_holder. make_function ( move |_| {
537- let index = proxy_state. with_state_mut ( |state| {
538- let i = state. filter_params . len ( ) ;
539- state. filter_params . push ( ( item. as_ref ( ) . clone ( ) , i) ) ;
540- i
541- } ) ?;
488+ let index = proxy_state
489+ . with_state_mut ( |state| state. insert_filter_params ( item. as_ref ( ) . clone ( ) ) ) ?;
542490
543491 let str = format ! ( "{{fp:{}}}" , index) ;
544492 Ok ( str)
@@ -620,15 +568,14 @@ impl<IT: InnerTypes> MemberSql for NativeMemberSql<IT> {
620568 !self . args_names . is_empty ( )
621569 }
622570
623- fn into_template_sql (
571+ fn compile_template_sql (
624572 & self ,
625573 security_context : Rc < dyn SecurityContext > ,
626- ) -> Result < TemplatedSql , CubeError > {
574+ ) -> Result < ( String , SqlTemplateArgs ) , CubeError > {
627575 let state = ProxyState :: new ( ) ;
628576 let weak_state = state. weak ( ) ;
629577 let context_holder = NativeContextHolder :: < IT > :: new ( self . native_object . get_context ( ) ) ;
630578 let mut proxy_args = vec ! [ ] ;
631- println ! ( "!!!! ============" ) ;
632579 for arg in self . args_names . iter ( ) . cloned ( ) {
633580 let proxy_arg = if arg == "FILTER_PARAMS" {
634581 Self :: filter_params_proxy ( context_holder. clone ( ) , weak_state. clone ( ) ) ?
@@ -638,7 +585,6 @@ impl<IT: InnerTypes> MemberSql for NativeMemberSql<IT> {
638585 || arg == "security_context"
639586 || arg == "securityContext"
640587 {
641- println ! ( "!!! sec context" ) ;
642588 let context_obj = if let Some ( security_context) = security_context
643589 . clone ( )
644590 . as_any ( )
@@ -661,23 +607,12 @@ impl<IT: InnerTypes> MemberSql for NativeMemberSql<IT> {
661607 } ;
662608 proxy_args. push ( proxy_arg) ;
663609 }
664- println ! ( "!!!! ------ {}" , proxy_args. len( ) ) ;
665610 let native_func = self . native_object . to_function ( ) ?;
666- println ! ( "!!!! eeeee" ) ;
667611 let evaluation_result = native_func. call ( proxy_args) ?;
668- println ! ( "!!!! kkkk" ) ;
669- if let Ok ( t) = Vec :: < String > :: from_native ( evaluation_result. clone ( ) ) {
670- println ! ( "!!!! fff {:?}" , t) ;
671- }
672612 let template = String :: from_native ( evaluation_result) ?;
673- println ! ( "!!!!! state: {:#?}" , state. get_state( ) ?) ;
674- let property_args = state. get_args ( ) ?;
675- let result = TemplatedSql {
676- args : property_args,
677- template,
678- } ;
613+ let sql_args = state. get_args ( ) ?;
679614
680- Ok ( result )
615+ Ok ( ( template , sql_args ) )
681616 }
682617}
683618
0 commit comments