(
(let $1 (DataType 'Int32))
(let $2 '('"payload" $1))
(let $3 (OptionalType $1))
(let $4 '('"ts" $3))
(let $5 '('"user" (OptionalType (DataType 'String))))
(let $6 (StructType $2 $4 $5))
(let $7 '('('"_logical_id" '3938) '('"_id" '"2dc52735-425d8e85-cd11e64e-193d1640") '('"_wide_channels" $6)))
(let $8 (DqPhyStage '() (lambda '() (block '(
  (let $27 '@@["StructType";[["user";["OptionalType";["DataType";"String"]]];["ts";["OptionalType";["DataType";"Int32"]]];["payload";["DataType";"Int32"]]]]@@)
  (let $28 (YqlRowSpec '('NativeYtTypeFlags '0) '('StrictSchema '1) '('UniqueKeys '0) '('Type $27)))
  (let $29 (YtMeta '('CanWrite '1) '('DoesExist '1) '('YqlCompatibleScheme '1) '('InferredScheme '0) '('IsDynamic '0) '('Attrs '('('"native_strict_schema" '"false")))))
  (let $30 (YtStat '('Id '"Input") '('RecordsCount '"18") '('DataSize '"656") '('ChunkCount '1) '('ModifyTime '0) '('Revision '0)))
  (let $31 (YtTable '"Input" $28 $29 $30 '() (Void) (Void) '"/Root/plato"))
  (let $32 '('"payload" '"ts" '"user"))
  (let $33 (YtReadTable! world (DataSource '"yt" '"/Root/plato") '((YtSection '((YtPath $31 $32 (Void) (Void))) '()))))
  (return (FromFlow (DqReadWideWrap $33 '())))
))) $7))
(let $9 (DqCnHashShuffle (TDqOutput $8 '0) '('2) '0 '"HashV2"))
(let $10 '('Auto 'Many))
(let $11 (Bool 'true))
(let $12 '"_yql_group_session_start")
(let $13 (StructType '($12 $3) $2 $4 $5))
(let $14 '('('"_logical_id" '4005) '('"_id" '"875abaa2-678a0d2-8ab3b254-d432f9cd") '('"_partition_mode" '"aggregate") '('"_wide_channels" $13)))
(let $15 (DqPhyStage '($9) (lambda '($34) (block '(
  (let $35 (NarrowSqueezeToDict (ToFlow $34) (lambda '($38 $39 $40) $40) (lambda '($41 $42 $43) (AsStruct '('"payload" $41) '('"ts" $42) '('"user" $43))) $10))
  (let $36 (Chain1Map (OrderedFlatMap $35 (lambda '($44) (OrderedFlatMap (DictPayloads $44) (lambda '($45) (Sort $45 $11 (lambda '($46) (Member $46 '"ts"))))))) (lambda '($47) (block '(
    (let $48 (Member $47 '"ts"))
    (return '($47 $48 $48 (Member $47 '"user")))
  ))) (lambda '($49 $50) (block '(
    (let $51 (Member $49 '"user"))
    (let $52 (Member $49 '"ts"))
    (let $53 '($49 $52 $52 $51))
    (let $54 (Nth $50 '2))
    (let $55 (If (Coalesce (< $54 $52) $11) (- $52 $54) (- $54 $52)))
    (let $56 (If (And (AggrNotEquals $52 $54) (Coalesce (< (Int32 '"10") $55) $11)) $52 (Nth $50 '1)))
    (let $57 '($49 $56 $52 $51))
    (return (If (AggrNotEquals $51 (Member (Nth $50 '0) '"user")) $53 $57))
  )))))
  (let $37 (lambda '($58) (block '(
    (let $59 (Nth $58 '0))
    (return (Nth $58 '1) (Member $59 '"payload") (Member $59 '"ts") (Member $59 '"user"))
  ))))
  (return (FromFlow (ExpandMap $36 $37)))
))) $14))
(let $16 (DqCnHashShuffle (TDqOutput $15 '0) '('"3" '0) '0 '"HashV2"))
(let $17 '"distinct_playloads")
(let $18 (DataType 'Uint64))
(let $19 (StructType '($17 $18) '('session (ListType $1)) '('"session_len" $18) '('"session_start" $1) $5))
(let $20 '('('"_logical_id" '4485) '('"_id" '"e189d3d0-2bebb49c-37710114-522878a4") '('"_wide_channels" $19)))
(let $21 (DqPhyStage '($16) (lambda '($60) (block '(
  (let $61 (NarrowSqueezeToDict (ToFlow $60) (lambda '($70 $71 $72 $73) '($73 $70)) (lambda '($74 $75 $76 $77) (AsStruct '($12 $74) '('"payload" $75) '('"ts" $76) '('"user" $77))) $10))
  (let $62 (Int32 '"100500"))
  (let $63 (ResourceType '"Set.SetResource.Int32"))
  (let $64 '($63))
  (let $65 '($1))
  (let $66 '('('"strict")))
  (let $67 (Condense1 (OrderedFlatMap $61 (lambda '($78) (OrderedFlatMap (DictPayloads $78) (lambda '($79) $79)))) (lambda '($80) (block '(
    (let $81 (Member $80 '"ts"))
    (let $82 (Uint64 '1))
    (let $83 (DataType 'Uint32))
    (let $84 (TupleType (TupleType $1 $83) (StructType) $1))
    (let $85 (CallableType '() $64 $65 '($83)))
    (let $86 (Udf '"Set.Create" (Void) $84 '"" $85 (VoidType) '"" $66))
    (let $87 (NamedApply $86 '((Member $80 '"payload") (Uint32 '0)) (AsStruct) (DependsOn (String '"payload"))))
    (return (AsStruct '('_yql_agg_0 (AsList (Coalesce $81 $62))) '('_yql_agg_1 $82) '('_yql_agg_2 '($87 $82)) '('_yql_agg_3 $81) '($12 (Member $80 $12)) '('"user" (Member $80 '"user"))))
  ))) (lambda '($88 $89) (Or (AggrNotEquals (Member $88 '"user") (Member $89 '"user")) (AggrNotEquals (Member $88 $12) (Member $89 $12)))) (lambda '($90 $91) (block '(
    (let $92 (Member $90 '"ts"))
    (let $93 (TupleType (TupleType $63) (StructType) $1))
    (let $94 (CallableType '() '((DataType 'Bool)) $64))
    (let $95 (Udf '"Set.WasChanged" (Void) $93 '"" $94 (VoidType) '"" $66))
    (let $96 (TupleType (TupleType $63 $1) (StructType) $1))
    (let $97 (CallableType '() $64 $64 $65))
    (let $98 (Udf '"Set.AddValue" (Void) $96 '"" $97 (VoidType) '"" $66))
    (let $99 (Member $91 '_yql_agg_2))
    (let $100 (NamedApply $98 '((Nth $99 '0) (Member $90 '"payload")) (AsStruct)))
    (let $101 (NamedApply $95 '($100) (AsStruct)))
    (let $102 (If $101 '($100 (Inc (Nth $99 '1))) $99))
    (return (AsStruct '('_yql_agg_0 (Insert (Member $91 '_yql_agg_0) (Coalesce $92 $62))) '('_yql_agg_1 (Inc (Member $91 '_yql_agg_1))) '('_yql_agg_2 $102) '('_yql_agg_3 (AggrMin $92 (Member $91 '_yql_agg_3))) '($12 (Member $91 $12)) '('"user" (Member $91 '"user"))))
  )))))
  (let $68 (Sort (Map $67 (lambda '($103) (block '(
    (let $104 (Sort (Member $103 '_yql_agg_0) $11 (lambda '($105) $105)))
    (return (AsStruct '($17 (Nth (Member $103 '_yql_agg_2) '1)) '('session $104) '('"session_len" (Member $103 '_yql_agg_1)) '('"session_start" (Coalesce (Member $103 '_yql_agg_3) $62)) '('"user" (Member $103 '"user"))))
  )))) '($11 $11) (lambda '($106) '((Member $106 '"user") (Member $106 '"session_start")))))
  (let $69 (lambda '($107) (Member $107 $17) (Member $107 'session) (Member $107 '"session_len") (Member $107 '"session_start") (Member $107 '"user")))
  (return (FromFlow (ExpandMap $68 $69)))
))) $20))
(let $22 (DqCnMerge (TDqOutput $21 '0) '('('"4" '"Asc") '('"3" '"Asc"))))
(let $23 (DqPhyStage '($22) (lambda '($108) (FromFlow (NarrowMap (ToFlow $108) (lambda '($109 $110 $111 $112 $113) (AsStruct '($17 $109) '('session $110) '('"session_len" $111) '('"session_start" $112) '('"user" $113)))))) '('('"_logical_id" '4497) '('"_id" '"9857928b-25a9833-ec9bb39a-ea29d41b"))))
(let $24 '($8 $15 $21 $23))
(let $25 '('"user" '"session_start" 'session '"session_len" $17))
(let $26 (DqCnResult (TDqOutput $23 '0) $25))
(return (KqpPhysicalQuery '((KqpPhysicalTx $24 '($26) '() '('('"type" '"generic")))) '((KqpTxResultBinding (ListType $19) '0 '0)) '('('"type" '"query"))))
)
