(
(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" '13250) '('"_id" '"d1be208-8d45e177-134bada7-43b226cf") '('"_wide_channels" $5)))
(let $7 (DqPhyStage '() (lambda '() (block '(
  (let $29 '@@["StructType";[["key";["DataType";"String"]];["subkey";["DataType";"String"]];["value";["DataType";"String"]]]]@@)
  (let $30 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $29)))
  (let $31 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" 'false)))))
  (let $32 (YtStat '('Id '"Input") '('RecordsCount '"30") '('DataSize '"1170") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $33 (YtTable '"Input" $30 $31 $32 '() (Void) (Void) '"/Root/plato"))
  (let $34 '('"key" '"subkey" '"value"))
  (let $35 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $33 $34 (Void) (Void))) '()))))
  (return (FromFlow (WideFilter (DqReadWideWrap $35 '()) (lambda '($36 $37 $38) (== $36 (String '1))))))
))) $6))
(let $8 (DqCnUnionAll (TDqOutput $7 '0)))
(let $9 (ListType $1))
(let $10 (DataType 'Uint32))
(let $11 (Uint64 '1))
(let $12 (OptionalType $10))
(let $13 (DataType 'Uint64))
(let $14 (StructType '('AggregateListDistinct0 $9) '('Count0 $13) '('Sum0 $13) $2 $3 $4))
(let $15 (Bool 'false))
(let $16 '('('"_logical_id" '13307) '('"_id" '"d39cdbd9-f321cc22-3115b9e9-ed3c8322") '('"_partition_mode" '"aggregate") '('"_wide_channels" $14)))
(let $17 (DqPhyStage '($8) (lambda '($39) (block '(
  (let $40 (ResourceType '"Set.SetResource.String"))
  (let $41 (TupleType (TupleType $40) (StructType) $1))
  (let $42 '($40))
  (let $43 (CallableType '() '($9) $42))
  (let $44 '('('"strict")))
  (let $45 (Udf '"Set.GetResult" (Void) $41 '"" $43 (VoidType) '"" $44))
  (let $46 '($1))
  (let $47 (DependsOn (Uint32 '1)))
  (let $48 (Chain1Map (NarrowMap (ToFlow $39) (lambda '($53 $54 $55) (AsStruct '('"key" $53) '('"subkey" $54) '('"value" $55)))) (lambda '($56) (block '(
    (let $57 (TupleType (TupleType $1 $10) (StructType) $1))
    (let $58 (CallableType '() $42 $46 '($10)))
    (let $59 (Udf '"Set.Create" (Void) $57 '"" $58 (VoidType) '"" $44))
    (let $60 (Member $56 '"subkey"))
    (let $61 (NamedApply $59 '($60 (Uint32 '0)) (AsStruct) $47))
    (let $62 '('Count0 $11))
    (let $63 '('Sum0 (SafeCast (Unwrap (SafeCast $60 $12)) $13)))
    (let $64 (AsStruct '('AggregateListDistinct0 (Apply $45 $61)) $62 $63 '('"key" (Member $56 '"key")) '('"subkey" $60) '('"value" (Member $56 '"value"))))
    (let $65 (AsStruct '('AggregateListDistinct0 $61) $62 $63))
    (return '($64 $65))
  ))) (lambda '($66 $67) (block '(
    (let $68 (TupleType (TupleType $40 $1) (StructType) $1))
    (let $69 (CallableType '() $42 $42 $46))
    (let $70 (Udf '"Set.AddValue" (Void) $68 '"" $69 (VoidType) '"" $44))
    (let $71 (Nth $67 '1))
    (let $72 (Member $66 '"subkey"))
    (let $73 (NamedApply $70 '((Member $71 'AggregateListDistinct0) $72) (AsStruct) $47))
    (let $74 '('Count0 (Inc (Member $71 'Count0))))
    (let $75 '('Sum0 (AggrAdd (SafeCast (Unwrap (SafeCast $72 $12)) $13) (Member $71 'Sum0))))
    (let $76 (AsStruct '('AggregateListDistinct0 (Apply $45 $73)) $74 $75 '('"key" (Member $66 '"key")) '('"subkey" $72) '('"value" (Member $66 '"value"))))
    (let $77 (AsStruct '('AggregateListDistinct0 $73) $74 $75))
    (return '($76 $77))
  )))))
  (let $49 (ListType $14))
  (let $50 (VariantType (StructType '('group $49) '('singleRow $14))))
  (let $51 (Condense1 $48 (lambda '($78) (block '(
    (let $79 (Variant (Nth $78 '0) 'singleRow $50))
    (return '((Void) $79))
  ))) (lambda '($80 $81) $15) (lambda '($82 $83) (block '(
    (let $84 (Nth $82 '0))
    (let $85 (Visit (Nth $83 '1) 'group (lambda '($86) (Variant (Insert $86 $84) 'group $50)) 'singleRow (lambda '($87) (Variant (AsList $87 $84) 'group $50))))
    (return '((Nth $83 '0) $85))
  )))))
  (let $52 (lambda '($94) (Member $94 'AggregateListDistinct0) (Member $94 'Count0) (Member $94 'Sum0) (Member $94 '"key") (Member $94 '"subkey") (Member $94 '"value")))
  (return (FromFlow (ExpandMap (OrderedFlatMap $51 (lambda '($88) (Visit (Nth $88 '1) 'group (lambda '($89) (IfPresent (Last $89) (lambda '($90) (OrderedMap (Collect (Map $89 (lambda '($91) (AsStruct '('"key" (Member $91 '"key")) '('"subkey" (Member $91 '"subkey")) '('"value" (Member $91 '"value")))))) (lambda '($92) (AsStruct '('AggregateListDistinct0 (Member $90 'AggregateListDistinct0)) '('Count0 (Member $90 'Count0)) '('Sum0 (Member $90 'Sum0)) '('"key" (Member $92 '"key")) '('"subkey" (Member $92 '"subkey")) '('"value" (Member $92 '"value")))))) (List $49))) 'singleRow (lambda '($93) (AsList $93))))) $52)))
))) $16))
(let $18 (DqCnHashShuffle (TDqOutput $17 '0) '('"3") '0 '"HashV2"))
(let $19 (OptionalType $13))
(let $20 '"agglist_distinct1")
(let $21 (StructType '('"agglist2" $9) '($20 $9) '('"count1" $13) '('"sum1" $13) '('"sum2" $19) $4))
(let $22 '('('"_logical_id" '14426) '('"_id" '"48d67db7-58672a21-461cfbdc-f4de014f") '('"_wide_channels" $21)))
(let $23 (DqPhyStage '($18) (lambda '($95) (block '(
  (let $96 (NarrowSqueezeToDict (ToFlow $95) (lambda '($101 $102 $103 $104 $105 $106) $104) (lambda '($107 $108 $109 $110 $111 $112) (AsStruct '('AggregateListDistinct0 $107) '('Count0 $108) '('Sum0 $109) '('"key" $110) '('"subkey" $111) '('"value" $112))) '('Auto 'Many)))
  (let $97 (Chopper (OrderedFlatMap $96 (lambda '($113) (OrderedFlatMap (DictPayloads $113) (lambda '($114) (Sort $114 $15 (lambda '($115) (Member $115 '"value"))))))) (lambda '($116) (Member $116 '"key")) (lambda '($117 $118) (AggrNotEquals $117 (Member $118 '"key"))) (lambda '($119 $120) (block '(
    (let $121 (FromFlow $120))
    (let $122 (QueueCreate $14 (Void) (Uint64 '0) (DependsOn $121)))
    (let $123 (Uint64 '"18446744073709551615"))
    (let $124 (Chain1Map (ToFlow (PreserveStream $121 $122 $123)) (lambda '($125) (block '(
      (let $126 (QueueRange $122 $11 $123 (DependsOn $125)))
      (let $127 (FlatMap $126 (lambda '($137) $137)))
      (let $128 (Member $125 '"subkey"))
      (let $129 (Fold $127 (AsList $128) (lambda '($138 $139) (Insert $139 (Member $138 '"subkey")))))
      (let $130 '('AggregateList0 $129))
      (let $131 (lambda '($140) (Just (SafeCast $140 $13))))
      (let $132 (Nothing $19))
      (let $133 (IfPresent (SafeCast $128 $12) $131 $132))
      (let $134 (Fold $127 $133 (lambda '($141 $142) (block '(
        (let $143 (IfPresent (SafeCast (Member $141 '"subkey") $12) $131 $132))
        (return (AggrAdd $143 $142))
      )))))
      (let $135 '('Sum1 $134))
      (let $136 (AsStruct $130 '('AggregateListDistinct0 (Member $125 'AggregateListDistinct0)) '('Count0 (Member $125 'Count0)) '('Sum0 (Member $125 'Sum0)) $135 '('"key" (Member $125 '"key")) '('"subkey" $128) '('"value" (Member $125 '"value"))))
      (return '($136 (AsStruct $130 $135)))
    ))) (lambda '($144 $145) (block '(
      (let $146 (Nth $145 '1))
      (let $147 (AsStruct '('AggregateList0 (Member $146 'AggregateList0)) '('AggregateListDistinct0 (Member $144 'AggregateListDistinct0)) '('Count0 (Member $144 'Count0)) '('Sum0 (Member $144 'Sum0)) '('Sum1 (Member $146 'Sum1)) '('"key" (Member $144 '"key")) '('"subkey" (Member $144 '"subkey")) '('"value" (Member $144 '"value"))))
      (return '($147 $146))
    )))))
    (return (OrderedMap $124 (lambda '($148) (block '(
      (let $149 (Nth $148 '0))
      (return (AsStruct '('AggregateList0 (Member $149 'AggregateList0)) '('AggregateListDistinct0 (Member $149 'AggregateListDistinct0)) '('Count0 (Member $149 'Count0)) '('Sum0 (Member $149 'Sum0)) '('Sum1 (Member $149 'Sum1)) '('"value" (Member $149 '"value"))))
    )))))
  )))))
  (let $98 (Bool 'true))
  (let $99 (Sort (Map $97 (lambda '($150) (block '(
    (let $151 (Sort (Member $150 'AggregateListDistinct0) $98 (lambda '($152) $152)))
    (return (AsStruct '('"agglist2" (Member $150 'AggregateList0)) '($20 $151) '('"count1" (Member $150 'Count0)) '('"sum1" (Member $150 'Sum0)) '('"sum2" (Member $150 'Sum1)) '('"value" (Member $150 '"value"))))
  )))) $98 (lambda '($153) (Member $153 '"value"))))
  (let $100 (lambda '($154) (Member $154 '"agglist2") (Member $154 $20) (Member $154 '"count1") (Member $154 '"sum1") (Member $154 '"sum2") (Member $154 '"value")))
  (return (FromFlow (ExpandMap $99 $100)))
))) $22))
(let $24 (DqCnMerge (TDqOutput $23 '0) '('('"5" '"Asc"))))
(let $25 (DqPhyStage '($24) (lambda '($155) (FromFlow (NarrowMap (ToFlow $155) (lambda '($156 $157 $158 $159 $160 $161) (AsStruct '('"agglist2" $156) '($20 $157) '('"count1" $158) '('"sum1" $159) '('"sum2" $160) '('"value" $161)))))) '('('"_logical_id" '14438) '('"_id" '"c23b7101-70ac8cf9-befd58aa-8c9f1156"))))
(let $26 '($7 $17 $23 $25))
(let $27 '('"value" '"sum1" '"count1" $20 '"sum2" '"agglist2"))
(let $28 (DqCnResult (TDqOutput $25 '0) $27))
(return (KqpPhysicalQuery '((KqpPhysicalTx $26 '($28) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $21) '0 '0)) '('('"type" '"query"))))
)
