Sample Header Ad - 728x90

generating all substrings (n-grams) for bit-strings

3 votes
1 answer
159 views
I followed https://dba.stackexchange.com/questions/81369/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?
Asked by Marcin (131 rep)
Apr 6, 2020, 05:27 PM
Last activity: Jul 13, 2025, 12:03 AM