-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patharray.html
275 lines (234 loc) · 17.1 KB
/
array.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6.15. Array Functions and Operators — Presto 0.192 Documentation</title>
<link rel="stylesheet" href="../_static/presto.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.192',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="top" title="Presto 0.192 Documentation" href="../index.html" />
<link rel="up" title="6. Functions and Operators" href="../functions.html" />
<link rel="next" title="6.16. Map Functions and Operators" href="map.html" />
<link rel="prev" title="6.14. Window Functions" href="window.html" />
</head>
<body role="document">
<div class="header">
<h1 class="heading"><a href="../index.html">
<span>Presto 0.192 Documentation</span></a></h1>
<h2 class="heading"><span>6.15. Array Functions and Operators</span></h2>
</div>
<div class="topnav">
<p class="nav">
<span class="left">
« <a href="window.html">6.14. Window Functions</a>
</span>
<span class="right">
<a href="map.html">6.16. Map Functions and Operators</a> »
</span>
</p>
</div>
<div class="content">
<div class="section" id="array-functions-and-operators">
<h1>6.15. Array Functions and Operators</h1>
<div class="section" id="subscript-operator">
<h2>Subscript Operator: []</h2>
<p>The <code class="docutils literal"><span class="pre">[]</span></code> operator is used to access an element of an array and is indexed starting from one:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">my_array</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">AS</span> <span class="n">first_element</span>
</pre></div>
</div>
</div>
<div class="section" id="concatenation-operator">
<h2>Concatenation Operator: ||</h2>
<p>The <code class="docutils literal"><span class="pre">||</span></code> operator is used to concatenate an array with an array or an element of the same type:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="nb">ARRAY</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">||</span> <span class="nb">ARRAY</span> <span class="p">[</span><span class="mi">2</span><span class="p">];</span> <span class="c1">-- [1, 2]</span>
<span class="k">SELECT</span> <span class="nb">ARRAY</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">||</span> <span class="mi">2</span><span class="p">;</span> <span class="c1">-- [1, 2]</span>
<span class="k">SELECT</span> <span class="mi">2</span> <span class="o">||</span> <span class="nb">ARRAY</span> <span class="p">[</span><span class="mi">1</span><span class="p">];</span> <span class="c1">-- [2, 1]</span>
</pre></div>
</div>
</div>
<div class="section" id="array-functions">
<h2>Array Functions</h2>
<dl class="function">
<dt id="array_distinct">
<code class="descname">array_distinct</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Remove duplicate values from the array <code class="docutils literal"><span class="pre">x</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="array_intersect">
<code class="descname">array_intersect</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Returns an array of the elements in the intersection of <code class="docutils literal"><span class="pre">x</span></code> and <code class="docutils literal"><span class="pre">y</span></code>, without duplicates.</p>
</dd></dl>
<dl class="function">
<dt id="array_union">
<code class="descname">array_union</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Returns an array of the elements in the union of <code class="docutils literal"><span class="pre">x</span></code> and <code class="docutils literal"><span class="pre">y</span></code>, without duplicates.</p>
</dd></dl>
<dl class="function">
<dt id="array_except">
<code class="descname">array_except</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Returns an array of elements in <code class="docutils literal"><span class="pre">x</span></code> but not in <code class="docutils literal"><span class="pre">y</span></code>, without duplicates.</p>
</dd></dl>
<dl class="function">
<dt id="array_join">
<code class="descname">array_join</code><span class="sig-paren">(</span><em>x</em>, <em>delimiter</em>, <em>null_replacement</em><span class="sig-paren">)</span> → varchar</dt>
<dd><p>Concatenates the elements of the given array using the delimiter and an optional string to replace nulls.</p>
</dd></dl>
<dl class="function">
<dt id="array_max">
<code class="descname">array_max</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → x</dt>
<dd><p>Returns the maximum value of input array.</p>
</dd></dl>
<dl class="function">
<dt id="array_min">
<code class="descname">array_min</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → x</dt>
<dd><p>Returns the minimum value of input array.</p>
</dd></dl>
<dl class="function">
<dt id="array_position">
<code class="descname">array_position</code><span class="sig-paren">(</span><em>x</em>, <em>element</em><span class="sig-paren">)</span> → bigint</dt>
<dd><p>Returns the position of the first occurrence of the <code class="docutils literal"><span class="pre">element</span></code> in array <code class="docutils literal"><span class="pre">x</span></code> (or 0 if not found).</p>
</dd></dl>
<dl class="function">
<dt id="array_remove">
<code class="descname">array_remove</code><span class="sig-paren">(</span><em>x</em>, <em>element</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Remove all elements that equal <code class="docutils literal"><span class="pre">element</span></code> from array <code class="docutils literal"><span class="pre">x</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="array_sort">
<code class="descname">array_sort</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Sorts and returns the array <code class="docutils literal"><span class="pre">x</span></code>. The elements of <code class="docutils literal"><span class="pre">x</span></code> must be orderable.
Null elements will be placed at the end of the returned array.</p>
</dd></dl>
<dl class="function">
<dt id="arrays_overlap">
<code class="descname">arrays_overlap</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span> → boolean</dt>
<dd><p>Tests if arrays <code class="docutils literal"><span class="pre">x</span></code> and <code class="docutils literal"><span class="pre">y</span></code> have any any non-null elements in common.
Returns null if there are no non-null elements in common but either array contains null.</p>
</dd></dl>
<dl class="function">
<dt id="cardinality">
<code class="descname">cardinality</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → bigint</dt>
<dd><p>Returns the cardinality (size) of the array <code class="docutils literal"><span class="pre">x</span></code>.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">concat</code><span class="sig-paren">(</span><em>array1</em>, <em>array2</em>, <em>...</em>, <em>arrayN</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Concatenates the arrays <code class="docutils literal"><span class="pre">array1</span></code>, <code class="docutils literal"><span class="pre">array2</span></code>, <code class="docutils literal"><span class="pre">...</span></code>, <code class="docutils literal"><span class="pre">arrayN</span></code>.
This function provides the same functionality as the SQL-standard concatenation operator (<code class="docutils literal"><span class="pre">||</span></code>).</p>
</dd></dl>
<dl class="function">
<dt id="contains">
<code class="descname">contains</code><span class="sig-paren">(</span><em>x</em>, <em>element</em><span class="sig-paren">)</span> → boolean</dt>
<dd><p>Returns true if the array <code class="docutils literal"><span class="pre">x</span></code> contains the <code class="docutils literal"><span class="pre">element</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="element_at">
<code class="descname">element_at</code><span class="sig-paren">(</span><em>array<E></em>, <em>index</em><span class="sig-paren">)</span> → E</dt>
<dd><p>Returns element of <code class="docutils literal"><span class="pre">array</span></code> at given <code class="docutils literal"><span class="pre">index</span></code>.
If <code class="docutils literal"><span class="pre">index</span></code> > 0, this function provides the same functionality as the SQL-standard subscript operator (<code class="docutils literal"><span class="pre">[]</span></code>).
If <code class="docutils literal"><span class="pre">index</span></code> < 0, <code class="docutils literal"><span class="pre">element_at</span></code> accesses elements from the last to the first.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">filter</code><span class="sig-paren">(</span><em>array</em>, <em>function</em><span class="sig-paren">)</span> → array</dt>
<dd><p>See <a class="reference internal" href="lambda.html#filter" title="filter"><code class="xref py py-func docutils literal"><span class="pre">filter()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="flatten">
<code class="descname">flatten</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Flattens an <code class="docutils literal"><span class="pre">array(array(T))</span></code> to an <code class="docutils literal"><span class="pre">array(T)</span></code> by concatenating the contained arrays.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">reduce</code><span class="sig-paren">(</span><em>array</em>, <em>initialState</em>, <em>inputFunction</em>, <em>outputFunction</em><span class="sig-paren">)</span> → x</dt>
<dd><p>See <a class="reference internal" href="lambda.html#reduce" title="reduce"><code class="xref py py-func docutils literal"><span class="pre">reduce()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="repeat">
<code class="descname">repeat</code><span class="sig-paren">(</span><em>element</em>, <em>count</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Repeat <code class="docutils literal"><span class="pre">element</span></code> for <code class="docutils literal"><span class="pre">count</span></code> times.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">reverse</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Returns an array which has the reversed order of array <code class="docutils literal"><span class="pre">x</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="sequence">
<code class="descname">sequence</code><span class="sig-paren">(</span><em>start</em>, <em>stop</em><span class="sig-paren">)</span> → array<bigint></dt>
<dd><p>Generate a sequence of integers from <code class="docutils literal"><span class="pre">start</span></code> to <code class="docutils literal"><span class="pre">stop</span></code>, incrementing
by <code class="docutils literal"><span class="pre">1</span></code> if <code class="docutils literal"><span class="pre">start</span></code> is less than or equal to <code class="docutils literal"><span class="pre">stop</span></code>, otherwise <code class="docutils literal"><span class="pre">-1</span></code>.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">sequence</code><span class="sig-paren">(</span><em>start</em>, <em>stop</em>, <em>step</em><span class="sig-paren">)</span> → array<bigint></dt>
<dd><p>Generate a sequence of integers from <code class="docutils literal"><span class="pre">start</span></code> to <code class="docutils literal"><span class="pre">stop</span></code>, incrementing by <code class="docutils literal"><span class="pre">step</span></code>.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">sequence</code><span class="sig-paren">(</span><em>start</em>, <em>stop</em>, <em>step</em><span class="sig-paren">)</span> → array<timestamp></dt>
<dd><p>Generate a sequence of timestamps from <code class="docutils literal"><span class="pre">start</span></code> to <code class="docutils literal"><span class="pre">stop</span></code>, incrementing by <code class="docutils literal"><span class="pre">step</span></code>.
The type of <code class="docutils literal"><span class="pre">step</span></code> can be either <code class="docutils literal"><span class="pre">INTERVAL</span> <span class="pre">DAY</span> <span class="pre">TO</span> <span class="pre">SECOND</span></code> or <code class="docutils literal"><span class="pre">INTERVAL</span> <span class="pre">YEAR</span> <span class="pre">TO</span> <span class="pre">MONTH</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="shuffle">
<code class="descname">shuffle</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Generate a random permutation of the given array <code class="docutils literal"><span class="pre">x</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="slice">
<code class="descname">slice</code><span class="sig-paren">(</span><em>x</em>, <em>start</em>, <em>length</em><span class="sig-paren">)</span> → array</dt>
<dd><p>Subsets array <code class="docutils literal"><span class="pre">x</span></code> starting from index <code class="docutils literal"><span class="pre">start</span></code> (or starting from the end
if <code class="docutils literal"><span class="pre">start</span></code> is negative) with a length of <code class="docutils literal"><span class="pre">length</span></code>.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">transform</code><span class="sig-paren">(</span><em>array</em>, <em>function</em><span class="sig-paren">)</span> → array</dt>
<dd><p>See <a class="reference internal" href="lambda.html#transform" title="transform"><code class="xref py py-func docutils literal"><span class="pre">transform()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="zip">
<code class="descname">zip</code><span class="sig-paren">(</span><em>array1</em>, <em>array2</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="sig-paren">)</span> → array<row></dt>
<dd><p>Merges the given arrays, element-wise, into a single array of rows. The M-th element of
the N-th argument will be the N-th field of the M-th output element.
If the arguments have an uneven length, missing values are filled with <code class="docutils literal"><span class="pre">NULL</span></code>.</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">zip</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="nb">ARRAY</span><span class="p">[</span><span class="s1">'1b'</span><span class="p">,</span> <span class="k">null</span><span class="p">,</span> <span class="s1">'3b'</span><span class="p">]);</span> <span class="c1">-- [ROW(1, '1b'), ROW(2, null), ROW(null, '3b')]</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">zip_with</code><span class="sig-paren">(</span><em>array1</em>, <em>array2</em>, <em>function</em><span class="sig-paren">)</span> → array</dt>
<dd><p>See <a class="reference internal" href="lambda.html#zip_with" title="zip_with"><code class="xref py py-func docutils literal"><span class="pre">zip_with()</span></code></a>.</p>
</dd></dl>
</div>
</div>
</div>
<div class="bottomnav">
<p class="nav">
<span class="left">
« <a href="window.html">6.14. Window Functions</a>
</span>
<span class="right">
<a href="map.html">6.16. Map Functions and Operators</a> »
</span>
</p>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>