(
(let $1 (DataType 'Int32))
(let $2 '('"payload" $1))
(let $3 (OptionalType $1))
(let $4 '('"ts" $3))
(let $5 '('"user" (OptionalType (DataType 'String))))
(let $6 (StructType $2 $4 $5))
(let $7 '('('"_logical_id" '9627) '('"_id" '"f148e470-7ded6af7-bc379db4-e642a01b") '('"_wide_channels" $6)))
(let $8 (DqPhyStage '() (lambda '() (block '(
  (let $18 '@@["StructType";[["user";["OptionalType";["DataType";"String"]]];["ts";["OptionalType";["DataType";"Int32"]]];["payload";["DataType";"Int32"]]]]@@)
  (let $19 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $18)))
  (let $20 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $21 (YtStat '('Id '"Input") '('RecordsCount '"18") '('DataSize '"655") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $22 (YtTable '"Input" $19 $20 $21 '() (Void) (Void) '"/Root/plato"))
  (let $23 '('"payload" '"ts" '"user"))
  (let $24 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $22 $23 (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $24 '())))
))) $7))
(let $9 (DqCnHashShuffle (TDqOutput $8 '0) '('2) '0 '"HashV2"))
(let $10 (StructType $2 '('"session_len" (DataType 'Uint64)) $4 '('"ts_session" (ListType $1)) $5))
(let $11 '('('"_logical_id" '10914) '('"_id" '"63f8177f-8750ffeb-255e29f9-dce840e7") '('"_wide_channels" $10)))
(let $12 (DqPhyStage '($9) (lambda '($25) (block '(
  (let $26 (NarrowSqueezeToDict (ToFlow $25) (lambda '($34 $35 $36) $36) (lambda '($37 $38 $39) (AsStruct '('"payload" $37) '('"ts" $38) '('"user" $39))) '('Auto 'Many)))
  (let $27 (Bool 'true))
  (let $28 (Chain1Map (OrderedFlatMap $26 (lambda '($40) (OrderedFlatMap (DictPayloads $40) (lambda '($41) (Sort $41 $27 (lambda '($42) (Member $42 '"ts"))))))) (lambda '($43) (block '(
    (let $44 (Member $43 '"ts"))
    (return '($43 $44 $44 (Member $43 '"user")))
  ))) (lambda '($45 $46) (block '(
    (let $47 (Member $45 '"user"))
    (let $48 (Member $45 '"ts"))
    (let $49 '($45 $48 $48 $47))
    (let $50 (Nth $46 '2))
    (let $51 (If (Coalesce (< $50 $48) $27) (- $48 $50) (- $50 $48)))
    (let $52 (If (And (AggrNotEquals $48 $50) (Coalesce (< (Int32 '"10") $51) $27)) $48 (Nth $46 '1)))
    (let $53 '($45 $52 $48 $47))
    (return (If (AggrNotEquals $47 (Member (Nth $46 '0) '"user")) $49 $53))
  )))))
  (let $29 '"_yql_window_session_params")
  (let $30 '"_yql_window_session_start")
  (let $31 (Chopper (OrderedMap $28 (lambda '($54) (block '(
    (let $55 (Nth $54 '1))
    (let $56 (Nth $54 '0))
    (return (AsStruct '($29 (AsStruct '('start $55) '('state (Nth $54 '2)))) '($30 $55) '('"payload" (Member $56 '"payload")) '('"ts" (Member $56 '"ts")) '('"user" (Member $56 '"user"))))
  )))) (lambda '($57) '((Member $57 '"user") (Member $57 $30))) (lambda '($58 $59) (Or (AggrNotEquals (Nth $58 '0) (Member $59 '"user")) (AggrNotEquals (Nth $58 '1) (Member $59 $30)))) (lambda '($60 $61) (block '(
    (let $62 (FromFlow $61))
    (let $63 (StructType '($29 (StructType '('start $3) '('state $3))) '($30 $3) $2 $4 $5))
    (let $64 (Uint64 '1))
    (let $65 (QueueCreate $63 (Uint64 '"4") $64 (DependsOn $62)))
    (let $66 (Uint64 '0))
    (let $67 (Uint64 '"3"))
    (let $68 (lambda '($81) $81))
    (let $69 (lambda '($82) (ToList (Member $82 '"ts"))))
    (let $70 (lambda '($83 $84) (IfPresent (Member $83 '"ts") (lambda '($85) (Insert $84 $85)) $84)))
    (let $71 (lambda '($86) $64))
    (let $72 (lambda '($87 $88) (Inc $88)))
    (let $73 (Chain1Map (ToFlow (PreserveStream $62 $65 $64)) (lambda '($74) (block '(
      (let $75 (QueueRange $65 $66 $67 (DependsOn $74)))
      (let $76 (FlatMap $75 $68))
      (let $77 (Fold1 $76 $69 $70))
      (let $78 (Member $74 '"ts"))
      (let $79 (Fold1 $76 $71 $72))
      (let $80 (AsStruct '('AggregateList0 (Coalesce $77 (ToList $78))) '('Count0 (Coalesce $79 $64)) '($29 (Member $74 $29)) '($30 (Member $74 $30)) '('"payload" (Member $74 '"payload")) '('"ts" $78) '('"user" (Member $74 '"user"))))
      (return '($80 (AsStruct '('AggregateList0 (Void)) '('Count0 (Void)))))
    ))) (lambda '($89 $90) (block '(
      (let $91 (QueueRange $65 $66 $67 (DependsOn $89)))
      (let $92 (FlatMap $91 $68))
      (let $93 (Fold1 $92 $69 $70))
      (let $94 (Member $89 '"ts"))
      (let $95 (Fold1 $92 $71 $72))
      (let $96 (AsStruct '('AggregateList0 (Coalesce $93 (ToList $94))) '('Count0 (Coalesce $95 $64)) '($29 (Member $89 $29)) '($30 (Member $89 $30)) '('"payload" (Member $89 '"payload")) '('"ts" $94) '('"user" (Member $89 '"user"))))
      (return '($96 (Nth $90 '1)))
    )))))
    (return (OrderedMap $73 (lambda '($97) (block '(
      (let $98 (Nth $97 '0))
      (return (AsStruct '('AggregateList0 (Member $98 'AggregateList0)) '('Count0 (Member $98 'Count0)) '('"payload" (Member $98 '"payload")) '('"ts" (Member $98 '"ts")) '('"user" (Member $98 '"user"))))
    )))))
  )))))
  (let $32 (Sort (Map $31 (lambda '($99) (AsStruct '('"payload" (Member $99 '"payload")) '('"session_len" (Member $99 'Count0)) '('"ts" (Member $99 '"ts")) '('"ts_session" (Member $99 'AggregateList0)) '('"user" (Member $99 '"user"))))) '($27 $27) (lambda '($100) '((Member $100 '"user") (Member $100 '"payload")))))
  (let $33 (lambda '($101) (Member $101 '"payload") (Member $101 '"session_len") (Member $101 '"ts") (Member $101 '"ts_session") (Member $101 '"user")))
  (return (FromFlow (ExpandMap $32 $33)))
))) $11))
(let $13 (DqCnMerge (TDqOutput $12 '0) '('('"4" '"Asc") '('0 '"Asc"))))
(let $14 (DqPhyStage '($13) (lambda '($102) (FromFlow (NarrowMap (ToFlow $102) (lambda '($103 $104 $105 $106 $107) (AsStruct '('"payload" $103) '('"session_len" $104) '('"ts" $105) '('"ts_session" $106) '('"user" $107)))))) '('('"_logical_id" '10926) '('"_id" '"625ca842-1625466-98548646-5ef5ec80"))))
(let $15 '($8 $12 $14))
(let $16 '('"user" '"ts" '"payload" '"ts_session" '"session_len"))
(let $17 (DqCnResult (TDqOutput $14 '0) $16))
(return (KqpPhysicalQuery '((KqpPhysicalTx $15 '($17) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $10) '0 '0)) '('('"type" '"query"))))
)
