        d e f   r u n _ s o u r c e ( 
 
                 s e l f , 
 
                 s o u r c e :   O p t i o n a l [ U n i o n [ s t r ,   P a t h ] ]   =   N o n e , 
 
                 * , 
 
                 r e p r o c e s s _ f r o m :   O p t i o n a l [ s t r ]   =   N o n e , 
 
                 r e p r o c e s s _ t o :   O p t i o n a l [ s t r ]   =   N o n e , 
 
                 r e p r o c e s s _ c o l u m n :   O p t i o n a l [ s t r ]   =   N o n e , 
 
                 r e p r o c e s s _ v a l u e s :   O p t i o n a l [ L i s t [ s t r ] ]   =   N o n e , 
 
                 l o o k b a c k _ d a y s :   O p t i o n a l [ i n t ]   =   N o n e , 
 
         )   - >   V a l i d a t i o n R e s u l t : 
 
                 " " " 
 
                 L o a d s   d a t a   f r o m   a   s o u r c e   f i l e   a n d   r u n s   t h e   c o n t r a c t   i n   o n e   s t e p . 
 
                 T h e   d a t a   i s   l o a d e d   u s i n g   t h e   e n g i n e ' s   o p t i m i z e d   r e a d e r . 
 
 
 
                 A r g s : 
 
                         s o u r c e :   O p t i o n a l   f i l e   p a t h   t o   l o a d .   I f   N o n e ,   u s e s   p a t h   f r o m   c o n t r a c t . 
 
                         r e p r o c e s s _ f r o m :   O p t i o n a l   s t a r t   d a t e   ( Y Y Y Y - M M - D D )   f o r   d a t e - r a n g e 
 
                                 r e p r o c e s s i n g .     W h e n   s e t ,   t h e   i n c r e m e n t a l   w a t e r m a r k   i s   b y p a s s e d 
 
                                 a n d   o n l y   r o w s   w h e r e   t h e   r e p r o c e s s   d a t e   c o l u m n   > =   t h i s   v a l u e 
 
                                 a r e   k e p t .     T h e   c o l u m n   i s   r e s o l v e d   f r o m 
 
                                 ` ` m a t e r i a l i z a t i o n . r e p r o c e s s _ d a t e _ c o l u m n ` ` ;   i f   n o t   s e t ,   t h e 
 
                                 f i r s t   ` ` p a r t i t i o n _ b y ` `   c o l u m n   i s   u s e d . 
 
                         r e p r o c e s s _ t o :   O p t i o n a l   e n d   d a t e   ( Y Y Y Y - M M - D D )   f o r   d a t e - r a n g e 
 
                                 r e p r o c e s s i n g .     R o w s   w h e r e   t h e   d a t e   c o l u m n   < =   t h i s   v a l u e   a r e 
 
                                 k e p t .     C a n   b e   u s e d   a l o n e   o r   w i t h   ` ` r e p r o c e s s _ f r o m ` ` . 
 
                         r e p r o c e s s _ c o l u m n :   O p t i o n a l   c o l u m n   n a m e   f o r   I D - b a s e d   r e p r o c e s s i n g . 
 
                         r e p r o c e s s _ v a l u e s :   O p t i o n a l   l i s t   o f   v a l u e s   f o r   I D - b a s e d   r e p r o c e s s i n g . 
 
                         l o o k b a c k _ d a y s :   O p t i o n a l   r u n t i m e   o v e r r i d e   f o r   p a r t i t i o n   l o o k b a c k _ d a y s . 
 
                                 O v e r r i d e s   t h e   c o n t r a c t ' s   ` ` s o u r c e . p a r t i t i o n . l o o k b a c k _ d a y s ` ` . 
 
 
 
                 R e t u r n s : 
 
                         V a l i d a t i o n R e s u l t   o b j e c t   ( u n p a c k s   t o   g o o d _ d f ,   b a d _ d f ) . 
 
                 " " " 
 
                 s e l f . _ a c t i v e _ t r a c e _ s t e p s   =   [ ] 
 
 
 
                 #   S t o r e   r e p r o c e s s   r a n g e   f o r   d o w n s t r e a m   u s e   b y   m a t e r i a l i z e 
 
                 s e l f . _ r e p r o c e s s _ f r o m   =   r e p r o c e s s _ f r o m 
 
                 s e l f . _ r e p r o c e s s _ t o   =   r e p r o c e s s _ t o 
 
                 s e l f . _ r e p r o c e s s _ v a l u e s   =   r e p r o c e s s _ v a l u e s 
 
 
 
                 #   P e r - c o n t r a c t   r e p r o c e s s _ c o l u m n :   e a c h   l a y e r   c a n   m a p   t o   i t s   o w n   c o l u m n   n a m e 
 
                 #   ( e . g .   b r o n z e = c u s _ i d ,   s i l v e r = c u s _ a p _ i d ,   g o l d = c u s _ s s _ i d ) . 
 
                 #   F a l l s   b a c k   t o   t h e   g l o b a l   r e p r o c e s s _ c o l u m n   p a r a m e t e r   f r o m   t h e   p i p e l i n e . 
 
                 m a t   =   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) 
 
                 p e r _ c o n t r a c t _ c o l   =   g e t a t t r ( m a t ,   " r e p r o c e s s _ c o l u m n " ,   N o n e )   i f   m a t   e l s e   N o n e 
 
                 r e s o l v e d _ r e p r o c e s s _ c o l u m n   =   p e r _ c o n t r a c t _ c o l   o r   r e p r o c e s s _ c o l u m n 
 
                 s e l f . _ r e p r o c e s s _ c o l u m n   =   r e s o l v e d _ r e p r o c e s s _ c o l u m n 
 
                 i f   p e r _ c o n t r a c t _ c o l   a n d   r e p r o c e s s _ c o l u m n   a n d   p e r _ c o n t r a c t _ c o l   ! =   r e p r o c e s s _ c o l u m n : 
 
                         l o g g e r . i n f o ( 
 
                                 f " U s i n g   p e r - c o n t r a c t   r e p r o c e s s _ c o l u m n   ' { p e r _ c o n t r a c t _ c o l } '   ( o v e r r i d i n g   g l o b a l   ' { r e p r o c e s s _ c o l u m n } ' ) " 
 
                         ) 
 
 
 
                 s e l f . _ i s _ r e p r o c e s s   =   b o o l ( r e p r o c e s s _ f r o m   o r   r e p r o c e s s _ t o   o r   ( r e s o l v e d _ r e p r o c e s s _ c o l u m n   a n d   r e p r o c e s s _ v a l u e s ) ) 
 
 
 
                 #           d l t   s o u r c e :   c o n t r a c t - d r i v e n   A P I   i n g e s t i o n                                                                              
 
                 i f   s e l f . c o n t r a c t . s o u r c e   a n d   s e l f . c o n t r a c t . s o u r c e . t y p e   = =   " d l t " : 
 
                         r e t u r n   s e l f . _ r u n _ d l t _ s o u r c e ( ) 
 
 
 
                 #           d a t a b a s e   s o u r c e :   n a t i v e   S Q L   i n g e s t i o n                                                                                          
 
                 i f   s e l f . c o n t r a c t . s o u r c e   a n d   s e l f . c o n t r a c t . s o u r c e . t y p e   = =   " d a t a b a s e " : 
 
                         r e t u r n   s e l f . _ r u n _ d a t a b a s e _ s o u r c e ( ) 
 
 
 
                 p a t h _ v a l   =   s o u r c e   o r   ( s e l f . c o n t r a c t . s o u r c e . p a t h   i f   s e l f . c o n t r a c t . s o u r c e   e l s e   N o n e ) 
 
                 i f   n o t   p a t h _ v a l : 
 
                         r a i s e   V a l u e E r r o r ( " N o   s o u r c e   p a t h   p r o v i d e d   a n d   n o   p a t h   f o u n d   i n   c o n t r a c t . " ) 
 
                 p a t h   =   s e l f . _ r e s o l v e _ s o u r c e _ p a t h ( p a t h _ v a l ) 
 
 
 
                 #   R e s o l v e   c a t a l o g   t a b l e   n a m e s   ( U n i t y   C a t a l o g ,   F a b r i c   L a k e D B ,   S y n a p s e )   t o   s t o r a g e   p a t h s 
 
                 i f   s e l f . e n g i n e _ n a m e   ! =   " s p a r k " :     #   S p a r k   h a n d l e s   c a t a l o g s   n a t i v e l y 
 
                         f r o m   l a k e l o g i c . e n g i n e s . c a t a l o g _ r e s o l v e r   i m p o r t   r e s o l v e _ c a t a l o g _ p a t h 
 
 
 
                         o r i g i n a l _ p a t h   =   p a t h 
 
                         p a t h   =   r e s o l v e _ c a t a l o g _ p a t h ( p a t h ) 
 
                         i f   p a t h   ! =   o r i g i n a l _ p a t h : 
 
                                 l o g g e r . i n f o ( f " R e s o l v e d   c a t a l o g   t a b l e :   { o r i g i n a l _ p a t h }   - >   { p a t h } " ) 
 
 
 
                 l o g g e r . i n f o ( f " L o a d i n g   s o u r c e :   { p a t h }   v i a   { s e l f . e n g i n e _ n a m e } " ) 
 
 
 
                 #           G u a r d :   v a l i d a t e   w a t e r m a r k _ s t r a t e g y   v s   s o u r c e   t y p e                                                      
 
                 #   p i p e l i n e _ l o g          c o m p a r e s   f i l e   m t i m e        o n l y   v a l i d   f o r   f i l e   s o u r c e s 
 
                 #   d e l t a _ v e r s i o n        r e a d s   D e l t a   t r a n s a c t i o n   l o g        o n l y   v a l i d   f o r   t a b l e   s o u r c e s 
 
                 #   M i s m a t c h e s   s i l e n t l y   d e g r a d e   t o   f u l l   r e l o a d s   e v e r y   r u n . 
 
                 _ s r c _ c f g   =   s e l f . c o n t r a c t . s o u r c e   i f   s e l f . c o n t r a c t . s o u r c e   e l s e   N o n e 
 
                 _ i s _ t a b l e   =   s t r ( p a t h ) . s t a r t s w i t h ( " t a b l e : " )   o r   ( _ s r c _ c f g   a n d   g e t a t t r ( _ s r c _ c f g ,   " t y p e " ,   N o n e )   = =   " t a b l e " ) 
 
                 _ w m _ s t r a t e g y   =   g e t a t t r ( _ s r c _ c f g ,   " w a t e r m a r k _ s t r a t e g y " ,   N o n e )   i f   _ s r c _ c f g   e l s e   N o n e 
 
                 _ l o a d _ m o d e   =   g e t a t t r ( _ s r c _ c f g ,   " l o a d _ m o d e " ,   " f u l l " )   i f   _ s r c _ c f g   e l s e   " f u l l " 
 
                 i f   _ i s _ t a b l e   a n d   _ w m _ s t r a t e g y   = =   " p i p e l i n e _ l o g "   a n d   _ l o a d _ m o d e   i n   ( " i n c r e m e n t a l " , ) : 
 
                         r a i s e   V a l u e E r r o r ( 
 
                                 " I n v a l i d   c o n f i g u r a t i o n :   s o u r c e   t y p e   ' t a b l e '   c a n n o t   u s e   " 
 
                                 " w a t e r m a r k _ s t r a t e g y   ' p i p e l i n e _ l o g '   ( i t   r e l i e s   o n   f i l e   m o d i f i c a t i o n   t i m e s   " 
 
                                 " w h i c h   d o n ' t   e x i s t   f o r   t a b l e   s o u r c e s ) .   " 
 
                                 " V a l i d   s t r a t e g i e s   f o r   t a b l e   s o u r c e s :   " 
 
                                 " ( 1 )   l o a d _ m o d e :   ' c d c '   w i t h   c d c _ t i m e s t a m p _ f i e l d :   ' _ l a k e l o g i c _ p r o c e s s e d _ a t ' ,   " 
 
                                 " ( 2 )   w a t e r m a r k _ s t r a t e g y :   ' d e l t a _ v e r s i o n ' . " 
 
                         ) 
 
                 i f   n o t   _ i s _ t a b l e   a n d   _ w m _ s t r a t e g y   = =   " d e l t a _ v e r s i o n "   a n d   _ l o a d _ m o d e   i n   ( " i n c r e m e n t a l " , ) : 
 
                         r a i s e   V a l u e E r r o r ( 
 
                                 " I n v a l i d   c o n f i g u r a t i o n :   f i l e - b a s e d   s o u r c e   c a n n o t   u s e   " 
 
                                 " w a t e r m a r k _ s t r a t e g y   ' d e l t a _ v e r s i o n '   ( i t   r e l i e s   o n   D e l t a   t r a n s a c t i o n   l o g   " 
 
                                 " v e r s i o n i n g   w h i c h   d o e s n ' t   e x i s t   f o r   f i l e   s o u r c e s ) .   " 
 
                                 " V a l i d   s t r a t e g i e s   f o r   f i l e   s o u r c e s :   " 
 
                                 " ( 1 )   w a t e r m a r k _ s t r a t e g y :   ' p i p e l i n e _ l o g ' ,   " 
 
                                 " ( 2 )   l o a d _ m o d e :   ' c d c '   w i t h   c d c _ t i m e s t a m p _ f i e l d . " 
 
                         ) 
 
 
 
                 #           R e p r o c e s s i n g   m o d e :   b y p a s s   i n c r e m e n t a l   w a t e r m a r k                                                            
 
                 _ i s _ r e p r o c e s s   =   b o o l ( r e p r o c e s s _ f r o m   o r   r e p r o c e s s _ t o   o r   ( r e s o l v e d _ r e p r o c e s s _ c o l u m n   a n d   r e p r o c e s s _ v a l u e s ) ) 
 
                 i f   _ i s _ r e p r o c e s s : 
 
                         i f   r e s o l v e d _ r e p r o c e s s _ c o l u m n   a n d   r e p r o c e s s _ v a l u e s : 
 
                                 l o g g e r . i n f o ( 
 
                                         f " T a r g e t e d   r e p r o c e s s i n g   m o d e :   f i l t e r   o n   { r e s o l v e d _ r e p r o c e s s _ c o l u m n }   " 
 
                                         f " I N   ( { l e n ( r e p r o c e s s _ v a l u e s ) }   v a l u e s )        i n c r e m e n t a l   w a t e r m a r k   b y p a s s e d " 
 
                                 ) 
 
                         e l s e : 
 
                                 l o g g e r . i n f o ( 
 
                                         f " R e p r o c e s s i n g   m o d e :   d a t e   r a n g e   [ { r e p r o c e s s _ f r o m   o r   ' * ' }   . .   " 
 
                                         f " { r e p r o c e s s _ t o   o r   ' * ' } ]        i n c r e m e n t a l   w a t e r m a r k   b y p a s s e d " 
 
                                 ) 
 
 
 
                 #           D a t e - p a r t i t i o n e d   l a n d i n g :   e x p a n d   p a t h   i n t o   p e r - d a t e   g l o b s                              
 
                 p a r t i t i o n _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " p a r t i t i o n " ,   N o n e )   i f   s e l f . c o n t r a c t . s o u r c e   e l s e   N o n e 
 
                 i f   p a r t i t i o n _ c f g   a n d   p a r t i t i o n _ c f g . f o r m a t : 
 
                         #   A u t o - d e t e c t   i n i t i a l   l o a d :   i f   n o   p r i o r   w a t e r m a r k   e x i s t s ,   t h i s   i s   a 
 
                         #   f i r s t   r u n        s k i p   l o o k b a c k   a n d   s c a n   A L L   p a r t i t i o n s   v i a   f u l l   g l o b . 
 
                         l o a d _ m o d e   =   g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " l o a d _ m o d e " ,   " f u l l " )   i f   s e l f . c o n t r a c t . s o u r c e   e l s e   " f u l l " 
 
                         _ i s _ i n i t i a l _ l o a d   =   F a l s e 
 
                         w a t e r m a r k   =   N o n e 
 
                         i f   l o a d _ m o d e   = =   " i n c r e m e n t a l "   a n d   n o t   _ i s _ r e p r o c e s s : 
 
                                 w a t e r m a r k   =   s e l f . _ g e t _ l a s t _ s o u r c e _ w a t e r m a r k ( ) 
 
                                 i f   w a t e r m a r k   i s   N o n e : 
 
                                         _ i s _ i n i t i a l _ l o a d   =   T r u e 
 
                                         #   I f   l o o k b a c k _ d a y s   i s   e x p l i c i t l y   s e t   ( r u n t i m e   o r   c o n t r a c t ) , 
 
                                         #   r e s p e c t   i t   e v e n   o n   i n i t i a l   l o a d        t h i s   l e t s   u s e r s   l i m i t 
 
                                         #   t h e   f i r s t   i n g e s t   t o   X   d a y s   i n s t e a d   o f   s c a n n i n g   e v e r y t h i n g . 
 
                                         _ e f f e c t i v e _ l o o k b a c k   =   l o o k b a c k _ d a y s   o r   g e t a t t r ( p a r t i t i o n _ c f g ,   " l o o k b a c k _ d a y s " ,   N o n e ) 
 
                                         i f   _ e f f e c t i v e _ l o o k b a c k : 
 
                                                 l o g g e r . i n f o ( 
 
                                                         f " I n i t i a l   l o a d   d e t e c t e d   ( n o   p r i o r   w a t e r m a r k )        " 
 
                                                         f " u s i n g   l o o k b a c k _ d a y s = { _ e f f e c t i v e _ l o o k b a c k }   t o   l i m i t   p a r t i t i o n   s c a n " 
 
                                                 ) 
 
                                         e l s e : 
 
                                                 l o g g e r . i n f o ( 
 
                                                         " I n i t i a l   l o a d   d e t e c t e d   ( n o   p r i o r   w a t e r m a r k )        " 
 
                                                         " s c a n n i n g   a l l   p a r t i t i o n s   ( s e t   l o o k b a c k _ d a y s   t o   l i m i t ) " 
 
                                                 ) 
 
 
 
                         i f   _ i s _ i n i t i a l _ l o a d : 
 
                                 _ e f f e c t i v e _ l o o k b a c k   =   l o o k b a c k _ d a y s   o r   g e t a t t r ( p a r t i t i o n _ c f g ,   " l o o k b a c k _ d a y s " ,   N o n e ) 
 
                                 i f   _ e f f e c t i v e _ l o o k b a c k : 
 
                                         #   U s e   t h e   p a r t i t i o n   s c a n n e r   w i t h   t h e   l o o k b a c k   c o n s t r a i n t 
 
                                         e f f e c t i v e _ c f g   =   p a r t i t i o n _ c f g . m o d e l _ c o p y ( u p d a t e = { " l o o k b a c k _ d a y s " :   _ e f f e c t i v e _ l o o k b a c k } ) 
 
                                         s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ p a r t i t i o n e d _ p a t h s ( p a t h ,   e f f e c t i v e _ c f g ) 
 
                                 e l s e : 
 
                                         #   N o   l o o k b a c k   c o n s t r a i n t        s c a n   e v e r y t h i n g 
 
                                         s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( p a t h   +   " / * * / * " ) 
 
                                         i f   n o t   s o u r c e _ f i l e s : 
 
                                                 s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( p a t h ) 
 
                         e l s e : 
 
                                 #   A p p l y   r u n t i m e   l o o k b a c k _ d a y s   o v e r r i d e   i f   p r o v i d e d 
 
                                 e f f e c t i v e _ c f g   =   p a r t i t i o n _ c f g 
 
                                 i f   l o o k b a c k _ d a y s   i s   n o t   N o n e : 
 
                                         e f f e c t i v e _ c f g   =   p a r t i t i o n _ c f g . m o d e l _ c o p y ( u p d a t e = { " l o o k b a c k _ d a y s " :   l o o k b a c k _ d a y s } ) 
 
 
 
                                 e f f _ s t a r t   =   r e p r o c e s s _ f r o m 
 
                                 i f   n o t   e f f _ s t a r t   a n d   w a t e r m a r k   i s   n o t   N o n e : 
 
                                         f r o m   d a t e t i m e   i m p o r t   d a t e t i m e ,   t i m e z o n e 
 
 
 
                                         e f f _ s t a r t   =   d a t e t i m e . f r o m t i m e s t a m p ( w a t e r m a r k ,   t z = t i m e z o n e . u t c ) . d a t e ( ) . i s o f o r m a t ( ) 
 
 
 
                                 s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ p a r t i t i o n e d _ p a t h s ( 
 
                                         p a t h , 
 
                                         e f f e c t i v e _ c f g , 
 
                                         o v e r r i d e _ s t a r t = e f f _ s t a r t , 
 
                                         o v e r r i d e _ e n d = r e p r o c e s s _ t o , 
 
                                 ) 
 
 
 
                         #   G u a r d :   i f   p a r t i t i o n e d   e x p a n s i o n   f o u n d   z e r o   f i l e s ,   r e t u r n   e a r l y 
 
                         #   r a t h e r   t h a n   l e t t i n g   t h e   e n g i n e   a t t e m p t   t o   r e a d   a n   e m p t y   d i r e c t o r y . 
 
                         i f   s o u r c e _ f i l e s   i s   n o t   N o n e   a n d   l e n ( s o u r c e _ f i l e s )   = =   0 : 
 
                                 l o g g e r . i n f o ( " N o   s o u r c e   f i l e s   f o u n d   i n   p a r t i t i o n e d   p a t h ;   s k i p p i n g   r u n . " ) 
 
                                 s e l f . _ w r i t e _ e m p t y _ r u n _ l o g ( " n o _ n e w _ d a t a " ) 
 
                                 r e t u r n   V a l i d a t i o n R e s u l t ( s e l f . _ e m p t y _ f r a m e ( ) ,   s e l f . _ e m p t y _ f r a m e ( ) ,   s e l f . _ e m p t y _ f r a m e ( ) ) 
 
                 e l s e : 
 
                         #   F o r c e   d i r e c t o r y   g l o b   t o   c a l c u l a t e   m t i m e s   f o r   i n c r e m e n t a l   f i l e   r e a d s 
 
                         #   i f   a   r a w   d i r e c t o r y   p a t h   w a s   p r o v i d e d   w i t h o u t   a n   e x p l i c i t   g l o b . 
 
                         _ i s _ t a b l e   =   s t r ( p a t h ) . s t a r t s w i t h ( " t a b l e : " )   o r   ( 
 
                                 s e l f . c o n t r a c t . s o u r c e   a n d   s e l f . c o n t r a c t . s o u r c e . t y p e   = =   " t a b l e " 
 
                         ) 
 
                         i f   ( 
 
                                 s e l f . c o n t r a c t . s o u r c e 
 
                                 a n d   g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " l o a d _ m o d e " ,   " f u l l " )   = =   " i n c r e m e n t a l " 
 
                                 a n d   n o t   a n y ( c h   i n   s t r ( p a t h )   f o r   c h   i n   [ " * " ,   " ? " ,   " [ " ] ) 
 
                                 a n d   n o t   _ i s _ t a b l e 
 
                         ) : 
 
                                 s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( s t r ( p a t h ) . r s t r i p ( " / " )   +   " / * " ) 
 
                                 i f   n o t   s o u r c e _ f i l e s : 
 
                                         s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( p a t h ) 
 
                         e l s e : 
 
                                 #   F o r   l a n d i n g   s o u r c e s   w i t h   a   b a r e   d i r e c t o r y   p a t h   ( n o   g l o b ) , 
 
                                 #   a u t o - a p p e n d   / *   s o   t h e   f i l e   s c a n n e r   d i s c o v e r s   c o n t e n t s . 
 
                                 _ s r c _ t y p e   =   g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " t y p e " ,   N o n e )   i f   s e l f . c o n t r a c t . s o u r c e   e l s e   N o n e 
 
                                 _ i s _ b a r e _ d i r   =   ( 
 
                                         n o t   a n y ( c h   i n   s t r ( p a t h )   f o r   c h   i n   [ " * " ,   " ? " ,   " [ " ] ) 
 
                                         a n d   n o t   _ i s _ t a b l e 
 
                                         a n d   _ s r c _ t y p e   n o t   i n   ( " d e l t a " ,   " i c e b e r g " ) 
 
                                         a n d   n o t   s e l f . _ i s _ u r i _ p a t h ( s t r ( p a t h ) ) 
 
                                         a n d   P a t h ( p a t h ) . i s _ d i r ( ) 
 
                                         a n d   n o t   ( P a t h ( p a t h )   /   " _ d e l t a _ l o g " ) . e x i s t s ( ) 
 
                                         a n d   n o t   ( ( P a t h ( p a t h )   /   " m e t a d a t a " ) . e x i s t s ( )   a n d   ( P a t h ( p a t h )   /   " d a t a " ) . e x i s t s ( ) ) 
 
                                 ) 
 
                                 i f   _ i s _ b a r e _ d i r : 
 
                                         s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( s t r ( p a t h ) . r s t r i p ( " / " )   +   " / * * / * " ) 
 
                                         i f   n o t   s o u r c e _ f i l e s : 
 
                                                 s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( s t r ( p a t h ) . r s t r i p ( " / " )   +   " / * " ) 
 
                                 e l s e : 
 
                                         s o u r c e _ f i l e s   =   s e l f . _ e x p a n d _ s o u r c e _ f i l e s ( p a t h ) 
 
                 l o a d _ m o d e   =   g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " l o a d _ m o d e " ,   " f u l l " )   i f   s e l f . c o n t r a c t . s o u r c e   e l s e   " f u l l " 
 
                 i f   l o a d _ m o d e   = =   " i n c r e m e n t a l "   a n d   s o u r c e _ f i l e s   a n d   n o t   _ i s _ r e p r o c e s s : 
 
                         w a t e r m a r k   =   s e l f . _ g e t _ l a s t _ s o u r c e _ w a t e r m a r k ( ) 
 
                         i f   w a t e r m a r k   i s   n o t   N o n e : 
 
                                 s o u r c e _ f i l e s   =   [ f   f o r   f   i n   s o u r c e _ f i l e s   i f   f . g e t ( " m t i m e " ,   0 )   >   w a t e r m a r k ] 
 
                         i f   n o t   s o u r c e _ f i l e s : 
 
                                 l o g g e r . i n f o ( " N o   n e w   f i l e s   d e t e c t e d   f o r   i n c r e m e n t a l   l o a d ;   s k i p p i n g   r u n . " ) 
 
                                 s e l f . _ w r i t e _ e m p t y _ r u n _ l o g ( " n o _ n e w _ d a t a " ) 
 
                                 r e t u r n   V a l i d a t i o n R e s u l t ( s e l f . _ e m p t y _ f r a m e ( ) ,   s e l f . _ e m p t y _ f r a m e ( ) ,   s e l f . _ e m p t y _ f r a m e ( ) ) 
 
 
 
                 s e l f . _ s o u r c e _ f i l e s   =   s o u r c e _ f i l e s   o r   [ ] 
 
                 s e l f . _ s o u r c e _ m a x _ m t i m e   =   N o n e 
 
                 i f   s o u r c e _ f i l e s : 
 
                         s e l f . _ s o u r c e _ m a x _ m t i m e   =   m a x ( f . g e t ( " m t i m e " ,   0 )   f o r   f   i n   s o u r c e _ f i l e s ) 
 
 
 
                 d f   =   N o n e 
 
                 f i l e _ p a t h s   =   [ f [ " p a t h " ]   f o r   f   i n   s o u r c e _ f i l e s ]   i f   s o u r c e _ f i l e s   e l s e   N o n e 
 
 
 
                 w i t h   s e l f . t r a c e _ s t e p ( " L o a d   S o u r c e " ,   p a t h = s t r ( p a t h ) ) : 
 
                         i f   s e l f . e n g i n e _ n a m e   i n   ( " p o l a r s " ,   " d u c k d b " ) : 
 
                                 i m p o r t   p o l a r s   a s   p l 
 
 
 
                                 i f   d f   i s   N o n e : 
 
                                         s o u r c e _ f m t   =   ( 
 
                                                 ( 
 
                                                         g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " f o r m a t " ,   N o n e ) 
 
                                                         i f   g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) 
 
                                                         e l s e   N o n e 
 
                                                 ) 
 
                                                 o r   ( 
 
                                                         g e t a t t r ( s e l f . c o n t r a c t . m a t e r i a l i z a t i o n ,   " f o r m a t " ,   N o n e ) 
 
                                                         i f   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) 
 
                                                         e l s e   N o n e 
 
                                                 ) 
 
                                                 o r   ( 
 
                                                         g e t a t t r ( s e l f . c o n t r a c t . s e r v e r ,   " f o r m a t " ,   N o n e ) 
 
                                                         i f   g e t a t t r ( s e l f . c o n t r a c t ,   " s e r v e r " ,   N o n e ) 
 
                                                         e l s e   N o n e 
 
                                                 ) 
 
                                                 o r   " " 
 
                                         ) . l o w e r ( ) 
 
 
 
                                         #   U s e   s c a n _ *   ( l a z y )   b y   d e f a u l t   f o r   f o r m a t s   t h a t   s u p p o r t   i t . 
 
                                         #   T h i s   e n a b l e s   p r e d i c a t e / p r o j e c t i o n   p u s h d o w n   a n d   q u e r y 
 
                                         #   o p t i m i s a t i o n .     W e   . c o l l e c t ( )   b e f o r e   r e t u r n i n g   s o   t h e 
 
                                         #   u s e r   a l w a y s   r e c e i v e s   a   r e g u l a r   D a t a F r a m e . 
 
                                         _ s c a n n a b l e   =   s o u r c e _ f m t   i n   ( " p a r q u e t " ,   " c s v " ,   " n d j s o n " )   o r   p a t h . e n d s w i t h ( 
 
                                                 ( " . p a r q u e t " ,   " . c s v " ,   " . n d j s o n " ,   " . j s o n l " ) 
 
                                         ) 
 
 
 
                                         i f   f i l e _ p a t h s : 
 
                                                 #   B u i l d   s t o r a g e _ o p t i o n s   f o r   c l o u d   p a t h s   ( P o l a r s   u s e s 
 
                                                 #   i t s   o w n   o b j e c t - s t o r e   b a c k e n d ,   n o t   f s s p e c ) 
 
                                                 _ p l _ s o p t s   =   s e l f . _ g e t _ c l o u d _ s t o r a g e _ o p t i o n s ( p a t h )   i f   s e l f . _ i s _ u r i _ p a t h ( p a t h )   e l s e   N o n e 
 
                                                 _ s c a n _ k w :   d i c t   =   { } 
 
                                                 i f   _ p l _ s o p t s : 
 
                                                         _ s c a n _ k w [ " s t o r a g e _ o p t i o n s " ]   =   _ p l _ s o p t s 
 
 
 
                                                 i f   _ s c a n n a b l e : 
 
                                                         #   L a z y :   s c a n   e a c h   f i l e   a n d   c o n c a t   l a z i l y . 
 
                                                         #   U s e   d i a g o n a l _ r e l a x e d   s o   t y p e - i n f e r e n c e   d i f f e r e n c e s 
 
                                                         #   a c r o s s   C S V   f i l e s   ( e . g .   I n t 6 4   v s   F l o a t 6 4 )   a r e 
 
                                                         #   c o e r c e d   t o   a   c o m m o n   s u p e r t y p e   a u t o m a t i c a l l y . 
 
                                                         _ c o n c a t _ h o w   =   " d i a g o n a l _ r e l a x e d " 
 
                                                         #   O n   W i n d o w s ,   P o l a r s '   i n t e r n a l   p a t h   r e s o l v e r   c a n 
 
                                                         #   m a n g l e   d r i v e - l e t t e r   p a t h s   ( C : )   e v e n   w i t h 
 
                                                         #   g l o b = F a l s e .     F o r   l o c a l   f i l e   p a t h s   ( n o n - U R I ) , 
 
                                                         #   u s e   e a g e r   r e a d   t o   b y p a s s   p a t h   h a n d l i n g   e n t i r e l y . 
 
                                                         _ i s _ l o c a l   =   n o t   s e l f . _ i s _ u r i _ p a t h ( p a t h ) 
 
                                                         _ s c a n _ k w [ " g l o b " ]   =   F a l s e 
 
                                                         #   T a g   e a c h   f i l e   w i t h   i t s   s o u r c e   p a t h   s o 
 
                                                         #   _ l a k e l o g i c _ s o u r c e   s h o w s   a c t u a l   f i l e n a m e s 
 
                                                         #   r a t h e r   t h a n   j u s t   t h e   p a r e n t   d i r e c t o r y . 
 
                                                         _ t a g _ s o u r c e   =   l e n ( f i l e _ p a t h s )   >   1 
 
 
 
                                                         i f   s o u r c e _ f m t   = =   " p a r q u e t "   o r   p a t h . e n d s w i t h ( " . p a r q u e t " ) : 
 
                                                                 i f   _ t a g _ s o u r c e : 
 
                                                                         d f   =   p l . c o n c a t ( 
 
                                                                                 [ 
 
                                                                                         p l . r e a d _ p a r q u e t ( p ) . w i t h _ c o l u m n s ( p l . l i t ( p ) . a l i a s ( " _ s o u r c e _ f i l e " ) ) 
 
                                                                                         f o r   p   i n   f i l e _ p a t h s 
 
                                                                                 ] , 
 
                                                                                 h o w = _ c o n c a t _ h o w , 
 
                                                                         ) 
 
                                                                 e l s e : 
 
                                                                         l f   =   p l . s c a n _ p a r q u e t ( f i l e _ p a t h s [ 0 ] ,   * * _ s c a n _ k w ) 
 
                                                                         d f   =   l f . c o l l e c t ( ) 
 
                                                         e l i f   s o u r c e _ f m t   = =   " n d j s o n "   o r   p a t h . e n d s w i t h ( ( " . n d j s o n " ,   " . j s o n l " ) ) : 
 
                                                                 i f   _ t a g _ s o u r c e : 
 
                                                                         d f   =   p l . c o n c a t ( 
 
                                                                                 [ 
 
                                                                                         p l . r e a d _ n d j s o n ( p ) . w i t h _ c o l u m n s ( p l . l i t ( p ) . a l i a s ( " _ s o u r c e _ f i l e " ) ) 
 
                                                                                         f o r   p   i n   f i l e _ p a t h s 
 
                                                                                 ] , 
 
                                                                                 h o w = _ c o n c a t _ h o w , 
 
                                                                         ) 
 
                                                                 e l s e : 
 
                                                                         _ n d j s o n _ k w   =   _ s c a n _ k w . c o p y ( ) 
 
                                                                         _ n d j s o n _ k w . p o p ( " g l o b " ,   N o n e ) 
 
                                                                         l f   =   p l . s c a n _ n d j s o n ( f i l e _ p a t h s [ 0 ] ,   * * _ n d j s o n _ k w ) 
 
                                                                         d f   =   l f . c o l l e c t ( ) 
 
                                                         e l s e :     #   C S V 
 
                                                                 i f   _ i s _ l o c a l : 
 
                                                                         #   E a g e r   r e a d        b y p a s s e s   P o l a r s '   i n t e r n a l 
 
                                                                         #   p a t h   c a n o n i c a l i s a t i o n   w h i c h   b r e a k s   o n 
 
                                                                         #   W i n d o w s   d r i v e - l e t t e r   p a t h s . 
 
                                                                         i f   _ t a g _ s o u r c e : 
 
                                                                                 d f   =   p l . c o n c a t ( 
 
                                                                                         [ 
 
                                                                                                 p l . r e a d _ c s v ( p ) . w i t h _ c o l u m n s ( p l . l i t ( p ) . a l i a s ( " _ s o u r c e _ f i l e " ) ) 
 
                                                                                                 f o r   p   i n   f i l e _ p a t h s 
 
                                                                                         ] , 
 
                                                                                         h o w = _ c o n c a t _ h o w , 
 
                                                                                 ) 
 
                                                                         e l s e : 
 
                                                                                 d f   =   p l . r e a d _ c s v ( f i l e _ p a t h s [ 0 ] ) 
 
                                                                 e l s e : 
 
                                                                         i f   _ t a g _ s o u r c e : 
 
                                                                                 d f   =   p l . c o n c a t ( 
 
                                                                                         [ 
 
                                                                                                 p l . r e a d _ c s v ( p ) . w i t h _ c o l u m n s ( p l . l i t ( p ) . a l i a s ( " _ s o u r c e _ f i l e " ) ) 
 
                                                                                                 f o r   p   i n   f i l e _ p a t h s 
 
                                                                                         ] , 
 
                                                                                         h o w = _ c o n c a t _ h o w , 
 
                                                                                 ) 
 
                                                                         e l s e : 
 
                                                                                 l f   =   p l . s c a n _ c s v ( f i l e _ p a t h s [ 0 ] ,   * * _ s c a n _ k w ) 
 
                                                                                 d f   =   l f . c o l l e c t ( ) 
 
                                                 e l s e : 
 
                                                         #   E a g e r   f a l l b a c k        J S O N ,   X M L ,   E x c e l   ( n o   s c a n _ *   s u p p o r t ) 
 
                                                         i m p o r t   j s o n   a s   _ j s o n 
 
 
 
                                                         d e f   _ r e a d _ j s o n _ f l a t ( f i l e p a t h :   s t r )   - >   " p l . D a t a F r a m e " : 
 
                                                                 " " " R e a d   a   . j s o n   f i l e   a n d   c a s t   a n y   n e s t e d   S t r u c t / L i s t   c o l u m n s 
 
                                                                 t o   J S O N   s t r i n g s   s o   t h e y   m a t c h   a   f l a t   c o n t r a c t   s c h e m a . 
 
                                                                 S u p p o r t s   c l o u d   U R I s   ( a b f s s : / / ,   s 3 : / / ,   g s : / / )   v i a   f s s p e c . " " " 
 
                                                                 i m p o r t   p o l a r s   a s   p l 
 
 
 
                                                                 i f   s e l f . _ i s _ u r i _ p a t h ( f i l e p a t h ) : 
 
                                                                         i m p o r t   f s s p e c 
 
 
 
                                                                         _ s o p t s   =   s e l f . _ g e t _ c l o u d _ s t o r a g e _ o p t i o n s ( f i l e p a t h ) 
 
                                                                         w i t h   f s s p e c . o p e n ( f i l e p a t h ,   " r " ,   * * _ s o p t s )   a s   f : 
 
                                                                                 t e x t   =   f . r e a d ( ) 
 
                                                                         t r y : 
 
                                                                                 r a w   =   _ j s o n . l o a d s ( t e x t ) 
 
                                                                         e x c e p t   _ j s o n . J S O N D e c o d e E r r o r : 
 
                                                                                 #   N D J S O N :   o n e   J S O N   o b j e c t   p e r   l i n e 
 
                                                                                 r a w   =   [ _ j s o n . l o a d s ( l i n e )   f o r   l i n e   i n   t e x t . s t r i p ( ) . s p l i t l i n e s ( )   i f   l i n e . s t r i p ( ) ] 
 
                                                                 e l s e : 
 
                                                                         r a w   =   _ j s o n . l o a d s ( P a t h ( f i l e p a t h ) . r e a d _ t e x t ( e n c o d i n g = " u t f - 8 " ) ) 
 
                                                                 r o w s   =   [ r a w ]   i f   i s i n s t a n c e ( r a w ,   d i c t )   e l s e   ( r a w   i f   i s i n s t a n c e ( r a w ,   l i s t )   e l s e   [ ] ) 
 
                                                                 #   N o r m a l i s e   n e s t e d   o b j e c t s   t o   J S O N   s t r i n g s 
 
                                                                 f l a t   =   [ 
 
                                                                         { 
 
                                                                                 k :   ( _ j s o n . d u m p s ( v ,   e n s u r e _ a s c i i = F a l s e )   i f   i s i n s t a n c e ( v ,   ( d i c t ,   l i s t ) )   e l s e   v ) 
 
                                                                                 f o r   k ,   v   i n   r o w . i t e m s ( ) 
 
                                                                         } 
 
                                                                         f o r   r o w   i n   r o w s 
 
                                                                 ] 
 
                                                                 r e t u r n   p l . D a t a F r a m e ( f l a t ) 
 
 
 
                                                         i f   l e n ( f i l e _ p a t h s )   = =   1 : 
 
                                                                 f p   =   f i l e _ p a t h s [ 0 ] 
 
                                                                 i f   f p . e n d s w i t h ( " . x m l " ) : 
 
                                                                         d f   =   p l . r e a d _ x m l ( f p ) 
 
                                                                 e l i f   f p . e n d s w i t h ( ( " . x l s x " ,   " . x l s " ) ) : 
 
                                                                         d f   =   p l . r e a d _ e x c e l ( f p ) 
 
                                                                 e l i f   f p . e n d s w i t h ( " . j s o n " ) : 
 
                                                                         d f   =   _ r e a d _ j s o n _ f l a t ( f p ) 
 
                                                                 e l s e : 
 
                                                                         d f   =   p l . r e a d _ c s v ( f p ) 
 
                                                         e l s e : 
 
                                                                 f r a m e s   =   [ ] 
 
                                                                 f o r   f p   i n   f i l e _ p a t h s : 
 
                                                                         i f   f p . e n d s w i t h ( " . x m l " ) : 
 
                                                                                 f r a m e s . a p p e n d ( p l . r e a d _ x m l ( f p ) ) 
 
                                                                         e l i f   f p . e n d s w i t h ( ( " . x l s x " ,   " . x l s " ) ) : 
 
                                                                                 f r a m e s . a p p e n d ( p l . r e a d _ e x c e l ( f p ) ) 
 
                                                                         e l i f   f p . e n d s w i t h ( " . j s o n " ) : 
 
                                                                                 f r a m e s . a p p e n d ( _ r e a d _ j s o n _ f l a t ( f p ) ) 
 
                                                                         e l s e : 
 
                                                                                 f r a m e s . a p p e n d ( p l . r e a d _ c s v ( f p ) ) 
 
                                                                 d f   =   p l . c o n c a t ( f r a m e s ,   h o w = " d i a g o n a l _ r e l a x e d " )     #   c o e r c e   t y p e   c o n f l i c t s   a c r o s s   f i l e s 
 
 
 
                                         e l s e : 
 
                                                 #   N o   g l o b   e x p a n s i o n        s i n g l e   p a t h   o r   t a b l e   d i r e c t o r y 
 
                                                 #           D e l t a   t a b l e   d i r e c t o r y                                                                                 
 
                                                 _ i s _ d e l t a _ d i r   =   F a l s e 
 
                                                 s o u r c e _ f m t   =   ( 
 
                                                         ( 
 
                                                                 g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " f o r m a t " ,   N o n e ) 
 
                                                                 i f   g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) 
 
                                                                 e l s e   N o n e 
 
                                                         ) 
 
                                                         o r   ( 
 
                                                                 g e t a t t r ( s e l f . c o n t r a c t . m a t e r i a l i z a t i o n ,   " f o r m a t " ,   N o n e ) 
 
                                                                 i f   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) 
 
                                                                 e l s e   N o n e 
 
                                                         ) 
 
                                                         o r   ( 
 
                                                                 g e t a t t r ( s e l f . c o n t r a c t . s e r v e r ,   " f o r m a t " ,   N o n e ) 
 
                                                                 i f   g e t a t t r ( s e l f . c o n t r a c t ,   " s e r v e r " ,   N o n e ) 
 
                                                                 e l s e   N o n e 
 
                                                         ) 
 
                                                         o r   " " 
 
                                                 ) . l o w e r ( ) 
 
 
 
                                                 i f   n o t   s e l f . _ i s _ u r i _ p a t h ( p a t h ) : 
 
                                                         p _ o b j   =   P a t h ( p a t h ) 
 
                                                         _ i s _ d e l t a _ d i r   =   p _ o b j . i s _ d i r ( )   a n d   ( p _ o b j   /   " _ d e l t a _ l o g " ) . e x i s t s ( ) 
 
 
 
                                                 #   E x p l i c i t   c h e c k   f o r   d e l t a   f o r m a t   o r   t a b l e   p r e f i x 
 
                                                 i f   n o t   _ i s _ d e l t a _ d i r : 
 
                                                         i f   p a t h . s t a r t s w i t h ( " t a b l e : " )   o r   s o u r c e _ f m t   = =   " d e l t a " : 
 
                                                                 _ i s _ d e l t a _ d i r   =   T r u e 
 
                                                         e l i f   g e t a t t r ( g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) ,   " t y p e " ,   N o n e )   = =   " t a b l e " : 
 
                                                                 _ i s _ d e l t a _ d i r   =   T r u e 
 
 
 
                                                 i f   _ i s _ d e l t a _ d i r   o r   s o u r c e _ f m t . l o w e r ( )   = =   " d e l t a " : 
 
                                                         #           R e s o l v e   i n c r e m e n t a l   w a t e r m a r k   B E F O R E   r e a d i n g         
 
                                                         #   Q u e r y   m a x ( w a t e r m a r k _ c o l )   f r o m   t h e   t a r g e t   ( s i l v e r ) 
 
                                                         #   t a b l e   o n c e ,   t h e n   p a s s   a s   a   f i l t e r   t o   t h e   D e l t a   r e a d 
 
                                                         #   s o   d i s c a r d e d   r o w s   a r e   n e v e r   l o a d e d   i n t o   m e m o r y . 
 
                                                         _ w m _ f i l t e r _ e x p r   =   N o n e 
 
                                                         _ s r c _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) 
 
                                                         i f   g e t a t t r ( _ s r c _ c f g ,   " l o a d _ m o d e " ,   N o n e )   i n   ( " i n c r e m e n t a l " ,   " c d c " )   a n d   n o t   o s . e n v i r o n . g e t ( 
 
                                                                 " L A K E L O G I C _ S K I P _ I N C R E M E N T A L _ C H E C K " 
 
                                                         ) : 
 
                                                                 _ w m _ f i e l d   =   g e t a t t r ( _ s r c _ c f g ,   " w a t e r m a r k _ f i e l d " ,   N o n e ) 
 
                                                                 #   D e f a u l t   t o   t h e   c o n f i g u r e d   l i n e a g e   t i m e s t a m p   w h e n   n o   e x p l i c i t   w a t e r m a r k   i s   s e t . 
 
                                                                 i f   n o t   _ w m _ f i e l d : 
 
                                                                         _ l i n _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " l i n e a g e " ,   N o n e ) 
 
                                                                         _ w m _ b a s e   =   ( 
 
                                                                                 g e t a t t r ( _ l i n _ c f g ,   " t i m e s t a m p _ c o l u m n _ n a m e " ,   " _ l a k e l o g i c _ p r o c e s s e d _ a t " ) 
 
                                                                                 i f   _ l i n _ c f g 
 
                                                                                 e l s e   " _ l a k e l o g i c _ p r o c e s s e d _ a t " 
 
                                                                         ) 
 
                                                                         _ w m _ f i e l d   =   g e t a t t r ( _ s r c _ c f g ,   " c d c _ t i m e s t a m p _ f i e l d " ,   _ w m _ b a s e ) 
 
 
 
                                                                 _ m a t   =   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) 
 
                                                                 _ t g t   =   g e t a t t r ( _ m a t ,   " t a r g e t _ p a t h " ,   N o n e )   i f   _ m a t   e l s e   N o n e 
 
                                                                 i f   _ w m _ f i e l d   a n d   _ t g t : 
 
                                                                         _ s r c _ c o l ,   _ t g t _ c o l   =   s e l f . _ r e s o l v e _ w a t e r m a r k _ c o l u m n s ( _ w m _ f i e l d ) 
 
                                                                         _ m a x _ w m   =   N o n e 
 
 
 
                                                                         #   W a t e r m a r k   c h e c k i n g   o n   t h e   t a r g e t   t a b l e   ( D e l t a ) 
 
                                                                         #   S k i p   P a t h ( )   c h e c k s   f o r   U R I s   t o   a v o i d   O S E r r o r   o n   W i n d o w s 
 
                                                                         _ t g t _ i s _ d e l t a _ d i r   =   F a l s e 
 
                                                                         i f   n o t   s e l f . _ i s _ u r i _ p a t h ( _ t g t ) : 
 
                                                                                 _ t g t _ d e l t a   =   P a t h ( _ t g t ) 
 
                                                                                 _ t g t _ i s _ d e l t a _ d i r   =   ( _ t g t _ d e l t a   /   " _ d e l t a _ l o g " ) . e x i s t s ( ) 
 
                                                                         e l s e : 
 
                                                                                 _ t g t _ i s _ d e l t a _ d i r   =   T r u e     #   A s s u m e   U R I   t a r g e t   i s   a c c e s s i b l e   i f   i t ' s   d e l t a 
 
 
 
                                                                         i f   _ t g t _ i s _ d e l t a _ d i r : 
 
                                                                                 t r y : 
 
                                                                                         #   U s e   D e l t a T a b l e        A r r o w        P o l a r s   t o   a v o i d 
 
                                                                                         #   d e l t a l a k e / p o l a r s   S c h e m a   i t e r a t i o n   b u g 
 
                                                                                         f r o m   d e l t a l a k e   i m p o r t   D e l t a T a b l e   a s   _ D T 
 
 
 
                                                                                         _ d t _ o p t s   =   ( 
 
                                                                                                 s e l f . _ g e t _ c l o u d _ s t o r a g e _ o p t i o n s ( s t r ( _ t g t ) ) 
 
                                                                                                 i f   s e l f . _ i s _ u r i _ p a t h ( s t r ( _ t g t ) ) 
 
                                                                                                 e l s e   N o n e 
 
                                                                                         ) 
 
                                                                                         _ d t _ t g t   =   _ D T ( s t r ( _ t g t ) ,   s t o r a g e _ o p t i o n s = _ d t _ o p t s ) 
 
                                                                                         _ t d f   =   p l . f r o m _ a r r o w ( _ d t _ t g t . t o _ p y a r r o w _ t a b l e ( ) ) 
 
                                                                                         i f   _ t g t _ c o l   i n   _ t d f . c o l u m n s : 
 
                                                                                                 _ m a x _ w m   =   _ t d f . s e l e c t ( p l . c o l ( _ t g t _ c o l ) . m a x ( ) ) . i t e m ( ) 
 
                                                                                 e x c e p t   E x c e p t i o n   a s   _ w m _ e r r : 
 
                                                                                         l o g g e r . d e b u g ( f " W a t e r m a r k   r e a d   f a i l e d   ( f u l l   l o a d ) :   { _ w m _ e r r } " ) 
 
                                                                         i f   _ m a x _ w m   i s   n o t   N o n e : 
 
                                                                                 l o g g e r . i n f o ( 
 
                                                                                         f " I n c r e m e n t a l   l o a d :   f i l t e r i n g   { _ s r c _ c o l }   >   { _ m a x _ w m ! r }   " 
 
                                                                                         f " ( t a r g e t   c o l u m n :   ' { _ t g t _ c o l } ' ) " 
 
                                                                                 ) 
 
                                                                                 _ w m _ f i l t e r _ e x p r   =   p l . c o l ( _ s r c _ c o l )   >   _ m a x _ w m 
 
                                                                         e l s e : 
 
                                                                                 l o g g e r . i n f o ( " I n c r e m e n t a l   l o a d :   f i r s t   r u n   o r   t a r g e t   e m p t y        l o a d i n g   a l l   r o w s . " ) 
 
 
 
                                                         #   U s e   D e l t a T a b l e        A r r o w        P o l a r s   t o   a v o i d 
 
                                                         #   d e l t a l a k e / p o l a r s   S c h e m a   i t e r a t i o n   b u g   a n d 
 
                                                         #   p a s s   c l o u d   s t o r a g e _ o p t i o n s   f o r   A D L S   a u t h . 
 
                                                         f r o m   d e l t a l a k e   i m p o r t   D e l t a T a b l e   a s   _ D T 
 
 
 
                                                         _ d t _ o p t s   =   s e l f . _ g e t _ c l o u d _ s t o r a g e _ o p t i o n s ( p a t h )   i f   s e l f . _ i s _ u r i _ p a t h ( p a t h )   e l s e   N o n e 
 
                                                         _ d t _ s r c   =   _ D T ( p a t h ,   s t o r a g e _ o p t i o n s = _ d t _ o p t s ) 
 
                                                         d f   =   p l . f r o m _ a r r o w ( _ d t _ s r c . t o _ p y a r r o w _ t a b l e ( ) ) 
 
 
 
                                                         #   A p p l y   w a t e r m a r k   f i l t e r   i m m e d i a t e l y   ( b e f o r e   f l a t t e n / r e n a m e ) 
 
                                                         i f   _ w m _ f i l t e r _ e x p r   i s   n o t   N o n e : 
 
                                                                 d f   =   d f . f i l t e r ( _ w m _ f i l t e r _ e x p r ) 
 
                                                                 i f   d f . i s _ e m p t y ( ) : 
 
                                                                         l o g g e r . i n f o ( 
 
                                                                                 " I n c r e m e n t a l   l o a d :   n o   n e w   r o w s   s i n c e   l a s t   r u n        " 
 
                                                                                 " p r o c e s s i n g   e m p t y   f r a m e   t o   p r e s e r v e   c o n t r a c t   s c h e m a . " 
 
                                                                         ) 
 
                                                                 #   C o n t i n u e   ( d o n ' t   r e t u r n   e a r l y ) :   t h e   e m p t y - b u t - s c h e m e d   d f 
 
                                                                 #   f l o w s   t h r o u g h   t r a n s f o r m s   s o   r e s u l t . g o o d   h a s   c o r r e c t   d t y p e s . 
 
 
 
                                                         #   C a p t u r e   a c t u a l   m a x   v a l u e   f r o m   s o u r c e   f o r   p i p e l i n e   l o g 
 
                                                         i f   s e l f . _ s o u r c e _ m a x _ m t i m e   i s   N o n e   a n d   n o t   d f . i s _ e m p t y ( ) : 
 
                                                                 _ s r c _ w m _ f i e l d   =   g e t a t t r ( _ s r c _ c f g ,   " w a t e r m a r k _ f i e l d " ,   N o n e ) 
 
                                                                 i f   n o t   _ s r c _ w m _ f i e l d : 
 
                                                                         _ l i n _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " l i n e a g e " ,   N o n e ) 
 
                                                                         _ w m _ b a s e   =   ( 
 
                                                                                 g e t a t t r ( _ l i n _ c f g ,   " t i m e s t a m p _ c o l u m n _ n a m e " ,   " _ l a k e l o g i c _ p r o c e s s e d _ a t " ) 
 
                                                                                 i f   _ l i n _ c f g 
 
                                                                                 e l s e   " _ l a k e l o g i c _ p r o c e s s e d _ a t " 
 
                                                                         ) 
 
                                                                         _ s r c _ w m _ f i e l d   =   g e t a t t r ( _ s r c _ c f g ,   " c d c _ t i m e s t a m p _ f i e l d " ,   _ w m _ b a s e ) 
 
 
 
                                                                 i f   _ s r c _ w m _ f i e l d   a n d   _ s r c _ w m _ f i e l d   i n   d f . c o l u m n s : 
 
                                                                         t r y : 
 
                                                                                 _ v a l   =   d f . s e l e c t ( p l . c o l ( _ s r c _ w m _ f i e l d ) . m a x ( ) ) . i t e m ( ) 
 
                                                                                 i f   _ v a l   i s   n o t   N o n e : 
 
                                                                                         f r o m   d a t e t i m e   i m p o r t   d a t e t i m e 
 
 
 
                                                                                         i f   i s i n s t a n c e ( _ v a l ,   d a t e t i m e ) : 
 
                                                                                                 s e l f . _ s o u r c e _ m a x _ m t i m e   =   _ v a l . t i m e s t a m p ( ) 
 
                                                                                         e l i f   i s i n s t a n c e ( _ v a l ,   s t r ) : 
 
                                                                                                 s e l f . _ s o u r c e _ m a x _ m t i m e   =   d a t e t i m e . f r o m i s o f o r m a t ( 
 
                                                                                                         _ v a l . r e p l a c e ( " Z " ,   " + 0 0 : 0 0 " ) 
 
                                                                                                 ) . t i m e s t a m p ( ) 
 
                                                                                         e l s e : 
 
                                                                                                 s e l f . _ s o u r c e _ m a x _ m t i m e   =   f l o a t ( _ v a l ) 
 
                                                                         e x c e p t   E x c e p t i o n : 
 
                                                                                 p a s s 
 
 
 
                                                 e l i f   _ s c a n n a b l e : 
 
                                                         f m t   =   ( 
 
                                                                 s e l f . c o n t r a c t . s o u r c e . f o r m a t . l o w e r ( ) 
 
                                                                 i f   g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " f o r m a t " ,   N o n e ) 
 
                                                                 e l s e   " " 
 
                                                         ) 
 
                                                         #   P o l a r s   n e e d s   e x p l i c i t   g l o b   i f   i t ' s   a   d i r e c t o r y 
 
                                                         _ s u p p o r t e d _ g l o b s   =   [ " c s v " ,   " p a r q u e t " ,   " j s o n " ,   " j s o n l " ,   " n d j s o n " ] 
 
                                                         i f   f m t   i n   _ s u p p o r t e d _ g l o b s   a n d   n o t   a n y ( c h r   i n   p a t h   f o r   c h r   i n   [ " * " ,   " ? " ,   " [ " ] )   a n d   n o t   p a t h . e n d s w i t h ( f " . { f m t } " ) : 
 
                                                                 p a t h   =   f " { p a t h . r s t r i p ( ' / ' ) } / * * / * . { f m t } " 
 
 
 
                                                         #   O n   W i n d o w s ,   P o l a r s '   i n t e r n a l   g l o b   m i s i n t e r p r e t s 
 
                                                         #   d r i v e - l e t t e r   c o l o n s   ( C : )   a s   U R I   s c h e m e s ,   p r o d u c i n g 
 
                                                         #   m a l f o r m e d   p a t h s   l i k e   " * . c s v : / / C : / . . . / d a t a . c s v " . 
 
                                                         #   P r e - e x p a n d   g l o b s   v i a   P y t h o n ' s   g l o b   m o d u l e   a n d   p a s s 
 
                                                         #   r e s o l v e d   f i l e   p a t h s   i n s t e a d . 
 
                                                         _ n e e d s _ p r e _ g l o b   =   n o t   s e l f . _ i s _ u r i _ p a t h ( p a t h )   a n d   a n y ( c h   i n   p a t h   f o r   c h   i n   [ " * " ,   " ? " ,   " [ " ] ) 
 
                                                         i f   _ n e e d s _ p r e _ g l o b : 
 
                                                                 f r o m   g l o b   i m p o r t   g l o b   a s   _ g l o b 
 
 
 
                                                                 _ r e s o l v e d   =   s o r t e d ( _ g l o b ( p a t h ,   r e c u r s i v e = T r u e ) ) 
 
                                                                 _ r e s o l v e d   =   [ f   f o r   f   i n   _ r e s o l v e d   i f   P a t h ( f ) . i s _ f i l e ( ) ] 
 
                                                                 i f   _ r e s o l v e d : 
 
                                                                         i f   p a t h . e n d s w i t h ( " . p a r q u e t " )   o r   f m t   = =   " p a r q u e t " : 
 
                                                                                 l f   =   p l . s c a n _ p a r q u e t ( _ r e s o l v e d   i f   l e n ( _ r e s o l v e d )   >   1   e l s e   _ r e s o l v e d [ 0 ] ) 
 
                                                                                 d f   =   l f . c o l l e c t ( ) 
 
                                                                         e l i f   p a t h . e n d s w i t h ( ( " . n d j s o n " ,   " . j s o n l " ) ) : 
 
                                                                                 l f   =   p l . c o n c a t ( 
 
                                                                                         [ p l . s c a n _ n d j s o n ( p )   f o r   p   i n   _ r e s o l v e d ] , 
 
                                                                                         h o w = " d i a g o n a l _ r e l a x e d " , 
 
                                                                                 ) 
 
                                                                                 d f   =   l f . c o l l e c t ( ) 
 
                                                                         e l s e : 
 
                                                                                 #   C S V        u s e   e a g e r   r e a d   f o r   l o c a l   p a t h s 
 
                                                                                 #   t o   b y p a s s   P o l a r s '   p a t h   c a n o n i c a l i s a t i o n 
 
                                                                                 #   t h a t   b r e a k s   o n   W i n d o w s   d r i v e   l e t t e r s . 
 
                                                                                 i f   n o t   s e l f . _ i s _ u r i _ p a t h ( p a t h ) : 
 
                                                                                         d f   =   p l . c o n c a t ( 
 
                                                                                                 [ p l . r e a d _ c s v ( p )   f o r   p   i n   _ r e s o l v e d ] , 
 
                                                                                                 h o w = " d i a g o n a l _ r e l a x e d " , 
 
                                                                                         ) 
 
                                                                                 e l s e : 
 
                                                                                         l f   =   p l . c o n c a t ( 
 
                                                                                                 [ p l . s c a n _ c s v ( p ,   g l o b = F a l s e )   f o r   p   i n   _ r e s o l v e d ] , 
 
                                                                                                 h o w = " d i a g o n a l _ r e l a x e d " , 
 
                                                                                         ) 
 
                                                                                         d f   =   l f . c o l l e c t ( ) 
 
                                                                 e l s e : 
 
                                                                         l o g g e r . w a r n i n g ( f " G l o b   p a t t e r n   m a t c h e d   0   f i l e s :   { p a t h } " ) 
 
                                                         e l s e : 
 
                                                                 i f   p a t h . e n d s w i t h ( " . p a r q u e t " )   o r   f m t   = =   " p a r q u e t " : 
 
                                                                         l f   =   p l . s c a n _ p a r q u e t ( p a t h ) 
 
                                                                         d f   =   l f . c o l l e c t ( ) 
 
                                                                 e l i f   p a t h . e n d s w i t h ( ( " . n d j s o n " ,   " . j s o n l " ) ) : 
 
                                                                         l f   =   p l . s c a n _ n d j s o n ( p a t h ) 
 
                                                                         d f   =   l f . c o l l e c t ( ) 
 
                                                                 e l s e : 
 
                                                                         i f   n o t   s e l f . _ i s _ u r i _ p a t h ( p a t h ) : 
 
                                                                                 d f   =   p l . r e a d _ c s v ( p a t h ) 
 
                                                                         e l s e : 
 
                                                                                 l f   =   p l . s c a n _ c s v ( p a t h ,   g l o b = F a l s e ) 
 
                                                                                 d f   =   l f . c o l l e c t ( ) 
 
                                                 e l s e : 
 
                                                         i m p o r t   j s o n   a s   _ j s o n 
 
 
 
                                                         i f   p a t h . e n d s w i t h ( " . j s o n " ) : 
 
                                                                 i f   s e l f . _ i s _ u r i _ p a t h ( p a t h ) : 
 
                                                                         #   C l o u d   U R I        r e a d   v i a   f s s p e c   ( p l . r e a d _ j s o n   d o e s n ' t   s u p p o r t   c l o u d   U R I s ) 
 
                                                                         i m p o r t   f s s p e c 
 
 
 
                                                                         _ s o p t s   =   s e l f . _ g e t _ c l o u d _ s t o r a g e _ o p t i o n s ( p a t h ) 
 
                                                                         w i t h   f s s p e c . o p e n ( p a t h ,   " r " ,   * * _ s o p t s )   a s   f : 
 
                                                                                 t e x t   =   f . r e a d ( ) 
 
                                                                         t r y : 
 
                                                                                 r a w   =   _ j s o n . l o a d s ( t e x t ) 
 
                                                                         e x c e p t   _ j s o n . J S O N D e c o d e E r r o r : 
 
                                                                                 #   N D J S O N :   o n e   J S O N   o b j e c t   p e r   l i n e 
 
                                                                                 r a w   =   [ _ j s o n . l o a d s ( l i n e )   f o r   l i n e   i n   t e x t . s t r i p ( ) . s p l i t l i n e s ( )   i f   l i n e . s t r i p ( ) ] 
 
                                                                         r o w s   =   [ r a w ]   i f   i s i n s t a n c e ( r a w ,   d i c t )   e l s e   ( r a w   i f   i s i n s t a n c e ( r a w ,   l i s t )   e l s e   [ ] ) 
 
                                                                         f l a t   =   [ 
 
                                                                                 { 
 
                                                                                         k :   ( 
 
                                                                                                 _ j s o n . d u m p s ( v ,   e n s u r e _ a s c i i = F a l s e )   i f   i s i n s t a n c e ( v ,   ( d i c t ,   l i s t ) )   e l s e   v 
 
                                                                                         ) 
 
                                                                                         f o r   k ,   v   i n   r . i t e m s ( ) 
 
                                                                                 } 
 
                                                                                 f o r   r   i n   r o w s 
 
                                                                         ] 
 
                                                                         d f   =   p l . D a t a F r a m e ( f l a t ) 
 
                                                                 e l s e : 
 
                                                                         r a w   =   _ j s o n . l o a d s ( P a t h ( p a t h ) . r e a d _ t e x t ( e n c o d i n g = " u t f - 8 " ) ) 
 
                                                                         r o w s   =   [ r a w ]   i f   i s i n s t a n c e ( r a w ,   d i c t )   e l s e   ( r a w   i f   i s i n s t a n c e ( r a w ,   l i s t )   e l s e   [ ] ) 
 
                                                                         f l a t   =   [ 
 
                                                                                 { 
 
                                                                                         k :   ( 
 
                                                                                                 _ j s o n . d u m p s ( v ,   e n s u r e _ a s c i i = F a l s e )   i f   i s i n s t a n c e ( v ,   ( d i c t ,   l i s t ) )   e l s e   v 
 
                                                                                         ) 
 
                                                                                         f o r   k ,   v   i n   r . i t e m s ( ) 
 
                                                                                 } 
 
                                                                                 f o r   r   i n   r o w s 
 
                                                                         ] 
 
                                                                         d f   =   p l . D a t a F r a m e ( f l a t ) 
 
                                                         e l i f   p a t h . e n d s w i t h ( " . x m l " ) : 
 
                                                                 d f   =   p l . r e a d _ x m l ( p a t h ) 
 
                                                         e l i f   p a t h . e n d s w i t h ( ( " . x l s x " ,   " . x l s " ) ) : 
 
                                                                 d f   =   p l . r e a d _ e x c e l ( p a t h ) 
 
                                                         e l s e : 
 
                                                                 d f   =   p l . r e a d _ c s v ( p a t h ) 
 
 
 
                         e l i f   s e l f . e n g i n e _ n a m e   = =   " s p a r k " : 
 
                                 f r o m   p y s p a r k . s q l   i m p o r t   S p a r k S e s s i o n 
 
 
 
                                 s p a r k   =   S p a r k S e s s i o n . b u i l d e r . g e t O r C r e a t e ( ) 
 
                                 #           F o r m a t   d e t e c t i o n                                                                                                                                 
 
                                 #   C h e c k   s o u r c e . f o r m a t   f i r s t ,   t h e n   m a t e r i a l i z a t i o n . f o r m a t , 
 
                                 #   t h e n   s e r v e r . f o r m a t ;   f a l l   b a c k   t o   f i l e   e x t e n s i o n   o r   p a r q u e t . 
 
                                 f m t   =   ( 
 
                                         ( g e t a t t r ( s e l f . c o n t r a c t . s o u r c e ,   " f o r m a t " ,   N o n e )   i f   g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e )   e l s e   N o n e ) 
 
                                         o r   ( 
 
                                                 g e t a t t r ( s e l f . c o n t r a c t . m a t e r i a l i z a t i o n ,   " f o r m a t " ,   N o n e ) 
 
                                                 i f   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) 
 
                                                 e l s e   N o n e 
 
                                         ) 
 
                                         o r   ( 
 
                                                 g e t a t t r ( s e l f . c o n t r a c t . s e r v e r ,   " f o r m a t " ,   N o n e ) 
 
                                                 i f   g e t a t t r ( s e l f . c o n t r a c t ,   " s e r v e r " ,   N o n e ) 
 
                                                 e l s e   N o n e 
 
                                         ) 
 
                                         o r   ( 
 
                                                 " j s o n " 
 
                                                 i f   p a t h . e n d s w i t h ( " . j s o n " ) 
 
                                                 e l s e   " c s v " 
 
                                                 i f   p a t h . e n d s w i t h ( " . c s v " ) 
 
                                                 e l s e   " e x c e l " 
 
                                                 i f   p a t h . e n d s w i t h ( ( " . x l s x " ,   " . x l s " ) ) 
 
                                                 e l s e   " p a r q u e t " 
 
                                         ) 
 
                                 ) . l o w e r ( ) 
 
 
 
                                 #   P r e - c a l c u l a t e   s o u r c e   o v e r r i d e s   f o r   i n c r e m e n t a l   l o a d s 
 
                                 _ s r c _ o v e r r i d e s   =   { } 
 
                                 _ s r c _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) 
 
                                 i f   ( 
 
                                         _ s r c _ c f g 
 
                                         a n d   g e t a t t r ( _ s r c _ c f g ,   " l o a d _ m o d e " ,   N o n e )   i n   ( " i n c r e m e n t a l " ,   " c d c " ) 
 
                                         a n d   n o t   o s . e n v i r o n . g e t ( " L A K E L O G I C _ S K I P _ I N C R E M E N T A L _ C H E C K " ) 
 
                                 ) : 
 
                                         _ w m _ s t r a t e g y   =   g e t a t t r ( _ s r c _ c f g ,   " w a t e r m a r k _ s t r a t e g y " ,   N o n e ) 
 
                                         _ i n f o   =   g e t a t t r ( s e l f . c o n t r a c t ,   " i n f o " ,   N o n e ) 
 
                                         _ m e t a   =   g e t a t t r ( s e l f . c o n t r a c t ,   " m e t a d a t a " ,   N o n e )   o r   { } 
 
                                         _ l o a d _ m o d e   =   g e t a t t r ( _ s r c _ c f g ,   " l o a d _ m o d e " ,   N o n e ) 
 
 
 
                                         #   C D C   t a b l e   s o u r c e s   s h o u l d   d e f a u l t   t o   p i p e l i n e _ l o g ,   n o t   m a x _ t a r g e t . 
 
                                         #   m a x _ t a r g e t   q u e r i e s   t h e   S I L V E R   t a b l e ' s   t i m e s t a m p s   w h i c h   d i f f e r   f r o m 
 
                                         #   t h e   B R O N Z E   t i m e s t a m p s   w e   n e e d   t o   c o m p a r e   a g a i n s t . 
 
                                         #   p i p e l i n e _ l o g   u s e s   m a x _ s o u r c e _ m t i m e   f r o m   _ r u n _ l o g s        t h e   c o r r e c t 
 
                                         #   u p s t r e a m   b o u n d a r y . 
 
                                         i f   _ l o a d _ m o d e   = =   " c d c "   a n d   _ w m _ s t r a t e g y   i s   N o n e : 
 
                                                 _ w m _ s t r a t e g y   =   " p i p e l i n e _ l o g " 
 
                                                 _ s r c _ o v e r r i d e s [ " w a t e r m a r k _ s t r a t e g y " ]   =   " p i p e l i n e _ l o g " 
 
 
 
                                         #   R e s o l v e   d a t a s e t   i d e n t i c a l l y   t o   h o w   r e p o r t   l o g g i n g   r e s o l v e s   i t 
 
                                         _ d a t a s e t   =   N o n e 
 
                                         i f   _ m a t   : =   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) : 
 
                                                 _ t p   =   g e t a t t r ( _ m a t ,   " t a r g e t _ p a t h " ,   " " )   o r   g e t a t t r ( _ m a t ,   " p a t h " ,   " " )   o r   " " 
 
                                                 i f   s t r ( _ t p ) . s t a r t s w i t h ( " t a b l e : " ) : 
 
                                                         _ t b l _ f u l l   =   s t r ( _ t p ) [ l e n ( " t a b l e : " )   : ] 
 
                                                         _ d a t a s e t   =   _ t b l _ f u l l . s p l i t ( " . " ) [ - 1 ]   i f   " . "   i n   _ t b l _ f u l l   e l s e   _ t b l _ f u l l 
 
 
 
                                         i f   n o t   _ d a t a s e t : 
 
                                                 _ d a t a s e t   =   ( 
 
                                                         _ m e t a . g e t ( " d a t a s e t " ) 
 
                                                         o r   g e t a t t r ( s e l f . c o n t r a c t ,   " d a t a s e t " ,   N o n e ) 
 
                                                         o r   ( g e t a t t r ( _ i n f o ,   " t i t l e " ,   N o n e )   i f   _ i n f o   e l s e   N o n e ) 
 
                                                 ) 
 
 
 
                                         _ d a t a _ l a y e r   =   _ m e t a . g e t ( " d a t a _ l a y e r " )   o r   ( g e t a t t r ( _ i n f o ,   " t a r g e t _ l a y e r " ,   N o n e )   i f   _ i n f o   e l s e   N o n e ) 
 
                                         _ d o m a i n   =   _ m e t a . g e t ( " d o m a i n " )   o r   ( g e t a t t r ( _ i n f o ,   " d o m a i n " ,   N o n e )   i f   _ i n f o   e l s e   N o n e ) 
 
                                         _ s y s t e m   =   _ m e t a . g e t ( " s y s t e m " )   o r   ( g e t a t t r ( _ i n f o ,   " s y s t e m " ,   N o n e )   i f   _ i n f o   e l s e   N o n e ) 
 
                                         _ l o g _ t a b l e   =   _ m e t a . g e t ( " r u n _ l o g _ t a b l e " ) 
 
 
 
                                         i f   _ d a t a s e t : 
 
                                                 _ s r c _ o v e r r i d e s [ " d a t a s e t " ]   =   _ d a t a s e t 
 
                                         i f   _ d a t a _ l a y e r : 
 
                                                 _ s r c _ o v e r r i d e s [ " d a t a _ l a y e r " ]   =   _ d a t a _ l a y e r 
 
                                         i f   _ d o m a i n : 
 
                                                 _ s r c _ o v e r r i d e s [ " d o m a i n " ]   =   _ d o m a i n 
 
                                         i f   _ s y s t e m : 
 
                                                 _ s r c _ o v e r r i d e s [ " s y s t e m " ]   =   _ s y s t e m 
 
                                         i f   _ l o g _ t a b l e : 
 
                                                 _ s r c _ o v e r r i d e s [ " p i p e l i n e _ l o g _ t a b l e " ]   =   _ l o g _ t a b l e 
 
 
 
                                         i f   _ w m _ s t r a t e g y   i n   ( N o n e ,   " m a x _ t a r g e t " ,   " d e l t a _ v e r s i o n " ) : 
 
                                                 i f   g e t a t t r ( _ s r c _ c f g ,   " t a r g e t _ p a t h " ,   N o n e )   i s   N o n e : 
 
                                                         _ m a t   =   g e t a t t r ( s e l f . c o n t r a c t ,   " m a t e r i a l i z a t i o n " ,   N o n e ) 
 
                                                         _ t a r g e t   =   g e t a t t r ( _ m a t ,   " t a r g e t " ,   N o n e )   i f   _ m a t   e l s e   N o n e 
 
                                                         i f   n o t   _ t a r g e t : 
 
                                                                 _ t b l   =   g e t a t t r ( _ i n f o ,   " t a b l e _ n a m e " ,   N o n e )   i f   _ i n f o   e l s e   N o n e 
 
                                                                 i f   _ t b l : 
 
                                                                         _ s r c _ p a t h   =   g e t a t t r ( _ s r c _ c f g ,   " p a t h " ,   " " )   o r   " " 
 
                                                                         _ c a t a l o g   =   _ s r c _ p a t h . s p l i t ( " . " ) [ 0 ]   i f   " . "   i n   _ s r c _ p a t h   e l s e   " " 
 
                                                                         _ t a r g e t   =   f " { _ c a t a l o g } . { _ t b l } "   i f   _ c a t a l o g   e l s e   _ t b l 
 
                                                         i f   _ t a r g e t : 
 
                                                                 _ s r c _ o v e r r i d e s [ " t a r g e t _ p a t h " ]   =   ( 
 
                                                                         _ t a r g e t   i f   _ t a r g e t . s t a r t s w i t h ( " t a b l e : " )   e l s e   f " t a b l e : { _ t a r g e t } " 
 
                                                                 ) 
 
 
 
                                 i f   d f   i s   N o n e : 
 
                                         i f   p a t h . s t a r t s w i t h ( " t a b l e : " ) : 
 
                                                 d f   =   s p a r k . t a b l e ( p a t h [ 6 : ] ) 
 
 
 
                                                 #           I n c r e m e n t a l   w a t e r m a r k   f o r   S p a r k   t a b l e   s o u r c e s                  
 
                                                 i f   _ s r c _ o v e r r i d e s   o r   g e t a t t r ( _ s r c _ c f g ,   " l o a d _ m o d e " ,   N o n e )   i n   ( " i n c r e m e n t a l " ,   " c d c " ) : 
 
                                                         f r o m   l a k e l o g i c . c o r e . i n c r e m e n t a l   i m p o r t   I n c r e m e n t a l B o u n d a r y 
 
 
 
                                                         t r y : 
 
                                                                 i f   _ s r c _ o v e r r i d e s : 
 
                                                                         b o u n d a r y   =   I n c r e m e n t a l B o u n d a r y . f r o m _ s o u r c e _ c o n f i g ( _ s r c _ c f g ,   * * _ s r c _ o v e r r i d e s ) 
 
                                                                 e l s e : 
 
                                                                         b o u n d a r y   =   I n c r e m e n t a l B o u n d a r y . f r o m _ s o u r c e _ c o n f i g ( _ s r c _ c f g ) 
 
 
 
                                                                 i f   b o u n d a r y . s t r a t e g y   = =   " d e l t a _ v e r s i o n " : 
 
                                                                         f v   =   b o u n d a r y . m e t a d a t a . g e t ( " f r o m _ v e r s i o n " ) 
 
                                                                         t v   =   b o u n d a r y . m e t a d a t a . g e t ( " t o _ v e r s i o n " ) 
 
                                                                         s k i p   =   b o u n d a r y . m e t a d a t a . g e t ( " s k i p _ s y n c " ,   F a l s e ) 
 
 
 
                                                                         #   S t o r e   m e t a d a t a   s o   m a t e r i a l i z a t i o n   c a n   w r i t e   b a c k   t h e   v e r s i o n 
 
                                                                         s e l f . _ i n c r e m e n t a l _ m e t a d a t a   =   b o u n d a r y . m e t a d a t a 
 
                                                                         t a b l e _ n a m e   =   p a t h [ 6 : ]   i f   p a t h . s t a r t s w i t h ( " t a b l e : " )   e l s e   p a t h 
 
 
 
                                                                         i f   s k i p : 
 
                                                                                 l o g g e r . i n f o ( 
 
                                                                                         f " I n c r e m e n t a l   l o a d   ( D e l t a   V e r s i o n s ) :   " 
 
                                                                                         f " S o u r c e   v e r s i o n   i s   u n c h a n g e d   ( { t v } ) .   S k i p p i n g   r e a d . " 
 
                                                                                 ) 
 
                                                                                 d f   =   s p a r k . t a b l e ( t a b l e _ n a m e ) . f i l t e r ( " 1   =   0 " ) 
 
                                                                         e l s e : 
 
                                                                                 l o g g e r . i n f o ( f " I n c r e m e n t a l   l o a d   ( D e l t a   V e r s i o n s ) :   { f v }   - >   { t v } " ) 
 
                                                                                 #   R e l o a d   w i t h   v e r s i o n   o p t i o n s 
 
                                                                                 d f   =   ( 
 
                                                                                         s p a r k . r e a d . f o r m a t ( " d e l t a " ) 
 
                                                                                         . o p t i o n ( " r e a d C h a n g e F e e d " ,   " t r u e " ) 
 
                                                                                         . o p t i o n ( " s t a r t i n g V e r s i o n " ,   f v ) 
 
                                                                                         . o p t i o n ( " e n d i n g V e r s i o n " ,   t v ) 
 
                                                                                         . t a b l e ( t a b l e _ n a m e ) 
 
                                                                                 ) 
 
                                                                 e l s e : 
 
                                                                         #   S t a n d a r d   w a t e r m a r k   f i l t e r 
 
                                                                         _ w m _ f i e l d   =   g e t a t t r ( _ s r c _ c f g ,   " w a t e r m a r k _ f i e l d " ,   N o n e ) 
 
                                                                         #   D e f a u l t   t o   t h e   l i n e a g e   t i m e s t a m p   w h e n   n o   w a t e r m a r k   i s   c o n f i g u r e d . 
 
                                                                         #   F o r   C D C   m o d e ,   p r e f e r   c d c _ t i m e s t a m p _ f i e l d   a s   t h e   w a t e r m a r k 
 
                                                                         #   s i n c e   i t ' s   t h e   u s e r ' s   d e c l a r e d   t i m e s t a m p   f o r   c h a n g e   t r a c k i n g . 
 
                                                                         #   B r o n z e   r e c o r d s   a r e   s t a m p e d   w i t h   t h e   l i n e a g e   t i m e s t a m p , 
 
                                                                         #   m a k i n g   i t   a   n a t u r a l   h i g h - w a t e r   m a r k   f o r   s i l v e r   r e a d s . 
 
                                                                         i f   n o t   _ w m _ f i e l d : 
 
                                                                                 _ l i n _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " l i n e a g e " ,   N o n e ) 
 
                                                                                 _ w m _ f i e l d   =   ( 
 
                                                                                         g e t a t t r ( _ l i n _ c f g ,   " t i m e s t a m p _ c o l u m n _ n a m e " ,   " _ l a k e l o g i c _ p r o c e s s e d _ a t " ) 
 
                                                                                         i f   _ l i n _ c f g 
 
                                                                                         e l s e   " _ l a k e l o g i c _ p r o c e s s e d _ a t " 
 
                                                                                 ) 
 
                                                                                 l o g g e r . d e b u g ( f " N o   w a t e r m a r k _ f i e l d   c o n f i g u r e d        d e f a u l t i n g   t o   ' { _ w m _ f i e l d } ' " ) 
 
                                                                         d f   =   d f . f i l t e r ( b o u n d a r y . s p a r k _ f i l t e r ( _ w m _ f i e l d ) ) 
 
                                                                         l o g g e r . i n f o ( 
 
                                                                                 f " I n c r e m e n t a l   l o a d   ( S p a r k ) :   a p p l i e d   " 
 
                                                                                 f " { b o u n d a r y . s t r a t e g y }   b o u n d a r y   o n   ' { _ w m _ f i e l d } ' " 
 
                                                                         ) 
 
                                                                         #   C o m p u t e   M a x   W a t e r m a r k   V a l u e   v i a   P y S p a r k   a g g r e g a t i o n   i f   p i p e l i n e _ l o g   s t r a t e g y 
 
                                                                         i f   b o u n d a r y . s t r a t e g y   = =   " p i p e l i n e _ l o g " : 
 
                                                                                 t r y : 
 
                                                                                         f r o m   p y s p a r k . s q l   i m p o r t   f u n c t i o n s   a s   F 
 
 
 
                                                                                         _ m a x _ r o w   =   d f . s e l e c t ( 
 
                                                                                                 F . m a x ( _ w m _ f i e l d ) . c a s t ( " s t r i n g " ) . a l i a s ( " m a x _ w m " ) 
 
                                                                                         ) . c o l l e c t ( ) [ 0 ] 
 
                                                                                         _ m a x _ v a l   =   _ m a x _ r o w [ " m a x _ w m " ] 
 
                                                                                         i f   _ m a x _ v a l : 
 
                                                                                                 b o u n d a r y . m e t a d a t a [ " m a x _ w a t e r m a r k _ v a l u e " ]   =   s t r ( _ m a x _ v a l ) 
 
                                                                                                 l o g g e r . d e b u g ( 
 
                                                                                                         f " C o m p u t e d   m a x _ w a t e r m a r k _ v a l u e = ' { _ m a x _ v a l } '   f o r   { _ w m _ f i e l d } " 
 
                                                                                                 ) 
 
                                                                                 e x c e p t   E x c e p t i o n   a s   w m _ e x c : 
 
                                                                                         l o g g e r . d e b u g ( f " F a i l e d   t o   c o m p u t e   m a x _ w a t e r m a r k _ v a l u e :   { w m _ e x c } " ) 
 
 
 
                                                                 s e l f . _ i n c r e m e n t a l _ m e t a d a t a   =   d i c t ( b o u n d a r y . m e t a d a t a ) 
 
                                                                 s e l f . _ i n c r e m e n t a l _ m e t a d a t a [ " s t r a t e g y " ]   =   b o u n d a r y . s t r a t e g y 
 
 
 
                                                                 i f   b o u n d a r y . s t r a t e g y   = =   " d e l t a _ v e r s i o n " : 
 
                                                                         _ t v   =   b o u n d a r y . m e t a d a t a . g e t ( " t o _ v e r s i o n " ) 
 
                                                                         i f   _ t v   i s   n o t   N o n e : 
 
                                                                                 s e l f . _ i n c r e m e n t a l _ m e t a d a t a [ " m a x _ w a t e r m a r k _ v a l u e " ]   =   s t r ( _ t v ) 
 
 
 
                                                                 #           C a p t u r e   m a x   s o u r c e   t i m e s t a m p   f o r   t a b l e   s o u r c e s         
 
                                                                 #   F o r   f i l e   s o u r c e s ,   _ s o u r c e _ m a x _ m t i m e   i s   s e t   f r o m   f i l e 
 
                                                                 #   m t i m e s .   F o r   t a b l e   s o u r c e s   w e   d e r i v e   i t   f r o m   t h e 
 
                                                                 #   u p s t r e a m   _ l a k e l o g i c _ p r o c e s s e d _ a t   s o   t h e   r u n _ l o g   e n t r y 
 
                                                                 #   r e f l e c t s   t h e   l a t e s t   c o n s u m e d   s o u r c e   r e c o r d . 
 
                                                                 i f   s e l f . _ s o u r c e _ m a x _ m t i m e   i s   N o n e : 
 
                                                                         t r y : 
 
                                                                                 f r o m   p y s p a r k . s q l   i m p o r t   f u n c t i o n s   a s   F 
 
 
 
                                                                                 _ t s _ c o l   =   " _ l a k e l o g i c _ p r o c e s s e d _ a t " 
 
                                                                                 i f   _ t s _ c o l   i n   d f . c o l u m n s : 
 
                                                                                         _ m a x _ t s   =   d f . s e l e c t ( 
 
                                                                                                 F . m a x ( F . u n i x _ t i m e s t a m p ( F . c o l ( _ t s _ c o l ) ) ) . a l i a s ( " m x " ) 
 
                                                                                         ) . c o l l e c t ( ) [ 0 ] [ " m x " ] 
 
                                                                                         i f   _ m a x _ t s   i s   n o t   N o n e : 
 
                                                                                                 s e l f . _ s o u r c e _ m a x _ m t i m e   =   f l o a t ( _ m a x _ t s ) 
 
                                                                                                 l o g g e r . d e b u g ( 
 
                                                                                                         f " C a p t u r e d   t a b l e   s o u r c e   m a x   m t i m e :   { s e l f . _ s o u r c e _ m a x _ m t i m e } " 
 
                                                                                                 ) 
 
                                                                         e x c e p t   E x c e p t i o n   a s   _ m t i m e _ e r r : 
 
                                                                                 l o g g e r . d e b u g ( f " F a i l e d   t o   c a p t u r e   t a b l e   s o u r c e   m t i m e :   { _ m t i m e _ e r r } " ) 
 
 
 
                                                         e x c e p t   E x c e p t i o n   a s   _ w m _ e r r : 
 
                                                                 l o g g e r . d e b u g ( 
 
                                                                         f " I n c r e m e n t a l   b o u n d a r y   r e s o l u t i o n   f a i l e d   ( f a l l i n g   b a c k   t o   f u l l ) :   { _ w m _ e r r } " 
 
                                                                 ) 
 
 
 
                                         e l s e : 
 
                                                 #   F i l e   p a t h   o r   e x p l i c i t   f o r m a t 
 
                                                 _ s r c _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) 
 
                                                 i f   ( 
 
                                                         _ s r c _ c f g 
 
                                                         a n d   g e t a t t r ( _ s r c _ c f g ,   " l o a d _ m o d e " ,   N o n e )   i n   ( " i n c r e m e n t a l " ,   " c d c " ) 
 
                                                         a n d   f m t   = =   " d e l t a " 
 
                                                 ) : 
 
                                                         f r o m   l a k e l o g i c . c o r e . i n c r e m e n t a l   i m p o r t   I n c r e m e n t a l B o u n d a r y 
 
 
 
                                                         t r y : 
 
                                                                 i f   _ s r c _ o v e r r i d e s : 
 
                                                                         b o u n d a r y   =   I n c r e m e n t a l B o u n d a r y . f r o m _ s o u r c e _ c o n f i g ( _ s r c _ c f g ,   * * _ s r c _ o v e r r i d e s ) 
 
                                                                 e l s e : 
 
                                                                         b o u n d a r y   =   I n c r e m e n t a l B o u n d a r y . f r o m _ s o u r c e _ c o n f i g ( _ s r c _ c f g ) 
 
                                                                 i f   b o u n d a r y . s t r a t e g y   = =   " d e l t a _ v e r s i o n " : 
 
                                                                         f v   =   b o u n d a r y . m e t a d a t a . g e t ( " f r o m _ v e r s i o n " ) 
 
                                                                         t v   =   b o u n d a r y . m e t a d a t a . g e t ( " t o _ v e r s i o n " ) 
 
                                                                         s k i p   =   b o u n d a r y . m e t a d a t a . g e t ( " s k i p _ s y n c " ,   F a l s e ) 
 
 
 
                                                                         i f   s k i p : 
 
                                                                                 l o g g e r . i n f o ( 
 
                                                                                         f " I n c r e m e n t a l   l o a d   ( D e l t a   V e r s i o n s ) :   " 
 
                                                                                         f " S o u r c e   v e r s i o n   i s   u n c h a n g e d   ( { t v } ) .   S k i p p i n g   r e a d . " 
 
                                                                                 ) 
 
                                                                                 d f   =   s p a r k . r e a d . f o r m a t ( " d e l t a " ) . l o a d ( p a t h ) . f i l t e r ( " 1   =   0 " ) 
 
                                                                         e l s e : 
 
                                                                                 l o g g e r . i n f o ( f " I n c r e m e n t a l   l o a d   ( D e l t a   V e r s i o n s ) :   { f v }   - >   { t v } " ) 
 
                                                                                 d f   =   ( 
 
                                                                                         s p a r k . r e a d . f o r m a t ( " d e l t a " ) 
 
                                                                                         . o p t i o n ( " r e a d C h a n g e F e e d " ,   " t r u e " ) 
 
                                                                                         . o p t i o n ( " s t a r t i n g V e r s i o n " ,   f v ) 
 
                                                                                         . o p t i o n ( " e n d i n g V e r s i o n " ,   t v ) 
 
                                                                                         . l o a d ( p a t h ) 
 
                                                                                 ) 
 
 
 
                                                                         s e l f . _ i n c r e m e n t a l _ m e t a d a t a   =   d i c t ( b o u n d a r y . m e t a d a t a ) 
 
                                                                         s e l f . _ i n c r e m e n t a l _ m e t a d a t a [ " s t r a t e g y " ]   =   b o u n d a r y . s t r a t e g y 
 
                                                                         i f   t v   i s   n o t   N o n e : 
 
                                                                                 s e l f . _ i n c r e m e n t a l _ m e t a d a t a [ " m a x _ w a t e r m a r k _ v a l u e " ]   =   s t r ( t v ) 
 
                                                                         #   I f   u s i n g   C D F ,   w e   d o n ' t   n e e d   a   n o r m a l   l o a d 
 
                                                                         r e t u r n   s e l f . r u n ( d f ,   s o u r c e _ p a t h = p a t h ,   r e s e t _ t r a c e = F a l s e ) 
 
                                                         e x c e p t   E x c e p t i o n   a s   _ c d f _ e r r : 
 
                                                                 l o g g e r . d e b u g ( f " D e l t a   C D F   r e a d   f a i l e d ,   f a l l i n g   b a c k   t o   s t a n d a r d   r e a d :   { _ c d f _ e r r } " ) 
 
 
 
                                                 r e a d e r   =   s p a r k . r e a d . f o r m a t ( f m t ) 
 
                                                 i f   f m t   = =   " c s v " : 
 
                                                         r e a d e r   =   r e a d e r . o p t i o n ( " h e a d e r " ,   " t r u e " ) 
 
                                                 e l i f   f m t   = =   " j s o n " : 
 
                                                         r e a d e r   =   r e a d e r . o p t i o n ( " m u l t i L i n e " ,   " t r u e " ) 
 
                                                 #   W h e n   r e a d i n g   a   d i r e c t o r y   ( n o   e x p l i c i t   f i l e   l i s t ) ,   e n a b l e 
 
                                                 #   r e c u r s i v e   s c a n n i n g   s o   S p a r k   f i n d s   f i l e s   i n   p a r t i t i o n 
 
                                                 #   s u b d i r e c t o r i e s   ( e . g .   y _ 2 0 2 6 / m _ 0 3 / d _ 2 1 / d a t a . c s v ) . 
 
                                                 i f   n o t   f i l e _ p a t h s : 
 
                                                         r e a d e r   =   r e a d e r . o p t i o n ( " r e c u r s i v e F i l e L o o k u p " ,   " t r u e " ) 
 
 
 
                                                 #           C o n t r a c t - d r i v e n   s c h e m a                                                                                       
 
                                                 #   B u i l d   a   S p a r k   S t r u c t T y p e   f r o m   t h e   c o n t r a c t ' s   m o d e l 
 
                                                 #   f i e l d s   s o   C S V / J S O N   r e a d s   d o n ' t   n e e d   t o   i n f e r   s c h e m a 
 
                                                 #   ( w h i c h   f a i l s   o n   e m p t y   d i r s   o r   V o l u m e s   F U S E   p a t h s ) . 
 
                                                 _ c o n t r a c t _ t y p e   =   t y p e ( s e l f . c o n t r a c t ) . _ _ n a m e _ _ 
 
                                                 l o g g e r . d e b u g ( 
 
                                                         f " S p a r k   r e a d :   f m t = { f m t } ,   " 
 
                                                         f " f i l e _ p a t h s = { l e n ( f i l e _ p a t h s )   i f   f i l e _ p a t h s   e l s e   ' N o n e ' } ,   " 
 
                                                         f " c o n t r a c t _ t y p e = { _ c o n t r a c t _ t y p e } " 
 
                                                 ) 
 
 
 
                                                 _ m o d e l   =   g e t a t t r ( s e l f . c o n t r a c t ,   " m o d e l " ,   N o n e ) 
 
                                                 _ f i e l d s _ l i s t   =   g e t a t t r ( _ m o d e l ,   " f i e l d s " ,   N o n e )   i f   _ m o d e l   e l s e   N o n e 
 
                                                 i f   n o t   _ f i e l d s _ l i s t   a n d   i s i n s t a n c e ( s e l f . c o n t r a c t ,   d i c t ) : 
 
                                                         _ f i e l d s _ l i s t   =   s e l f . c o n t r a c t . g e t ( " m o d e l " ,   { } ) . g e t ( " f i e l d s " ,   [ ] ) 
 
 
 
                                                 l o g g e r . d e b u g ( 
 
                                                         f " S p a r k   s c h e m a :   m o d e l = { _ m o d e l   i s   n o t   N o n e } ,   " 
 
                                                         f " f i e l d s _ l i s t = { l e n ( _ f i e l d s _ l i s t )   i f   _ f i e l d s _ l i s t   e l s e   ' N o n e ' } " 
 
                                                 ) 
 
 
 
                                                 i f   _ f i e l d s _ l i s t : 
 
                                                         f r o m   p y s p a r k . s q l . t y p e s   i m p o r t   ( 
 
                                                                 S t r u c t T y p e , 
 
                                                                 S t r u c t F i e l d , 
 
                                                                 S t r i n g T y p e , 
 
                                                                 L o n g T y p e , 
 
                                                                 I n t e g e r T y p e , 
 
                                                                 D o u b l e T y p e , 
 
                                                                 F l o a t T y p e , 
 
                                                                 B o o l e a n T y p e , 
 
                                                                 T i m e s t a m p T y p e , 
 
                                                                 D a t e T y p e , 
 
                                                         ) 
 
 
 
                                                         _ t y p e _ m a p   =   { 
 
                                                                 " s t r i n g " :   S t r i n g T y p e ( ) , 
 
                                                                 " l o n g " :   L o n g T y p e ( ) , 
 
                                                                 " b i g i n t " :   L o n g T y p e ( ) , 
 
                                                                 " i n t e g e r " :   I n t e g e r T y p e ( ) , 
 
                                                                 " i n t " :   I n t e g e r T y p e ( ) , 
 
                                                                 " d o u b l e " :   D o u b l e T y p e ( ) , 
 
                                                                 " f l o a t " :   F l o a t T y p e ( ) , 
 
                                                                 " b o o l e a n " :   B o o l e a n T y p e ( ) , 
 
                                                                 " b o o l " :   B o o l e a n T y p e ( ) , 
 
                                                                 " t i m e s t a m p " :   T i m e s t a m p T y p e ( ) , 
 
                                                                 " d a t e " :   D a t e T y p e ( ) , 
 
                                                         } 
 
                                                         s p a r k _ f i e l d s   =   [ ] 
 
                                                         f o r   f   i n   _ f i e l d s _ l i s t : 
 
                                                                 f n a m e   =   f . g e t ( " n a m e " )   i f   i s i n s t a n c e ( f ,   d i c t )   e l s e   g e t a t t r ( f ,   " n a m e " ,   N o n e ) 
 
                                                                 f t y p e   =   f . g e t ( " t y p e " ,   " s t r i n g " )   i f   i s i n s t a n c e ( f ,   d i c t )   e l s e   g e t a t t r ( f ,   " t y p e " ,   " s t r i n g " ) 
 
                                                                 i f   i s i n s t a n c e ( f ,   d i c t ) : 
 
                                                                         f r e q   =   f . g e t ( " r e q u i r e d " ,   F a l s e ) 
 
                                                                 e l s e : 
 
                                                                         f r e q   =   g e t a t t r ( f ,   " r e q u i r e d " ,   F a l s e ) 
 
                                                                 n u l l a b l e   =   n o t   f r e q 
 
                                                                 s p a r k _ t y p e   =   _ t y p e _ m a p . g e t ( ( f t y p e   o r   " s t r i n g " ) . l o w e r ( ) ,   S t r i n g T y p e ( ) ) 
 
                                                                 i f   f n a m e : 
 
                                                                         s p a r k _ f i e l d s . a p p e n d ( S t r u c t F i e l d ( f n a m e ,   s p a r k _ t y p e ,   n u l l a b l e ) ) 
 
                                                         i f   s p a r k _ f i e l d s : 
 
                                                                 s c h e m a   =   S t r u c t T y p e ( s p a r k _ f i e l d s ) 
 
                                                                 r e a d e r   =   r e a d e r . s c h e m a ( s c h e m a ) 
 
                                                                 l o g g e r . i n f o ( f "      A p p l i e d   c o n t r a c t   s c h e m a   ( { l e n ( s p a r k _ f i e l d s ) }   f i e l d s )   t o   S p a r k   r e a d e r " ) 
 
                                                         e l s e : 
 
                                                                 l o g g e r . w a r n i n g ( "      C o n t r a c t   m o d e l . f i e l d s   f o u n d   b u t   p r o d u c e d   0   S p a r k   f i e l d s " ) 
 
                                                 e l s e : 
 
                                                         l o g g e r . w a r n i n g ( "      N o   c o n t r a c t   m o d e l . f i e l d s   f o u n d        S p a r k   w i l l   i n f e r   s c h e m a " ) 
 
 
 
                                                 _ l o a d _ t a r g e t   =   f i l e _ p a t h s   i f   f i l e _ p a t h s   e l s e   p a t h 
 
                                                 i f   i s i n s t a n c e ( _ l o a d _ t a r g e t ,   l i s t ) : 
 
                                                         l o g g e r . i n f o ( f " S p a r k   l o a d i n g   { l e n ( _ l o a d _ t a r g e t ) }   f i l e ( s ) " ) 
 
                                                 e l s e : 
 
                                                         l o g g e r . i n f o ( f " S p a r k   l o a d i n g :   { _ l o a d _ t a r g e t } " ) 
 
                                                 d f   =   r e a d e r . l o a d ( _ l o a d _ t a r g e t ) 
 
 
 
                                                 #   C a p t u r e   p e r - r o w   f i l e   p a t h   f r o m   S p a r k ' s   h i d d e n 
 
                                                 #   _ m e t a d a t a   c o l u m n   b e f o r e   t r a n s f o r m a t i o n s   s t r i p   i t . 
 
                                                 #   T h i s   e n a b l e s   p e r - r o w   s o u r c e   t r a c e a b i l i t y   i n 
 
                                                 #   _ l a k e l o g i c _ s o u r c e   ( v s .   a   s i n g l e   d i r e c t o r y   p a t h ) . 
 
                                                 _ s o u r c e _ c a p t u r e d   =   F a l s e 
 
                                                 t r y : 
 
                                                         f r o m   p y s p a r k . s q l   i m p o r t   f u n c t i o n s   a s   F 
 
 
 
                                                         #   S t r a t e g y   1 :   _ m e t a d a t a . f i l e _ p a t h   ( f u l l   p a t h        p r e f e r r e d ) 
 
                                                         d f   =   d f . s e l e c t ( " * " ,   F . c o l ( " _ m e t a d a t a . f i l e _ p a t h " ) . a l i a s ( " _ s o u r c e _ f i l e " ) ) 
 
                                                         _ s o u r c e _ c a p t u r e d   =   T r u e 
 
                                                 e x c e p t   E x c e p t i o n : 
 
                                                         t r y : 
 
                                                                 f r o m   p y s p a r k . s q l   i m p o r t   f u n c t i o n s   a s   F 
 
 
 
                                                                 #   S t r a t e g y   2 :   _ m e t a d a t a . f i l e _ n a m e   ( j u s t   f i l e n a m e ) 
 
                                                                 d f   =   d f . s e l e c t ( " * " ,   F . c o l ( " _ m e t a d a t a . f i l e _ n a m e " ) . a l i a s ( " _ s o u r c e _ f i l e " ) ) 
 
                                                                 _ s o u r c e _ c a p t u r e d   =   T r u e 
 
                                                         e x c e p t   E x c e p t i o n : 
 
                                                                 t r y : 
 
                                                                         f r o m   p y s p a r k . s q l   i m p o r t   f u n c t i o n s   a s   F 
 
 
 
                                                                         #   S t r a t e g y   3 :   i n p u t _ f i l e _ n a m e ( )   ( w o r k s   f o r   n o n - U C   f i l e   r e a d s ) 
 
                                                                         i f n   =   F . i n p u t _ f i l e _ n a m e ( ) 
 
                                                                         d f   =   d f . w i t h C o l u m n ( 
 
                                                                                 " _ s o u r c e _ f i l e " ,   F . w h e n ( i f n   ! =   F . l i t ( " " ) ,   i f n ) . o t h e r w i s e ( F . l i t ( p a t h ) ) 
 
                                                                         ) 
 
                                                                         _ s o u r c e _ c a p t u r e d   =   T r u e 
 
                                                                 e x c e p t   E x c e p t i o n   a s   e x c : 
 
                                                                         l o g g e r . d e b u g ( f " i n p u t _ f i l e _ n a m e ( )   f a l l b a c k   f a i l e d :   { e x c } " ) 
 
                                                 i f   n o t   _ s o u r c e _ c a p t u r e d : 
 
                                                         l o g g e r . d e b u g ( 
 
                                                                 f " C o u l d   n o t   c a p t u r e   p e r - r o w   s o u r c e   f i l e   p a t h " 
 
                                                                 f "   f o r   { p a t h }        l i n e a g e   w i l l   u s e   d i r e c t o r y   p a t h " 
 
                                                         ) 
 
 
 
                         e l i f   s e l f . e n g i n e _ n a m e   i n   [ " s n o w f l a k e " ,   " b i g q u e r y " ] : 
 
                                 t a b l e _ n a m e   =   p a t h [ 6 : ]   i f   p a t h . s t a r t s w i t h ( " t a b l e : " )   e l s e   p a t h 
 
                                 r e t u r n   s e l f . r u n ( t a b l e _ n a m e ,   s o u r c e _ p a t h = t a b l e _ n a m e ,   r e s e t _ t r a c e = F a l s e ) 
 
 
 
                 i f   d f   i s   N o n e : 
 
                         r a i s e   V a l u e E r r o r ( f " C o u l d   n o t   l o a d   d a t a   f r o m   { p a t h }   u s i n g   e n g i n e   { s e l f . e n g i n e _ n a m e } " ) 
 
 
 
                 #           T a r g e t   R e p r o c e s s i n g   f i l t e r                                                                                                                              
 
                 #   A p p l y   s t r i c t   t a r g e t e d   r e p r o c e s s i n g   ( i f   s u p p l i e d ) .   W e   a p p l y   t h i s   h e r e 
 
                 #   s o   P o l a r s / S p a r k   l a z y   l o g i c   c a n   p u s h   t h e   p r e d i c a t e   a l l   t h e   w a y   d o w n . 
 
                 #   U s e s   r e s o l v e d _ r e p r o c e s s _ c o l u m n   w h i c h   m a y   c o m e   f r o m   t h e   c o n t r a c t ' s 
 
                 #   m a t e r i a l i z a t i o n . r e p r o c e s s _ c o l u m n   o r   t h e   g l o b a l   p i p e l i n e   p a r a m e t e r . 
 
                 i f   r e s o l v e d _ r e p r o c e s s _ c o l u m n   a n d   r e p r o c e s s _ v a l u e s : 
 
                         i f   s e l f . e n g i n e _ n a m e   = =   " p o l a r s " : 
 
                                 i m p o r t   p o l a r s   a s   p l 
 
 
 
                                 #   C a s t   t h e   a r r a y   v a l u e s   t o   U t f 8   t o   s a f e l y   m a t c h 
 
                                 s t r i n g _ v a l s   =   [ s t r ( x )   f o r   x   i n   r e p r o c e s s _ v a l u e s ] 
 
                                 d f   =   d f . f i l t e r ( p l . c o l ( r e s o l v e d _ r e p r o c e s s _ c o l u m n ) . c a s t ( p l . U t f 8 ) . i s _ i n ( s t r i n g _ v a l s ) ) 
 
                         e l i f   s e l f . e n g i n e _ n a m e   = =   " s p a r k " : 
 
                                 f r o m   p y s p a r k . s q l . f u n c t i o n s   i m p o r t   c o l 
 
 
 
                                 s t r i n g _ v a l s   =   [ s t r ( x )   f o r   x   i n   r e p r o c e s s _ v a l u e s ] 
 
                                 d f   =   d f . f i l t e r ( c o l ( r e s o l v e d _ r e p r o c e s s _ c o l u m n ) . c a s t ( " s t r i n g " ) . i s i n ( s t r i n g _ v a l s ) ) 
 
                         e l i f   s e l f . e n g i n e _ n a m e   = =   " p a n d a s " : 
 
                                 #   P a n d a s   i s   e a g e r ,   b u t   w e   f i l t e r   t o   s a v e   r a m   o n   t r a n s f o r m 
 
                                 s t r i n g _ v a l s   =   [ s t r ( x )   f o r   x   i n   r e p r o c e s s _ v a l u e s ] 
 
                                 d f   =   d f [ d f [ r e s o l v e d _ r e p r o c e s s _ c o l u m n ] . a s t y p e ( s t r ) . i s i n ( s t r i n g _ v a l s ) ] 
 
                         e l i f   s e l f . e n g i n e _ n a m e   = =   " d u c k d b " : 
 
                                 i m p o r t   d u c k d b 
 
 
 
                                 #   N e e d   t o   r u n   a   q u e r y   t o   e n f o r c e   i t 
 
                                 s t r i n g _ v a l s   =   " ,   " . j o i n ( [ f " ' { s t r ( x ) . r e p l a c e ( c h r ( 3 9 ) ,   c h r ( 3 9 )   *   2 ) } ' "   f o r   x   i n   r e p r o c e s s _ v a l u e s ] ) 
 
                                 q u e r y   =   f " S E L E C T   *   F R O M   d f   W H E R E   { r e s o l v e d _ r e p r o c e s s _ c o l u m n }   I N   ( { s t r i n g _ v a l s } ) " 
 
                                 d f   =   d u c k d b . s q l ( q u e r y ) . f e t c h d f ( ) 
 
 
 
                         l o g g e r . i n f o ( f " T a r g e t e d   r e p r o c e s s i n g   f i l t e r   p u s h d o w n   a p p l i e d   o n   { r e s o l v e d _ r e p r o c e s s _ c o l u m n } " ) 
 
 
 
                 #           J S O N - s t r i n g   f l a t t e n i n g   ( s o u r c e . f l a t t e n _ n e s t e d )                                                               
 
                 #   W h e n   a   b r o n z e   t a b l e   s t o r e s   n e s t e d   o b j e c t s   a s   J S O N   s t r i n g s   w e   n e e d   t o 
 
                 #   e x p a n d   t h e m   i n t o   f l a t   p a r e n t _ c h i l d   c o l u m n s   s o   t h e y   m a t c h   t h e   s i l v e r 
 
                 #   s c h e m a   b e f o r e   v a l i d a t i o n   r u n s . 
 
                 f l a t t e n _ n e s t e d   =   g e t a t t r ( g e t a t t r ( s e l f . c o n t r a c t ,   " s o u r c e " ,   N o n e ) ,   " f l a t t e n _ n e s t e d " ,   F a l s e ) 
 
                 i f   f l a t t e n _ n e s t e d : 
 
                         d f   =   s e l f . _ f l a t t e n _ j s o n _ d f ( d f ,   f l a t t e n _ n e s t e d ) 
 
 
 
                 #           D a t e - r a n g e   r e p r o c e s s i n g   f i l t e r                                                                                                               
 
                 #   W h e n   r e p r o c e s s _ f r o m   /   r e p r o c e s s _ t o   a r e   s e t ,   f i l t e r   t h e   l o a d e d   D a t a F r a m e 
 
                 #   t o   o n l y   i n c l u d e   r o w s   w i t h i n   t h e   r e q u e s t e d   d a t e   r a n g e .     T h e   d a t e   c o l u m n 
 
                 #   i s   r e s o l v e d   f r o m   m a t e r i a l i z a t i o n . r e p r o c e s s _ d a t e _ c o l u m n   ( e x p l i c i t )   o r 
 
                 #   t h e   f i r s t   p a r t i t i o n _ b y   c o l u m n   ( i m p l i c i t   f a l l b a c k ) . 
 
                 i f   _ i s _ r e p r o c e s s : 
 
                         d f   =   s e l f . _ a p p l y _ r e p r o c e s s _ d a t e _ f i l t e r ( d f ,   r e p r o c e s s _ f r o m ,   r e p r o c e s s _ t o ) 
 
 
 
                 #           P r e - v a l i d a t i o n   u p s t r e a m   r e n a m e   ( l i n e a g e . p r e s e r v e _ u p s t r e a m )                           
 
                 #   S c h e m a   v a l i d a t i o n   r u n s   i n s i d e   s e l f . r u n ( )   B E F O R E   i n j e c t _ l i n e a g e   i s 
 
                 #   c a l l e d .     I f   t h e   c o n t r a c t   d e c l a r e s   p r e s e r v e _ u p s t r e a m ,   t h e   u p s t r e a m 
 
                 #   c o l u m n s   m u s t   b e   r e n a m e d   N O W   ( b e f o r e   v a l i d a t i o n )   s o   t h e   s c h e m a   c h e c k e r 
 
                 #   s e e s   t h e   c o r r e c t   _ u p s t r e a m _ l a k e l o g i c _ *   n a m e s   r a t h e r   t h a n   m a r k i n g   t h e m 
 
                 #   a s   " m i s s i n g "   a n d   t h e   r a w   _ l a k e l o g i c _ *   a s   " u n k n o w n " . 
 
                 # 
 
                 #   i n j e c t _ l i n e a g e ' s   o w n   _ p r e s e r v e _ u p s t r e a m _ l i n e a g e   c a l l   i s   t h e n   a   n o - o p 
 
                 #   ( d e s t i n a t i o n   a l r e a d y   e x i s t s )   a n d   j u s t   s t a m p s   f r e s h   _ l a k e l o g i c _ *   v a l u e s . 
 
                 l i n e a g e _ c f g   =   g e t a t t r ( s e l f . c o n t r a c t ,   " l i n e a g e " ,   N o n e ) 
 
                 i f   l i n e a g e _ c f g   a n d   g e t a t t r ( l i n e a g e _ c f g ,   " e n a b l e d " ,   F a l s e ) : 
 
                         p r e s e r v e _ c o l s   =   l i s t ( g e t a t t r ( l i n e a g e _ c f g ,   " p r e s e r v e _ u p s t r e a m " ,   [ ] )   o r   [ ] ) 
 
                         i f   p r e s e r v e _ c o l s : 
 
                                 f r o m   l a k e l o g i c . c o r e . l i n e a g e   i m p o r t   _ p r e s e r v e _ u p s t r e a m _ l i n e a g e 
 
 
 
                                 p r e f i x   =   g e t a t t r ( l i n e a g e _ c f g ,   " u p s t r e a m _ p r e f i x " ,   " _ u p s t r e a m " )   o r   " _ u p s t r e a m " 
 
                                 d f   =   _ p r e s e r v e _ u p s t r e a m _ l i n e a g e ( d f ,   p r e s e r v e _ c o l s ,   p r e f i x ,   s e l f . e n g i n e _ n a m e ) 
 
 
 
                 r e t u r n   s e l f . r u n ( d f ,   s o u r c e _ p a t h = p a t h ,   r e s e t _ t r a c e = F a l s e ) 
 
 
 
 