(
(let $1 (DataType 'Int32))
(let $2 '('"user" (OptionalType (DataType 'String))))
(let $3 '('('"_logical_id" '4064) '('"_id" '"df9612b3-4f3b5c7-b552d73d-c3a59824") '('"_wide_channels" (StructType '('"ts" (OptionalType $1)) $2))))
(let $4 (DqPhyStage '() (lambda '() (block '(
  (let $14 '@@["StructType";[["user";["OptionalType";["DataType";"String"]]];["ts";["OptionalType";["DataType";"Int32"]]];["payload";["DataType";"Int32"]]]]@@)
  (let $15 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $14)))
  (let $16 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $17 (YtStat '('Id '"Input") '('RecordsCount '"18") '('DataSize '"656") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $18 (YtTable '"Input" $15 $16 $17 '() (Void) (Void) '"/Root/plato"))
  (let $19 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $18 '('"ts" '"user") (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $19 '())))
))) $3))
(let $5 (DqCnHashShuffle (TDqOutput $4 '0) '('1) '0 '"HashV2"))
(let $6 (StructType '('session (ListType $1)) '('"session_len" (DataType 'Uint64)) '('"session_start" $1) $2))
(let $7 '('('"_logical_id" '4372) '('"_id" '"db7cf716-5d80045f-f08aef5a-ec40013b") '('"_wide_channels" $6)))
(let $8 (DqPhyStage '($5) (lambda '($20) (block '(
  (let $21 (NarrowSqueezeToDict (ToFlow $20) (lambda '($32 $33) $33) (lambda '($34 $35) (AsStruct '('"ts" $34) '('"user" $35))) '('Auto 'Many)))
  (let $22 (Bool 'true))
  (let $23 (Int32 '0))
  (let $24 (Int32 '1))
  (let $25 '('"count" $24))
  (let $26 (Chain1Map (OrderedFlatMap $21 (lambda '($36) (OrderedFlatMap (DictPayloads $36) (lambda '($37) (Sort $37 $22 (lambda '($38) (Member $38 '"ts"))))))) (lambda '($39) (block '(
    (let $40 (Coalesce (Member $39 '"ts") $23))
    (return '($39 $40 (AsStruct $25 '('"value" $40)) (Member $39 '"user")))
  ))) (lambda '($41 $42) (block '(
    (let $43 (Member $41 '"user"))
    (let $44 (Coalesce (Member $41 '"ts") $23))
    (let $45 '($41 $44 (AsStruct $25 '('"value" $44)) $43))
    (let $46 (Nth $42 '2))
    (let $47 (+ (Member $46 '"count") $24))
    (let $48 (+ (Member $46 '"value") $44))
    (let $49 (If (== (Unwrap (% $47 (Int32 '2))) $24) $48 (Nth $42 '1)))
    (let $50 '($41 $49 (AsStruct '('"count" $47) '('"value" $48)) $43))
    (return (If (AggrNotEquals $43 (Member (Nth $42 '0) '"user")) $45 $50))
  )))))
  (let $27 (Int32 '"100500"))
  (let $28 '"_yql_group_session_start")
  (let $29 (Condense1 $26 (lambda '($51) (block '(
    (let $52 (Nth $51 '0))
    (return (AsStruct '('_yql_agg_0 (AsList (Coalesce (Member $52 '"ts") $27))) '('_yql_agg_1 (Uint64 '1)) '($28 (Nth $51 '1)) '('"user" (Member $52 '"user"))))
  ))) (lambda '($53 $54) (Or (AggrNotEquals (Member (Nth $53 '0) '"user") (Member $54 '"user")) (AggrNotEquals (Nth $53 '1) (Member $54 $28)))) (lambda '($55 $56) (AsStruct '('_yql_agg_0 (Insert (Member $56 '_yql_agg_0) (Coalesce (Member (Nth $55 '0) '"ts") $27))) '('_yql_agg_1 (Inc (Member $56 '_yql_agg_1))) '($28 (Member $56 $28)) '('"user" (Member $56 '"user"))))))
  (let $30 (Sort (Map $29 (lambda '($57) (block '(
    (let $58 (Sort (Member $57 '_yql_agg_0) $22 (lambda '($59) $59)))
    (return (AsStruct '('session $58) '('"session_len" (Member $57 '_yql_agg_1)) '('"session_start" (Member $57 $28)) '('"user" (Member $57 '"user"))))
  )))) '($22 $22) (lambda '($60) '((Member $60 '"user") (Member $60 '"session_start")))))
  (let $31 (lambda '($61) (Member $61 'session) (Member $61 '"session_len") (Member $61 '"session_start") (Member $61 '"user")))
  (return (FromFlow (ExpandMap $30 $31)))
))) $7))
(let $9 (DqCnMerge (TDqOutput $8 '0) '('('"3" '"Asc") '('2 '"Asc"))))
(let $10 (DqPhyStage '($9) (lambda '($62) (FromFlow (NarrowMap (ToFlow $62) (lambda '($63 $64 $65 $66) (AsStruct '('session $63) '('"session_len" $64) '('"session_start" $65) '('"user" $66)))))) '('('"_logical_id" '4384) '('"_id" '"3537ee7f-72791ae3-2cdb8c7c-b57e0842"))))
(let $11 '($4 $8 $10))
(let $12 '('"user" '"session_start" 'session '"session_len"))
(let $13 (DqCnResult (TDqOutput $10 '0) $12))
(return (KqpPhysicalQuery '((KqpPhysicalTx $11 '($13) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $6) '0 '0)) '('('"type" '"query"))))
)
