Returns contiguous subsequences of n adjacent elements (n-grams) from an array.
Syntax
ngrams(array(T), n)Parameters
| Parameter | Description |
|---|---|
array | The input array. |
n | The number of elements in each n-gram. Must be greater than 0. |
Return value
An array of n-grams, where each n-gram is an array of n adjacent elements from the input.
If
n <= 0, an error is returned.If
nis greater than or equal to the length of the input array, a single n-gram containing all elements is returned.
Examples
SELECT ngrams(array('foo', 'bar', 'baz', 'foo'), 2); -- [['foo', 'bar'], ['bar', 'baz'], ['baz', 'foo']]
SELECT ngrams(array('foo', 'bar', 'baz', 'foo'), 3); -- [['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']]
SELECT ngrams(array('foo', 'bar', 'baz', 'foo'), 4); -- [['foo', 'bar', 'baz', 'foo']]
SELECT ngrams(array('foo', 'bar', 'baz', 'foo'), 5); -- [['foo', 'bar', 'baz', 'foo']]
SELECT ngrams(array(1, 2, 3, 4), 2); -- [[1, 2], [2, 3], [3, 4]]When n equals or exceeds the array length (examples 3 and 4 above), the result contains a single n-gram with all elements.
Related functions
ngrams is a complex type function. For more information about functions that process complex data types, see Complex type functions.