đź”® Sanity Create is here. Writing is reinvented. Try now, no developer setup
Back to changelog

New GROQ functions and versioning

This release targeting v2021-03-25 and v2021-10-21 of the Content Lake brings several new function namespaces to GROQ, as well as introduces the first formalized version of the GROQ language specification.

New GROQ functions

There are three new namespaces for functions added to the specification, which have been implemented across all GROQ tooling. These are:

array:: functions: Perform array operations on lists, such as removing all null values, building text strings from a list of names, or generating a list of all unique document _types.

  • array::compact(<array>) - removes all null values from an array
  • array::join(<array>, <token>) - concatenates all array elements into one string, separated by a specified token.
  • array::unique(<array>) - removes duplicate values from an array (this works for values that can be compared for equality, specifically numbers, strings, booleans, and null, and will not work for values that are arrays or objects)

math:: functions: Run common mathematical operators on numeric values. For example, you can add the prices across multiple products or return the maximum discount available within a cart of products.

  • math::avg(<array-of-numbers>) - calculates the average value (arithmetic mean) of an array of numbers.
  • math::max(<array-of-numbers>) - returns the largest numeric value of an array of numbers.
  • math::min(<array-of-numbers>) - returns the smallest numeric value of an array of numbers.
  • math::sum(<array-of-numbers>) - calculates the sum of an array of numbers.

string:: functions: Manipulate text or validate that information matches a given prefix. For example, get a list of the articles that start with “How to” or split a comma-separated string of author names into an array.

  • string::split(<string>, <delimiter-token>) - turns a string into an array of substrings based on a delimiting token.
  • string::startsWith(<string>, <string-pattern>) - checks if a prefix string exactly matches the start of another string.

To learn all about the new GROQ functions, read the developer update accompanying this release, and visit our documentation.

GROQ1.revision1

With this release, we are also pleased to announce the formal language specification of GROQ, as well as a new versioning scheme. Taking inspiration from other well-known language specs, like HTML and SQL, we settled on the following format:

GROQ-<major version #>.revision<#>

The current version of the specification is GROQ-1.revision1. This version does not include any breaking changes. To learn more about the history and future of the GROQ language, read the blog post from Co-founder and CTO Simen Svale Skogsrud: Content is Queryable: (Re)Introducing GROQ.

Documentation affected by this release

Published September 15, 2022