blob: f36a5461232d07e36cebb1002c803e677368db54 [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>SQL ARRAY functions · Apache Druid</title><meta name="viewport" content="width=device-width, initial-scale=1.0"/><link rel="canonical" href="https://druid.apache.org/docs/26.0.0/querying/sql-array-functions.html"/><meta name="generator" content="Docusaurus"/><meta name="description" content="&lt;!--"/><meta name="docsearch:language" content="en"/><meta name="docsearch:version" content="26.0.0" /><meta property="og:title" content="SQL ARRAY functions · Apache Druid"/><meta property="og:type" content="website"/><meta property="og:url" content="https://druid.apache.org/index.html"/><meta property="og:description" content="&lt;!--"/><meta property="og:image" content="https://druid.apache.org/img/druid_nav.png"/><meta name="twitter:card" content="summary"/><meta name="twitter:image" content="https://druid.apache.org/img/druid_nav.png"/><link rel="shortcut icon" href="/img/favicon.png"/><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css"/><script async="" src="https://www.googletagmanager.com/gtag/js?id=UA-131010415-1"></script><script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-131010415-1');
</script><link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"/><link rel="stylesheet" href="/css/code-block-buttons.css"/><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script><script type="text/javascript" src="/js/code-block-buttons.js"></script><script src="/js/scrollSpy.js"></script><link rel="stylesheet" href="/css/main.css"/><script src="/js/codetabs.js"></script></head><body class="sideNavVisible separateOnPageNav"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/"><img class="logo" src="/img/druid_nav.png" alt="Apache Druid"/></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class=""><a href="/technology" target="_self">Technology</a></li><li class=""><a href="/use-cases" target="_self">Use Cases</a></li><li class=""><a href="/druid-powered" target="_self">Powered By</a></li><li class=""><a href="/docs/26.0.0/design/index.html" target="_self">Docs</a></li><li class=""><a href="/community/" target="_self">Community</a></li><li class=""><a href="https://www.apache.org" target="_self">Apache</a></li><li class=""><a href="/downloads.html" target="_self">Download</a></li><li class="navSearchWrapper reactNavSearchWrapper"><input type="text" id="search_input_react" placeholder="Search" title="Search"/></li></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="container mainContainer docsContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="edit-page-link button" href="https://github.com/apache/druid/edit/master/docs/querying/sql-array-functions.md" target="_blank" rel="noreferrer noopener">Edit</a><h1 id="__docusaurus" class="postHeaderTitle">SQL ARRAY functions</h1></header><article><div><span><!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<!--
The format of the tables that describe the functions and operators
should not be changed without updating the script create-sql-docs
in web-console/script/create-sql-docs, because the script detects
patterns in this markdown file and parse it to TypeScript file for web console
-->
<blockquote>
<p>Apache Druid supports two query languages: Druid SQL and <a href="/docs/26.0.0/querying/querying.html">native queries</a>.
This document describes the SQL language.</p>
</blockquote>
<p>This page describes the operations you can perform on arrays using <a href="/docs/26.0.0/querying/sql.html">Druid SQL</a>. See <a href="/docs/26.0.0/querying/sql-data-types.html#arrays"><code>ARRAY</code> data type documentation</a> for additional details.</p>
<p>All array references in the array function documentation can refer to multi-value string columns or <code>ARRAY</code> literals. These functions are largely
identical to the <a href="/docs/26.0.0/querying/sql-multivalue-string-functions.html">multi-value string functions</a>, but use <code>ARRAY</code> types and behavior.</p>
<table>
<thead>
<tr><th>Function</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td><code>ARRAY[expr1, expr2, ...]</code></td><td>Constructs a SQL <code>ARRAY</code> literal from the expression arguments, using the type of the first argument as the output array type.</td></tr>
<tr><td><code>ARRAY_LENGTH(arr)</code></td><td>Returns length of the array expression.</td></tr>
<tr><td><code>ARRAY_OFFSET(arr, long)</code></td><td>Returns the array element at the 0-based index supplied, or null for an out of range index.</td></tr>
<tr><td><code>ARRAY_ORDINAL(arr, long)</code></td><td>Returns the array element at the 1-based index supplied, or null for an out of range index.</td></tr>
<tr><td><code>ARRAY_CONTAINS(arr, expr)</code></td><td>If <code>expr</code> is a scalar type, returns 1 if <code>arr</code> contains <code>expr</code>. If <code>expr</code> is an array, returns 1 if <code>arr</code> contains all elements of <code>expr</code>. Otherwise returns 0.</td></tr>
<tr><td><code>ARRAY_OVERLAP(arr1, arr2)</code></td><td>Returns 1 if <code>arr1</code> and <code>arr2</code> have any elements in common, else 0.</td></tr>
<tr><td><code>ARRAY_OFFSET_OF(arr, expr)</code></td><td>Returns the 0-based index of the first occurrence of <code>expr</code> in the array. If no matching elements exist in the array, returns <code>-1</code> or <code>null</code> if <code>druid.generic.useDefaultValueForNull=false</code>.</td></tr>
<tr><td><code>ARRAY_ORDINAL_OF(arr, expr)</code></td><td>Returns the 1-based index of the first occurrence of <code>expr</code> in the array. If no matching elements exist in the array, returns <code>-1</code> or <code>null</code> if <code>druid.generic.useDefaultValueForNull=false</code>.</td></tr>
<tr><td><code>ARRAY_PREPEND(expr, arr)</code></td><td>Prepends <code>expr</code> to <code>arr</code> at the beginning, the resulting array type determined by the type of <code>arr</code>.</td></tr>
<tr><td><code>ARRAY_APPEND(arr1, expr)</code></td><td>Appends <code>expr</code> to <code>arr</code>, the resulting array type determined by the type of <code>arr1</code>.</td></tr>
<tr><td><code>ARRAY_CONCAT(arr1, arr2)</code></td><td>Concatenates <code>arr2</code> to <code>arr1</code>. The resulting array type is determined by the type of <code>arr1</code>.</td></tr>
<tr><td><code>ARRAY_SLICE(arr, start, end)</code></td><td>Returns the subarray of <code>arr</code> from the 0-based index <code>start</code> (inclusive) to <code>end</code> (exclusive). Returns <code>null</code>, if <code>start</code> is less than 0, greater than length of <code>arr</code>, or greater than <code>end</code>.</td></tr>
<tr><td><code>ARRAY_TO_STRING(arr, str)</code></td><td>Joins all elements of <code>arr</code> by the delimiter specified by <code>str</code>.</td></tr>
<tr><td><code>STRING_TO_ARRAY(str1, str2)</code></td><td>Splits <code>str1</code> into an array on the delimiter specified by <code>str2</code>.</td></tr>
</tbody>
</table>
</span></div></article></div><div class="docs-prevnext"></div></div></div><nav class="onPageNav"></nav></div><footer class="nav-footer druid-footer" id="footer"><div class="container"><div class="text-center"><p><a href="/technology">Technology</a> · <a href="/use-cases">Use Cases</a> · <a href="/druid-powered">Powered by Druid</a> · <a href="/docs/26.0.0/">Docs</a> · <a href="/community/">Community</a> · <a href="/downloads.html">Download</a> · <a href="/faq">FAQ</a></p></div><div class="text-center"><a title="Join the user group" href="https://groups.google.com/forum/#!forum/druid-user" target="_blank"><span class="fa fa-comments"></span></a> · <a title="Follow Druid" href="https://twitter.com/druidio" target="_blank"><span class="fab fa-twitter"></span></a> · <a title="Download via Apache" href="https://www.apache.org/dyn/closer.cgi?path=/incubator/druid/{{ site.druid_versions[0].versions[0].version }}/apache-druid-{{ site.druid_versions[0].versions[0].version }}-bin.tar.gz" target="_blank"><span class="fas fa-feather"></span></a> · <a title="GitHub" href="https://github.com/apache/druid" target="_blank"><span class="fab fa-github"></span></a></div><div class="text-center license">Copyright © 2022 <a href="https://www.apache.org/" target="_blank">Apache Software Foundation</a>.<br/>Except where otherwise noted, licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.<br/>Apache Druid, Druid, and the Druid logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.</div></div></footer></div><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script><script>
document.addEventListener('keyup', function(e) {
if (e.target !== document.body) {
return;
}
// keyCode for '/' (slash)
if (e.keyCode === 191) {
const search = document.getElementById('search_input_react');
search && search.focus();
}
});
</script><script>
var search = docsearch({
appId: 'CPK9PMSCEY',
apiKey: 'd4ef4ffe3a2f0c7d1e34b062fd98736b',
indexName: 'apache_druid',
inputSelector: '#search_input_react',
algoliaOptions: {"facetFilters":["language:en","version:26.0.0"]}
});
</script></body></html>