Github user iyerr3 commented on a diff in the pull request: https://github.com/apache/incubator-madlib/pull/44#discussion_r66131234 --- Diff: src/ports/postgres/modules/utilities/sessionize.py_in --- @@ -0,0 +1,103 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import plpy +import string + +from control import MinWarning +from utilities import unique_string, _assert +from validate_args import get_cols +from validate_args import input_tbl_valid, output_tbl_valid, is_var_valid + +m4_changequote(`<!', `!>') + +def sessionize(schema_madlib, source_table, output_table, partition_expr, + time_stamp, max_time, **kwargs): + """ + Perform sessionization over a sequence of rows. + + Args: + @param schema_madlib: str, Name of the MADlib schema + @param source_table: str, Name of the input table/view + @param output_table: str, Name of the table to store result + @param partition_expr: str, Expression to partition (group) the input data + @param time_stamp: str, Column name with time used for sessionization calculation + @param max_time: interval, Delta time between subsequent events to define a session + + """ + with MinWarning("error"): + _validate(source_table, output_table, partition_expr, time_stamp, max_time) + + all_input_cols_str = ', '.join([i.strip() for i in get_cols(source_table, schema_madlib)]) + session_id = 'session_id' if not is_var_valid(source_table, 'session_id') else unique_string('session_id') + + # Create temp column names for intermediate columns. + new_partition = unique_string('new_partition') + new_session = unique_string('new_session') + + plpy.execute(""" + CREATE TABLE {output_table} AS + SELECT + {all_input_cols_str}, + CASE WHEN {time_stamp} IS NOT NULL + THEN SUM(CASE WHEN {new_partition} OR {new_session} THEN 1 END) OVER (PARTITION BY {partition_expr} ORDER BY {time_stamp}) END AS {session_id} + FROM ( + SELECT *, + ROW_NUMBER() OVER (w) = 1 AND {time_stamp} IS NOT NULL AS {new_partition}, + ({time_stamp}-LAG({time_stamp}, 1) OVER (w)) > '{max_time}'::INTERVAL AS {new_session} + FROM {source_table} WINDOW w AS (PARTITION BY {partition_expr} ORDER BY {time_stamp}) + ) a + """.format(**locals())) + + +def _validate(source_table, output_table, partition_expr, time_stamp, max_time): + input_tbl_valid(source_table, 'Sessionization') + output_tbl_valid(output_table, 'Sessionization') + # ensure the expressions are not None or empty strings + _assert(partition_expr, "Sessionization error: Invalid partition expression") + _assert(time_stamp, "Sessionization error: Invalid time stamp column") + _assert(max_time, "Sessionization error: Invalid max time value") + # ensure the partition/order expression can actually be used + _assert(is_var_valid(source_table, partition_expr, time_stamp), + "Sessionization error: invalid partition expression or time stamp column name") + +def sessionize_help_message(schema_madlib, message, **kwargs): + """ + Help message for sessionize function + """ + help_string = """ +------------------------------------------------------------ + SUMMARY +------------------------------------------------------------ +Functionality: Sessionize + +The goal of the MADlib sessionize function is to perform sessionization over +a time-series based data. + +------------------------------------------------------------ + USAGE +------------------------------------------------------------ +SELECT {schema_madlib}.sessionize( + 'source_table', -- str, Name of the table + 'output_table', -- str, Table name to store the Sessionization results + 'partition_expr', -- str, Partition expression to group the data table + 'time_stamp' -- str, Column name with time used for sessionization calculation + 'max_time' -- str, Delta time between subsequent events to define a session +); +""" + --- End diff -- it would be good to have 1 of the examples from docs here as well.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---