(
(let $1 (DataType 'String))
(let $2 '('"key" $1))
(let $3 '('"subkey" $1))
(let $4 '('('"_logical_id" '7442) '('"_id" '"31ba8382-5eb520c0-b8a10651-b3252b05") '('"_wide_channels" (StructType $2 $3))))
(let $5 (DqPhyStage '() (lambda '() (block '(
  (let $12 '@@["StructType";[["key";["DataType";"String"]];["subkey";["DataType";"String"]];["value";["DataType";"String"]]]]@@)
  (let $13 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $12)))
  (let $14 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" 'false)))))
  (let $15 (YtStat '('Id '"Input") '('RecordsCount '"14") '('DataSize '"621") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $16 (YtTable '"Input" $13 $14 $15 '() (Void) (Void) '"/Root/plato"))
  (let $17 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $16 '('"key" '"subkey") (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $17 '())))
))) $4))
(let $6 (DqCnUnionAll (TDqOutput $5 '0)))
(let $7 (OptionalType (DataType 'Uint32)))
(let $8 '('('"_logical_id" '7503) '('"_id" '"b5d718cb-777aa27d-64229aff-a0e1aea9") '('"_partition_mode" '"aggregate")))
(let $9 (DqPhyStage '($6) (lambda '($18) (block '(
  (let $19 (Chain1Map (NarrowMap (WideSort (ToFlow $18) '('('0 (Bool 'false)) '('1 (Bool 'true)))) (lambda '($24 $25) (AsStruct '('"key" $24) '('"subkey" $25)))) (lambda '($26) (block '(
    (let $27 (Member $26 '"subkey"))
    (let $28 (SafeCast $27 $7))
    (let $29 '('FirstValue0 $28))
    (let $30 '('FirstValueIgnoreNulls0 $28))
    (let $31 '('LastValue0 $28))
    (let $32 '('LastValueIgnoreNulls0 $28))
    (let $33 (AsStruct $29 $30 $31 $32 '('"key" (Member $26 '"key")) '('"subkey" $27)))
    (let $34 (AsStruct $29 $30 $31 $32))
    (return '($33 $34))
  ))) (lambda '($35 $36) (block '(
    (let $37 (Nth $36 '1))
    (let $38 '('FirstValue0 (Member $37 'FirstValue0)))
    (let $39 (Member $37 'FirstValueIgnoreNulls0))
    (let $40 (Member $35 '"subkey"))
    (let $41 (SafeCast $40 $7))
    (let $42 (If (Exists $39) $39 $41))
    (let $43 '('FirstValueIgnoreNulls0 $42))
    (let $44 '('LastValue0 $41))
    (let $45 (If (Exists $41) $41 (Member $37 'LastValueIgnoreNulls0)))
    (let $46 '('LastValueIgnoreNulls0 $45))
    (let $47 (AsStruct $38 $43 $44 $46 '('"key" (Member $35 '"key")) '('"subkey" $40)))
    (let $48 (AsStruct $38 $43 $44 $46))
    (return '($47 $48))
  )))))
  (let $20 (StructType '('FirstValue0 $7) '('FirstValueIgnoreNulls0 $7) '('LastValue0 $7) '('LastValueIgnoreNulls0 $7) $2 $3))
  (let $21 (ListType $20))
  (let $22 (VariantType (StructType '('group $21) '('singleRow $20))))
  (let $23 (Condense1 $19 (lambda '($49) (block '(
    (let $50 (Nth $49 '0))
    (let $51 (Variant $50 'singleRow $22))
    (return '('((Member $50 '"key") (Member $50 '"subkey")) $51))
  ))) (lambda '($52 $53) (block '(
    (let $54 (Nth $52 '0))
    (let $55 (Nth $53 '0))
    (return (Or (AggrNotEquals (Member $54 '"key") (Nth $55 '0)) (AggrNotEquals (Member $54 '"subkey") (Nth $55 '1))))
  ))) (lambda '($56 $57) (block '(
    (let $58 (Nth $56 '0))
    (let $59 (Visit (Nth $57 '1) 'group (lambda '($60) (Variant (Insert $60 $58) 'group $22)) 'singleRow (lambda '($61) (Variant (AsList $61 $58) 'group $22))))
    (return '((Nth $57 '0) $59))
  )))))
  (return (FromFlow (OrderedMap (FlatMap $23 (lambda '($62) (Visit (Nth $62 '1) 'group (lambda '($63) (block '(
    (let $64 (IfPresent (Last $63) (lambda '($65) (OrderedMap (Collect (Map $63 (lambda '($66) (AsStruct '('"key" (Member $66 '"key")) '('"subkey" (Member $66 '"subkey")))))) (lambda '($67) (AsStruct '('FirstValue0 (Member $65 'FirstValue0)) '('FirstValueIgnoreNulls0 (Member $65 'FirstValueIgnoreNulls0)) '('LastValue0 (Member $65 'LastValue0)) '('LastValueIgnoreNulls0 (Member $65 'LastValueIgnoreNulls0)) '('"key" (Member $67 '"key")) '('"subkey" (Member $67 '"subkey")))))) (List $21)))
    (return (Map $64 (lambda '($68) (AsStruct '('"column1" (Member $68 'FirstValue0)) '('"column2" (Member $68 'FirstValueIgnoreNulls0)) '('"column3" (Member $68 'LastValue0)) '('"column4" (Member $68 'LastValueIgnoreNulls0)) '('"key" (Member $68 '"key")) '('"subkey" (Member $68 '"subkey"))))))
  ))) 'singleRow (lambda '($69) (AsList (AsStruct '('"column1" (Member $69 'FirstValue0)) '('"column2" (Member $69 'FirstValueIgnoreNulls0)) '('"column3" (Member $69 'LastValue0)) '('"column4" (Member $69 'LastValueIgnoreNulls0)) '('"key" (Member $69 '"key")) '('"subkey" (Member $69 '"subkey")))))))) (lambda '($70) (AsStruct '('"column1" (Member $70 '"column1")) '('"column2" (Member $70 '"column2")) '('"column3" (Member $70 '"column3")) '('"column4" (Member $70 '"column4")) '('"key" (Member $70 '"key")) '('"subkey" (Member $70 '"subkey")))))))
))) $8))
(let $10 '('"key" '"column1" '"column2" '"column3" '"column4" '"subkey"))
(let $11 (DqCnResult (TDqOutput $9 '0) $10))
(return (KqpPhysicalQuery '((KqpPhysicalTx '($5 $9) '($11) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType (StructType '('"column1" $7) '('"column2" $7) '('"column3" $7) '('"column4" $7) $2 $3)) '0 '0)) '('('"type" '"query"))))
)
