I followed SELECT all substrings (n-grams) of length n? to get a function for generating all n-grams for text types. Works great. I figured out how to cast my bit-strings to text, and the function from that link works.
However I need it to be as fast as possible, thus I wanted a bit-string native implementation, and I came up with this:
CREATE OR REPLACE FUNCTION public.ngrams(in input bit varying, in len integer)
RETURNS SETOF bit
LANGUAGE sql
AS $function$
SELECT cast((input << alpha) as bit(4))
FROM GENERATE_SERIES(0, LENGTH($1)-($2+1), 1) alpha;
$function$
This works, for N=4. However when I tried to change it to $2
or len
I would get:
ERROR: 22P02: invalid input syntax for type integer: "len"
LINE 5: SELECT cast((input << alpha) as bit(len))
^
How come I can use the named parameter from the function declaration for input
but not for len
? Or better yet, how can I feed a variable into that spot?
Best Answer
You can solve the error by using the
bit varying
datatype. That being said, I don't actually know if this is the behavior you're looking for - it looks weird to me and doesn't match what the text version does.