(
(let $1 (DataType 'String))
(let $2 '('"key" $1))
(let $3 '('('"_logical_id" '9910) '('"_id" '"769d513d-401952b2-dc7e90f7-f6b61a19") '('"_wide_channels" (StructType $2))))
(let $4 (DqPhyStage '() (lambda '() (block '(
  (let $15 '@@["StructType";[["key";["DataType";"String"]];["subkey";["DataType";"String"]];["value";["DataType";"String"]]]]@@)
  (let $16 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $15)))
  (let $17 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" 'false)))))
  (let $18 (YtStat '('Id '"Input") '('RecordsCount '"4") '('DataSize '"166") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $19 (YtTable '"Input" $16 $17 $18 '() (Void) (Void) '"/Root/plato"))
  (let $20 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $19 '('"key") (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $20 '())))
))) $3))
(let $5 (DqCnUnionAll (TDqOutput $4 '0)))
(let $6 (OptionalType $1))
(let $7 (StructType $2 '('"keys" (ListType $1)) '('"next_key" $6) '('"running_max" $1)))
(let $8 '('('"_logical_id" '11082) '('"_id" '"2d097254-7157cc8-67ab7d2b-c5a535c4") '('"_wide_channels" $7)))
(let $9 (DqPhyStage '($5) (lambda '($21) (block '(
  (let $22 (Bool 'true))
  (let $23 (FromFlow (NarrowMap (WideSort (ToFlow $21) '('('0 $22))) (lambda '($36) (AsStruct '('"key" $36))))))
  (let $24 (Chain1Map $23 (lambda '($37) (block '(
    (let $38 (Member $37 '"key"))
    (let $39 '('Max0 $38))
    (return '((AsStruct $39 '('"key" $38)) (AsStruct $39)))
  ))) (lambda '($40 $41) (block '(
    (let $42 (Member $40 '"key"))
    (let $43 '('Max0 (AggrMax $42 (Member (Nth $41 '1) 'Max0))))
    (return '((AsStruct $43 '('"key" $42)) (AsStruct $43)))
  )))))
  (let $25 (StructType '('Max0 $1) $2))
  (let $26 (ListType $25))
  (let $27 (VariantType (StructType '('group $26) '('singleRow $25))))
  (let $28 (Condense1 $24 (lambda '($44) (block '(
    (let $45 (Nth $44 '0))
    (let $46 (Variant $45 'singleRow $27))
    (return '((Member $45 '"key") $46))
  ))) (lambda '($47 $48) (AggrNotEquals (Member (Nth $47 '0) '"key") (Nth $48 '0))) (lambda '($49 $50) (block '(
    (let $51 (Nth $49 '0))
    (let $52 (Visit (Nth $50 '1) 'group (lambda '($53) (Variant (Insert $53 $51) 'group $27)) 'singleRow (lambda '($54) (Variant (AsList $54 $51) 'group $27))))
    (return '((Nth $50 '0) $52))
  )))))
  (let $29 (QueueCreate $25 (Uint64 '"3") (Uint64 '0) (DependsOn $23)))
  (let $30 (Uint64 '1))
  (let $31 (lambda '($67) (Just (Member $67 '"key"))))
  (let $32 (Nothing $6))
  (let $33 (Chain1Map (ToFlow (PreserveStream (OrderedFlatMap $28 (lambda '($55) (Visit (Nth $55 '1) 'group (lambda '($56) (IfPresent (Last $56) (lambda '($57) (OrderedMap (Collect (Map $56 (lambda '($58) (AsStruct '('"key" (Member $58 '"key")))))) (lambda '($59) (AsStruct '('Max0 (Member $57 'Max0)) '('"key" (Member $59 '"key")))))) (List $26))) 'singleRow (lambda '($60) (AsList $60))))) $29 $30)) (lambda '($61) (block '(
    (let $62 (Member $61 '"key"))
    (let $63 '('AggregateList0 (AsList $62)))
    (let $64 (QueuePeek $29 $30 (DependsOn $61)))
    (let $65 (IfPresent $64 $31 $32))
    (let $66 (AsStruct $63 '('Max0 (Member $61 'Max0)) '('_yql_Lead0 $65) '('"key" $62)))
    (return '($66 (AsStruct $63 '('_yql_Lead0 (Void)))))
  ))) (lambda '($68 $69) (block '(
    (let $70 (Nth $69 '1))
    (let $71 (Member $68 '"key"))
    (let $72 '('AggregateList0 (Insert (Member $70 'AggregateList0) $71)))
    (let $73 (QueuePeek $29 $30 (DependsOn $68)))
    (let $74 (IfPresent $73 $31 $32))
    (let $75 (AsStruct $72 '('Max0 (Member $68 'Max0)) '('_yql_Lead0 $74) '('"key" $71)))
    (return '($75 (AsStruct $72 '('_yql_Lead0 (Member $70 '_yql_Lead0)))))
  )))))
  (let $34 (Sort (Map $33 (lambda '($76) (block '(
    (let $77 (Nth $76 '0))
    (return (AsStruct '('"key" (Member $77 '"key")) '('"keys" (Member $77 'AggregateList0)) '('"next_key" (Member $77 '_yql_Lead0)) '('"running_max" (Member $77 'Max0))))
  )))) $22 (lambda '($78) (Member $78 '"key"))))
  (let $35 (lambda '($79) (Member $79 '"key") (Member $79 '"keys") (Member $79 '"next_key") (Member $79 '"running_max")))
  (return (FromFlow (ExpandMap $34 $35)))
))) $8))
(let $10 (DqCnMerge (TDqOutput $9 '0) '('('0 '"Asc"))))
(let $11 (DqPhyStage '($10) (lambda '($80) (FromFlow (NarrowMap (ToFlow $80) (lambda '($81 $82 $83 $84) (AsStruct '('"key" $81) '('"keys" $82) '('"next_key" $83) '('"running_max" $84)))))) '('('"_logical_id" '11094) '('"_id" '"5069b856-b4019ab3-4445cbad-4d9d54e5"))))
(let $12 '($4 $9 $11))
(let $13 '('"key" '"running_max" '"next_key" '"keys"))
(let $14 (DqCnResult (TDqOutput $11 '0) $13))
(return (KqpPhysicalQuery '((KqpPhysicalTx $12 '($14) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $7) '0 '0)) '('('"type" '"query"))))
)
