(
(let $1 (DataType 'String))
(let $2 '('"key" $1))
(let $3 '('"subkey" $1))
(let $4 '('"value" $1))
(let $5 (StructType $2 $3 $4))
(let $6 '('('"_logical_id" '17214) '('"_id" '"58ad459d-c7267bb9-1be0fc2f-5e929f60") '('"_wide_channels" $5)))
(let $7 (DqPhyStage '() (lambda '() (block '(
  (let $30 '@@["StructType";[["key";["DataType";"String"]];["subkey";["DataType";"String"]];["value";["DataType";"String"]]]]@@)
  (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 '"30") '('DataSize '"1170") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $34 (YtTable '"Input" $31 $32 $33 '() (Void) (Void) '"/Root/plato"))
  (let $35 '('"key" '"subkey" '"value"))
  (let $36 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $34 $35 (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $36 '())))
))) $6))
(let $8 (DqCnHashShuffle (TDqOutput $7 '0) '('0) '0 '"HashV2"))
(let $9 (Bool 'true))
(let $10 (OptionalType (DataType 'Uint32)))
(let $11 (DataType 'Uint64))
(let $12 '('Sum0 $11))
(let $13 (Uint64 '0))
(let $14 (Uint64 '3))
(let $15 (OptionalType $1))
(let $16 '('_yql_Lead0 $15))
(let $17 (StructType $12 $16 $3 $4))
(let $18 '('('"_logical_id" '18112) '('"_id" '"c5e5f522-51a4a289-7e4e7761-dbd0ff22") '('"_wide_channels" $17)))
(let $19 (DqPhyStage '($8) (lambda '($37) (block '(
  (let $38 (NarrowSqueezeToDict (ToFlow $37) (lambda '($41 $42 $43) $41) (lambda '($44 $45 $46) (AsStruct '('"key" $44) '('"subkey" $45) '('"value" $46))) '('Auto 'Many)))
  (let $39 (Chopper (OrderedFlatMap $38 (lambda '($47) (OrderedFlatMap (DictPayloads $47) (lambda '($48) (Sort $48 $9 (lambda '($49) (Member $49 '"value"))))))) (lambda '($50) (Member $50 '"key")) (lambda '($51 $52) (AggrNotEquals $51 (Member $52 '"key"))) (lambda '($53 $54) (block '(
    (let $55 (FromFlow $54))
    (let $56 (Chain1Map $55 (lambda '($65) (block '(
      (let $66 (Member $65 '"subkey"))
      (let $67 '('Sum0 (SafeCast (Unwrap (SafeCast $66 $10)) $11)))
      (let $68 (AsStruct $67 '('"key" (Member $65 '"key")) '('"subkey" $66) '('"value" (Member $65 '"value"))))
      (return '($68 (AsStruct $67)))
    ))) (lambda '($69 $70) (block '(
      (let $71 (Member $69 '"subkey"))
      (let $72 '('Sum0 (AggrAdd (SafeCast (Unwrap (SafeCast $71 $10)) $11) (Member (Nth $70 '1) 'Sum0))))
      (let $73 (AsStruct $72 '('"key" (Member $69 '"key")) '('"subkey" $71) '('"value" (Member $69 '"value"))))
      (return '($73 (AsStruct $72)))
    )))))
    (let $57 (StructType $12 $2 $3 $4))
    (let $58 (ListType $57))
    (let $59 (VariantType (StructType '('group $58) '('singleRow $57))))
    (let $60 (Condense1 $56 (lambda '($74) (block '(
      (let $75 (Nth $74 '0))
      (let $76 (Variant $75 'singleRow $59))
      (return '((Member $75 '"value") $76))
    ))) (lambda '($77 $78) (AggrNotEquals (Member (Nth $77 '0) '"value") (Nth $78 '0))) (lambda '($79 $80) (block '(
      (let $81 (Nth $79 '0))
      (let $82 (Visit (Nth $80 '1) 'group (lambda '($83) (Variant (Insert $83 $81) 'group $59)) 'singleRow (lambda '($84) (Variant (AsList $84 $81) 'group $59))))
      (return '((Nth $80 '0) $82))
    )))))
    (let $61 (QueueCreate $57 (Uint64 '"5") $13 (DependsOn $55)))
    (let $62 (lambda '($95) (block '(
      (let $96 (Member $95 '"value"))
      (return (Just (Concat $96 $96)))
    ))))
    (let $63 (Nothing $15))
    (let $64 (Chain1Map (ToFlow (PreserveStream (OrderedFlatMap $60 (lambda '($85) (Visit (Nth $85 '1) 'group (lambda '($86) (IfPresent (Last $86) (lambda '($87) (OrderedMap (Collect (Map $86 (lambda '($88) (AsStruct '('"key" (Member $88 '"key")) '('"subkey" (Member $88 '"subkey")) '('"value" (Member $88 '"value")))))) (lambda '($89) (AsStruct '('Sum0 (Member $87 'Sum0)) '('"key" (Member $89 '"key")) '('"subkey" (Member $89 '"subkey")) '('"value" (Member $89 '"value")))))) (List $58))) 'singleRow (lambda '($90) (AsList $90))))) $61 $14)) (lambda '($91) (block '(
      (let $92 (QueuePeek $61 $14 (DependsOn $91)))
      (let $93 (IfPresent $92 $62 $63))
      (let $94 (AsStruct '('Sum0 (Member $91 'Sum0)) '('_yql_Lead0 $93) '('"key" (Member $91 '"key")) '('"subkey" (Member $91 '"subkey")) '('"value" (Member $91 '"value"))))
      (return '($94 (AsStruct '('_yql_Lead0 (Void)))))
    ))) (lambda '($97 $98) (block '(
      (let $99 (QueuePeek $61 $14 (DependsOn $97)))
      (let $100 (IfPresent $99 $62 $63))
      (let $101 (AsStruct '('Sum0 (Member $97 'Sum0)) '('_yql_Lead0 $100) '('"key" (Member $97 '"key")) '('"subkey" (Member $97 '"subkey")) '('"value" (Member $97 '"value"))))
      (return '($101 (Nth $98 '1)))
    )))))
    (return (OrderedMap $64 (lambda '($102) (block '(
      (let $103 (Nth $102 '0))
      (return (AsStruct '('Sum0 (Member $103 'Sum0)) '('_yql_Lead0 (Member $103 '_yql_Lead0)) '('"subkey" (Member $103 '"subkey")) '('"value" (Member $103 '"value"))))
    )))))
  )))))
  (let $40 (lambda '($104) (Member $104 'Sum0) (Member $104 '_yql_Lead0) (Member $104 '"subkey") (Member $104 '"value")))
  (return (FromFlow (ExpandMap $39 $40)))
))) $18))
(let $20 (DqCnUnionAll (TDqOutput $19 '0)))
(let $21 (OptionalType $11))
(let $22 (StructType '('"dvalue_lead1" $15) '('"sum1" $11) '('"sum2" $21) $4 '('"value_lag2" $10)))
(let $23 '('('"_logical_id" '19681) '('"_id" '"ab934ad9-a311b65d-2e09c142-14bca803") '('"_wide_channels" $22)))
(let $24 (DqPhyStage '($20) (lambda '($105) (block '(
  (let $106 (FromFlow (NarrowMap (WideSort (ToFlow $105) '('('3 $9))) (lambda '($120 $121 $122 $123) (AsStruct '('Sum0 $120) '('_yql_Lead0 $121) '('"subkey" $122) '('"value" $123))))))
  (let $107 (lambda '($129) (Just (SafeCast $129 $11))))
  (let $108 (Nothing $21))
  (let $109 (Chain1Map $106 (lambda '($124) (block '(
    (let $125 (Member $124 '"subkey"))
    (let $126 (IfPresent (SafeCast $125 $10) $107 $108))
    (let $127 '('Sum1 $126))
    (let $128 (AsStruct '('Sum0 (Member $124 'Sum0)) $127 '('_yql_Lead0 (Member $124 '_yql_Lead0)) '('"subkey" $125) '('"value" (Member $124 '"value"))))
    (return '($128 (AsStruct $127)))
  ))) (lambda '($130 $131) (block '(
    (let $132 (Member $130 '"subkey"))
    (let $133 (IfPresent (SafeCast $132 $10) $107 $108))
    (let $134 '('Sum1 (AggrAdd $133 (Member (Nth $131 '1) 'Sum1))))
    (let $135 (AsStruct '('Sum0 (Member $130 'Sum0)) $134 '('_yql_Lead0 (Member $130 '_yql_Lead0)) '('"subkey" $132) '('"value" (Member $130 '"value"))))
    (return '($135 (AsStruct $134)))
  )))))
  (let $110 (StructType $12 '('Sum1 $21) $16 $3 $4))
  (let $111 (ListType $110))
  (let $112 (VariantType (StructType '('group $111) '('singleRow $110))))
  (let $113 (Condense1 $109 (lambda '($136) (block '(
    (let $137 (Nth $136 '0))
    (let $138 (Variant $137 'singleRow $112))
    (return '((Member $137 '"value") $138))
  ))) (lambda '($139 $140) (AggrNotEquals (Member (Nth $139 '0) '"value") (Nth $140 '0))) (lambda '($141 $142) (block '(
    (let $143 (Nth $141 '0))
    (let $144 (Visit (Nth $142 '1) 'group (lambda '($145) (Variant (Insert $145 $143) 'group $112)) 'singleRow (lambda '($146) (Variant (AsList $146 $143) 'group $112))))
    (return '((Nth $142 '0) $144))
  )))))
  (let $114 (QueueCreate $110 $14 (Uint64 '1) (DependsOn $106)))
  (let $115 (lambda '($157) (SafeCast (Member $157 '"value") $10)))
  (let $116 (Nothing $10))
  (let $117 (Chain1Map (ToFlow (PreserveStream (OrderedFlatMap $113 (lambda '($147) (Visit (Nth $147 '1) 'group (lambda '($148) (IfPresent (Last $148) (lambda '($149) (OrderedMap (Collect (Map $148 (lambda '($150) (AsStruct '('Sum0 (Member $150 'Sum0)) '('_yql_Lead0 (Member $150 '_yql_Lead0)) '('"subkey" (Member $150 '"subkey")) '('"value" (Member $150 '"value")))))) (lambda '($151) (AsStruct '('Sum0 (Member $151 'Sum0)) '('Sum1 (Member $149 'Sum1)) '('_yql_Lead0 (Member $151 '_yql_Lead0)) '('"subkey" (Member $151 '"subkey")) '('"value" (Member $151 '"value")))))) (List $111))) 'singleRow (lambda '($152) (AsList $152))))) $114 $13)) (lambda '($153) (block '(
    (let $154 (QueuePeek $114 $13 (DependsOn $153)))
    (let $155 (IfPresent $154 $115 $116))
    (let $156 (AsStruct '('Sum0 (Member $153 'Sum0)) '('Sum1 (Member $153 'Sum1)) '('_yql_Lag0 $155) '('_yql_Lead0 (Member $153 '_yql_Lead0)) '('"subkey" (Member $153 '"subkey")) '('"value" (Member $153 '"value"))))
    (return '($156 (AsStruct '('_yql_Lag0 (Void)))))
  ))) (lambda '($158 $159) (block '(
    (let $160 (QueuePeek $114 $13 (DependsOn $158)))
    (let $161 (IfPresent $160 $115 $116))
    (let $162 (AsStruct '('Sum0 (Member $158 'Sum0)) '('Sum1 (Member $158 'Sum1)) '('_yql_Lag0 $161) '('_yql_Lead0 (Member $158 '_yql_Lead0)) '('"subkey" (Member $158 '"subkey")) '('"value" (Member $158 '"value"))))
    (return '($162 (Nth $159 '1)))
  )))))
  (let $118 (Sort (Map $117 (lambda '($163) (block '(
    (let $164 (Nth $163 '0))
    (return (AsStruct '('"dvalue_lead1" (Member $164 '_yql_Lead0)) '('"sum1" (Member $164 'Sum0)) '('"sum2" (Member $164 'Sum1)) '('"value" (Member $164 '"value")) '('"value_lag2" (Member $164 '_yql_Lag0))))
  )))) $9 (lambda '($165) (Member $165 '"value"))))
  (let $119 (lambda '($166) (Member $166 '"dvalue_lead1") (Member $166 '"sum1") (Member $166 '"sum2") (Member $166 '"value") (Member $166 '"value_lag2")))
  (return (FromFlow (ExpandMap $118 $119)))
))) $23))
(let $25 (DqCnMerge (TDqOutput $24 '0) '('('3 '"Asc"))))
(let $26 (DqPhyStage '($25) (lambda '($167) (FromFlow (NarrowMap (ToFlow $167) (lambda '($168 $169 $170 $171 $172) (AsStruct '('"dvalue_lead1" $168) '('"sum1" $169) '('"sum2" $170) '('"value" $171) '('"value_lag2" $172)))))) '('('"_logical_id" '19693) '('"_id" '"aa4afaff-2d848b9d-495a60-3619826a"))))
(let $27 '($7 $19 $24 $26))
(let $28 '('"value" '"sum1" '"dvalue_lead1" '"sum2" '"value_lag2"))
(let $29 (DqCnResult (TDqOutput $26 '0) $28))
(return (KqpPhysicalQuery '((KqpPhysicalTx $27 '($29) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $22) '0 '0)) '('('"type" '"query"))))
)
