(
(let $1 (DataType 'Int32))
(let $2 (OptionalType $1))
(let $3 '('"ts" $2))
(let $4 '('"user" (OptionalType (DataType 'String))))
(let $5 '('('"_logical_id" '8139) '('"_id" '"e903fa8d-baeb3c19-ab7f5dba-290bf641") '('"_wide_channels" (StructType $3 $4))))
(let $6 (DqPhyStage '() (lambda '() (block '(
  (let $19 '@@["StructType";[["user";["OptionalType";["DataType";"String"]]];["ts";["OptionalType";["DataType";"Int32"]]];["payload";["DataType";"Int32"]]]]@@)
  (let $20 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $19)))
  (let $21 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $22 (YtStat '('Id '"Input") '('RecordsCount '"18") '('DataSize '"655") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $23 (YtTable '"Input" $20 $21 $22 '() (Void) (Void) '"/Root/plato"))
  (let $24 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $23 '('"ts" '"user") (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $24 '())))
))) $5))
(let $7 (DqCnHashShuffle (TDqOutput $6 '0) '('1) '0 '"HashV2"))
(let $8 (ListType $1))
(let $9 (DataType 'Uint64))
(let $10 (StructType '('"session_len" $9) '('"ss" $2) '('"ss1" $2) $3 '('"ts_session" $8) $4))
(let $11 '('('"_logical_id" '8638) '('"_id" '"d2a45cf4-b65da5eb-32d89c13-331d413d") '('"_wide_channels" $10)))
(let $12 (DqPhyStage '($7) (lambda '($25) (block '(
  (let $26 (NarrowSqueezeToDict (ToFlow $25) (lambda '($35 $36) $36) (lambda '($37 $38) (AsStruct '('"ts" $37) '('"user" $38))) '('Auto 'Many)))
  (let $27 (Bool 'true))
  (let $28 (Chain1Map (OrderedFlatMap $26 (lambda '($39) (OrderedFlatMap (DictPayloads $39) (lambda '($40) (Sort $40 $27 (lambda '($41) (Member $41 '"ts"))))))) (lambda '($42) (block '(
    (let $43 (Member $42 '"ts"))
    (return '($42 $43 $43 (Member $42 '"user")))
  ))) (lambda '($44 $45) (block '(
    (let $46 (Member $44 '"user"))
    (let $47 (Member $44 '"ts"))
    (let $48 '($44 $47 $47 $46))
    (let $49 (Nth $45 '2))
    (let $50 (If (Coalesce (< $49 $47) $27) (- $47 $49) (- $49 $47)))
    (let $51 (If (And (AggrNotEquals $47 $49) (Coalesce (< (Int32 '"10") $50) $27)) $47 (Nth $45 '1)))
    (let $52 '($44 $51 $47 $46))
    (return (If (AggrNotEquals $46 (Member (Nth $45 '0) '"user")) $48 $52))
  )))))
  (let $29 '"_yql_window_session_params")
  (let $30 '"_yql_window_session_start")
  (let $31 (Chopper (OrderedMap $28 (lambda '($53) (block '(
    (let $54 (Nth $53 '1))
    (let $55 (Nth $53 '0))
    (return (AsStruct '($29 (AsStruct '('start $54) '('state (Nth $53 '2)))) '($30 $54) '('"ts" (Member $55 '"ts")) '('"user" (Member $55 '"user"))))
  )))) (lambda '($56) '((Member $56 '"user") (Member $56 $30))) (lambda '($57 $58) (Or (AggrNotEquals (Nth $57 '0) (Member $58 '"user")) (AggrNotEquals (Nth $57 '1) (Member $58 $30)))) (lambda '($59 $60) (block '(
    (let $61 (Chain1Map $60 (lambda '($66) (block '(
      (let $67 (Member $66 '"ts"))
      (let $68 '('AggregateList0 (ToList $67)))
      (let $69 '('Count0 (Uint64 '1)))
      (let $70 (AsStruct $68 $69 '($29 (Member $66 $29)) '($30 (Member $66 $30)) '('"ts" $67) '('"user" (Member $66 '"user"))))
      (return '($70 (AsStruct $68 $69)))
    ))) (lambda '($71 $72) (block '(
      (let $73 (Member $71 '"ts"))
      (let $74 (Nth $72 '1))
      (let $75 (Member $74 'AggregateList0))
      (let $76 (IfPresent $73 (lambda '($80) (Insert $75 $80)) $75))
      (let $77 '('AggregateList0 $76))
      (let $78 '('Count0 (Inc (Member $74 'Count0))))
      (let $79 (AsStruct $77 $78 '($29 (Member $71 $29)) '($30 (Member $71 $30)) '('"ts" $73) '('"user" (Member $71 '"user"))))
      (return '($79 (AsStruct $77 $78)))
    )))))
    (let $62 (StructType '('AggregateList0 $8) '('Count0 $9) '($29 (StructType '('start $2) '('state $2))) '($30 $2) $3 $4))
    (let $63 (ListType $62))
    (let $64 (VariantType (StructType '('group $63) '('singleRow $62))))
    (let $65 (Condense1 $61 (lambda '($81) (block '(
      (let $82 (Nth $81 '0))
      (let $83 (Variant $82 'singleRow $64))
      (return '((Member $82 '"ts") $83))
    ))) (lambda '($84 $85) (AggrNotEquals (Member (Nth $84 '0) '"ts") (Nth $85 '0))) (lambda '($86 $87) (block '(
      (let $88 (Nth $86 '0))
      (let $89 (Visit (Nth $87 '1) 'group (lambda '($90) (Variant (Insert $90 $88) 'group $64)) 'singleRow (lambda '($91) (Variant (AsList $91 $88) 'group $64))))
      (return '((Nth $87 '0) $89))
    )))))
    (return (FlatMap $65 (lambda '($92) (Visit (Nth $92 '1) 'group (lambda '($93) (block '(
      (let $94 (IfPresent (Last $93) (lambda '($95) (OrderedMap (Collect (Map $93 (lambda '($96) (AsStruct '($29 (Member $96 $29)) '($30 (Member $96 $30)) '('"ts" (Member $96 '"ts")) '('"user" (Member $96 '"user")))))) (lambda '($97) (AsStruct '('AggregateList0 (Member $95 'AggregateList0)) '('Count0 (Member $95 'Count0)) '($29 (Member $97 $29)) '($30 (Member $97 $30)) '('"ts" (Member $97 '"ts")) '('"user" (Member $97 '"user")))))) (List $63)))
      (return (Map $94 (lambda '($98) (block '(
        (let $99 (Member $98 $29))
        (return (AsStruct '('AggregateList0 (Member $98 'AggregateList0)) '('Count0 (Member $98 'Count0)) '('"group_w1_0" $99) '('"ss0" $99) '('"ts" (Member $98 '"ts")) '('"user" (Member $98 '"user"))))
      )))))
    ))) 'singleRow (lambda '($100) (block '(
      (let $101 (Member $100 $29))
      (return (AsList (AsStruct '('AggregateList0 (Member $100 'AggregateList0)) '('Count0 (Member $100 'Count0)) '('"group_w1_0" $101) '('"ss0" $101) '('"ts" (Member $100 '"ts")) '('"user" (Member $100 '"user")))))
    )))))))
  )))))
  (let $32 '($27 $27 $27))
  (let $33 (Sort (Map $31 (lambda '($102) (AsStruct '('"session_len" (Member $102 'Count0)) '('"ss" (Member (Member $102 '"ss0") 'start)) '('"ss1" (Member (Member $102 '"group_w1_0") 'start)) '('"ts" (Member $102 '"ts")) '('"ts_session" (Member $102 'AggregateList0)) '('"user" (Member $102 '"user"))))) $32 (lambda '($103) '((Member $103 '"user") (Member $103 '"ts") (Member $103 '"session_len")))))
  (let $34 (lambda '($104) (Member $104 '"session_len") (Member $104 '"ss") (Member $104 '"ss1") (Member $104 '"ts") (Member $104 '"ts_session") (Member $104 '"user")))
  (return (FromFlow (ExpandMap $33 $34)))
))) $11))
(let $13 '('('"5" '"Asc") '('"3" '"Asc") '('0 '"Asc")))
(let $14 (DqCnMerge (TDqOutput $12 '0) $13))
(let $15 (DqPhyStage '($14) (lambda '($105) (FromFlow (NarrowMap (ToFlow $105) (lambda '($106 $107 $108 $109 $110 $111) (AsStruct '('"session_len" $106) '('"ss" $107) '('"ss1" $108) '('"ts" $109) '('"ts_session" $110) '('"user" $111)))))) '('('"_logical_id" '8650) '('"_id" '"97d6b5fb-b11c8d9-943f24f0-d7bed88f"))))
(let $16 '($6 $12 $15))
(let $17 '('"user" '"ts" '"ss1" '"ss" '"ts_session" '"session_len"))
(let $18 (DqCnResult (TDqOutput $15 '0) $17))
(return (KqpPhysicalQuery '((KqpPhysicalTx $16 '($18) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $10) '0 '0)) '('('"type" '"query"))))
)
