Skip to content

Instantly share code, notes, and snippets.

@thekensta
Last active December 9, 2015 14:21
Show Gist options
  • Save thekensta/1602ce9b86f100adc41e to your computer and use it in GitHub Desktop.
Save thekensta/1602ce9b86f100adc41e to your computer and use it in GitHub Desktop.
Example of a UDF written in python on Redshift
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