(
(let $1 (DataType 'String))
(let $2 '('"key" $1))
(let $3 '('"subkey" $1))
(let $4 '('('"_logical_id" '7442) '('"_id" '"dd72a7b2-3e490dbd-4419fc3-1bf2dcf6") '('"_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" '"91ac1a8f-203008f8-daade5c7-20f2fe11") '('"_partition_mode" '"aggregate")))
(let $9 (DqPhyStage '($6) (lambda '($18) (block '(
  (let $19 (Bool 'true))
  (let $20 (Chain1Map (NarrowMap (WideSort (ToFlow $18) '('('0 $19) '('1 $19))) (lambda '($25 $26) (AsStruct '('"key" $25) '('"subkey" $26)))) (lambda '($27) (block '(
    (let $28 (Member $27 '"subkey"))
    (let $29 (SafeCast $28 $7))
    (let $30 '('FirstValue0 $29))
    (let $31 '('FirstValueIgnoreNulls0 $29))
    (let $32 '('LastValue0 $29))
    (let $33 '('LastValueIgnoreNulls0 $29))
    (let $34 (AsStruct $30 $31 $32 $33 '('"key" (Member $27 '"key")) '('"subkey" $28)))
    (let $35 (AsStruct $30 $31 $32 $33))
    (return '($34 $35))
  ))) (lambda '($36 $37) (block '(
    (let $38 (Nth $37 '1))
    (let $39 '('FirstValue0 (Member $38 'FirstValue0)))
    (let $40 (Member $38 'FirstValueIgnoreNulls0))
    (let $41 (Member $36 '"subkey"))
    (let $42 (SafeCast $41 $7))
    (let $43 (If (Exists $40) $40 $42))
    (let $44 '('FirstValueIgnoreNulls0 $43))
    (let $45 '('LastValue0 $42))
    (let $46 (If (Exists $42) $42 (Member $38 'LastValueIgnoreNulls0)))
    (let $47 '('LastValueIgnoreNulls0 $46))
    (let $48 (AsStruct $39 $44 $45 $47 '('"key" (Member $36 '"key")) '('"subkey" $41)))
    (let $49 (AsStruct $39 $44 $45 $47))
    (return '($48 $49))
  )))))
  (let $21 (StructType '('FirstValue0 $7) '('FirstValueIgnoreNulls0 $7) '('LastValue0 $7) '('LastValueIgnoreNulls0 $7) $2 $3))
  (let $22 (ListType $21))
  (let $23 (VariantType (StructType '('group $22) '('singleRow $21))))
  (let $24 (Condense1 $20 (lambda '($50) (block '(
    (let $51 (Nth $50 '0))
    (let $52 (Variant $51 'singleRow $23))
    (return '('((Member $51 '"key") (Member $51 '"subkey")) $52))
  ))) (lambda '($53 $54) (block '(
    (let $55 (Nth $53 '0))
    (let $56 (Nth $54 '0))
    (return (Or (AggrNotEquals (Member $55 '"key") (Nth $56 '0)) (AggrNotEquals (Member $55 '"subkey") (Nth $56 '1))))
  ))) (lambda '($57 $58) (block '(
    (let $59 (Nth $57 '0))
    (let $60 (Visit (Nth $58 '1) 'group (lambda '($61) (Variant (Insert $61 $59) 'group $23)) 'singleRow (lambda '($62) (Variant (AsList $62 $59) 'group $23))))
    (return '((Nth $58 '0) $60))
  )))))
  (return (FromFlow (OrderedMap (FlatMap $24 (lambda '($63) (Visit (Nth $63 '1) 'group (lambda '($64) (block '(
    (let $65 (IfPresent (Last $64) (lambda '($66) (OrderedMap (Collect (Map $64 (lambda '($67) (AsStruct '('"key" (Member $67 '"key")) '('"subkey" (Member $67 '"subkey")))))) (lambda '($68) (AsStruct '('FirstValue0 (Member $66 'FirstValue0)) '('FirstValueIgnoreNulls0 (Member $66 'FirstValueIgnoreNulls0)) '('LastValue0 (Member $66 'LastValue0)) '('LastValueIgnoreNulls0 (Member $66 'LastValueIgnoreNulls0)) '('"key" (Member $68 '"key")) '('"subkey" (Member $68 '"subkey")))))) (List $22)))
    (return (Map $65 (lambda '($69) (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"))))))
  ))) 'singleRow (lambda '($70) (AsList (AsStruct '('"column1" (Member $70 'FirstValue0)) '('"column2" (Member $70 'FirstValueIgnoreNulls0)) '('"column3" (Member $70 'LastValue0)) '('"column4" (Member $70 'LastValueIgnoreNulls0)) '('"key" (Member $70 '"key")) '('"subkey" (Member $70 '"subkey")))))))) (lambda '($71) (AsStruct '('"column1" (Member $71 '"column1")) '('"column2" (Member $71 '"column2")) '('"column3" (Member $71 '"column3")) '('"column4" (Member $71 '"column4")) '('"key" (Member $71 '"key")) '('"subkey" (Member $71 '"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"))))
)
