11const segmentCommon = require ( "./common" ) ;
2- const crossdb = require ( "./crossdb" ) ;
2+ const sql = require ( "@dataform/sql" ) ( ) ;
33
44module . exports = ( params ) => {
55
@@ -24,23 +24,27 @@ ${params.includePages ?
2424select distinct
2525 session_id,
2626 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
27- ( [ key , value ] ) => `${ crossdb . windowFunction ( {
28- func : "first_value" ,
29- value : value ,
30- ignore_nulls : false ,
31- partition_fields : "session_id" ,
32- order_fields : 'sessionized_pages.timestamp asc' ,
33- frame_clause : "rows between unbounded preceding and unbounded following" ,
34- } ) } as first_${ value } `) . join ( ",\n " ) } ,
27+ ( [ key , value ] ) => `${ sql . windowFunction (
28+ "first_value" ,
29+ value ,
30+ false ,
31+ {
32+ partitionFields : [ "session_id" ] ,
33+ orderFields : [ 'sessionized_pages.timestamp asc' ] ,
34+ frameClause : "rows between unbounded preceding and unbounded following"
35+ }
36+ ) } as first_${ value } `) . join ( ",\n " ) } ,
3537 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
36- ( [ key , value ] ) => `${ crossdb . windowFunction ( {
37- func : "last_value" ,
38- value : value ,
39- ignore_nulls : false ,
40- partition_fields : "session_id" ,
41- order_fields : 'sessionized_pages.timestamp asc' ,
42- frame_clause : "rows between unbounded preceding and unbounded following" ,
43- } ) } as last_${ value } `) . join ( ",\n " ) }
38+ ( [ key , value ] ) => `${ sql . windowFunction (
39+ "last_value" ,
40+ value ,
41+ false ,
42+ {
43+ partitionFields : [ "session_id" ] ,
44+ orderFields : [ 'sessionized_pages.timestamp asc' ] ,
45+ frameClause : "rows between unbounded preceding and unbounded following"
46+ }
47+ ) } as last_${ value } `) . join ( ",\n " ) }
4448 from
4549 ${ ctx . ref ( params . defaultConfig . schema , "segment_sessionized_pages" ) } as sessionized_pages
4650 ),` : `` }
@@ -50,23 +54,27 @@ ${params.includeScreens ?
5054select distinct
5155 session_id,
5256 ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
53- ( [ key , value ] ) => `${ crossdb . windowFunction ( {
54- func : "first_value" ,
55- value : value ,
56- ignore_nulls : false ,
57- partition_fields : "session_id" ,
58- order_fields : 'sessionized_screens.timestamp asc' ,
59- frame_clause : "rows between unbounded preceding and unbounded following" ,
60- } ) } as first_${ value } `) . join ( ",\n " ) } ,
57+ ( [ key , value ] ) => `${ sql . windowFunction (
58+ "first_value" ,
59+ value ,
60+ false ,
61+ {
62+ partitionFields : [ "session_id" ] ,
63+ orderFields : [ 'sessionized_screens.timestamp asc' ] ,
64+ frameClause : "rows between unbounded preceding and unbounded following"
65+ }
66+ ) } as first_${ value } `) . join ( ",\n " ) } ,
6167 ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
62- ( [ key , value ] ) => `${ crossdb . windowFunction ( {
63- func : "last_value" ,
64- value : value ,
65- ignore_nulls : false ,
66- partition_fields : "session_id" ,
67- order_fields : 'sessionized_screens.timestamp asc' ,
68- frame_clause : "rows between unbounded preceding and unbounded following" ,
69- } ) } as last_${ value } `) . join ( ",\n " ) }
68+ ( [ key , value ] ) => `${ sql . windowFunction (
69+ "last_value" ,
70+ value ,
71+ false ,
72+ {
73+ partitionFields : [ "session_id" ] ,
74+ orderFields : [ 'sessionized_screens.timestamp asc' ] ,
75+ frameClause : "rows between unbounded preceding and unbounded following"
76+ }
77+ ) } as last_${ value } `) . join ( ",\n " ) }
7078 from
7179 ${ ctx . ref ( params . defaultConfig . schema , "segment_sessionized_screens" ) } as sessionized_screens
7280 ),` : `` }
8492 ${ ctx . when ( global . dataform . projectConfig . warehouse == "bigquery" , `struct(\n ` ) }
8593 ${ segmentCommon . enabledEvents ( params ) . map ( ( event ) =>
8694 `count(segment_sessionized_events.${ event } _id) as total_${ event } s` ) . join ( `,\n ` ) } ,
87- ${ crossdb . timestampDiff ( "millisecond" , "min(segment_sessionized_events.timestamp)" , "max(segment_sessionized_events.timestamp)" ) } as duration_millis
95+ ${ sql . timestamps . diff ( "millisecond" , "min(segment_sessionized_events.timestamp)" , "max(segment_sessionized_events.timestamp)" ) } as duration_millis
8896 ${ ctx . when ( global . dataform . projectConfig . warehouse == "bigquery" , `) as stats` ) }
8997
9098 -- first values in the session for page fields
@@ -168,4 +176,4 @@ group by
168176 )
169177
170178select * from output` )
171- }
179+ }
0 commit comments