(
(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" '7345) '('"_id" '"1ff55f72-a1808599-ddf736b4-59f61b1") '('"_wide_channels" $6)))
(let $8 (DqPhyStage '() (lambda '() (block '(
  (let $30 '@@["StructType";[["user";["OptionalType";["DataType";"String"]]];["ts";["OptionalType";["DataType";"Int32"]]];["payload";["DataType";"Int32"]]]]@@)
  (let $31 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $30)))
  (let $32 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $33 (YtStat '('Id '"Input") '('RecordsCount '"18") '('DataSize '"655") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $34 (YtTable '"Input" $31 $32 $33 '() (Void) (Void) '"/Root/plato"))
  (let $35 '('"payload" '"ts" '"user"))
  (let $36 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $34 $35 (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $36 '())))
))) $7))
(let $9 (DqCnHashShuffle (TDqOutput $8 '0) '('2) '0 '"HashV2"))
(let $10 '('Auto 'Many))
(let $11 (Bool 'true))
(let $12 '"_yql_window_session_params")
(let $13 '($12 (StructType '('start $3) '('state $3))))
(let $14 '"_yql_window_session_start")
(let $15 '($14 $3))
(let $16 (StructType $13 $15 $2 $4 $5))
(let $17 '('('"_logical_id" '7410) '('"_id" '"29fefa53-7a127d75-8a92a00d-4223e598") '('"_partition_mode" '"aggregate") '('"_wide_channels" $16)))
(let $18 (DqPhyStage '($9) (lambda '($37) (block '(
  (let $38 (NarrowSqueezeToDict (ToFlow $37) (lambda '($41 $42 $43) $43) (lambda '($44 $45 $46) (AsStruct '('"payload" $44) '('"ts" $45) '('"user" $46))) $10))
  (let $39 (Chain1Map (OrderedFlatMap $38 (lambda '($47) (OrderedFlatMap (DictPayloads $47) (lambda '($48) (Sort $48 $11 (lambda '($49) (Member $49 '"ts"))))))) (lambda '($50) (block '(
    (let $51 (Member $50 '"ts"))
    (return '($50 $51 $51 (Member $50 '"user")))
  ))) (lambda '($52 $53) (block '(
    (let $54 (Member $52 '"user"))
    (let $55 (Member $52 '"ts"))
    (let $56 '($52 $55 $55 $54))
    (let $57 (Nth $53 '2))
    (let $58 (If (Coalesce (< $57 $55) $11) (- $55 $57) (- $57 $55)))
    (let $59 (If (And (AggrNotEquals $55 $57) (Coalesce (< (Int32 '"10") $58) $11)) $55 (Nth $53 '1)))
    (let $60 '($52 $59 $55 $54))
    (return (If (AggrNotEquals $54 (Member (Nth $53 '0) '"user")) $56 $60))
  )))))
  (let $40 (lambda '($61) (block '(
    (let $62 (Nth $61 '1))
    (let $63 (Nth $61 '0))
    (return (AsStruct '('start $62) '('state (Nth $61 '2))) $62 (Member $63 '"payload") (Member $63 '"ts") (Member $63 '"user"))
  ))))
  (return (FromFlow (ExpandMap $39 $40)))
))) $17))
(let $19 (DqCnHashShuffle (TDqOutput $18 '0) '('"4" '1) '0 '"HashV2"))
(let $20 (ListType $1))
(let $21 (DataType 'Uint64))
(let $22 (StructType $2 '('"session_len" $21) '('"session_start" $3) $4 '('"ts_session" $20) $5))
(let $23 '('('"_logical_id" '7995) '('"_id" '"f0da2ae6-49d736ef-1ef3fb5b-4f5b3b4b") '('"_wide_channels" $22)))
(let $24 (DqPhyStage '($19) (lambda '($64) (block '(
  (let $65 (NarrowSqueezeToDict (ToFlow $64) (lambda '($69 $70 $71 $72 $73) '($73 $70)) (lambda '($74 $75 $76 $77 $78) (AsStruct '($12 $74) '($14 $75) '('"payload" $76) '('"ts" $77) '('"user" $78))) $10))
  (let $66 (Chopper (OrderedFlatMap $65 (lambda '($79) (OrderedFlatMap (DictPayloads $79) (lambda '($80) (Sort $80 $11 (lambda '($81) (Member $81 '"payload"))))))) (lambda '($82) '((Member $82 '"user") (Member $82 $14))) (lambda '($83 $84) (Or (AggrNotEquals (Nth $83 '0) (Member $84 '"user")) (AggrNotEquals (Nth $83 '1) (Member $84 $14)))) (lambda '($85 $86) (block '(
    (let $87 (Chain1Map $86 (lambda '($92) (block '(
      (let $93 (Member $92 '"ts"))
      (let $94 '('AggregateList0 (ToList $93)))
      (let $95 '('Count0 (Uint64 '1)))
      (let $96 (AsStruct $94 $95 '($12 (Member $92 $12)) '($14 (Member $92 $14)) '('"payload" (Member $92 '"payload")) '('"ts" $93) '('"user" (Member $92 '"user"))))
      (return '($96 (AsStruct $94 $95)))
    ))) (lambda '($97 $98) (block '(
      (let $99 (Member $97 '"ts"))
      (let $100 (Nth $98 '1))
      (let $101 (Member $100 'AggregateList0))
      (let $102 (IfPresent $99 (lambda '($106) (Insert $101 $106)) $101))
      (let $103 '('AggregateList0 $102))
      (let $104 '('Count0 (Inc (Member $100 'Count0))))
      (let $105 (AsStruct $103 $104 '($12 (Member $97 $12)) '($14 (Member $97 $14)) '('"payload" (Member $97 '"payload")) '('"ts" $99) '('"user" (Member $97 '"user"))))
      (return '($105 (AsStruct $103 $104)))
    )))))
    (let $88 (StructType '('AggregateList0 $20) '('Count0 $21) $13 $15 $2 $4 $5))
    (let $89 (ListType $88))
    (let $90 (VariantType (StructType '('group $89) '('singleRow $88))))
    (let $91 (Condense1 $87 (lambda '($107) (block '(
      (let $108 (Nth $107 '0))
      (let $109 (Variant $108 'singleRow $90))
      (return '((Member $108 '"payload") $109))
    ))) (lambda '($110 $111) (AggrNotEquals (Member (Nth $110 '0) '"payload") (Nth $111 '0))) (lambda '($112 $113) (block '(
      (let $114 (Nth $112 '0))
      (let $115 (Visit (Nth $113 '1) 'group (lambda '($116) (Variant (Insert $116 $114) 'group $90)) 'singleRow (lambda '($117) (Variant (AsList $117 $114) 'group $90))))
      (return '((Nth $113 '0) $115))
    )))))
    (return (FlatMap $91 (lambda '($118) (Visit (Nth $118 '1) 'group (lambda '($119) (block '(
      (let $120 (IfPresent (Last $119) (lambda '($121) (OrderedMap (Collect (Map $119 (lambda '($122) (AsStruct '($12 (Member $122 $12)) '($14 (Member $122 $14)) '('"payload" (Member $122 '"payload")) '('"ts" (Member $122 '"ts")) '('"user" (Member $122 '"user")))))) (lambda '($123) (AsStruct '('AggregateList0 (Member $121 'AggregateList0)) '('Count0 (Member $121 'Count0)) '($12 (Member $123 $12)) '($14 (Member $123 $14)) '('"payload" (Member $123 '"payload")) '('"ts" (Member $123 '"ts")) '('"user" (Member $123 '"user")))))) (List $89)))
      (return (Map $120 (lambda '($124) (AsStruct '('AggregateList0 (Member $124 'AggregateList0)) '('Count0 (Member $124 'Count0)) '('"group_w_0" (Member $124 $12)) '('"payload" (Member $124 '"payload")) '('"ts" (Member $124 '"ts")) '('"user" (Member $124 '"user"))))))
    ))) 'singleRow (lambda '($125) (AsList (AsStruct '('AggregateList0 (Member $125 'AggregateList0)) '('Count0 (Member $125 'Count0)) '('"group_w_0" (Member $125 $12)) '('"payload" (Member $125 '"payload")) '('"ts" (Member $125 '"ts")) '('"user" (Member $125 '"user")))))))))
  )))))
  (let $67 (Sort (Map $66 (lambda '($126) (AsStruct '('"payload" (Member $126 '"payload")) '('"session_len" (Member $126 'Count0)) '('"session_start" (Member (Member $126 '"group_w_0") 'start)) '('"ts" (Member $126 '"ts")) '('"ts_session" (Member $126 'AggregateList0)) '('"user" (Member $126 '"user"))))) '($11 $11) (lambda '($127) '((Member $127 '"user") (Member $127 '"payload")))))
  (let $68 (lambda '($128) (Member $128 '"payload") (Member $128 '"session_len") (Member $128 '"session_start") (Member $128 '"ts") (Member $128 '"ts_session") (Member $128 '"user")))
  (return (FromFlow (ExpandMap $67 $68)))
))) $23))
(let $25 (DqCnMerge (TDqOutput $24 '0) '('('"5" '"Asc") '('0 '"Asc"))))
(let $26 (DqPhyStage '($25) (lambda '($129) (FromFlow (NarrowMap (ToFlow $129) (lambda '($130 $131 $132 $133 $134 $135) (AsStruct '('"payload" $130) '('"session_len" $131) '('"session_start" $132) '('"ts" $133) '('"ts_session" $134) '('"user" $135)))))) '('('"_logical_id" '8007) '('"_id" '"5f62ec8d-e090e602-92e85c29-6e8703b8"))))
(let $27 '($8 $18 $24 $26))
(let $28 '('"user" '"ts" '"payload" '"ts_session" '"session_len" '"session_start"))
(let $29 (DqCnResult (TDqOutput $26 '0) $28))
(return (KqpPhysicalQuery '((KqpPhysicalTx $27 '($29) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $22) '0 '0)) '('('"type" '"query"))))
)
