(
(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" '6622) '('"_id" '"2bae94a9-8f87ab5a-ec194751-8f2e6176") '('"_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" '7525) '('"_id" '"10d772f4-dfa5e86c-820cd447-5eaa704b") '('"_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 (QueueCreate $63 (Void) (Uint64 '0) (DependsOn $62)))
    (let $65 (Uint64 '"18446744073709551615"))
    (let $66 (Chain1Map (ToFlow (PreserveStream $62 $64 $65)) (lambda '($67) (block '(
      (let $68 (Uint64 '1))
      (let $69 (QueueRange $64 $68 $65 (DependsOn $67)))
      (let $70 (FlatMap $69 (lambda '($77) $77)))
      (let $71 (Member $67 '"ts"))
      (let $72 (Fold $70 (ToList $71) (lambda '($78 $79) (IfPresent (Member $78 '"ts") (lambda '($80) (Insert $79 $80)) $79))))
      (let $73 '('AggregateList0 $72))
      (let $74 (Fold $70 $68 (lambda '($81 $82) (Inc $82))))
      (let $75 '('Count0 $74))
      (let $76 (AsStruct $73 $75 '($29 (Member $67 $29)) '($30 (Member $67 $30)) '('"payload" (Member $67 '"payload")) '('"ts" $71) '('"user" (Member $67 '"user"))))
      (return '($76 (AsStruct $73 $75)))
    ))) (lambda '($83 $84) (block '(
      (let $85 (Nth $84 '1))
      (let $86 (AsStruct '('AggregateList0 (Member $85 'AggregateList0)) '('Count0 (Member $85 'Count0)) '($29 (Member $83 $29)) '($30 (Member $83 $30)) '('"payload" (Member $83 '"payload")) '('"ts" (Member $83 '"ts")) '('"user" (Member $83 '"user"))))
      (return '($86 $85))
    )))))
    (return (OrderedMap $66 (lambda '($87) (block '(
      (let $88 (Nth $87 '0))
      (return (AsStruct '('AggregateList0 (Member $88 'AggregateList0)) '('Count0 (Member $88 'Count0)) '('"payload" (Member $88 '"payload")) '('"ts" (Member $88 '"ts")) '('"user" (Member $88 '"user"))))
    )))))
  )))))
  (let $32 (Sort (Map $31 (lambda '($89) (AsStruct '('"payload" (Member $89 '"payload")) '('"session_len" (Member $89 'Count0)) '('"ts" (Member $89 '"ts")) '('"ts_session" (Member $89 'AggregateList0)) '('"user" (Member $89 '"user"))))) '($27 $27) (lambda '($90) '((Member $90 '"user") (Member $90 '"payload")))))
  (let $33 (lambda '($91) (Member $91 '"payload") (Member $91 '"session_len") (Member $91 '"ts") (Member $91 '"ts_session") (Member $91 '"user")))
  (return (FromFlow (ExpandMap $32 $33)))
))) $11))
(let $13 (DqCnMerge (TDqOutput $12 '0) '('('"4" '"Asc") '('0 '"Asc"))))
(let $14 (DqPhyStage '($13) (lambda '($92) (FromFlow (NarrowMap (ToFlow $92) (lambda '($93 $94 $95 $96 $97) (AsStruct '('"payload" $93) '('"session_len" $94) '('"ts" $95) '('"ts_session" $96) '('"user" $97)))))) '('('"_logical_id" '7537) '('"_id" '"9aafacb5-78aa4396-73ee1c1a-efac6295"))))
(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"))))
)
