(
(let $1 (DataSource '"yt" '"/Root/plato"))
(let $2 '@@["StructType";[["key";["DataType";"String"]];["subkey";["DataType";"String"]];["value";["DataType";"String"]]]]@@)
(let $3 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $2)))
(let $4 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
(let $5 '('ChunkCount '1))
(let $6 '('ModifyTime '0))
(let $7 '('Revision '0))
(let $8 '('"key" '"subkey" '"value"))
(let $9 (Bool 'true))
(let $10 (DataType 'String))
(let $11 '('"key" $10))
(let $12 '('"subkey" $10))
(let $13 (StructType $11 $12 '('"value" $10)))
(let $14 '('('"_logical_id" '8872) '('"_id" '"64a6857c-c2d807ae-3d1366eb-e64fd1a6") '('"_wide_channels" $13)))
(let $15 (DqPhyStage '() (lambda '() (block '(
  (let $37 (YtStat '('Id '"Input") '('RecordsCount '"4") '('DataSize '"166") $5 $6 $7))
  (let $38 (YtTable '"Input" $3 $4 $37 '() (Void) (Void) '"/Root/plato"))
  (let $39 (YtReadTable! world $1 '((YtSection '((YtPath $38 $8 (Void) (Void))) '()))))
  (return (FromFlow (WideSort (DqReadWideWrap $39 '()) '('('0 $9) '('1 $9)))))
))) $14))
(let $16 '('('0 '"Asc") '('1 '"Asc")))
(let $17 (DqCnMerge (TDqOutput $15 '0) $16))
(let $18 (DqPhyStage '($17) (lambda '($40) (FromFlow (NarrowMap (ToFlow $40) (lambda '($41 $42 $43) (AsStruct '('"key" $41) '('"subkey" $42) '('"value" $43)))))) '('('"_logical_id" '8884) '('"_id" '"3133e2c0-2d9b6b6b-bdff5022-8945ab6b"))))
(let $19 (DqCnResult (TDqOutput $18 '0) $8))
(let $20 '('('"type" '"generic")))
(let $21 (KqpPhysicalTx '($15 $18) '($19) '() $20))
(let $22 '('('"_logical_id" '8922) '('"_id" '"a13a837b-1720f0f-1470fa20-42a976ea") '('"_wide_channels" (StructType $11 $12))))
(let $23 (DqPhyStage '() (lambda '() (block '(
  (let $44 (YtStat '('Id '"Input4") '('RecordsCount '"9") '('DataSize '"369") $5 $6 $7))
  (let $45 (YtTable '"Input4" $3 $4 $44 '() (Void) (Void) '"/Root/plato"))
  (let $46 (YtReadTable! world $1 '((YtSection '((YtPath $45 '('"key" '"subkey") (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $46 '())))
))) $22))
(let $24 (DqCnHashShuffle (TDqOutput $23 '0) '('0) '0 '"HashV2"))
(let $25 (DataType 'Double))
(let $26 (OptionalType $25))
(let $27 (StructType $11 '('"ska" $26)))
(let $28 '('('"_logical_id" '9445) '('"_id" '"7dbeb4a4-e486f76-a8a2f68b-3851337a") '('"_wide_channels" $27)))
(let $29 (DqPhyStage '($24) (lambda '($47) (block '(
  (let $48 (NarrowSqueezeToDict (ToFlow $47) (lambda '($52 $53) $52) (lambda '($54 $55) (AsStruct '('"key" $54) '('"subkey" $55))) '('Auto 'Many)))
  (let $49 (Chopper (OrderedFlatMap $48 (lambda '($56) (OrderedFlatMap (DictPayloads $56) (lambda '($57) (Sort $57 $9 (lambda '($58) (Member $58 '"subkey"))))))) (lambda '($59) (Member $59 '"key")) (lambda '($60 $61) (AggrNotEquals $60 (Member $61 '"key"))) (lambda '($62 $63) (block '(
    (let $64 (OptionalType (DataType 'Uint32)))
    (let $65 (Nothing $26))
    (let $66 (lambda '($81) (Just '((Convert $81 'Double) (Uint64 '1)))))
    (let $67 (Nothing (OptionalType (TupleType $25 (DataType 'Uint64)))))
    (let $68 (Chain1Map $63 (lambda '($73) (block '(
      (let $74 (Member $73 '"subkey"))
      (let $75 (SafeCast $74 $64))
      (let $76 (Uint64 '1))
      (let $77 (IfPresent $75 (lambda '($80) (Just (Div (Convert $80 'Double) $76))) $65))
      (let $78 (AsStruct '('Avg0 $77) '('"key" (Member $73 '"key")) '('"subkey" $74)))
      (let $79 (IfPresent $75 $66 $67))
      (return '($78 (AsStruct '('Avg0 $79))))
    ))) (lambda '($82 $83) (block '(
      (let $84 (Member $82 '"subkey"))
      (let $85 (SafeCast $84 $64))
      (let $86 (IfPresent $85 $66 $67))
      (let $87 (IfPresent (Member (Nth $83 '1) 'Avg0) (lambda '($90) (IfPresent $85 (lambda '($91) (Just '((AggrAdd (Nth $90 '0) (Convert $91 'Double)) (Inc (Nth $90 '1))))) (Just $90))) $86))
      (let $88 (IfPresent $87 (lambda '($92) (Just (Div (Nth $92 '0) (Nth $92 '1)))) $65))
      (let $89 (AsStruct '('Avg0 $88) '('"key" (Member $82 '"key")) '('"subkey" $84)))
      (return '($89 (AsStruct '('Avg0 $87))))
    )))))
    (let $69 (StructType '('Avg0 $26) $11 $12))
    (let $70 (ListType $69))
    (let $71 (VariantType (StructType '('group $70) '('singleRow $69))))
    (let $72 (Condense1 $68 (lambda '($93) (block '(
      (let $94 (Nth $93 '0))
      (let $95 (Variant $94 'singleRow $71))
      (return '((Member $94 '"subkey") $95))
    ))) (lambda '($96 $97) (AggrNotEquals (Member (Nth $96 '0) '"subkey") (Nth $97 '0))) (lambda '($98 $99) (block '(
      (let $100 (Nth $98 '0))
      (let $101 (Visit (Nth $99 '1) 'group (lambda '($102) (Variant (Insert $102 $100) 'group $71)) 'singleRow (lambda '($103) (Variant (AsList $103 $100) 'group $71))))
      (return '((Nth $99 '0) $101))
    )))))
    (return (OrderedFlatMap $72 (lambda '($104) (block '(
      (let $105 (Visit (Nth $104 '1) 'group (lambda '($106) (IfPresent (Last $106) (lambda '($107) (OrderedMap (Collect (Map $106 (lambda '($108) (AsStruct '('"key" (Member $108 '"key")) '('"subkey" (Member $108 '"subkey")))))) (lambda '($109) (AsStruct '('Avg0 (Member $107 'Avg0)) '('"key" (Member $109 '"key")) '('"subkey" (Member $109 '"subkey")))))) (List $70))) 'singleRow (lambda '($110) (AsList $110))))
      (return (Map $105 (lambda '($111) (AsStruct '('Avg0 (Member $111 'Avg0)) '('"key" (Member $111 '"key"))))))
    )))))
  )))))
  (let $50 (Sort (Map $49 (lambda '($112) (AsStruct '('"key" (Member $112 '"key")) '('"ska" (Member $112 'Avg0))))) '($9 $9) (lambda '($113) '((Member $113 '"key") (Member $113 '"ska")))))
  (let $51 (lambda '($114) (Member $114 '"key") (Member $114 '"ska")))
  (return (FromFlow (ExpandMap $50 $51)))
))) $28))
(let $30 (DqCnMerge (TDqOutput $29 '0) $16))
(let $31 (DqPhyStage '($30) (lambda '($115) (FromFlow (NarrowMap (ToFlow $115) (lambda '($116 $117) (AsStruct '('"key" $116) '('"ska" $117)))))) '('('"_logical_id" '9457) '('"_id" '"2d25e336-16567a33-2ba3b829-86fe8be8"))))
(let $32 '($23 $29 $31))
(let $33 (DqCnResult (TDqOutput $31 '0) '('"key" '"ska")))
(let $34 (KqpPhysicalTx $32 '($33) '() $20))
(let $35 (KqpTxResultBinding (ListType $13) '0 '0))
(let $36 (KqpTxResultBinding (ListType $27) '1 '0))
(return (KqpPhysicalQuery '($21 $34) '($35 $36) '('('"type" '"query"))))
)
