@@ -69,6 +69,32 @@ impl Default for ParserOptions {
6969 }
7070}
7171
72+ /// Ident Normalizer
73+ #[ derive( Debug ) ]
74+ pub struct IdentNormalizer {
75+ normalize : bool ,
76+ }
77+
78+ impl Default for IdentNormalizer {
79+ fn default ( ) -> Self {
80+ Self { normalize : true }
81+ }
82+ }
83+
84+ impl IdentNormalizer {
85+ pub fn new ( normalize : bool ) -> Self {
86+ Self { normalize }
87+ }
88+
89+ pub fn normalize ( & self , ident : Ident ) -> String {
90+ if self . normalize {
91+ crate :: utils:: normalize_ident ( ident)
92+ } else {
93+ ident. value
94+ }
95+ }
96+ }
97+
7298/// Struct to store the states used by the Planner. The Planner will leverage the states to resolve
7399/// CTEs, Views, subqueries and PREPARE statements. The states include
74100/// Common Table Expression (CTE) provided with WITH clause and
@@ -155,6 +181,7 @@ impl PlannerContext {
155181pub struct SqlToRel < ' a , S : ContextProvider > {
156182 pub ( crate ) schema_provider : & ' a S ,
157183 pub ( crate ) options : ParserOptions ,
184+ pub ( crate ) normalizer : IdentNormalizer ,
158185}
159186
160187impl < ' a , S : ContextProvider > SqlToRel < ' a , S > {
@@ -165,9 +192,11 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
165192
166193 /// Create a new query planner
167194 pub fn new_with_options ( schema_provider : & ' a S , options : ParserOptions ) -> Self {
195+ let normalize = options. enable_ident_normalization ;
168196 SqlToRel {
169197 schema_provider,
170198 options,
199+ normalizer : IdentNormalizer :: new ( normalize) ,
171200 }
172201 }
173202
@@ -181,7 +210,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
181210 . iter ( )
182211 . any ( |x| x. option == ColumnOption :: NotNull ) ;
183212 fields. push ( Field :: new (
184- normalize_ident ( column. name , self . options . enable_ident_normalization ) ,
213+ self . normalizer . normalize ( column. name ) ,
185214 data_type,
186215 !not_nullable,
187216 ) ) ;
@@ -198,7 +227,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
198227 ) -> Result < LogicalPlan > {
199228 let apply_name_plan = LogicalPlan :: SubqueryAlias ( SubqueryAlias :: try_new (
200229 plan,
201- normalize_ident ( alias. name , self . options . enable_ident_normalization ) ,
230+ self . normalizer . normalize ( alias. name ) ,
202231 ) ?) ;
203232
204233 self . apply_expr_alias ( apply_name_plan, alias. columns )
@@ -221,10 +250,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
221250 let fields = plan. schema ( ) . fields ( ) . clone ( ) ;
222251 LogicalPlanBuilder :: from ( plan)
223252 . project ( fields. iter ( ) . zip ( idents. into_iter ( ) ) . map ( |( field, ident) | {
224- col ( field. name ( ) ) . alias ( normalize_ident (
225- ident,
226- self . options . enable_ident_normalization ,
227- ) )
253+ col ( field. name ( ) ) . alias ( self . normalizer . normalize ( ident) )
228254 } ) ) ?
229255 . build ( )
230256 }
@@ -411,7 +437,7 @@ pub(crate) fn idents_to_table_reference(
411437 impl IdentTaker {
412438 fn take ( & mut self , enable_normalization : bool ) -> String {
413439 let ident = self . 0 . pop ( ) . expect ( "no more identifiers" ) ;
414- normalize_ident ( ident , enable_normalization)
440+ IdentNormalizer :: new ( enable_normalization) . normalize ( ident )
415441 }
416442 }
417443
@@ -447,6 +473,7 @@ pub fn object_name_to_qualifier(
447473 enable_normalization : bool ,
448474) -> String {
449475 let columns = vec ! [ "table_name" , "table_schema" , "table_catalog" ] . into_iter ( ) ;
476+ let normalizer = IdentNormalizer :: new ( enable_normalization) ;
450477 sql_table_name
451478 . 0
452479 . iter ( )
@@ -456,17 +483,9 @@ pub fn object_name_to_qualifier(
456483 format ! (
457484 r#"{} = '{}'"# ,
458485 column_name,
459- normalize_ident ( ident. clone( ) , enable_normalization )
486+ normalizer . normalize ( ident. clone( ) )
460487 )
461488 } )
462489 . collect :: < Vec < _ > > ( )
463490 . join ( " AND " )
464491}
465-
466- fn normalize_ident ( id : Ident , enable_normalization : bool ) -> String {
467- if enable_normalization {
468- return crate :: utils:: normalize_ident ( id) ;
469- }
470-
471- id. value
472- }
0 commit comments