Github user decibel commented on a diff in the pull request:

    https://github.com/apache/incubator-madlib/pull/44#discussion_r65589213
  
    --- Diff: src/ports/postgres/modules/utilities/sessionize.py_in ---
    @@ -0,0 +1,101 @@
    +# 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, time_out, **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: float, Column name with time used for 
sessionization calculation
    +           @param time_out: float, Delta time between subsequent events to 
define a sessions
    +           
    +   """
    +   with MinWarning("error"):
    +           _validate(source_table, output_table, partition_expr, 
time_stamp, time_out)
    +
    +           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')
    +
    +           plpy.execute("""
    +                           CREATE TABLE {output_table} AS
    +                                   SELECT
    +                                           {all_input_cols_str},
    +                                           CASE WHEN {time_stamp} NOTNULL
    +                                           THEN (SUM(new_event_boundary) 
OVER (PARTITION BY {partition_expr} ORDER BY {time_stamp})) END AS {session_id}
    +                                   FROM (
    +                                           SELECT *, 
    +                                                   CASE WHEN {time_stamp} 
NOTNULL and ({time_stamp}-LAG({time_stamp},1) OVER (w) > '{time_out}' OR 
ROW_NUMBER() OVER (w) = '1')
    --- End diff --
    
    > Do you think we should mandate time_out/min_time to always be of type
    > interval? Another option is to force it to be of type float
    > (representing seconds), then we may have to use epoch() while computing
    > the time_stamp diffs.
    
    On the Postgres side, interval is what's wanted.
    
    On the python side, I could see use for either of them. float might be 
    the simplest, but there are options available with interval that you 
    don't have with seconds, because interval tracks seconds, days, and 
    months separately. In this specific case I can't see days or months 
    being very useful, but from an overall MADlib API standpoint I think 
    it's best if anything that accepts a time delta can accept an interval.
    
    Code-wise, I think the best way to handle this is to see if the python 
    interval parameter is some kind of number and if it is pass it directly 
    to Postgres and multiply it by a 1 second interval, ie: "{time_out} * 
    interval '1 second'". Postgres will do the correct thing regardless of 
    what numeric type it is (int vs numeric vs float). Otherwise just assume 
    it's something that can be cast to an interval and do 
    "{time_out}::interval".



---
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.
---

Reply via email to