All Products
Search
Document Center

MaxCompute:NGRAMS

Last Updated:Mar 26, 2026

Returns contiguous subsequences of n adjacent elements (n-grams) from an array.

Syntax

ngrams(array(T), n)

Parameters

ParameterDescription
arrayThe input array.
nThe 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 n is 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.