Skip to content

Instantly share code, notes, and snippets.

@nwalker
Created December 1, 2017 12:15
Show Gist options
  • Select an option

  • Save nwalker/a8e43736bc43451c8e66a2ef159c9805 to your computer and use it in GitHub Desktop.

Select an option

Save nwalker/a8e43736bc43451c8e66a2ef159c9805 to your computer and use it in GitHub Desktop.
PlPython3 longest common prefix aggregate
drop function lcp_text(text,text) cascade;
create function lcp_text(acc text, b text)
returns text as $$
import itertools as i
if acc is None:
return b
return ''.join(list(
a for (a, _)
in i.takewhile(lambda x: x[0] == x[1], zip(acc, b))))
$$ language plpython3u;
create aggregate LCP(text) (
stype = text,
sfunc = lcp_text
);
select lcp_text('aa', 'aaad');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment