blob: d0b4293f8c03167336840fa21a3875a799bfdd5f [file] [log] [blame]
Ian Maxonbf2c56b2017-01-24 14:14:49 -08001<!DOCTYPE html>
2<!--
Ian Maxond5b11d82017-01-25 10:48:05 -08003 | Generated by Apache Maven Doxia at 2017-01-25
Ian Maxonbf2c56b2017-01-24 14:14:49 -08004 | Rendered using Apache Maven Fluido Skin 1.3.0
5-->
6<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7 <head>
8 <meta charset="UTF-8" />
9 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
Ian Maxond5b11d82017-01-25 10:48:05 -080010 <meta name="Date-Revision-yyyymmdd" content="20170125" />
Ian Maxonbf2c56b2017-01-24 14:14:49 -080011 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB &#x2013; The Asterix Query Language, Version 1.0</title>
13 <link rel="stylesheet" href="../css/apache-maven-fluido-1.3.0.min.css" />
14 <link rel="stylesheet" href="../css/site.css" />
15 <link rel="stylesheet" href="../css/print.css" media="print" />
16
17
18 <script type="text/javascript" src="../js/apache-maven-fluido-1.3.0.min.js"></script>
19
20
21
22<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
23 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
24 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
25 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
26
27 ga('create', 'UA-41536543-1', 'uci.edu');
28 ga('send', 'pageview');</script>
29
30 </head>
31 <body class="topBarDisabled">
32
33
34
35
36 <div class="container-fluid">
37 <div id="banner">
38 <div class="pull-left">
39 <a href=".././" id="bannerLeft">
40 <img src="../images/asterixlogo.png" alt="AsterixDB"/>
41 </a>
42 </div>
43 <div class="pull-right"> </div>
44 <div class="clear"><hr/></div>
45 </div>
46
47 <div id="breadcrumbs">
48 <ul class="breadcrumb">
49
50
Ian Maxond5b11d82017-01-25 10:48:05 -080051 <li id="publishDate">Last Published: 2017-01-25</li>
Ian Maxonbf2c56b2017-01-24 14:14:49 -080052
53
54
55 <li id="projectVersion" class="pull-right">Version: 0.9.0</li>
56
57 <li class="divider pull-right">|</li>
58
59 <li class="pull-right"> <a href="../index.html" title="Documentation Home">
60 Documentation Home</a>
61 </li>
62
63 </ul>
64 </div>
65
66
67 <div class="row-fluid">
68 <div id="leftColumn" class="span3">
69 <div class="well sidebar-nav">
70
71
72 <ul class="nav nav-list">
73 <li class="nav-header">Get Started - Installation</li>
74
75 <li>
76
77 <a href="../ncservice.html" title="Option 1: using NCService">
78 <i class="none"></i>
79 Option 1: using NCService</a>
80 </li>
81
82 <li>
83
84 <a href="../install.html" title="Option 2: using Managix">
85 <i class="none"></i>
86 Option 2: using Managix</a>
87 </li>
88
89 <li>
90
91 <a href="../yarn.html" title="Option 3: using YARN">
92 <i class="none"></i>
93 Option 3: using YARN</a>
94 </li>
95 <li class="nav-header">AsterixDB Primer</li>
96
97 <li>
98
99 <a href="../sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
100 <i class="none"></i>
101 Option 1: using SQL++</a>
102 </li>
103
104 <li>
105
106 <a href="../aql/primer.html" title="Option 2: using AQL">
107 <i class="none"></i>
108 Option 2: using AQL</a>
109 </li>
110 <li class="nav-header">Data Model</li>
111
112 <li>
113
114 <a href="../datamodel.html" title="The Asterix Data Model">
115 <i class="none"></i>
116 The Asterix Data Model</a>
117 </li>
118 <li class="nav-header">Queries - SQL++</li>
119
120 <li>
121
122 <a href="../sqlpp/manual.html" title="The SQL++ Query Language">
123 <i class="none"></i>
124 The SQL++ Query Language</a>
125 </li>
126
127 <li>
128
129 <a href="../sqlpp/builtins.html" title="Builtin Functions">
130 <i class="none"></i>
131 Builtin Functions</a>
132 </li>
133 <li class="nav-header">Queries - AQL</li>
134
135 <li class="active">
136
137 <a href="#"><i class="none"></i>The Asterix Query Language (AQL)</a>
138 </li>
139
140 <li>
141
142 <a href="../aql/builtins.html" title="Builtin Functions">
143 <i class="none"></i>
144 Builtin Functions</a>
145 </li>
146 <li class="nav-header">Advanced Features</li>
147
148 <li>
149
150 <a href="../aql/similarity.html" title="Support of Similarity Queries">
151 <i class="none"></i>
152 Support of Similarity Queries</a>
153 </li>
154
155 <li>
156
157 <a href="../aql/fulltext.html" title="Support of Full-text Queries">
158 <i class="none"></i>
159 Support of Full-text Queries</a>
160 </li>
161
162 <li>
163
164 <a href="../aql/externaldata.html" title="Accessing External Data">
165 <i class="none"></i>
166 Accessing External Data</a>
167 </li>
168
169 <li>
170
171 <a href="../feeds/tutorial.html" title="Support for Data Ingestion">
172 <i class="none"></i>
173 Support for Data Ingestion</a>
174 </li>
175
176 <li>
177
178 <a href="../udf.html" title="User Defined Functions">
179 <i class="none"></i>
180 User Defined Functions</a>
181 </li>
182
183 <li>
184
185 <a href="../aql/filters.html" title="Filter-Based LSM Index Acceleration">
186 <i class="none"></i>
187 Filter-Based LSM Index Acceleration</a>
188 </li>
189 <li class="nav-header">API/SDK</li>
190
191 <li>
192
193 <a href="../api.html" title="HTTP API">
194 <i class="none"></i>
195 HTTP API</a>
196 </li>
197 </ul>
198
199
200
201 <hr class="divider" />
202
203 <div id="poweredBy">
204 <div class="clear"></div>
205 <div class="clear"></div>
206 <div class="clear"></div>
207 <a href=".././" title="AsterixDB" class="builtBy">
208 <img class="builtBy" alt="AsterixDB" src="../images/asterixlogo.png" />
209 </a>
210 </div>
211 </div>
212 </div>
213
214
215 <div id="bodyColumn" class="span9" >
216
217 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
218 ! or more contributor license agreements. See the NOTICE file
219 ! distributed with this work for additional information
220 ! regarding copyright ownership. The ASF licenses this file
221 ! to you under the Apache License, Version 2.0 (the
222 ! "License"); you may not use this file except in compliance
223 ! with the License. You may obtain a copy of the License at
224 !
225 ! http://www.apache.org/licenses/LICENSE-2.0
226 !
227 ! Unless required by applicable law or agreed to in writing,
228 ! software distributed under the License is distributed on an
229 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
230 ! KIND, either express or implied. See the License for the
231 ! specific language governing permissions and limitations
232 ! under the License.
233 ! --><h1>The Asterix Query Language, Version 1.0</h1>
234<div class="section">
235<h2><a name="Table_of_Contents"></a><a name="toc" id="toc">Table of Contents</a></h2>
236
237<ul>
238
239<li><a href="#Introduction">1. Introduction</a></li>
240
241<li><a href="#Expressions">2. Expressions</a></li>
242
243<li><a href="#Statements">3. Statements</a></li>
244</ul></div>
245<div class="section">
246<h2><a name="a1._Introduction_Back_to_TOC"></a><a name="Introduction" id="Introduction">1. Introduction</a><font size="4"> <a href="#toc">[Back to TOC]</a></font></h2>
247<p>This document is intended as a reference guide to the full syntax and semantics of the Asterix Query Language (AQL), the language for talking to AsterixDB. This guide covers both the data manipulation language (DML) aspects of AQL, including its support for queries and data modification, as well as its data definition language (DDL) aspects. New AsterixDB users are encouraged to read and work through the (friendlier) guide &#x201c;AsterixDB 101: An ADM and AQL Primer&#x201d; before attempting to make use of this document. In addition, readers are advised to read and understand the Asterix Data Model (ADM) reference guide since a basic understanding of ADM concepts is a prerequisite to understanding AQL. In what follows, we detail the features of the AQL language in a grammar-guided manner: We list and briefly explain each of the productions in the AQL grammar, offering examples for clarity in cases where doing so seems needed or helpful.</p></div>
248<div class="section">
249<h2><a name="a2._Expressions_Back_to_TOC"></a><a name="Expressions" id="Expressions">2. Expressions</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
250
251<div class="source">
252<div class="source">
253<pre>Query ::= Expression
254</pre></div></div>
255<p>An AQL query can be any legal AQL expression.</p>
256
257<div class="source">
258<div class="source">
259<pre>Expression ::= ( OperatorExpr | IfThenElse | FLWOR | QuantifiedExpression )
260</pre></div></div>
261<p>AQL is a fully composable expression language. Each AQL expression returns zero or more Asterix Data Model (ADM) instances. There are four major kinds of expressions in AQL. At the topmost level, an AQL expression can be an OperatorExpr (similar to a mathematical expression), an IfThenElse (to choose between two alternative values), a FLWOR expression (the heart of AQL, pronounced &#x201c;flower expression&#x201d;), or a QuantifiedExpression (which yields a boolean value). Each will be detailed as we explore the full AQL grammar.</p>
262<div class="section">
263<h3><a name="Primary_Expressions"></a>Primary Expressions</h3>
264
265<div class="source">
266<div class="source">
267<pre>PrimaryExpr ::= Literal
268 | VariableRef
269 | ParenthesizedExpression
270 | FunctionCallExpr
271 | DatasetAccessExpression
272 | ListConstructor
273 | ObjectConstructor
274</pre></div></div>
275<p>The most basic building block for any AQL expression is the PrimaryExpr. This can be a simple literal (constant) value, a reference to a query variable that is in scope, a parenthesized expression, a function call, an expression accessing the ADM contents of a dataset, a newly constructed list of ADM instances, or a newly constructed ADM object.</p>
276<div class="section">
277<h4><a name="Literals"></a>Literals</h4>
278
279<div class="source">
280<div class="source">
281<pre>Literal ::= StringLiteral
282 | IntegerLiteral
283 | FloatLiteral
284 | DoubleLiteral
285 | &quot;null&quot;
286 | &quot;true&quot;
287 | &quot;false&quot;
288StringLiteral ::= (&quot;\&quot;&quot; (&lt;ESCAPE_QUOT&gt; | ~[&quot;\&quot;&quot;])* &quot;\&quot;&quot;)
289 | (&quot;\'&quot; (&lt;ESCAPE_APOS&gt; | ~[&quot;\'&quot;])* &quot;\'&quot;)
290&lt;ESCAPE_QUOT&gt; ::= &quot;\\\&quot;&quot;
291&lt;ESCAPE_APOS&gt; ::= &quot;\\\'&quot;
292IntegerLiteral ::= &lt;DIGITS&gt;
293&lt;DIGITS&gt; ::= [&quot;0&quot; - &quot;9&quot;]+
294FloatLiteral ::= &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
295 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; ) )?
296 | &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
297DoubleLiteral ::= &lt;DIGITS&gt;
298 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; )?
299 | &quot;.&quot; &lt;DIGITS&gt;
300</pre></div></div>
301<p>Literals (constants) in AQL can be strings, integers, floating point values, double values, boolean constants, or the constant value null. The null value in AQL has &#x201c;unknown&#x201d; or &#x201c;missing&#x201d; value semantics, similar to (though not identical to) nulls in the relational query language SQL.</p>
302<p>The following are some simple examples of AQL literals. Since AQL is an expression language, each example is also a complete, legal AQL query (!).</p>
303<div class="section">
304<h5><a name="Examples"></a>Examples</h5>
305
306<div class="source">
307<div class="source">
308<pre>&quot;a string&quot;
30942
310</pre></div></div></div></div>
311<div class="section">
312<h4><a name="Variable_References"></a>Variable References</h4>
313
314<div class="source">
315<div class="source">
316<pre>VariableRef ::= &lt;VARIABLE&gt;
317&lt;VARIABLE&gt; ::= &quot;$&quot; &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &quot;_&quot;)*
318&lt;LETTER&gt; ::= [&quot;A&quot; - &quot;Z&quot;, &quot;a&quot; - &quot;z&quot;]
319</pre></div></div>
320<p>A variable in AQL can be bound to any legal ADM value. A variable reference refers to the value to which an in-scope variable is bound. (E.g., a variable binding may originate from one of the for or let clauses of a FLWOR expression or from an input parameter in the context of an AQL function body.)</p>
321<div class="section">
322<h5><a name="Examples"></a>Examples</h5>
323
324<div class="source">
325<div class="source">
326<pre>$tweet
327$id
328</pre></div></div></div></div>
329<div class="section">
330<h4><a name="Parenthesized_Expressions"></a>Parenthesized Expressions</h4>
331
332<div class="source">
333<div class="source">
334<pre>ParenthesizedExpression ::= &quot;(&quot; Expression &quot;)&quot;
335</pre></div></div>
336<p>As in most languages, an expression may be parenthesized.</p>
337<p>Since AQL is an expression language, the following example expression is actually also a complete, legal AQL query whose result is the value 2. (As such, you can have Big Fun explaining to your boss how AsterixDB and AQL can turn your 1000-node shared-nothing Big Data cluster into a $5M calculator in its spare time.)</p>
338<div class="section">
339<h5><a name="Example"></a>Example</h5>
340
341<div class="source">
342<div class="source">
343<pre>( 1 + 1 )
344</pre></div></div></div></div>
345<div class="section">
346<h4><a name="Function_Calls"></a>Function Calls</h4>
347
348<div class="source">
349<div class="source">
350<pre>FunctionCallExpr ::= FunctionOrTypeName &quot;(&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;)&quot;
351</pre></div></div>
352<p>Functions are included in AQL, like most languages, as a way to package useful functionality or to componentize complicated or reusable AQL computations. A function call is a legal AQL query expression that represents the ADM value resulting from the evaluation of its body expression with the given parameter bindings; the parameter value bindings can themselves be any AQL expressions.</p>
353<p>The following example is a (built-in) function call expression whose value is 8.</p>
354<div class="section">
355<h5><a name="Example"></a>Example</h5>
356
357<div class="source">
358<div class="source">
359<pre>string-length(&quot;a string&quot;)
360</pre></div></div></div></div>
361<div class="section">
362<h4><a name="Dataset_Access"></a>Dataset Access</h4>
363
364<div class="source">
365<div class="source">
366<pre>DatasetAccessExpression ::= &quot;dataset&quot; ( ( Identifier ( &quot;.&quot; Identifier )? )
367 | ( &quot;(&quot; Expression &quot;)&quot; ) )
368Identifier ::= &lt;IDENTIFIER&gt; | StringLiteral
369&lt;IDENTIFIER&gt; ::= &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &lt;SPECIALCHARS&gt;)*
370&lt;SPECIALCHARS&gt; ::= [&quot;$&quot;, &quot;_&quot;, &quot;-&quot;]
371</pre></div></div>
372<p>Querying Big Data is the main point of AsterixDB and AQL. Data in AsterixDB reside in datasets (collections of ADM objects), each of which in turn resides in some namespace known as a dataverse (data universe). Data access in a query expression is accomplished via a DatasetAccessExpression. Dataset access expressions are most commonly used in FLWOR expressions, where variables are bound to their contents.</p>
373<p>Note that the Identifier that identifies a dataset (or any other Identifier in AQL) can also be a StringLiteral. This is especially useful to avoid conficts with AQL keywords (e.g. &#x201c;dataset&#x201d;, &#x201c;null&#x201d;, or &#x201c;type&#x201d;).</p>
374<p>The following are three examples of legal dataset access expressions. The first one accesses a dataset called Customers in the dataverse called SalesDV. The second one accesses the Customers dataverse in whatever the current dataverse is. The third one does the same thing as the second but uses a slightly older AQL syntax.</p>
375<div class="section">
376<h5><a name="Examples"></a>Examples</h5>
377
378<div class="source">
379<div class="source">
380<pre>dataset SalesDV.Customers
381dataset Customers
382dataset(&quot;Customers&quot;)
383</pre></div></div></div></div>
384<div class="section">
385<h4><a name="Constructors"></a>Constructors</h4>
386
387<div class="source">
388<div class="source">
389<pre>ListConstructor ::= ( OrderedListConstructor | UnorderedListConstructor )
390OrderedListConstructor ::= &quot;[&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;]&quot;
391UnorderedListConstructor ::= &quot;{{&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;}}&quot;
392ObjectConstructor ::= &quot;{&quot; ( FieldBinding ( &quot;,&quot; FieldBinding )* )? &quot;}&quot;
393FieldBinding ::= Expression &quot;:&quot; Expression
394</pre></div></div>
395<p>A major feature of AQL is its ability to construct new ADM data instances. This is accomplished using its constructors for each of the major ADM complex object structures, namely lists (ordered or unordered) and objects. Ordered lists are like JSON arrays, while unordered lists have bag (multiset) semantics. Objects are built from attributes that are field-name/field-value pairs, again like JSON. (See the AsterixDB Data Model document for more details on each.)</p>
396<p>The following examples illustrate how to construct a new ordered list with 3 items, a new unordered list with 4 items, and a new object with 2 fields, respectively. List elements can be homogeneous (as in the first example), which is the common case, or they may be heterogeneous (as in the second example). The data values and field name values used to construct lists and objects in constructors are all simply AQL expressions. Thus the list elements, field names, and field values used in constructors can be simple literals (as in these three examples) or they can come from query variable references or even arbitrarily complex AQL expressions.</p>
397<div class="section">
398<h5><a name="Examples"></a>Examples</h5>
399
400<div class="source">
401<div class="source">
402<pre>[ &quot;a&quot;, &quot;b&quot;, &quot;c&quot; ]
403
404{{ 42, &quot;forty-two&quot;, &quot;AsterixDB!&quot;, 3.14f }}
405
406{
407 &quot;project name&quot;: &quot;AsterixDB&quot;
408 &quot;project members&quot;: {{ &quot;vinayakb&quot;, &quot;dtabass&quot;, &quot;chenli&quot; }}
409}
410</pre></div></div></div>
411<div class="section">
412<h5><a name="Note"></a>Note</h5>
413<p>When constructing nested objects there needs to be a space between the closing braces to avoid confusion with the <tt>}}</tt> token that ends an unordered list constructor: <tt>{ &quot;a&quot; : { &quot;b&quot; : &quot;c&quot; }}</tt> will fail to parse while <tt>{ &quot;a&quot; : { &quot;b&quot; : &quot;c&quot; } }</tt> will work.</p></div></div></div>
414<div class="section">
415<h3><a name="Path_Expressions"></a>Path Expressions</h3>
416
417<div class="source">
418<div class="source">
419<pre>ValueExpr ::= PrimaryExpr ( Field | Index )*
420Field ::= &quot;.&quot; Identifier
421Index ::= &quot;[&quot; ( Expression | &quot;?&quot; ) &quot;]&quot;
422</pre></div></div>
423<p>Components of complex types in ADM are accessed via path expressions. Path access can be applied to the result of an AQL expression that yields an instance of such a type, e.g., a object or list instance. For objects, path access is based on field names. For ordered lists, path access is based on (zero-based) array-style indexing. AQL also supports an &#x201c;I&#x2019;m feeling lucky&#x201d; style index accessor, [?], for selecting an arbitrary element from an ordered list. Attempts to access non-existent fields or list elements produce a null (i.e., missing information) result as opposed to signaling a runtime error.</p>
424<p>The following examples illustrate field access for a object, index-based element access for an ordered list, and also a composition thereof.</p>
425<div class="section">
426<div class="section">
427<h5><a name="Examples"></a>Examples</h5>
428
429<div class="source">
430<div class="source">
431<pre>({&quot;list&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).list
432
433([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])[2]
434
435({ &quot;list&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).list[2]
436</pre></div></div></div></div></div>
437<div class="section">
438<h3><a name="Logical_Expressions"></a>Logical Expressions</h3>
439
440<div class="source">
441<div class="source">
442<pre>OperatorExpr ::= AndExpr ( &quot;or&quot; AndExpr )*
443AndExpr ::= RelExpr ( &quot;and&quot; RelExpr )*
444</pre></div></div>
445<p>As in most languages, boolean expressions can be built up from smaller expressions by combining them with the logical connectives and/or. Legal boolean values in AQL are true, false, and null. (Nulls in AQL are treated much like SQL treats its unknown truth value in boolean expressions.)</p>
446<p>The following is an example of a conjuctive range predicate in AQL. It will yield true if $a is bound to 4, null if $a is bound to null, and false otherwise.</p>
447<div class="section">
448<div class="section">
449<h5><a name="Example"></a>Example</h5>
450
451<div class="source">
452<div class="source">
453<pre>$a &gt; 3 and $a &lt; 5
454</pre></div></div></div></div></div>
455<div class="section">
456<h3><a name="Comparison_Expressions"></a>Comparison Expressions</h3>
457
458<div class="source">
459<div class="source">
460<pre>RelExpr ::= AddExpr ( ( &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;&lt;=&quot; | &quot;&gt;=&quot; | &quot;=&quot; | &quot;!=&quot; | &quot;~=&quot; ) AddExpr )?
461</pre></div></div>
462<p>AQL has the usual list of suspects, plus one, for comparing pairs of atomic values. The &#x201c;plus one&#x201d; is the last operator listed above, which is the &#x201c;roughly equal&#x201d; operator provided for similarity queries. (See the separate document on <a href="similarity.html">AsterixDB Similarity Queries</a> for more details on similarity matching.)</p>
463<p>An example comparison expression (which yields the boolean value true) is shown below.</p>
464<div class="section">
465<div class="section">
466<h5><a name="Example"></a>Example</h5>
467
468<div class="source">
469<div class="source">
470<pre>5 &gt; 3
471</pre></div></div></div></div></div>
472<div class="section">
473<h3><a name="Arithmetic_Expressions"></a>Arithmetic Expressions</h3>
474
475<div class="source">
476<div class="source">
477<pre>AddExpr ::= MultExpr ( ( &quot;+&quot; | &quot;-&quot; ) MultExpr )*
478MultExpr ::= UnaryExpr ( ( &quot;*&quot; | &quot;/&quot; | &quot;%&quot; | &quot;^&quot;| &quot;idiv&quot; ) UnaryExpr )*
479UnaryExpr ::= ( ( &quot;+&quot; | &quot;-&quot; ) )? ValueExpr
480</pre></div></div>
481<p>AQL also supports the usual cast of characters for arithmetic expressions. The example below evaluates to 25.</p>
482<div class="section">
483<div class="section">
484<h5><a name="Example"></a>Example</h5>
485
486<div class="source">
487<div class="source">
488<pre>3 ^ 2 + 4 ^ 2
489</pre></div></div></div></div></div>
490<div class="section">
491<h3><a name="FLWOR_Expression"></a>FLWOR Expression</h3>
492
493<div class="source">
494<div class="source">
495<pre>FLWOR ::= ( ForClause | LetClause ) ( Clause )* (&quot;return&quot;|&quot;select&quot;) Expression
496Clause ::= ForClause | LetClause | WhereClause | OrderbyClause
497 | GroupClause | LimitClause | DistinctClause
498ForClause ::= (&quot;for&quot;|&quot;from&quot;) Variable ( &quot;at&quot; Variable )? &quot;in&quot; ( Expression )
499LetClause ::= (&quot;let&quot;|&quot;with&quot;) Variable &quot;:=&quot; Expression
500WhereClause ::= &quot;where&quot; Expression
501OrderbyClause ::= &quot;order&quot; &quot;by&quot; Expression ( ( &quot;asc&quot; ) | ( &quot;desc&quot; ) )?
502 ( &quot;,&quot; Expression ( ( &quot;asc&quot; ) | ( &quot;desc&quot; ) )? )*
503GroupClause ::= &quot;group&quot; &quot;by&quot; ( Variable &quot;:=&quot; )? Expression ( &quot;,&quot; ( Variable &quot;:=&quot; )? Expression )*
504 (&quot;with&quot;|&quot;keeping&quot;) VariableRef ( &quot;,&quot; VariableRef )*
505LimitClause ::= &quot;limit&quot; Expression ( &quot;offset&quot; Expression )?
506DistinctClause ::= &quot;distinct&quot; &quot;by&quot; Expression ( &quot;,&quot; Expression )*
507Variable ::= &lt;VARIABLE&gt;
508</pre></div></div>
509<p>The heart of AQL is the FLWOR (for-let-where-orderby-return) expression. The roots of this expression were borrowed from the expression of the same name in XQuery. A FLWOR expression starts with one or more clauses that establish variable bindings. A <tt>for</tt> clause binds a variable incrementally to each element of its associated expression; it includes an optional positional variable for counting/numbering the bindings. By default no ordering is implied or assumed by a <tt>for</tt> clause. A <tt>let</tt> clause binds a variable to the collection of elements computed by its associated expression.</p>
510<p>Following the initial <tt>for</tt> or <tt>let</tt> clause(s), a FLWOR expression may contain an arbitrary sequence of other clauses. The <tt>where</tt> clause in a FLWOR expression filters the preceding bindings via a boolean expression, much like a <tt>where</tt> clause does in a SQL query. The <tt>order by</tt> clause in a FLWOR expression induces an ordering on the data. The <tt>group by</tt> clause, discussed further below, forms groups based on its group by expressions, optionally naming the expressions&#x2019; values (which together form the grouping key for the expression). The <tt>with</tt> subclause of a <tt>group by</tt> clause specifies the variable(s) whose values should be grouped based on the grouping key(s); following the grouping clause, only the grouping key(s) and the variables named in the with subclause remain in scope, and the named grouping variables now contain lists formed from their input values. The <tt>limit</tt> clause caps the number of values returned, optionally starting its result count from a specified offset. (Web applications can use this feature for doing pagination.) The <tt>distinct</tt> clause is similar to the <tt>group-by</tt> clause, but it forms no groups; it serves only to eliminate duplicate values. As indicated by the grammar, the clauses in an AQL query can appear in any order. To interpret a query, one can think of data as flowing down through the query from the first clause to the <tt>return</tt> clause.</p>
511<p>The following example shows a FLWOR expression that selects and returns one user from the dataset FacebookUsers.</p>
512<div class="section">
513<div class="section">
514<h5><a name="Example"></a>Example</h5>
515
516<div class="source">
517<div class="source">
518<pre>for $user in dataset FacebookUsers
519where $user.id = 8
520return $user
521</pre></div></div>
522<p>The next example shows a FLWOR expression that joins two datasets, FacebookUsers and FacebookMessages, returning user/message pairs. The results contain one object per pair, with result objects containing the user&#x2019;s name and an entire message.</p></div>
523<div class="section">
524<h5><a name="Example"></a>Example</h5>
525
526<div class="source">
527<div class="source">
528<pre>for $user in dataset FacebookUsers
529for $message in dataset FacebookMessages
530where $message.author-id = $user.id
531return
532 {
533 &quot;uname&quot;: $user.name,
534 &quot;message&quot;: $message.message
535 };
536</pre></div></div>
537<p>In the next example, a <tt>let</tt> clause is used to bind a variable to all of a user&#x2019;s FacebookMessages. The query returns one object per user, with result objects containing the user&#x2019;s name and the set of all messages by that user.</p></div>
538<div class="section">
539<h5><a name="Example"></a>Example</h5>
540
541<div class="source">
542<div class="source">
543<pre>for $user in dataset FacebookUsers
544let $messages :=
545 for $message in dataset FacebookMessages
546 where $message.author-id = $user.id
547 return $message.message
548return
549 {
550 &quot;uname&quot;: $user.name,
551 &quot;messages&quot;: $messages
552 };
553</pre></div></div>
554<p>The following example returns all TwitterUsers ordered by their followers count (most followers first) and language. When ordering <tt>null</tt> is treated as being smaller than any other value if <tt>null</tt>s are encountered in the ordering key(s).</p></div>
555<div class="section">
556<h5><a name="Example"></a>Example</h5>
557
558<div class="source">
559<div class="source">
560<pre> for $user in dataset TwitterUsers
561 order by $user.followers_count desc, $user.lang asc
562 return $user
563</pre></div></div>
564<p>The next example illustrates the use of the <tt>group by</tt> clause in AQL. After the <tt>group by</tt> clause in the query, only variables that are either in the <tt>group by</tt> list or in the <tt>with</tt> list are in scope. The variables in the clause&#x2019;s <tt>with</tt> list will each contain a collection of items following the <tt>group by</tt> clause; the collected items are the values that the source variable was bound to in the tuples that formed the group. For grouping <tt>null</tt> is handled as a single value.</p></div>
565<div class="section">
566<h5><a name="Example"></a>Example</h5>
567
568<div class="source">
569<div class="source">
570<pre> for $x in dataset FacebookMessages
571 let $messages := $x.message
572 group by $loc := $x.sender-location with $messages
573 return
574 {
575 &quot;location&quot; : $loc,
576 &quot;message&quot; : $messages
577 }
578</pre></div></div>
579<p>The use of the <tt>limit</tt> clause is illustrated in the next example.</p></div>
580<div class="section">
581<h5><a name="Example"></a>Example</h5>
582
583<div class="source">
584<div class="source">
585<pre> for $user in dataset TwitterUsers
586 order by $user.followers_count desc
587 limit 2
588 return $user
589</pre></div></div>
590<p>The final example shows how AQL&#x2019;s <tt>distinct by</tt> clause works. Each variable in scope before the distinct clause is also in scope after the <tt>distinct by</tt> clause. This clause works similarly to <tt>group by</tt>, but for each variable that contains more than one value after the <tt>distinct by</tt> clause, one value is picked nondeterministically. (If the variable is in the <tt>distinct by</tt> list, then its value will be deterministic.) Nulls are treated as a single value when they occur in a grouping field.</p></div>
591<div class="section">
592<h5><a name="Example"></a>Example</h5>
593
594<div class="source">
595<div class="source">
596<pre> for $x in dataset FacebookMessages
597 distinct by $x.sender-location
598 return
599 {
600 &quot;location&quot; : $x.sender-location,
601 &quot;message&quot; : $x.message
602 }
603</pre></div></div>
604<p>In order to allow SQL fans to write queries in their favored ways, AQL provides synonyms: <i>from</i> for <i>for</i>, <i>select</i> for <i>return</i>, <i>with</i> for <i>let</i>, and <i>keeping</i> for <i>with</i> in the group by clause. The following query is such an example.</p></div>
605<div class="section">
606<h5><a name="Example"></a>Example</h5>
607
608<div class="source">
609<div class="source">
610<pre> from $x in dataset FacebookMessages
611 with $messages := $x.message
612 group by $loc := $x.sender-location keeping $messages
613 select
614 {
615 &quot;location&quot; : $loc,
616 &quot;message&quot; : $messages
617 }
618</pre></div></div></div></div></div>
619<div class="section">
620<h3><a name="Conditional_Expression"></a>Conditional Expression</h3>
621
622<div class="source">
623<div class="source">
624<pre>IfThenElse ::= &quot;if&quot; &quot;(&quot; Expression &quot;)&quot; &quot;then&quot; Expression &quot;else&quot; Expression
625</pre></div></div>
626<p>A conditional expression is useful for choosing between two alternative values based on a boolean condition. If its first (<tt>if</tt>) expression is true, its second (<tt>then</tt>) expression&#x2019;s value is returned, and otherwise its third (<tt>else</tt>) expression is returned.</p>
627<p>The following example illustrates the form of a conditional expression.</p>
628<div class="section">
629<div class="section">
630<h5><a name="Example"></a>Example</h5>
631
632<div class="source">
633<div class="source">
634<pre>if (2 &lt; 3) then &quot;yes&quot; else &quot;no&quot;
635</pre></div></div></div></div></div>
636<div class="section">
637<h3><a name="Quantified_Expressions"></a>Quantified Expressions</h3>
638
639<div class="source">
640<div class="source">
641<pre>QuantifiedExpression ::= ( ( &quot;some&quot; ) | ( &quot;every&quot; ) ) Variable &quot;in&quot; Expression
642 ( &quot;,&quot; Variable &quot;in&quot; Expression )* &quot;satisfies&quot; Expression
643</pre></div></div>
644<p>Quantified expressions are used for expressing existential or universal predicates involving the elements of a collection.</p>
645<p>The following pair of examples illustrate the use of a quantified expression to test that every (or some) element in the set [1, 2, 3] of integers is less than three. The first example yields <tt>false</tt> and second example yields <tt>true</tt>.</p>
646<p>It is useful to note that if the set were instead the empty set, the first expression would yield <tt>true</tt> (&#x201c;every&#x201d; value in an empty set satisfies the condition) while the second expression would yield <tt>false</tt> (since there isn&#x2019;t &#x201c;some&#x201d; value, as there are no values in the set, that satisfies the condition).</p>
647<div class="section">
648<div class="section">
649<h5><a name="Examples"></a>Examples</h5>
650
651<div class="source">
652<div class="source">
653<pre>every $x in [ 1, 2, 3 ] satisfies $x &lt; 3
654some $x in [ 1, 2, 3 ] satisfies $x &lt; 3
655</pre></div></div></div></div></div></div>
656<div class="section">
657<h2><a name="a3._Statements_Back_to_TOC"></a><a name="Statements" id="Statements">3. Statements</a> <font size="4"><a href="#toc">[Back to TOC]</a></font></h2>
658
659<div class="source">
660<div class="source">
661<pre>Statement ::= ( SingleStatement ( &quot;;&quot; )? )* &lt;EOF&gt;
662SingleStatement ::= DataverseDeclaration
663 | FunctionDeclaration
664 | CreateStatement
665 | DropStatement
666 | LoadStatement
667 | SetStatement
668 | InsertStatement
669 | DeleteStatement
670 | UpsertStatement
671 | Query
672</pre></div></div>
673<p>In addition to expresssions for queries, AQL supports a variety of statements for data definition and manipulation purposes as well as controlling the context to be used in evaluating AQL expressions. AQL supports object-level ACID transactions that begin and terminate implicitly for each object inserted, deleted, upserted, or searched while a given AQL statement is being executed.</p>
674<p>This section details the statements supported in the AQL language.</p>
675<div class="section">
676<h3><a name="Declarations"></a>Declarations</h3>
677
678<div class="source">
679<div class="source">
680<pre>DataverseDeclaration ::= &quot;use&quot; &quot;dataverse&quot; Identifier
681</pre></div></div>
682<p>The world of data in an AsterixDB cluster is organized into data namespaces called dataverses. To set the default dataverse for a series of statements, the use dataverse statement is provided.</p>
683<p>As an example, the following statement sets the default dataverse to be TinySocial.</p>
684<div class="section">
685<div class="section">
686<h5><a name="Example"></a>Example</h5>
687
688<div class="source">
689<div class="source">
690<pre>use dataverse TinySocial;
691</pre></div></div>
692<p>The set statement in AQL is used to control aspects of the expression evalation context for queries.</p>
693
694<div class="source">
695<div class="source">
696<pre>SetStatement ::= &quot;set&quot; Identifier StringLiteral
697</pre></div></div>
698<p>As an example, the following set statements request that Jaccard similarity with a similarity threshold 0.6 be used for set similarity matching when the ~= operator is used in a query expression.</p></div>
699<div class="section">
700<h5><a name="Example"></a>Example</h5>
701
702<div class="source">
703<div class="source">
704<pre>set simfunction &quot;jaccard&quot;;
705set simthreshold &quot;0.6f&quot;;
706</pre></div></div>
707<p>When writing a complex AQL query, it can sometimes be helpful to define one or more auxilliary functions that each address a sub-piece of the overall query. The declare function statement supports the creation of such helper functions.</p>
708
709<div class="source">
710<div class="source">
711<pre>FunctionDeclaration ::= &quot;declare&quot; &quot;function&quot; Identifier ParameterList &quot;{&quot; Expression &quot;}&quot;
712ParameterList ::= &quot;(&quot; ( &lt;VARIABLE&gt; ( &quot;,&quot; &lt;VARIABLE&gt; )* )? &quot;)&quot;
713</pre></div></div>
714<p>The following is a very simple example of a temporary AQL function definition.</p></div>
715<div class="section">
716<h5><a name="Example"></a>Example</h5>
717
718<div class="source">
719<div class="source">
720<pre>declare function add($a, $b) {
721 $a + $b
722};
723</pre></div></div></div></div></div>
724<div class="section">
725<h3><a name="Lifecycle_Management_Statements"></a>Lifecycle Management Statements</h3>
726
727<div class="source">
728<div class="source">
729<pre>CreateStatement ::= &quot;create&quot; ( DataverseSpecification
730 | TypeSpecification
731 | DatasetSpecification
732 | IndexSpecification
733 | FunctionSpecification )
734
735QualifiedName ::= Identifier ( &quot;.&quot; Identifier )?
736DoubleQualifiedName ::= Identifier &quot;.&quot; Identifier ( &quot;.&quot; Identifier )?
737</pre></div></div>
738<p>The create statement in AQL is used for creating persistent artifacts in the context of dataverses. It can be used to create new dataverses, datatypes, datasets, indexes, and user-defined AQL functions.</p>
739<div class="section">
740<h4><a name="Dataverses"></a>Dataverses</h4>
741
742<div class="source">
743<div class="source">
744<pre>DataverseSpecification ::= &quot;dataverse&quot; Identifier IfNotExists ( &quot;with format&quot; StringLiteral )?
745</pre></div></div>
746<p>The create dataverse statement is used to create new dataverses. To ease the authoring of reusable AQL scripts, its optional IfNotExists clause allows creation to be requested either unconditionally or only if the the dataverse does not already exist. If this clause is absent, an error will be returned if the specified dataverse already exists. The <tt>with format</tt> clause is a placeholder for future functionality that can safely be ignored.</p>
747<p>The following example creates a dataverse named TinySocial.</p>
748<div class="section">
749<h5><a name="Example"></a>Example</h5>
750
751<div class="source">
752<div class="source">
753<pre>create dataverse TinySocial;
754</pre></div></div></div></div>
755<div class="section">
756<h4><a name="Types"></a>Types</h4>
757
758<div class="source">
759<div class="source">
760<pre>TypeSpecification ::= &quot;type&quot; FunctionOrTypeName IfNotExists &quot;as&quot; TypeExpr
761FunctionOrTypeName ::= QualifiedName
762IfNotExists ::= ( &quot;if not exists&quot; )?
763TypeExpr ::= ObjectTypeDef | TypeReference | OrderedListTypeDef | UnorderedListTypeDef
764ObjectTypeDef ::= ( &quot;closed&quot; | &quot;open&quot; )? &quot;{&quot; ( ObjectField ( &quot;,&quot; ObjectField )* )? &quot;}&quot;
765ObjectField ::= Identifier &quot;:&quot; ( TypeExpr ) ( &quot;?&quot; )?
766NestedField ::= Identifier ( &quot;.&quot; Identifier )*
767IndexField ::= NestedField ( &quot;:&quot; TypeReference )?
768TypeReference ::= Identifier
769OrderedListTypeDef ::= &quot;[&quot; ( TypeExpr ) &quot;]&quot;
770UnorderedListTypeDef ::= &quot;{{&quot; ( TypeExpr ) &quot;}}&quot;
771</pre></div></div>
772<p>The create type statement is used to create a new named ADM datatype. This type can then be used to create datasets or utilized when defining one or more other ADM datatypes. Much more information about the Asterix Data Model (ADM) is available in the <a href="datamodel.html">data model reference guide</a> to ADM. A new type can be a object type, a renaming of another type, an ordered list type, or an unordered list type. A object type can be defined as being either open or closed. Instances of a closed object type are not permitted to contain fields other than those specified in the create type statement. Instances of an open object type may carry additional fields, and open is the default for a new type (if neither option is specified).</p>
773<p>The following example creates a new ADM object type called FacebookUser type. Since it is closed, its instances will contain only what is specified in the type definition. The first four fields are traditional typed name/value pairs. The friend-ids field is an unordered list of 32-bit integers. The employment field is an ordered list of instances of another named object type, EmploymentType.</p>
774<div class="section">
775<h5><a name="Example"></a>Example</h5>
776
777<div class="source">
778<div class="source">
779<pre>create type FacebookUserType as closed {
780 &quot;id&quot; : int32,
781 &quot;alias&quot; : string,
782 &quot;name&quot; : string,
783 &quot;user-since&quot; : datetime,
784 &quot;friend-ids&quot; : {{ int32 }},
785 &quot;employment&quot; : [ EmploymentType ]
786}
787</pre></div></div>
788<p>The next example creates a new ADM object type called FbUserType. Note that the type of the id field is UUID. You need to use this field type if you want to have this field be an autogenerated-PK field. Refer to the Datasets section later for more details.</p></div>
789<div class="section">
790<h5><a name="Example"></a>Example</h5>
791
792<div class="source">
793<div class="source">
794<pre>create type FbUserType as closed {
795 &quot;id&quot; : uuid,
796 &quot;alias&quot; : string,
797 &quot;name&quot; : string
798}
799</pre></div></div></div></div>
800<div class="section">
801<h4><a name="Datasets"></a>Datasets</h4>
802
803<div class="source">
804<div class="source">
805<pre>DatasetSpecification ::= &quot;internal&quot;? &quot;dataset&quot; QualifiedName &quot;(&quot; QualifiedName &quot;)&quot; IfNotExists
806 PrimaryKey ( &quot;on&quot; Identifier )? ( &quot;hints&quot; Properties )?
807 ( &quot;using&quot; &quot;compaction&quot; &quot;policy&quot; CompactionPolicy ( Configuration )? )?
808 ( &quot;with filter on&quot; Identifier )?
809 | &quot;external&quot; &quot;dataset&quot; QualifiedName &quot;(&quot; QualifiedName &quot;)&quot; IfNotExists
810 &quot;using&quot; AdapterName Configuration ( &quot;hints&quot; Properties )?
811 ( &quot;using&quot; &quot;compaction&quot; &quot;policy&quot; CompactionPolicy ( Configuration )? )?
812AdapterName ::= Identifier
813Configuration ::= &quot;(&quot; ( KeyValuePair ( &quot;,&quot; KeyValuePair )* )? &quot;)&quot;
814KeyValuePair ::= &quot;(&quot; StringLiteral &quot;=&quot; StringLiteral &quot;)&quot;
815Properties ::= ( &quot;(&quot; Property ( &quot;,&quot; Property )* &quot;)&quot; )?
816Property ::= Identifier &quot;=&quot; ( StringLiteral | IntegerLiteral )
817FunctionSignature ::= FunctionOrTypeName &quot;@&quot; IntegerLiteral
818PrimaryKey ::= &quot;primary&quot; &quot;key&quot; NestedField ( &quot;,&quot; NestedField )* ( &quot;autogenerated &quot;)?
819CompactionPolicy ::= Identifier
820PrimaryKey ::= &quot;primary&quot; &quot;key&quot; Identifier ( &quot;,&quot; Identifier )* ( &quot;autogenerated &quot;)?
821</pre></div></div>
822<p>The create dataset statement is used to create a new dataset. Datasets are named, unordered collections of ADM object instances; they are where data lives persistently and are the targets for queries in AsterixDB. Datasets are typed, and AsterixDB will ensure that their contents conform to their type definitions. An Internal dataset (the default) is a dataset that is stored in and managed by AsterixDB. It must have a specified unique primary key that can be used to partition data across nodes of an AsterixDB cluster. The primary key is also used in secondary indexes to uniquely identify the indexed primary data objects. Random primary key (UUID) values can be auto-generated by declaring the field to be UUID and putting &#x201c;autogenerated&#x201d; after the &#x201c;primary key&#x201d; identifier. In this case, values for the auto-generated PK field should not be provided by the user since it will be auto-generated by AsterixDB. Optionally, a filter can be created on a field to further optimize range queries with predicates on the filter&#x2019;s field. (Refer to <a href="filters.html">Filter-Based LSM Index Acceleration</a> for more information about filters.)</p>
823<p>An External dataset is stored outside of AsterixDB (currently datasets in HDFS or on the local filesystem(s) of the cluster&#x2019;s nodes are supported). External dataset support allows AQL queries to treat external data as though it were stored in AsterixDB, making it possible to query &#x201c;legacy&#x201d; file data (e.g., Hive data) without having to physically import it into AsterixDB. For an external dataset, an appropriate adapter must be selected to handle the nature of the desired external data. (See the <a href="externaldata.html">guide to external data</a> for more information on the available adapters.)</p>
824<p>When creating a dataset, it is possible to choose a merge policy that controls which of the underlaying LSM storage components to be merged. Currently, AsterixDB provides four different merge policies that can be configured per dataset: no-merge, constant, prefix, and correlated-prefix. The no-merge policy simply never merges disk components. While the constant policy merges disk components when the number of components reaches some constant number k, which can be configured by the user. The prefix policy relies on component sizes and the number of components to decide which components to merge. Specifically, it works by first trying to identify the smallest ordered (oldest to newest) sequence of components such that the sequence does not contain a single component that exceeds some threshold size M and that either the sum of the component&#x2019;s sizes exceeds M or the number of components in the sequence exceeds another threshold C. If such a sequence of components exists, then each of the components in the sequence are merged together to form a single component. Finally, the correlated-prefix is similar to the prefix policy but it delegates the decision of merging the disk components of all the indexes in a dataset to the primary index. When the policy decides that the primary index needs to be merged (using the same decision criteria as for the prefix policy), then it will issue successive merge requests on behalf of all other indexes associated with the same dataset. The default policy for AsterixDB is the prefix policy except when there is a filter on a dataset, where the preferred policy for filters is the correlated-prefix.</p>
825<p>The following example creates an internal dataset for storing FacefookUserType objects. It specifies that their id field is their primary key.</p>
826<div class="section">
827<h5><a name="Example"></a>Example</h5>
828
829<div class="source">
830<div class="source">
831<pre>create internal dataset FacebookUsers(FacebookUserType) primary key id;
832</pre></div></div>
833<p>The following example creates an internal dataset for storing FbUserType objects. It specifies that their id field is their primary key. It also specifies that the id field is an auto-generated field, meaning that a randomly generated UUID value will be assigned to each object by the system. (A user should therefore not proivde a value for this field.) Note that the id field should be UUID.</p></div>
834<div class="section">
835<h5><a name="Example"></a>Example</h5>
836
837<div class="source">
838<div class="source">
839<pre>create internal dataset FbMsgs(FbUserType) primary key id autogenerated;
840</pre></div></div>
841<p>The next example creates an external dataset for storing LineitemType objects. The choice of the <tt>hdfs</tt> adapter means that its data will reside in HDFS. The create statement provides parameters used by the hdfs adapter: the URL and path needed to locate the data in HDFS and a description of the data format.</p></div>
842<div class="section">
843<h5><a name="Example"></a>Example</h5>
844
845<div class="source">
846<div class="source">
847<pre>create external dataset Lineitem('LineitemType) using hdfs (
848 (&quot;hdfs&quot;=&quot;hdfs://HOST:PORT&quot;),
849 (&quot;path&quot;=&quot;HDFS_PATH&quot;),
850 (&quot;input-format&quot;=&quot;text-input-format&quot;),
851 (&quot;format&quot;=&quot;delimited-text&quot;),
852 (&quot;delimiter&quot;=&quot;|&quot;));
853</pre></div></div></div></div>
854<div class="section">
855<h4><a name="Indices"></a>Indices</h4>
856
857<div class="source">
858<div class="source">
859<pre>IndexSpecification ::= &quot;index&quot; Identifier IfNotExists &quot;on&quot; QualifiedName
860 &quot;(&quot; ( IndexField ) ( &quot;,&quot; IndexField )* &quot;)&quot; ( &quot;type&quot; IndexType )? ( &quot;enforced&quot; )?
861IndexType ::= &quot;btree&quot;
862 | &quot;rtree&quot;
863 | &quot;keyword&quot;
864 | &quot;ngram&quot; &quot;(&quot; IntegerLiteral &quot;)&quot;
865 | &quot;fulltext&quot;
866</pre></div></div>
867<p>The create index statement creates a secondary index on one or more fields of a specified dataset. Supported index types include <tt>btree</tt> for totally ordered datatypes, <tt>rtree</tt> for spatial data, and <tt>keyword</tt>, <tt>ngram</tt>, and <tt>fulltext</tt> for textual (string) data. An index can be created on a nested field (or fields) by providing a valid path expression as an index field identifier. An index field is not required to be part of the datatype associated with a dataset if that datatype is declared as open and the field&#x2019;s type is provided along with its type and the <tt>enforced</tt> keyword is specified in the end of index definition. <tt>Enforcing</tt> an open field will introduce a check that will make sure that the actual type of an indexed field (if the field exists in the object) always matches this specified (open) field type.</p>
868<p>The following example creates a btree index called fbAuthorIdx on the author-id field of the FacebookMessages dataset. This index can be useful for accelerating exact-match queries, range search queries, and joins involving the author-id field.</p>
869<div class="section">
870<h5><a name="Example"></a>Example</h5>
871
872<div class="source">
873<div class="source">
874<pre>create index fbAuthorIdx on FacebookMessages(author-id) type btree;
875</pre></div></div>
876<p>The following example creates an open btree index called fbSendTimeIdx on the open send-time field of the FacebookMessages dataset having datetime type. This index can be useful for accelerating exact-match queries, range search queries, and joins involving the send-time field.</p></div>
877<div class="section">
878<h5><a name="Example"></a>Example</h5>
879
880<div class="source">
881<div class="source">
882<pre>create index fbSendTimeIdx on FacebookMessages(send-time:datetime) type btree enforced;
883</pre></div></div>
884<p>The following example creates a btree index called twUserScrNameIdx on the screen-name field, which is a nested field of the user field in the TweetMessages dataset. This index can be useful for accelerating exact-match queries, range search queries, and joins involving the screen-name field.</p></div>
885<div class="section">
886<h5><a name="Example"></a>Example</h5>
887
888<div class="source">
889<div class="source">
890<pre>create index twUserScrNameIdx on TweetMessages(user.screen-name) type btree;
891</pre></div></div>
892<p>The following example creates an rtree index called fbSenderLocIdx on the sender-location field of the FacebookMessages dataset. This index can be useful for accelerating queries that use the <a href="functions.html#spatial-intersect"><tt>spatial-intersect</tt> function</a> in a predicate involving the sender-location field.</p></div>
893<div class="section">
894<h5><a name="Example"></a>Example</h5>
895
896<div class="source">
897<div class="source">
898<pre>create index fbSenderLocIndex on FacebookMessages(sender-location) type rtree;
899</pre></div></div>
900<p>The following example creates a 3-gram index called fbUserIdx on the name field of the FacebookUsers dataset. This index can be used to accelerate some similarity or substring maching queries on the name field. For details refer to the <a href="similarity.html#NGram_Index">document on similarity queries</a>.</p></div>
901<div class="section">
902<h5><a name="Example"></a>Example</h5>
903
904<div class="source">
905<div class="source">
906<pre>create index fbUserIdx on FacebookUsers(name) type ngram(3);
907</pre></div></div>
908<p>The following example creates a keyword index called fbMessageIdx on the message field of the FacebookMessages dataset. This keyword index can be used to optimize queries with token-based similarity predicates on the message field. For details refer to the <a href="similarity.html#Keyword_Index">document on similarity queries</a>.</p></div>
909<div class="section">
910<h5><a name="Example"></a>Example</h5>
911
912<div class="source">
913<div class="source">
914<pre>create index fbMessageIdx on FacebookMessages(message) type keyword;
915</pre></div></div>
916<p>The following example creates a full-text index called fbMessageIdx on the message field of the FacebookMessages dataset. This full-text index can be used to optimize queries with full-text search predicates on the message field. For details refer to the <a href="fulltext.html#toc">document on full-text queries</a>.</p></div>
917<div class="section">
918<h5><a name="Example"></a>Example</h5>
919
920<div class="source">
921<div class="source">
922<pre>create index fbMessageIdx on FacebookMessages(message) type fulltext;
923</pre></div></div></div></div>
924<div class="section">
925<h4><a name="Functions"></a>Functions</h4>
926<p>The create function statement creates a named function that can then be used and reused in AQL queries. The body of a function can be any AQL expression involving the function&#x2019;s parameters.</p>
927
928<div class="source">
929<div class="source">
930<pre>FunctionSpecification ::= &quot;function&quot; FunctionOrTypeName IfNotExists ParameterList &quot;{&quot; Expression &quot;}&quot;
931</pre></div></div>
932<p>The following is a very simple example of a create function statement. It differs from the declare function example shown previously in that it results in a function that is persistently registered by name in the specified dataverse.</p>
933<div class="section">
934<h5><a name="Example"></a>Example</h5>
935
936<div class="source">
937<div class="source">
938<pre>create function add($a, $b) {
939 $a + $b
940};
941</pre></div></div></div></div>
942<div class="section">
943<h4><a name="Removal"></a>Removal</h4>
944
945<div class="source">
946<div class="source">
947<pre>DropStatement ::= &quot;drop&quot; ( &quot;dataverse&quot; Identifier IfExists
948 | &quot;type&quot; FunctionOrTypeName IfExists
949 | &quot;dataset&quot; QualifiedName IfExists
950 | &quot;index&quot; DoubleQualifiedName IfExists
951 | &quot;function&quot; FunctionSignature IfExists )
952IfExists ::= ( &quot;if&quot; &quot;exists&quot; )?
953</pre></div></div>
954<p>The drop statement in AQL is the inverse of the create statement. It can be used to drop dataverses, datatypes, datasets, indexes, and functions.</p>
955<p>The following examples illustrate uses of the drop statement.</p>
956<div class="section">
957<h5><a name="Example"></a>Example</h5>
958
959<div class="source">
960<div class="source">
961<pre>drop dataset FacebookUsers if exists;
962
963drop index FacebookUsers.fbSenderLocIndex;
964
965drop type FacebookUserType;
966
967drop dataverse TinySocial;
968
969drop function add;
970</pre></div></div></div></div></div>
971<div class="section">
972<h3><a name="ImportExport_Statements"></a>Import/Export Statements</h3>
973
974<div class="source">
975<div class="source">
976<pre>LoadStatement ::= &quot;load&quot; &quot;dataset&quot; QualifiedName &quot;using&quot; AdapterName Configuration ( &quot;pre-sorted&quot; )?
977</pre></div></div>
978<p>The load statement is used to initially populate a dataset via bulk loading of data from an external file. An appropriate adapter must be selected to handle the nature of the desired external data. The load statement accepts the same adapters and the same parameters as external datasets. (See the <a href="externaldata.html">guide to external data</a> for more information on the available adapters.) If a dataset has an auto-generated primary key field, a file to be imported should not include that field in it.</p>
979<p>The following example shows how to bulk load the FacebookUsers dataset from an external file containing data that has been prepared in ADM format.</p>
980<div class="section">
981<div class="section">
982<h5><a name="Example"></a>Example</h5>
983
984<div class="source">
985<div class="source">
986<pre>load dataset FacebookUsers using localfs
987((&quot;path&quot;=&quot;localhost:///Users/zuck/AsterixDB/load/fbu.adm&quot;),(&quot;format&quot;=&quot;adm&quot;));
988</pre></div></div></div></div></div>
989<div class="section">
990<h3><a name="Modification_Statements"></a>Modification Statements</h3>
991<div class="section">
992<h4><a name="Insert"></a>Insert</h4>
993
994<div class="source">
995<div class="source">
996<pre>InsertStatement ::= &quot;insert&quot; &quot;into&quot; &quot;dataset&quot; QualifiedName ( &quot;as&quot; Variable )? Query ( &quot;returning&quot; Query )?
997</pre></div></div>
998<p>The AQL insert statement is used to insert data into a dataset. The data to be inserted comes from an AQL query expression. The expression can be as simple as a constant expression, or in general it can be any legal AQL query. Inserts in AsterixDB are processed transactionally, with the scope of each insert transaction being the insertion of a single object plus its affiliated secondary index entries (if any). If the query part of an insert returns a single object, then the insert statement itself will be a single, atomic transaction. If the query part returns multiple objects, then each object inserted will be handled independently as a tranaction. If a dataset has an auto-generated primary key field, an insert statement should not include a value for that field in it. (The system will automatically extend the provided object with this additional field and a corresponding value.). The optional &#x201c;as Variable&#x201d; provides a variable binding for the inserted objects, which can be used in the &#x201c;returning&#x201d; clause. The optional &#x201c;returning Query&#x201d; allows users to run simple queries/functions on the objects returned by the insert. This query cannot refer to any datasets.</p>
999<p>The following example illustrates a query-based insertion.</p>
1000<div class="section">
1001<h5><a name="Example"></a>Example</h5>
1002
1003<div class="source">
1004<div class="source">
1005<pre>insert into dataset UsersCopy as $inserted (for $user in dataset FacebookUsers return $user ) returning $inserted.screen-name
1006</pre></div></div></div></div>
1007<div class="section">
1008<h4><a name="Delete"></a>Delete</h4>
1009
1010<div class="source">
1011<div class="source">
1012<pre>DeleteStatement ::= &quot;delete&quot; Variable &quot;from&quot; &quot;dataset&quot; QualifiedName ( &quot;where&quot; Expression )?
1013</pre></div></div>
1014<p>The AQL delete statement is used to delete data from a target dataset. The data to be deleted is identified by a boolean expression involving the variable bound to the target dataset in the delete statement. Deletes in AsterixDB are processed transactionally, with the scope of each delete transaction being the deletion of a single object plus its affiliated secondary index entries (if any). If the boolean expression for a delete identifies a single object, then the delete statement itself will be a single, atomic transaction. If the expression identifies multiple objects, then each object deleted will be handled independently as a transaction.</p>
1015<p>The following example illustrates a single-object deletion.</p>
1016<div class="section">
1017<h5><a name="Example"></a>Example</h5>
1018
1019<div class="source">
1020<div class="source">
1021<pre>delete $user from dataset FacebookUsers where $user.id = 8;
1022</pre></div></div></div></div>
1023<div class="section">
1024<h4><a name="Upsert"></a>Upsert</h4>
1025
1026<div class="source">
1027<div class="source">
1028<pre>UpsertStatement ::= &quot;upsert&quot; &quot;into&quot; &quot;dataset&quot; QualifiedName Query
1029</pre></div></div>
1030<p>The AQL upsert statement is used to couple delete (if found) with insert data into a dataset. The data to be upserted comes from an AQL query expression. The expression can be as simple as a constant expression, or in general it can be any legal AQL query. Upserts in AsterixDB are processed transactionally, with the scope of each upsert transaction being the upsertion (deletion if found + insertion) of a single object plus its affiliated secondary index entries (if any). If the query part of an upsert returns a single object, then the upsert statement itself will be a single, atomic transaction. If the query part returns multiple objects, then each object upserted will be handled independently as a tranaction.</p>
1031<p>The following example illustrates a query-based upsertion.</p>
1032<div class="section">
1033<h5><a name="Example"></a>Example</h5>
1034
1035<div class="source">
1036<div class="source">
1037<pre>upsert into dataset Users (for $user in dataset FacebookUsers return $user)
1038</pre></div></div>
1039<p>We close this guide to AQL with one final example of a query expression.</p></div>
1040<div class="section">
1041<h5><a name="Example"></a>Example</h5>
1042
1043<div class="source">
1044<div class="source">
1045<pre>for $praise in {{ &quot;great&quot;, &quot;brilliant&quot;, &quot;awesome&quot; }}
1046return
1047 string-concat([&quot;AsterixDB is &quot;, $praise])
1048</pre></div></div></div></div></div></div>
1049 </div>
1050 </div>
1051 </div>
1052
1053 <hr/>
1054
1055 <footer>
1056 <div class="container-fluid">
1057 <div class="row span12">Copyright &copy; 2017
1058 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
1059 All Rights Reserved.
1060
1061 </div>
1062
1063 <?xml version="1.0" encoding="UTF-8"?>
1064<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
1065 feather logo, and the Apache AsterixDB project logo are either
1066 registered trademarks or trademarks of The Apache Software
1067 Foundation in the United States and other countries.
1068 All other marks mentioned may be trademarks or registered
1069 trademarks of their respective owners.</div>
1070
1071
1072 </div>
1073 </footer>
1074 </body>
1075</html>