(
(let $1 (DataType 'Int32))
(let $2 '('"user" (OptionalType (DataType 'String))))
(let $3 '('('"_logical_id" '3861) '('"_id" '"6d85db3f-a0b464c2-bc331334-caaee8c") '('"_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" '4285) '('"_id" '"d7c1a7bf-db90802c-12690cf3-1fbd9d2c") '('"_wide_channels" $6)))
(let $8 (DqPhyStage '($5) (lambda '($20) (block '(
  (let $21 (NarrowSqueezeToDict (ToFlow $20) (lambda '($29 $30) $30) (lambda '($31 $32) (AsStruct '('"ts" $31) '('"user" $32))) '('Auto 'Many)))
  (let $22 (Bool 'true))
  (let $23 (Chain1Map (OrderedFlatMap $21 (lambda '($33) (OrderedFlatMap (DictPayloads $33) (lambda '($34) (Sort $34 $22 (lambda '($35) (Member $35 '"ts"))))))) (lambda '($36) (block '(
    (let $37 (Member $36 '"ts"))
    (return '($36 $37 $37 (Member $36 '"user")))
  ))) (lambda '($38 $39) (block '(
    (let $40 (Member $38 '"user"))
    (let $41 (Member $38 '"ts"))
    (let $42 '($38 $41 $41 $40))
    (let $43 (Nth $39 '2))
    (let $44 (If (Coalesce (< $43 $41) $22) (- $41 $43) (- $43 $41)))
    (let $45 (If (And (AggrNotEquals $41 $43) (Coalesce (< (Int32 '"10") $44) $22)) $41 (Nth $39 '1)))
    (let $46 '($38 $45 $41 $40))
    (return (If (AggrNotEquals $40 (Member (Nth $39 '0) '"user")) $42 $46))
  )))))
  (let $24 (Int32 '"100500"))
  (let $25 '"_yql_group_session_start")
  (let $26 (Condense1 $23 (lambda '($47) (block '(
    (let $48 (Nth $47 '0))
    (let $49 (Member $48 '"ts"))
    (return (AsStruct '('_yql_agg_0 (AsList (Coalesce $49 $24))) '('_yql_agg_1 (Uint64 '1)) '('_yql_agg_2 $49) '($25 (Nth $47 '1)) '('"user" (Member $48 '"user"))))
  ))) (lambda '($50 $51) (Or (AggrNotEquals (Member (Nth $50 '0) '"user") (Member $51 '"user")) (AggrNotEquals (Nth $50 '1) (Member $51 $25)))) (lambda '($52 $53) (block '(
    (let $54 (Member (Nth $52 '0) '"ts"))
    (return (AsStruct '('_yql_agg_0 (Insert (Member $53 '_yql_agg_0) (Coalesce $54 $24))) '('_yql_agg_1 (Inc (Member $53 '_yql_agg_1))) '('_yql_agg_2 (AggrMin $54 (Member $53 '_yql_agg_2))) '($25 (Member $53 $25)) '('"user" (Member $53 '"user"))))
  )))))
  (let $27 (Sort (Map $26 (lambda '($55) (block '(
    (let $56 (Sort (Member $55 '_yql_agg_0) $22 (lambda '($57) $57)))
    (return (AsStruct '('session $56) '('"session_len" (Member $55 '_yql_agg_1)) '('"session_start" (Coalesce (Member $55 '_yql_agg_2) $24)) '('"user" (Member $55 '"user"))))
  )))) '($22 $22) (lambda '($58) '((Member $58 '"user") (Member $58 '"session_start")))))
  (let $28 (lambda '($59) (Member $59 'session) (Member $59 '"session_len") (Member $59 '"session_start") (Member $59 '"user")))
  (return (FromFlow (ExpandMap $27 $28)))
))) $7))
(let $9 (DqCnMerge (TDqOutput $8 '0) '('('"3" '"Asc") '('2 '"Asc"))))
(let $10 (DqPhyStage '($9) (lambda '($60) (FromFlow (NarrowMap (ToFlow $60) (lambda '($61 $62 $63 $64) (AsStruct '('session $61) '('"session_len" $62) '('"session_start" $63) '('"user" $64)))))) '('('"_logical_id" '4297) '('"_id" '"9040d798-400b5264-c0a49bc7-c30d9c88"))))
(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"))))
)
