{"title":"SESSIONIZE","description":"Assign ordered events to gap-based sessions and calculate session-level measures with DuckDB SQL in Altertable.","order":700,"askSuggestions":["How do I get one row per session with event counts?","Why is a session longer than the gap I set?","How do I remove duplicate events before assigning sessions?"],"navigation":true,"body":{"code":"var Component=(()=>{var p=Object.create;var t=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var T=(i,n)=>()=>(n||i((n={exports:{}}).exports,n),n.exports),R=(i,n)=>{for(var r in n)t(i,r,{get:n[r],enumerable:!0})},c=(i,n,r,o)=>{if(n&&typeof n==\"object\"||typeof n==\"function\")for(let s of E(n))!I.call(i,s)&&s!==r&&t(i,s,{get:()=>n[s],enumerable:!(o=m(n,s))||o.enumerable});return i};var v=(i,n,r)=>(r=i!=null?p(S(i)):{},c(n||!i||!i.__esModule?t(r,\"default\",{value:i,enumerable:!0}):r,i)),A=i=>c(t({},\"__esModule\",{value:!0}),i);var l=T((w,a)=>{a.exports=_jsx_runtime});var O={};R(O,{default:()=>u,frontmatter:()=>N});var e=v(l()),N={title:\"SESSIONIZE\",description:\"Assign ordered events to gap-based sessions and calculate session-level measures with DuckDB SQL in Altertable.\",order:700,askSuggestions:[\"How do I get one row per session with event counts?\",\"Why is a session longer than the gap I set?\",\"How do I remove duplicate events before assigning sessions?\"]};function h(i){let n={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",h3:\"h3\",li:\"li\",ol:\"ol\",p:\"p\",pre:\"pre\",strong:\"strong\",ul:\"ul\",...i.components},{DocsBeta:r,DocsPreviewDocument:o,DocsPreviewSection:s}=n;return r||d(\"DocsBeta\",!0),o||d(\"DocsPreviewDocument\",!0),s||d(\"DocsPreviewSection\",!0),(0,e.jsxs)(o,{anchors:\"sessionize first-example operator-syntax how-sessions-are-formed remove-duplicate-events choose-the-output-mode preserve-every-input-row number-rows-within-each-partition return-one-row-per-session calculate-session-measures define-the-event-streams partition-by order-by gap count-gap-boundaries-with-granularity output-names-and-ordering persist-session-results learn-more\",children:[(0,e.jsxs)(s,{sectionId:\"sessionize\",sectionTitle:\"SESSIONIZE\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,e.jsx)(n.h1,{id:\"sessionize\",children:\"SESSIONIZE\"}),`\n`,(0,e.jsx)(r,{children:(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"SESSIONIZE\"}),` is available in beta. Its syntax and behavior may change as the\nimplementation evolves.`]})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"SESSIONIZE\"}),\" assigns ordered rows to sessions separated by a period of inactivity. Use it to label events with a session ID or to calculate one result row per session without building the usual \",(0,e.jsx)(n.code,{children:\"lag\"}),\", cumulative-sum, and grouping pipeline.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"Run these statements through Altertable's \",(0,e.jsx)(n.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"}),\".\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"first-example\",sectionTitle:\"First example\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,e.jsx)(n.h2,{id:\"first-example\",children:\"First example\"}),`\n`,(0,e.jsx)(n.p,{children:\"Suppose an event table contains one row for each user action:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`WITH events(user_id, ts, event) AS (\n    VALUES\n        (1, TIMESTAMP '2024-01-01 10:00:00', 'view'),\n        (1, TIMESTAMP '2024-01-01 10:10:00', 'click'),\n        (1, TIMESTAMP '2024-01-01 10:50:00', 'purchase'),\n        (2, TIMESTAMP '2024-01-01 10:00:00', 'view')\n)\nSELECT\n    user_id,\n    ts,\n    event,\n    session_start,\n    session_end,\n    session_id\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    GAP INTERVAL 30 MINUTE\n)\nORDER BY user_id, ts;\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"The first two events for user \",(0,e.jsx)(n.code,{children:\"1\"}),\" belong to session \",(0,e.jsx)(n.code,{children:\"1\"}),\" because they are 10 minutes apart. The 40-minute gap before \",(0,e.jsx)(n.code,{children:\"purchase\"}),\" starts session \",(0,e.jsx)(n.code,{children:\"2\"}),\". Session IDs restart at \",(0,e.jsx)(n.code,{children:\"1\"}),\" for each user.\"]}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"PARTITION BY user_id\"}),\" creates an independent event stream for each user.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"ORDER BY ts\"}),\" defines event order.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"GAP INTERVAL 30 MINUTE\"}),\" starts a new session after more than 30 minutes of inactivity.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"session_start\"}),\" and \",(0,e.jsx)(n.code,{children:\"session_end\"}),\" contain the first and last time-key values in the session.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"session_id\"}),\" is a 1-based number within each partition.\"]}),`\n`]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"operator-syntax\",sectionTitle:\"Operator syntax\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,e.jsx)(n.h2,{id:\"operator-syntax\",children:\"Operator syntax\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"The supported \",(0,e.jsx)(n.code,{children:\"FROM\"}),\"-clause shape is:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`FROM input_relation [input_alias]\nSESSIONIZE (\n    [PARTITION BY partition_column [, ...]]\n    ORDER BY time_column [, tie_breaker_column ...]\n    GAP interval_expression [GRANULARITY unit]\n    [DEDUPLICATE BY column [, ...]\n     KEEP { FIRST BY (column [ASC | DESC] [NULLS FIRST | LAST] [, ...])\n          | LAST BY (column [, ...]) }]\n    [SESSION_ID AS alias]\n    [SESSION_START AS alias]\n    [SESSION_END AS alias]\n    [ROW_NUMBER [AS alias]]\n    [MEASURES measure_expression AS output_name [, ...]]\n    [ONE ROW PER SESSION]\n)\n[session_alias]\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" and \",(0,e.jsx)(n.code,{children:\"GAP\"}),\" are required. \",(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\" is optional. \",(0,e.jsx)(n.code,{children:\"MEASURES\"}),\" can only be used with \",(0,e.jsx)(n.code,{children:\"ONE ROW PER SESSION\"}),\". The metadata options (\",(0,e.jsx)(n.code,{children:\"SESSION_ID\"}),\", \",(0,e.jsx)(n.code,{children:\"SESSION_START\"}),\", \",(0,e.jsx)(n.code,{children:\"SESSION_END\"}),\", and \",(0,e.jsx)(n.code,{children:\"ROW_NUMBER\"}),\") can appear in any order after deduplication options and before \",(0,e.jsx)(n.code,{children:\"MEASURES\"}),\" or \",(0,e.jsx)(n.code,{children:\"ONE ROW PER SESSION\"}),\".\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"The source relation must appear immediately before \",(0,e.jsx)(n.code,{children:\"SESSIONIZE\"}),\". The result can be aliased after the clause:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT s.user_id, s.session_id\nFROM events AS e\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    GAP INTERVAL 30 MINUTE\n) AS s;\n`})}),`\n`,(0,e.jsx)(n.p,{children:\"Common SQL constructs such as CTEs, subqueries, joins, outer filters, grouping, and aliases can surround the operator.\"}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"how-sessions-are-formed\",sectionTitle:\"How sessions are formed\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,e.jsx)(n.h2,{id:\"how-sessions-are-formed\",children:\"How sessions are formed\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Within each partition, \",(0,e.jsx)(n.code,{children:\"SESSIONIZE\"}),\" sorts rows by the \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" columns and compares each time-key value with the preceding value. By default, a new session starts when:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-text\",children:`current_time > previous_time + GAP\n`})}),`\n`,(0,e.jsx)(n.p,{children:\"Equality stays in the current session. With a 30-minute gap, events exactly 30 minutes apart belong to the same session; an event 30 minutes and one second later starts a new one.\"}),`\n`,(0,e.jsx)(n.p,{children:\"The comparison uses consecutive events, not the session start. A session can therefore last longer than the gap as long as each event arrives within the gap after the previous event.\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"With \",(0,e.jsx)(n.code,{children:\"GRANULARITY\"}),\", the comparison counts unit boundaries instead of exact elapsed time. If \",(0,e.jsx)(n.code,{children:\"DEDUPLICATE BY\"}),\" is present, only surviving rows participate in gap comparisons and determine session boundaries.\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"remove-duplicate-events\",sectionTitle:\"Remove duplicate events\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,e.jsx)(n.h2,{id:\"remove-duplicate-events\",children:\"Remove duplicate events\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Use \",(0,e.jsx)(n.code,{children:\"DEDUPLICATE BY\"}),\" with \",(0,e.jsx)(n.code,{children:\"KEEP\"}),\" to choose one row for each duplicate key before assigning sessions. For example, keep the earliest received copy of each event:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts, event_id\n    GAP INTERVAL 30 MINUTE\n    DEDUPLICATE BY event_id KEEP FIRST BY (received_at ASC NULLS LAST)\n)\nORDER BY user_id, ts, event_id;\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Deduplication happens within each \",(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\" partition. If the same \",(0,e.jsx)(n.code,{children:\"event_id\"}),\" occurs for two users, each user keeps a copy. It is equivalent to global deduplication by event ID only when every copy has the same partition values.\"]}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"KEEP FIRST BY (columns)\"}),\" keeps the first row under the specified sort order. It accepts \",(0,e.jsx)(n.code,{children:\"ASC\"}),\", \",(0,e.jsx)(n.code,{children:\"DESC\"}),\", \",(0,e.jsx)(n.code,{children:\"NULLS FIRST\"}),\", and \",(0,e.jsx)(n.code,{children:\"NULLS LAST\"}),\", using DuckDB's defaults when omitted.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"KEEP LAST BY (columns)\"}),\" keeps the greatest tuple, with \",(0,e.jsx)(n.code,{children:\"NULL\"}),\" ranked lowest. It does not accept sort direction or null-order modifiers.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[\"Ties on all \",(0,e.jsx)(n.code,{children:\"KEEP\"}),\" columns are resolved by the operator's \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\". If those columns also tie, the surviving row is unspecified.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[\"Null deduplication keys compare equal, as in \",(0,e.jsx)(n.code,{children:\"GROUP BY\"}),\".\"]}),`\n`,(0,e.jsxs)(n.li,{children:[\"Deduplication and \",(0,e.jsx)(n.code,{children:\"KEEP\"}),\" keys must be input columns. \",(0,e.jsx)(n.code,{children:\"KEEP\"}),\" is required; parentheses are optional for a single \",(0,e.jsx)(n.code,{children:\"KEEP\"}),\" key.\"]}),`\n`]}),`\n`,(0,e.jsx)(n.p,{children:\"Dropped rows are absent from both output modes. They do not bridge or split sessions and do not contribute to row numbers, session boundaries, or measures.\"}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"choose-the-output-mode\",sectionTitle:\"Choose the output mode\",anchors:\"\",ancestors:\"\",index:\"5\",children:[(0,e.jsx)(n.h2,{id:\"choose-the-output-mode\",children:\"Choose the output mode\"}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"preserve-every-input-row\",sectionTitle:\"Preserve every input row\",anchors:\"\",ancestors:\"choose-the-output-mode\",index:\"6\",children:[(0,e.jsx)(n.h3,{id:\"preserve-every-input-row\",children:\"Preserve every input row\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Row-preserving mode is the default. It returns every surviving input row, with the input columns in their original order, followed by \",(0,e.jsx)(n.code,{children:\"session_start\"}),\", \",(0,e.jsx)(n.code,{children:\"session_end\"}),\", and \",(0,e.jsx)(n.code,{children:\"session_id\"}),\" (or their aliases):\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    GAP INTERVAL 30 MINUTE\n);\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Every row in the same session receives the same metadata. \",(0,e.jsx)(n.code,{children:\"session_start\"}),\" and \",(0,e.jsx)(n.code,{children:\"session_end\"}),\" are the first and last time-key values, not calculated window boundaries such as \",(0,e.jsx)(n.code,{children:\"session_end + GAP\"}),\".\"]}),`\n`,(0,e.jsx)(n.p,{children:\"Use this mode when downstream logic still needs individual events:\"}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`WITH labeled_events AS (\n    SELECT *\n    FROM events\n    SESSIONIZE (\n        PARTITION BY user_id\n        ORDER BY ts, event_id\n        GAP INTERVAL 30 MINUTE\n    )\n)\nSELECT *\nFROM labeled_events\nWHERE session_id = 1\nORDER BY user_id, ts, event_id;\n`})}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"number-rows-within-each-partition\",sectionTitle:\"Number rows within each partition\",anchors:\"\",ancestors:\"choose-the-output-mode\",index:\"7\",children:[(0,e.jsx)(n.h3,{id:\"number-rows-within-each-partition\",children:\"Number rows within each partition\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Add \",(0,e.jsx)(n.code,{children:\"ROW_NUMBER\"}),\" to append a \",(0,e.jsx)(n.code,{children:\"BIGINT\"}),\" column named \",(0,e.jsx)(n.code,{children:\"row_number\"}),\" after the session ID, or use \",(0,e.jsx)(n.code,{children:\"ROW_NUMBER AS alias\"}),\" to name it:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts, event_id\n    GAP INTERVAL 30 MINUTE\n    DEDUPLICATE BY event_id KEEP FIRST BY (received_at ASC NULLS LAST)\n    ROW_NUMBER AS event_number\n)\nORDER BY user_id, event_number;\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Numbering starts at \",(0,e.jsx)(n.code,{children:\"1\"}),\" in each partition and counts only rows that survive deduplication. It continues across session boundaries. Add stable tie-breakers to \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" when row numbering must be deterministic.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"ROW_NUMBER\"}),\" is optional and cannot be used with \",(0,e.jsx)(n.code,{children:\"ONE ROW PER SESSION\"}),\".\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"return-one-row-per-session\",sectionTitle:\"Return one row per session\",anchors:\"\",ancestors:\"choose-the-output-mode\",index:\"8\",children:[(0,e.jsx)(n.h3,{id:\"return-one-row-per-session\",children:\"Return one row per session\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Add \",(0,e.jsx)(n.code,{children:\"ONE ROW PER SESSION\"}),\" to emit session metadata without preserving the input rows:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    GAP INTERVAL 30 MINUTE\n    ONE ROW PER SESSION\n)\nORDER BY user_id, session_id;\n`})}),`\n`,(0,e.jsx)(n.p,{children:\"The output contains, in order:\"}),`\n`,(0,e.jsxs)(n.ol,{children:[`\n`,(0,e.jsxs)(n.li,{children:[\"The \",(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\" columns, when present.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"session_start\"}),\", \",(0,e.jsx)(n.code,{children:\"session_end\"}),\", and \",(0,e.jsx)(n.code,{children:\"session_id\"}),\", or their aliases.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[\"The explicitly declared \",(0,e.jsx)(n.code,{children:\"MEASURES\"}),\", when present.\"]}),`\n`]}),`\n`,(0,e.jsxs)(n.p,{children:[\"No event count is added automatically. Add \",(0,e.jsx)(n.code,{children:\"count(*)\"}),\" as a measure when you need one.\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"calculate-session-measures\",sectionTitle:\"Calculate session measures\",anchors:\"\",ancestors:\"\",index:\"9\",children:[(0,e.jsx)(n.h2,{id:\"calculate-session-measures\",children:\"Calculate session measures\"}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"MEASURES\"}),\" calculates values across all surviving rows in a session. Every measure needs an \",(0,e.jsx)(n.code,{children:\"AS\"}),\" alias, and the clause requires \",(0,e.jsx)(n.code,{children:\"ONE ROW PER SESSION\"}),\":\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`WITH events(user_id, ts, event, amount) AS (\n    VALUES\n        (1, TIMESTAMP '2024-01-01 10:00:00', 'view', 0),\n        (1, TIMESTAMP '2024-01-01 10:10:00', 'purchase', 49),\n        (1, TIMESTAMP '2024-01-01 10:50:00', 'view', 0)\n)\nSELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts\n    GAP INTERVAL 30 MINUTE\n    MEASURES\n        count(*) AS event_count,\n        sum(amount) AS revenue,\n        FIRST(event) AS first_event,\n        event AS last_event,\n        list(event) AS events\n    ONE ROW PER SESSION\n)\nORDER BY user_id, session_id;\n`})}),`\n`,(0,e.jsx)(n.p,{children:\"Measure expressions support:\"}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[\"Aggregates such as \",(0,e.jsx)(n.code,{children:\"count\"}),\", \",(0,e.jsx)(n.code,{children:\"sum\"}),\", \",(0,e.jsx)(n.code,{children:\"avg\"}),\", \",(0,e.jsx)(n.code,{children:\"min\"}),\", \",(0,e.jsx)(n.code,{children:\"max\"}),\", \",(0,e.jsx)(n.code,{children:\"list\"}),\", and \",(0,e.jsx)(n.code,{children:\"count(DISTINCT ...)\"}),\".\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"FIRST(expression)\"}),\" and \",(0,e.jsx)(n.code,{children:\"LAST(expression)\"}),\" for values from the first and last rows.\"]}),`\n`,(0,e.jsx)(n.li,{children:\"A bare input column, which returns its value from the last row of the session.\"}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\" columns, which are constant throughout a session.\"]}),`\n`,(0,e.jsx)(n.li,{children:\"Constants, scalar functions, and arithmetic composed from these values.\"}),`\n`]}),`\n`,(0,e.jsxs)(n.p,{children:[\"Measures use whole-session semantics. Pattern-only expressions from \",(0,e.jsx)(n.code,{children:\"MATCH_RECOGNIZE\"}),\", including pattern variables, \",(0,e.jsx)(n.code,{children:\"CLASSIFIER()\"}),\", \",(0,e.jsx)(n.code,{children:\"MATCH_NUMBER()\"}),\", \",(0,e.jsx)(n.code,{children:\"PREV\"}),\", \",(0,e.jsx)(n.code,{children:\"NEXT\"}),\", \",(0,e.jsx)(n.code,{children:\"RUNNING\"}),\", and \",(0,e.jsx)(n.code,{children:\"FINAL\"}),\", are not supported.\"]}),`\n`,(0,e.jsxs)(n.p,{children:[\"Aggregate \",(0,e.jsx)(n.code,{children:\"FILTER\"}),\" clauses and aggregate-local \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" clauses are not supported in \",(0,e.jsx)(n.code,{children:\"SESSIONIZE\"}),\" measures. When you need them, preserve the sessionized rows and aggregate by the partition columns and \",(0,e.jsx)(n.code,{children:\"session_id\"}),\" in an outer query.\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"define-the-event-streams\",sectionTitle:\"Define the event streams\",anchors:\"\",ancestors:\"\",index:\"10\",children:[(0,e.jsx)(n.h2,{id:\"define-the-event-streams\",children:\"Define the event streams\"}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"partition-by\",sectionTitle:\"PARTITION BY\",anchors:\"\",ancestors:\"define-the-event-streams\",index:\"11\",children:[(0,e.jsx)(n.h3,{id:\"partition-by\",children:(0,e.jsx)(n.code,{children:\"PARTITION BY\"})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\" splits the input into independent event streams:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`PARTITION BY user_id, device_id\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"Sessions never cross a partition boundary. Without \",(0,e.jsx)(n.code,{children:\"PARTITION BY\"}),\", the entire input is treated as one stream.\"]}),`\n`,(0,e.jsx)(n.p,{children:\"Partition expressions must be input columns. In summary mode, partition columns appear first in the result and can be referenced directly in measures.\"}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"order-by\",sectionTitle:\"ORDER BY\",anchors:\"\",ancestors:\"define-the-event-streams\",index:\"12\",children:[(0,e.jsx)(n.h3,{id:\"order-by\",children:(0,e.jsx)(n.code,{children:\"ORDER BY\"})}),`\n`,(0,e.jsxs)(n.p,{children:[\"The first \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" column is the time key:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`ORDER BY ts, event_id\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"It must be an ascending input column with a \",(0,e.jsx)(n.code,{children:\"DATE\"}),\", \",(0,e.jsx)(n.code,{children:\"TIMESTAMP\"}),\", or \",(0,e.jsx)(n.code,{children:\"TIMESTAMPTZ\"}),\" type. Supported timestamp precisions include seconds, milliseconds, microseconds, and nanoseconds.\"]}),`\n`,(0,e.jsx)(n.p,{children:\"A null time key on a surviving row or temporal overflow causes a runtime error. Null time keys on rows removed by deduplication do not cause an error.\"}),`\n`,(0,e.jsx)(n.p,{children:\"Additional columns are tie-breakers. Add a stable tie-breaker when multiple events can have the same timestamp and their relative order matters.\"}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"gap\",sectionTitle:\"GAP\",anchors:\"\",ancestors:\"define-the-event-streams\",index:\"13\",children:[(0,e.jsx)(n.h3,{id:\"gap\",children:(0,e.jsx)(n.code,{children:\"GAP\"})}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"GAP\"}),\" sets the maximum inactivity period within a session:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`GAP INTERVAL 30 MINUTE\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"The expression must be a non-null, strictly positive, foldable DuckDB \",(0,e.jsx)(n.code,{children:\"INTERVAL\"}),\". Dynamic per-row gaps are not supported.\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"count-gap-boundaries-with-granularity\",sectionTitle:\"Count gap boundaries with GRANULARITY\",anchors:\"\",ancestors:\"define-the-event-streams\",index:\"14\",children:[(0,e.jsxs)(n.h3,{id:\"count-gap-boundaries-with-granularity\",children:[\"Count gap boundaries with \",(0,e.jsx)(n.code,{children:\"GRANULARITY\"})]}),`\n`,(0,e.jsxs)(n.p,{children:[\"Add \",(0,e.jsx)(n.code,{children:\"GRANULARITY unit\"}),\" to compare \",(0,e.jsx)(n.code,{children:\"date_diff(unit, previous_time, current_time)\"}),\" with the gap expressed in that unit:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`GAP INTERVAL 30 MINUTE GRANULARITY SECOND\n`})}),`\n`,(0,e.jsxs)(n.p,{children:[\"For example, events at \",(0,e.jsx)(n.code,{children:\"00:00:00.1\"}),\" and \",(0,e.jsx)(n.code,{children:\"00:30:00.9\"}),\" are 1,800.8 seconds apart but cross 1,800 second boundaries. They stay in the same session with \",(0,e.jsx)(n.code,{children:\"GRANULARITY SECOND\"}),\"; the default exact-time comparison would split them.\"]}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[\"Supported units are \",(0,e.jsx)(n.code,{children:\"MICROSECOND\"}),\", \",(0,e.jsx)(n.code,{children:\"MILLISECOND\"}),\", \",(0,e.jsx)(n.code,{children:\"SECOND\"}),\", \",(0,e.jsx)(n.code,{children:\"MINUTE\"}),\", \",(0,e.jsx)(n.code,{children:\"HOUR\"}),\", and \",(0,e.jsx)(n.code,{children:\"DAY\"}),\", including DuckDB's \",(0,e.jsx)(n.code,{children:\"date_diff\"}),\" spellings such as \",(0,e.jsx)(n.code,{children:\"seconds\"}),\" and \",(0,e.jsx)(n.code,{children:\"ms\"}),\". Units are case-insensitive; calendar units such as \",(0,e.jsx)(n.code,{children:\"WEEK\"}),\", \",(0,e.jsx)(n.code,{children:\"MONTH\"}),\", and \",(0,e.jsx)(n.code,{children:\"YEAR\"}),\" are not supported.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[\"The gap must be a fixed-length interval and a whole number of the chosen units. Days count as 24 hours; months and years are not allowed. For example, \",(0,e.jsx)(n.code,{children:\"GAP INTERVAL 90 SECOND GRANULARITY MINUTE\"}),\" is rejected.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.code,{children:\"TIMESTAMPTZ\"}),\" keys support only \",(0,e.jsx)(n.code,{children:\"MICROSECOND\"}),\", \",(0,e.jsx)(n.code,{children:\"MILLISECOND\"}),\", and \",(0,e.jsx)(n.code,{children:\"SECOND\"}),\", because coarser \",(0,e.jsx)(n.code,{children:\"date_diff\"}),\" boundaries depend on the time zone.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[\"Boundaries are counted by flooring epoch values to the unit, including before 1970. \",(0,e.jsx)(n.code,{children:\"DATE\"}),\" and second-, millisecond-, or nanosecond-precision timestamp keys are compared as \",(0,e.jsx)(n.code,{children:\"TIMESTAMP\"}),\".\"]}),`\n`,(0,e.jsx)(n.li,{children:\"With granularity, an infinite timestamp never continues a session.\"}),`\n`]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"output-names-and-ordering\",sectionTitle:\"Output names and ordering\",anchors:\"\",ancestors:\"\",index:\"15\",children:[(0,e.jsx)(n.h2,{id:\"output-names-and-ordering\",children:\"Output names and ordering\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Rename metadata with \",(0,e.jsx)(n.code,{children:\"SESSION_ID AS\"}),\", \",(0,e.jsx)(n.code,{children:\"SESSION_START AS\"}),\", or \",(0,e.jsx)(n.code,{children:\"SESSION_END AS\"}),\". For example, an input column named \",(0,e.jsx)(n.code,{children:\"session_id\"}),\" can remain in the output if the generated session ID has a different name:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY session_id\n    ORDER BY ts\n    GAP INTERVAL 30 MINUTE\n    SESSION_ID AS gap_session_id\n    SESSION_START AS gap_start\n    SESSION_END AS gap_end\n);\n`})}),`\n`,(0,e.jsx)(n.p,{children:\"Aliases change only the emitted metadata names. They work in both output modes and do not change column order.\"}),`\n`,(0,e.jsx)(n.p,{children:\"Final metadata names, including an optional row-number column, must not collide with each other or with input columns in row-preserving mode. In summary mode, they must not collide with partition columns or measure aliases. Measure aliases must also be distinct from partition columns and each other. Rename the metadata or the conflicting input columns to resolve a collision.\"}),`\n`,(0,e.jsxs)(n.p,{children:[\"Result order is not guaranteed. Add an outer \",(0,e.jsx)(n.code,{children:\"ORDER BY\"}),\" whenever downstream behavior depends on row order:\"]}),`\n`,(0,e.jsx)(n.pre,{children:(0,e.jsx)(n.code,{className:\"language-sql\",children:`SELECT *\nFROM events\nSESSIONIZE (\n    PARTITION BY user_id\n    ORDER BY ts, event_id\n    GAP INTERVAL 30 MINUTE\n)\nORDER BY user_id, session_id, ts, event_id;\n`})}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"persist-session-results\",sectionTitle:\"Persist session results\",anchors:\"\",ancestors:\"\",index:\"16\",children:[(0,e.jsx)(n.h2,{id:\"persist-session-results\",children:\"Persist session results\"}),`\n`,(0,e.jsxs)(n.p,{children:[(0,e.jsx)(n.code,{children:\"CREATE VIEW\"}),\" and \",(0,e.jsx)(n.code,{children:\"CREATE MACRO\"}),\" cannot store a \",(0,e.jsx)(n.code,{children:\"SESSIONIZE\"}),\" clause. To persist results, use \",(0,e.jsx)(n.code,{children:\"CREATE TABLE AS SELECT ... SESSIONIZE (...)\"}),\", or run the clause inline when querying.\"]}),`\n`]}),(0,e.jsxs)(s,{sectionId:\"learn-more\",sectionTitle:\"Learn more\",anchors:\"\",ancestors:\"\",index:\"17\",children:[(0,e.jsx)(n.h2,{id:\"learn-more\",children:\"Learn more\"}),`\n`,(0,e.jsxs)(n.ul,{children:[`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/features/journeys\",children:\"JOURNEYS\"})}),\": discover observed paths through event sequences.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/features/match-recognize\",children:\"MATCH_RECOGNIZE\"})}),\": find more complex ordered event patterns.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/query/sql-explorer\",children:\"SQL Explorer\"})}),\": run and refine session queries interactively.\"]}),`\n`,(0,e.jsxs)(n.li,{children:[(0,e.jsx)(n.strong,{children:(0,e.jsx)(n.a,{href:\"/docs/lakehouse/sql-engine\",children:\"SQL engine\"})}),\": learn about Altertable's DuckDB SQL dialect.\"]}),`\n`]})]})]})}function u(i={}){let{wrapper:n}=i.components||{};return n?(0,e.jsx)(n,{...i,children:(0,e.jsx)(h,{...i})}):h(i)}function d(i,n){throw new Error(\"Expected \"+(n?\"component\":\"object\")+\" `\"+i+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return A(O);})();\n;return Component;"},"_id":"docs/lakehouse/features/sessionize.mdx","_raw":{"sourceFilePath":"docs/lakehouse/features/sessionize.mdx","sourceFileName":"sessionize.mdx","sourceFileDir":"docs/lakehouse/features","contentType":"mdx","flattenedPath":"docs/lakehouse/features/sessionize"},"type":"Docs","path":"/lakehouse/features/sessionize","level":3,"parent":"lakehouse/features"}