Last active
December 9, 2015 14:21
-
-
Save thekensta/1602ce9b86f100adc41e to your computer and use it in GitHub Desktop.
Example of a UDF written in python on Redshift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create function f_extract_query_param(query_string VARCHAR, param VARCHAR) | |
returns varchar | |
STABLE | |
AS $$ | |
# Split on ampersand and then on =, ensuring every param has a pair | |
params = query_string.split("&") | |
# split(.., 1) because there might be multiple '=' in the param | |
kp = [p.split("=", 1) for p in params] | |
# create dict, with "" for missing values where the key is not an empty string | |
# ''.split('=', 1) -> [''] | |
keys = {k[0]: k[1] if len(k) > 1 else "" for k in kp if k[0]} | |
return keys.get(param) | |
$$ LANGUAGE plpythonu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment