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