/* ----------------------------------------------------------------------- */ /** * 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. * * @file path.sql_in * * @brief SQL functions for pathing functions * @date Sep 2015 * */ /* ----------------------------------------------------------------------- */ m4_include(`SQLCommon.m4') /** @addtogroup grp_path
path(
source_table,
output_table,
partition_expr,
order_expr,
symbol,
pattern,
aggregate_func,
persist_rows,
overlapping_patterns
)
\b Arguments
DROP TABLE IF EXISTS eventlog;
CREATE TABLE eventlog (event_timestamp TIMESTAMP,
user_id INT,
session_id INT,
page TEXT,
revenue FLOAT);
INSERT INTO eventlog VALUES
('04/15/2015 01:03:00', 100821, 100, 'LANDING', 0),
('04/15/2015 01:04:00', 100821, 100, 'WINE', 0),
('04/15/2015 01:05:00', 100821, 100, 'CHECKOUT', 39),
('04/15/2015 02:06:00', 100821, 101, 'WINE', 0),
('04/15/2015 02:09:00', 100821, 101, 'WINE', 0),
('04/15/2015 01:15:00', 101121, 102, 'LANDING', 0),
('04/15/2015 01:16:00', 101121, 102, 'WINE', 0),
('04/15/2015 01:17:00', 101121, 102, 'CHECKOUT', 15),
('04/15/2015 01:18:00', 101121, 102, 'LANDING', 0),
('04/15/2015 01:19:00', 101121, 102, 'HELP', 0),
('04/15/2015 01:21:00', 101121, 102, 'WINE', 0),
('04/15/2015 01:22:00', 101121, 102, 'CHECKOUT', 23),
('04/15/2015 02:15:00', 101331, 103, 'LANDING', 0),
('04/15/2015 02:16:00', 101331, 103, 'WINE', 0),
('04/15/2015 02:17:00', 101331, 103, 'HELP', 0),
('04/15/2015 02:18:00', 101331, 103, 'WINE', 0),
('04/15/2015 02:19:00', 101331, 103, 'CHECKOUT', 16),
('04/15/2015 02:22:00', 101443, 104, 'BEER', 0),
('04/15/2015 02:25:00', 101443, 104, 'CHECKOUT', 12),
('04/15/2015 02:29:00', 101881, 105, 'LANDING', 0),
('04/15/2015 02:30:00', 101881, 105, 'BEER', 0),
('04/15/2015 01:05:00', 102201, 106, 'LANDING', 0),
('04/15/2015 01:06:00', 102201, 106, 'HELP', 0),
('04/15/2015 01:09:00', 102201, 106, 'LANDING', 0),
('04/15/2015 02:15:00', 102201, 107, 'WINE', 0),
('04/15/2015 02:16:00', 102201, 107, 'BEER', 0),
('04/15/2015 02:17:00', 102201, 107, 'WINE', 0),
('04/15/2015 02:18:00', 102871, 108, 'BEER', 0),
('04/15/2015 02:19:00', 102871, 108, 'WINE', 0),
('04/15/2015 02:22:00', 102871, 108, 'CHECKOUT', 21),
('04/15/2015 02:25:00', 102871, 108, 'LANDING', 0),
('04/15/2015 02:17:00', 103711, 109, 'BEER', 0),
('04/15/2015 02:18:00', 103711, 109, 'LANDING', 0),
('04/15/2015 02:19:00', 103711, 109, 'WINE', 0);
-# Calculate the revenue by checkout:
DROP TABLE IF EXISTS path_output, path_output_tuples;
SELECT madlib.path(
'eventlog', -- Name of input table
'path_output', -- Table name to store path results
'session_id', -- Partition input table by session
'event_timestamp ASC', -- Order partitions in input table by time
'buy:=page=''CHECKOUT''', -- Define a symbol for checkout events
'(buy)', -- Pattern search: purchase
'sum(revenue) as checkout_rev', -- Aggregate: sum revenue by checkout
TRUE -- Persist matches
);
SELECT * FROM path_output ORDER BY session_id, match_id;
Result:
session_id | match_id | checkout_rev
------------+----------+--------------
100 | 1 | 39
102 | 1 | 15
102 | 2 | 23
103 | 1 | 16
104 | 1 | 12
108 | 1 | 21
(6 rows)
Note that there are 2 checkouts within session 102, which is apparent
from the 'match_id' column. This serves to illustrate that the
'aggregate_func' operates on a per pattern match basis,
not on a per partition basis. If in fact we wanted revenue
by partition ('session_id' in this example), then we could do:
SELECT session_id, sum(checkout_rev) FROM path_output GROUP BY session_id ORDER BY session_id;Result:
session_id | sum
------------+-----
100 | 39
102 | 38
103 | 16
104 | 12
108 | 21
(5 rows)
Since we set TRUE for 'persist_rows', we can view the associated
pattern matches:
SELECT * FROM path_output_tuples ORDER BY session_id ASC, event_timestamp ASC;Result:
event_timestamp | user_id | session_id | page | revenue | symbol | match_id ---------------------+---------+------------+----------+---------+--------+---------- 2015-04-15 01:05:00 | 100821 | 100 | CHECKOUT | 39 | buy | 1 2015-04-15 01:17:00 | 101121 | 102 | CHECKOUT | 15 | buy | 1 2015-04-15 01:22:00 | 101121 | 102 | CHECKOUT | 23 | buy | 2 2015-04-15 02:19:00 | 101331 | 103 | CHECKOUT | 16 | buy | 1 2015-04-15 02:25:00 | 101443 | 104 | CHECKOUT | 12 | buy | 1 2015-04-15 02:22:00 | 102871 | 108 | CHECKOUT | 21 | buy | 1 (6 rows)Notice that the 'symbol' and 'match_id' columns are added to the right of the matched rows. -# We are interested in sessions with an order placed within 4 pages of entering the shopping site via the landing page. We represent this by the regular expression: '(land)[^(land)(buy)]{0,2}(buy)'. In other words, visit to the landing page followed by from 0 to 2 non-entry, non-sale pages, followed by a purchase. The SQL is as follows:
DROP TABLE IF EXISTS path_output, path_output_tuples;
SELECT madlib.path(
'eventlog', -- Name of input table
'path_output', -- Table name to store path results
'session_id', -- Partition input table by session
'event_timestamp ASC', -- Order partitions in input table by time
'land:=page=''LANDING'',
wine:=page=''WINE'',
beer:=page=''BEER'',
buy:=page=''CHECKOUT'',
other:=page<>''LANDING'' AND page<>''WINE'' AND page<>''BEER'' AND page<>''CHECKOUT''', -- Symbols for page types
'(land)[^(land)(buy)]{0,2}(buy)', -- Purchase within 4 pages entering site
'sum(revenue) as checkout_rev', -- Aggregate: sum revenue by checkout
TRUE -- Persist matches
);
SELECT * FROM path_output ORDER BY session_id, match_id;
Result:
session_id | match_id | session_rev
------------+----------+-------------
100 | 1 | 39
102 | 1 | 15
102 | 2 | 23
(3 rows)
Now view the associated pattern matches:
SELECT * FROM path_output_tuples ORDER BY session_id ASC, event_timestamp ASC;Result:
event_timestamp | user_id | session_id | page | revenue | symbol | match_id ---------------------+---------+------------+----------+---------+--------+---------- 2015-04-15 01:03:00 | 100821 | 100 | LANDING | 0 | land | 1 2015-04-15 01:04:00 | 100821 | 100 | WINE | 0 | wine | 1 2015-04-15 01:05:00 | 100821 | 100 | CHECKOUT | 39 | buy | 1 2015-04-15 01:15:00 | 101121 | 102 | LANDING | 0 | land | 1 2015-04-15 01:16:00 | 101121 | 102 | WINE | 0 | wine | 1 2015-04-15 01:17:00 | 101121 | 102 | CHECKOUT | 15 | buy | 1 2015-04-15 01:18:00 | 101121 | 102 | LANDING | 0 | land | 2 2015-04-15 01:19:00 | 101121 | 102 | HELP | 0 | other | 2 2015-04-15 01:21:00 | 101121 | 102 | WINE | 0 | wine | 2 2015-04-15 01:22:00 | 101121 | 102 | CHECKOUT | 23 | buy | 2 (10 rows)-# We may want to use a window function instead of an aggregate. Currently, only aggregates are supported in the core path function in the parameter 'aggregate_func'. However, you can write window functions on the output tuples to achieve the desired result. Continuing the previous example, let’s say we want to compute average revenue for checkouts within 4 pages of entering the shopping site via the landing page:
SELECT DATE(event_timestamp), user_id, session_id, revenue,
avg(revenue) OVER (PARTITION BY DATE(event_timestamp)) as avg_checkout_rev
FROM path_output_tuples
WHERE page='CHECKOUT'
ORDER BY user_id, session_id;
Result:
date | user_id | session_id | revenue | avg_checkout_rev
------------+---------+------------+---------+------------------
2015-04-15 | 100821 | 100 | 39 | 25.6666666666667
2015-04-15 | 101121 | 102 | 15 | 25.6666666666667
2015-04-15 | 101121 | 102 | 23 | 25.6666666666667
(3 rows)
Here we are partitioning the window function by day because we want
daily averages, although our sample data set only has a single day.
-# Now we want to do a golden path analysis to find the most successful
shopper paths through the site. Since our data set is small, we decide
this means the most frequently viewed page just before a checkout is made:
DROP TABLE IF EXISTS path_output, path_output_tuples;
SELECT madlib.path(
'eventlog', -- Name of input table
'path_output', -- Table name to store path results
'session_id', -- Partition input table by session
'event_timestamp ASC', -- Order partitions in input table by time
'land:=page=''LANDING'',
wine:=page=''WINE'',
beer:=page=''BEER'',
buy:=page=''CHECKOUT'',
other:=page<>''LANDING'' AND page<>''WINE'' AND page<>''BEER'' AND page<>''CHECKOUT''', -- Symbols for page types
'[^(buy)](buy)', -- Pattern to match
'array_agg(page ORDER BY session_id ASC, event_timestamp ASC) as page_path', -- Build array with shopper paths
FALSE -- Don't persist matches
);
Now count the common paths and print the most frequent:
SELECT count(*), page_path from
(SELECT * FROM path_output) q
GROUP BY page_path
ORDER BY count(*) DESC
LIMIT 10;
Result:
count | page_path
-------+-----------------
5 | {WINE,CHECKOUT}
1 | {BEER,CHECKOUT}
(2 rows)
There are only 2 different paths. The wine page is viewed more frequently
than the beer page just before checkout.
-# To demonstrate the use of 'overlapping_patterns', consider
a pattern with at least one page followed by and ending with a checkout:
DROP TABLE IF EXISTS path_output, path_output_tuples;
SELECT madlib.path(
'eventlog', -- Name of the table
'path_output', -- Table name to store the path results
'session_id', -- Partition by session
'event_timestamp ASC', -- Order partitions in input table by time
$$ nobuy:=page<>'CHECKOUT',
buy:=page='CHECKOUT'
$$, -- Definition of symbols used in the pattern definition
'(nobuy)+(buy)', -- At least one page followed by and ending with a CHECKOUT.
'array_agg(page ORDER BY session_id ASC, event_timestamp ASC) as page_path',
FALSE, -- Don't persist matches
TRUE -- Turn on overlapping patterns
);
SELECT * FROM path_output ORDER BY session_id, match_id;
Result with overlap turned on:
session_id | match_id | page_path
------------+----------+-----------------------------------
100 | 1 | {LANDING,WINE,CHECKOUT}
100 | 2 | {WINE,CHECKOUT}
102 | 1 | {LANDING,WINE,CHECKOUT}
102 | 2 | {WINE,CHECKOUT}
102 | 3 | {LANDING,HELP,WINE,CHECKOUT}
102 | 4 | {HELP,WINE,CHECKOUT}
102 | 5 | {WINE,CHECKOUT}
103 | 1 | {LANDING,WINE,HELP,WINE,CHECKOUT}
103 | 2 | {WINE,HELP,WINE,CHECKOUT}
103 | 3 | {HELP,WINE,CHECKOUT}
103 | 4 | {WINE,CHECKOUT}
104 | 1 | {BEER,CHECKOUT}
108 | 1 | {BEER,WINE,CHECKOUT}
108 | 2 | {WINE,CHECKOUT}
(14 rows)
With overlap turned off, the result would be:
session_id | match_id | page_path
------------+----------+-----------------------------------
100 | 1 | {LANDING,WINE,CHECKOUT}
102 | 1 | {LANDING,WINE,CHECKOUT}
102 | 2 | {LANDING,HELP,WINE,CHECKOUT}
103 | 1 | {LANDING,WINE,HELP,WINE,CHECKOUT}
104 | 1 | {BEER,CHECKOUT}
108 | 1 | {BEER,WINE,CHECKOUT}
(6 rows)
@anchor note
@note
Please note some current limitations of the path algorithm.
- Window functions cannot currently be used in the parameter
'aggregate_func'. Instead, output the pattern matches and write
a SQL query with a window function over the output tuples.
- A given row can only match one symbol. If a row matches
multiple symbols, the symbol that comes first in the symbol
definition list will take precedence.
- Maximum number of symbols that can be defined is 35.
- The columns 'match_id' and 'symbol' are generated by the path
algorithm. If coincidently you have columns in your input data
named 'match_id' or 'symbol', the system generated column names
will be changed to "__madlib_path_match_id__" and "__madlib_path_symbol__"
@anchor nomenclature
@par Nomenclature
Partition
- scope of rows to be searched for pattern match
- typical examples: user id, session id, portfolio id
Order
- sort order of input rows in partition
- typical example: time
Symbol
- a row of a particular type that you’re searching for, that you want to
include in a pattern
Pattern
- regular PostgreSQL pattern match expression of symbols and operators
that you want to match across rows
Pattern match
- rows that result from a pattern match expression of symbols
- can be multiple matches per partition
@anchor literature
@par Literature
NOTE: The following links refer to documentation resources for the
current PostgreSQL database version. Depending upon your database
platform version, you may need to change "current" references in the
links to your database version.
If your database platform uses the Greenplum Database (or related
variants), please check with the project community and/or your
database vendor to identify the PostgreSQL version it is based on.
[1] PostgreSQL basic statements/assignment operator,
http://www.postgresql.org/docs/current/static/plpgsql-statements.html
[2] PostgreSQL pattern matching,
http://www.postgresql.org/docs/current/static/functions-matching.html
[3] PostgreSQL aggregate functions,
http://www.postgresql.org/docs/current/static/tutorial-agg.html
[4] PostgreSQL window functions,
http://www.postgresql.org/docs/current/static/tutorial-window.html
*/
-------------------------------------------------------------------------
DROP TYPE IF EXISTS MADLIB_SCHEMA.path_match_result CASCADE;
CREATE TYPE MADLIB_SCHEMA.path_match_result AS (
id FLOAT8[],
row_id FLOAT8[]
);
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path_pattern_match
(
symbols TEXT,
pattern TEXT,
row_id FLOAT8[],
overlapping_patterns BOOLEAN
) RETURNS MADLIB_SCHEMA.path_match_result
AS 'MODULE_PATHNAME', 'path_pattern_match'
LANGUAGE C STRICT
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `NO SQL', `');
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path(
source_table VARCHAR,
output_table VARCHAR,
partition_expr VARCHAR,
order_expr VARCHAR,
symbol_expr VARCHAR,
pattern_expr VARCHAR,
agg_func VARCHAR,
persist_rows BOOLEAN,
overlapping_patterns BOOLEAN
) RETURNS TEXT AS $$
PythonFunction(utilities, path, path)
$$ LANGUAGE plpythonu
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `MODIFIES SQL DATA', `');
-------------------------------------------------------------------------
-- Overloaded functions for default arguments ---------------
-- Do not allow overlapping by default
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path(
source_table VARCHAR,
output_table VARCHAR,
partition_expr VARCHAR,
order_expr VARCHAR,
symbol_expr VARCHAR,
pattern_expr VARCHAR,
agg_func VARCHAR,
persist_rows BOOLEAN
) RETURNS TEXT AS $$
SELECT MADLIB_SCHEMA.path($1, $2, $3, $4, $5, $6, $7, $8, False)
$$ LANGUAGE SQL
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `MODIFIES SQL DATA', `');
-- Do not output matched rows by default
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path(
source_table VARCHAR,
output_table VARCHAR,
partition_expr VARCHAR,
order_expr VARCHAR,
symbol_expr VARCHAR,
pattern_expr VARCHAR,
agg_func VARCHAR
) RETURNS TEXT AS $$
SELECT MADLIB_SCHEMA.path($1, $2, $3, $4, $5, $6, $7, False, False)
$$ LANGUAGE SQL
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `MODIFIES SQL DATA', `');
-- if no agg func provided then output the matched rows only
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path(
source_table VARCHAR,
output_table VARCHAR,
partition_expr VARCHAR,
order_expr VARCHAR,
symbol_expr VARCHAR,
pattern_expr VARCHAR
) RETURNS TEXT AS $$
SELECT MADLIB_SCHEMA.path($1, $2, $3, $4, $5, $6, NULL, False, False)
$$ LANGUAGE SQL
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `MODIFIES SQL DATA', `');
-------------------------------------------------------------------------
-- To Implement -----------------------------------------------------------
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path(message TEXT)
RETURNS text AS $$
PythonFunction(utilities, path, path_help_message)
$$ language plpythonu
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `CONTAINS SQL', `');
CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.path()
RETURNS text AS $$
SELECT MADLIB_SCHEMA.path('');
$$ language SQL
m4_ifdef(`__HAS_FUNCTION_PROPERTIES__', `CONTAINS SQL', `');