(
(let $1 (DataType 'Int32))
(let $2 (OptionalType $1))
(let $3 '('"user" (OptionalType (DataType 'String))))
(let $4 '('('"_logical_id" '3840) '('"_id" '"7f5519bd-8a29c2bd-f7ed9931-9bc59258") '('"_wide_channels" (StructType '('"ts" $2) $3))))
(let $5 (DqPhyStage '() (lambda '() (block '(
  (let $15 '@@["StructType";[["user";["OptionalType";["DataType";"String"]]];["ts";["OptionalType";["DataType";"Int32"]]];["payload";["DataType";"Int32"]]]]@@)
  (let $16 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $15)))
  (let $17 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $18 (YtStat '('Id '"Input") '('RecordsCount '"18") '('DataSize '"656") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $19 (YtTable '"Input" $16 $17 $18 '() (Void) (Void) '"/Root/plato"))
  (let $20 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $19 '('"ts" '"user") (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $20 '())))
))) $4))
(let $6 (DqCnHashShuffle (TDqOutput $5 '0) '('1) '0 '"HashV2"))
(let $7 (StructType '('session (ListType $1)) '('"session_len" (DataType 'Uint64)) '('session_start $2) '('"session_start1" $2) '('"session_start2" $1) $3))
(let $8 '('('"_logical_id" '4148) '('"_id" '"bd94eb89-7fff438c-70f0f96e-6fe54913") '('"_wide_channels" $7)))
(let $9 (DqPhyStage '($6) (lambda '($21) (block '(
  (let $22 (NarrowSqueezeToDict (ToFlow $21) (lambda '($30 $31) $31) (lambda '($32 $33) (AsStruct '('"ts" $32) '('"user" $33))) '('Auto 'Many)))
  (let $23 (Bool 'true))
  (let $24 (Chain1Map (OrderedFlatMap $22 (lambda '($34) (OrderedFlatMap (DictPayloads $34) (lambda '($35) (Sort $35 $23 (lambda '($36) (Member $36 '"ts"))))))) (lambda '($37) (block '(
    (let $38 (Member $37 '"ts"))
    (return '($37 $38 $38 (Member $37 '"user")))
  ))) (lambda '($39 $40) (block '(
    (let $41 (Member $39 '"user"))
    (let $42 (Member $39 '"ts"))
    (let $43 '($39 $42 $42 $41))
    (let $44 (Nth $40 '2))
    (let $45 (If (Coalesce (< $44 $42) $23) (- $42 $44) (- $44 $42)))
    (let $46 (If (And (AggrNotEquals $42 $44) (Coalesce (< (Int32 '"10") $45) $23)) $42 (Nth $40 '1)))
    (let $47 '($39 $46 $42 $41))
    (return (If (AggrNotEquals $41 (Member (Nth $40 '0) '"user")) $43 $47))
  )))))
  (let $25 (Int32 '"100500"))
  (let $26 '"_yql_group_session_start")
  (let $27 (Condense1 $24 (lambda '($48) (block '(
    (let $49 (Nth $48 '0))
    (return (AsStruct '('_yql_agg_0 (AsList (Coalesce (Member $49 '"ts") $25))) '('_yql_agg_1 (Uint64 '1)) '($26 (Nth $48 '1)) '('"user" (Member $49 '"user"))))
  ))) (lambda '($50 $51) (Or (AggrNotEquals (Member (Nth $50 '0) '"user") (Member $51 '"user")) (AggrNotEquals (Nth $50 '1) (Member $51 $26)))) (lambda '($52 $53) (AsStruct '('_yql_agg_0 (Insert (Member $53 '_yql_agg_0) (Coalesce (Member (Nth $52 '0) '"ts") $25))) '('_yql_agg_1 (Inc (Member $53 '_yql_agg_1))) '($26 (Member $53 $26)) '('"user" (Member $53 '"user"))))))
  (let $28 (Sort (Map $27 (lambda '($54) (block '(
    (let $55 (Sort (Member $54 '_yql_agg_0) $23 (lambda '($57) $57)))
    (let $56 (Member $54 $26))
    (return (AsStruct '('session $55) '('"session_len" (Member $54 '_yql_agg_1)) '('session_start $56) '('"session_start1" $56) '('"session_start2" (Coalesce $56 $25)) '('"user" (Member $54 '"user"))))
  )))) '($23 $23) (lambda '($58) '((Member $58 '"user") (Member $58 'session_start)))))
  (let $29 (lambda '($59) (Member $59 'session) (Member $59 '"session_len") (Member $59 'session_start) (Member $59 '"session_start1") (Member $59 '"session_start2") (Member $59 '"user")))
  (return (FromFlow (ExpandMap $28 $29)))
))) $8))
(let $10 (DqCnMerge (TDqOutput $9 '0) '('('"5" '"Asc") '('2 '"Asc"))))
(let $11 (DqPhyStage '($10) (lambda '($60) (FromFlow (NarrowMap (ToFlow $60) (lambda '($61 $62 $63 $64 $65 $66) (AsStruct '('session $61) '('"session_len" $62) '('session_start $63) '('"session_start1" $64) '('"session_start2" $65) '('"user" $66)))))) '('('"_logical_id" '4160) '('"_id" '"1c2025b2-a6209bd2-756911a4-14d23be4"))))
(let $12 '($5 $9 $11))
(let $13 '('"user" 'session_start '"session_start1" '"session_start2" 'session '"session_len"))
(let $14 (DqCnResult (TDqOutput $11 '0) $13))
(return (KqpPhysicalQuery '((KqpPhysicalTx $12 '($14) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $7) '0 '0)) '('('"type" '"query"))))
)
