(
(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" '4329) '('"_id" '"a24029cb-cda46416-323ebca4-f5341b6d") '('"_wide_channels" $5)))
(let $7 (DqPhyStage '() (lambda '() (block '(
  (let $20 '@@["StructType";[["key";["DataType";"String"]];["subkey";["DataType";"String"]];["value";["DataType";"String"]]]]@@)
  (let $21 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $20)))
  (let $22 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $23 (YtStat '('Id '"Input") '('RecordsCount '"9") '('DataSize '"369") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $24 (YtTable '"Input" $21 $22 $23 '() (Void) (Void) '"/Root/plato"))
  (let $25 '('"key" '"subkey" '"value"))
  (let $26 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $24 $25 (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $26 '())))
))) $6))
(let $8 (DqCnHashShuffle (TDqOutput $7 '0) '('"2") '0 '"HashV2"))
(let $9 (DataType 'Int64))
(let $10 (OptionalType $9))
(let $11 (StructType $2 $3 '('"subkey_sum" $10) $4))
(let $12 '('('"_logical_id" '4804) '('"_id" '"3fcf0d36-6e3364b7-b5f859d7-8042a53a") '('"_wide_channels" $11)))
(let $13 (DqPhyStage '($8) (lambda '($27) (block '(
  (let $28 (NarrowSqueezeToDict (ToFlow $27) (lambda '($34 $35 $36) $36) (lambda '($37 $38 $39) (AsStruct '('"key" $37) '('"subkey" $38) '('"value" $39))) '('Auto 'Many)))
  (let $29 (Bool 'true))
  (let $30 (Chopper (OrderedFlatMap $28 (lambda '($40) (OrderedFlatMap (DictPayloads $40) (lambda '($41) (Sort $41 $29 (lambda '($42) (Member $42 '"key"))))))) (lambda '($43) (Member $43 '"value")) (lambda '($44 $45) (AggrNotEquals $44 (Member $45 '"value"))) (lambda '($46 $47) (block '(
    (let $48 (OptionalType (DataType 'Int32)))
    (let $49 (lambda '($61) (Just (SafeCast $61 $9))))
    (let $50 (Nothing $10))
    (let $51 (Chain1Map $47 (lambda '($56) (block '(
      (let $57 (Member $56 '"subkey"))
      (let $58 (IfPresent (SafeCast $57 $48) $49 $50))
      (let $59 '('Sum0 $58))
      (let $60 (AsStruct $59 '('"key" (Member $56 '"key")) '('"subkey" $57) '('"value" (Member $56 '"value"))))
      (return '($60 (AsStruct $59)))
    ))) (lambda '($62 $63) (block '(
      (let $64 (Member $62 '"subkey"))
      (let $65 (IfPresent (SafeCast $64 $48) $49 $50))
      (let $66 '('Sum0 (AggrAdd $65 (Member (Nth $63 '1) 'Sum0))))
      (let $67 (AsStruct $66 '('"key" (Member $62 '"key")) '('"subkey" $64) '('"value" (Member $62 '"value"))))
      (return '($67 (AsStruct $66)))
    )))))
    (let $52 (StructType '('Sum0 $10) $2 $3 $4))
    (let $53 (ListType $52))
    (let $54 (VariantType (StructType '('group $53) '('singleRow $52))))
    (let $55 (Condense1 $51 (lambda '($68) (block '(
      (let $69 (Nth $68 '0))
      (let $70 (Variant $69 'singleRow $54))
      (return '((Member $69 '"key") $70))
    ))) (lambda '($71 $72) (AggrNotEquals (Member (Nth $71 '0) '"key") (Nth $72 '0))) (lambda '($73 $74) (block '(
      (let $75 (Nth $73 '0))
      (let $76 (Visit (Nth $74 '1) 'group (lambda '($77) (Variant (Insert $77 $75) 'group $54)) 'singleRow (lambda '($78) (Variant (AsList $78 $75) 'group $54))))
      (return '((Nth $74 '0) $76))
    )))))
    (return (OrderedFlatMap $55 (lambda '($79) (Visit (Nth $79 '1) 'group (lambda '($80) (IfPresent (Last $80) (lambda '($81) (OrderedMap (Collect (Map $80 (lambda '($82) (AsStruct '('"key" (Member $82 '"key")) '('"subkey" (Member $82 '"subkey")) '('"value" (Member $82 '"value")))))) (lambda '($83) (AsStruct '('Sum0 (Member $81 'Sum0)) '('"key" (Member $83 '"key")) '('"subkey" (Member $83 '"subkey")) '('"value" (Member $83 '"value")))))) (List $53))) 'singleRow (lambda '($84) (AsList $84))))))
  )))))
  (let $31 '($29 $29 $29))
  (let $32 (Sort (Map $30 (lambda '($85) (AsStruct '('"key" (Member $85 '"key")) '('"subkey" (Member $85 '"subkey")) '('"subkey_sum" (Member $85 'Sum0)) '('"value" (Member $85 '"value"))))) $31 (lambda '($86) '((Member $86 '"value") (Member $86 '"key") (Member $86 '"subkey")))))
  (let $33 (lambda '($87) (Member $87 '"key") (Member $87 '"subkey") (Member $87 '"subkey_sum") (Member $87 '"value")))
  (return (FromFlow (ExpandMap $32 $33)))
))) $12))
(let $14 '('('"3" '"Asc") '('0 '"Asc") '('1 '"Asc")))
(let $15 (DqCnMerge (TDqOutput $13 '0) $14))
(let $16 (DqPhyStage '($15) (lambda '($88) (FromFlow (NarrowMap (ToFlow $88) (lambda '($89 $90 $91 $92) (AsStruct '('"key" $89) '('"subkey" $90) '('"subkey_sum" $91) '('"value" $92)))))) '('('"_logical_id" '4816) '('"_id" '"c1bd567c-dcae485-7d8e10cf-f527bdbf"))))
(let $17 '($7 $13 $16))
(let $18 '('"value" '"key" '"subkey" '"subkey_sum"))
(let $19 (DqCnResult (TDqOutput $16 '0) $18))
(return (KqpPhysicalQuery '((KqpPhysicalTx $17 '($19) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $11) '0 '0)) '('('"type" '"query"))))
)
