blob: 4b7f47735ec6f096cd5891ce53334bdf13b77f18 [file] [log] [blame]
Ian Maxon100cb802017-04-24 18:48:07 -07001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia at 2017-04-24
4 | 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" />
10 <meta name="Date-Revision-yyyymmdd" content="20170424" />
11 <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
51 <li id="publishDate">Last Published: 2017-04-24</li>
52
53
54
55 <li id="projectVersion" class="pull-right">Version: 0.9.1</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="../ansible.html" title="Option 2: using Ansible">
85 <i class="none"></i>
86 Option 2: using Ansible</a>
87 </li>
88
89 <li>
90
91 <a href="../aws.html" title="Option 3: using Amazon Web Services">
92 <i class="none"></i>
93 Option 3: using Amazon Web Services</a>
94 </li>
95
96 <li>
97
98 <a href="../yarn.html" title="Option 4: using YARN">
99 <i class="none"></i>
100 Option 4: using YARN</a>
101 </li>
102
103 <li>
104
105 <a href="../install.html" title="Option 5: using Managix (deprecated)">
106 <i class="none"></i>
107 Option 5: using Managix (deprecated)</a>
108 </li>
109 <li class="nav-header">AsterixDB Primer</li>
110
111 <li>
112
113 <a href="../sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
114 <i class="none"></i>
115 Option 1: using SQL++</a>
116 </li>
117
118 <li>
119
120 <a href="../aql/primer.html" title="Option 2: using AQL">
121 <i class="none"></i>
122 Option 2: using AQL</a>
123 </li>
124 <li class="nav-header">Data Model</li>
125
126 <li>
127
128 <a href="../datamodel.html" title="The Asterix Data Model">
129 <i class="none"></i>
130 The Asterix Data Model</a>
131 </li>
132 <li class="nav-header">Queries - SQL++</li>
133
134 <li>
135
136 <a href="../sqlpp/manual.html" title="The SQL++ Query Language">
137 <i class="none"></i>
138 The SQL++ Query Language</a>
139 </li>
140
141 <li>
142
143 <a href="../sqlpp/builtins.html" title="Builtin Functions">
144 <i class="none"></i>
145 Builtin Functions</a>
146 </li>
147 <li class="nav-header">Queries - AQL</li>
148
149 <li class="active">
150
151 <a href="#"><i class="none"></i>The Asterix Query Language (AQL)</a>
152 </li>
153
154 <li>
155
156 <a href="../aql/builtins.html" title="Builtin Functions">
157 <i class="none"></i>
158 Builtin Functions</a>
159 </li>
160 <li class="nav-header">API/SDK</li>
161
162 <li>
163
164 <a href="../api.html" title="HTTP API">
165 <i class="none"></i>
166 HTTP API</a>
167 </li>
168
169 <li>
170
171 <a href="../csv.html" title="CSV Output">
172 <i class="none"></i>
173 CSV Output</a>
174 </li>
175 <li class="nav-header">Advanced Features</li>
176
177 <li>
178
179 <a href="../aql/fulltext.html" title="Support of Full-text Queries">
180 <i class="none"></i>
181 Support of Full-text Queries</a>
182 </li>
183
184 <li>
185
186 <a href="../aql/externaldata.html" title="Accessing External Data">
187 <i class="none"></i>
188 Accessing External Data</a>
189 </li>
190
191 <li>
192
193 <a href="../feeds/tutorial.html" title="Support for Data Ingestion">
194 <i class="none"></i>
195 Support for Data Ingestion</a>
196 </li>
197
198 <li>
199
200 <a href="../udf.html" title="User Defined Functions">
201 <i class="none"></i>
202 User Defined Functions</a>
203 </li>
204
205 <li>
206
207 <a href="../aql/filters.html" title="Filter-Based LSM Index Acceleration">
208 <i class="none"></i>
209 Filter-Based LSM Index Acceleration</a>
210 </li>
211
212 <li>
213
214 <a href="../aql/similarity.html" title="Support of Similarity Queries">
215 <i class="none"></i>
216 Support of Similarity Queries</a>
217 </li>
218 </ul>
219
220
221
222 <hr class="divider" />
223
224 <div id="poweredBy">
225 <div class="clear"></div>
226 <div class="clear"></div>
227 <div class="clear"></div>
228 <a href=".././" title="AsterixDB" class="builtBy">
229 <img class="builtBy" alt="AsterixDB" src="../images/asterixlogo.png" />
230 </a>
231 </div>
232 </div>
233 </div>
234
235
236 <div id="bodyColumn" class="span9" >
237
238 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
239 ! or more contributor license agreements. See the NOTICE file
240 ! distributed with this work for additional information
241 ! regarding copyright ownership. The ASF licenses this file
242 ! to you under the Apache License, Version 2.0 (the
243 ! "License"); you may not use this file except in compliance
244 ! with the License. You may obtain a copy of the License at
245 !
246 ! http://www.apache.org/licenses/LICENSE-2.0
247 !
248 ! Unless required by applicable law or agreed to in writing,
249 ! software distributed under the License is distributed on an
250 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
251 ! KIND, either express or implied. See the License for the
252 ! specific language governing permissions and limitations
253 ! under the License.
254 ! --><h1>The Asterix Query Language, Version 1.0</h1>
255<div class="section">
256<h2><a name="Table_of_Contents"></a><a name="toc" id="toc">Table of Contents</a></h2>
257
258<ul>
259
260<li><a href="#Introduction">1. Introduction</a></li>
261
262<li><a href="#Expressions">2. Expressions</a></li>
263
264<li><a href="#Statements">3. Statements</a></li>
265</ul></div>
266<div class="section">
267<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>
268<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>
269<div class="section">
270<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>
271
272<div class="source">
273<div class="source">
274<pre>Query ::= Expression
275</pre></div></div>
276<p>An AQL query can be any legal AQL expression.</p>
277
278<div class="source">
279<div class="source">
280<pre>Expression ::= ( OperatorExpr | IfThenElse | FLWOR | QuantifiedExpression )
281</pre></div></div>
282<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>
283<div class="section">
284<h3><a name="Primary_Expressions"></a>Primary Expressions</h3>
285
286<div class="source">
287<div class="source">
288<pre>PrimaryExpr ::= Literal
289 | VariableRef
290 | ParenthesizedExpression
291 | FunctionCallExpr
292 | DatasetAccessExpression
293 | ListConstructor
294 | ObjectConstructor
295</pre></div></div>
296<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>
297<div class="section">
298<h4><a name="Literals"></a>Literals</h4>
299
300<div class="source">
301<div class="source">
302<pre>Literal ::= StringLiteral
303 | IntegerLiteral
304 | FloatLiteral
305 | DoubleLiteral
306 | &quot;null&quot;
307 | &quot;true&quot;
308 | &quot;false&quot;
309StringLiteral ::= (&quot;\&quot;&quot; (&lt;ESCAPE_QUOT&gt; | ~[&quot;\&quot;&quot;])* &quot;\&quot;&quot;)
310 | (&quot;\'&quot; (&lt;ESCAPE_APOS&gt; | ~[&quot;\'&quot;])* &quot;\'&quot;)
311&lt;ESCAPE_QUOT&gt; ::= &quot;\\\&quot;&quot;
312&lt;ESCAPE_APOS&gt; ::= &quot;\\\'&quot;
313IntegerLiteral ::= &lt;DIGITS&gt;
314&lt;DIGITS&gt; ::= [&quot;0&quot; - &quot;9&quot;]+
315FloatLiteral ::= &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
316 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; ) )?
317 | &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
318DoubleLiteral ::= &lt;DIGITS&gt;
319 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; )?
320 | &quot;.&quot; &lt;DIGITS&gt;
321</pre></div></div>
322<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>
323<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>
324<div class="section">
325<h5><a name="Examples"></a>Examples</h5>
326
327<div class="source">
328<div class="source">
329<pre>&quot;a string&quot;
33042
331</pre></div></div></div></div>
332<div class="section">
333<h4><a name="Variable_References"></a>Variable References</h4>
334
335<div class="source">
336<div class="source">
337<pre>VariableRef ::= &lt;VARIABLE&gt;
338&lt;VARIABLE&gt; ::= &quot;$&quot; &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &quot;_&quot;)*
339&lt;LETTER&gt; ::= [&quot;A&quot; - &quot;Z&quot;, &quot;a&quot; - &quot;z&quot;]
340</pre></div></div>
341<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>
342<div class="section">
343<h5><a name="Examples"></a>Examples</h5>
344
345<div class="source">
346<div class="source">
347<pre>$tweet
348$id
349</pre></div></div></div></div>
350<div class="section">
351<h4><a name="Parenthesized_Expressions"></a>Parenthesized Expressions</h4>
352
353<div class="source">
354<div class="source">
355<pre>ParenthesizedExpression ::= &quot;(&quot; Expression &quot;)&quot;
356</pre></div></div>
357<p>As in most languages, an expression may be parenthesized.</p>
358<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>
359<div class="section">
360<h5><a name="Example"></a>Example</h5>
361
362<div class="source">
363<div class="source">
364<pre>( 1 + 1 )
365</pre></div></div></div></div>
366<div class="section">
367<h4><a name="Function_Calls"></a>Function Calls</h4>
368
369<div class="source">
370<div class="source">
371<pre>FunctionCallExpr ::= FunctionOrTypeName &quot;(&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;)&quot;
372</pre></div></div>
373<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>
374<p>The following example is a (built-in) function call expression whose value is 8.</p>
375<div class="section">
376<h5><a name="Example"></a>Example</h5>
377
378<div class="source">
379<div class="source">
380<pre>string-length(&quot;a string&quot;)
381</pre></div></div></div></div>
382<div class="section">
383<h4><a name="Dataset_Access"></a>Dataset Access</h4>
384
385<div class="source">
386<div class="source">
387<pre>DatasetAccessExpression ::= &quot;dataset&quot; ( ( Identifier ( &quot;.&quot; Identifier )? )
388 | ( &quot;(&quot; Expression &quot;)&quot; ) )
389Identifier ::= &lt;IDENTIFIER&gt; | StringLiteral
390&lt;IDENTIFIER&gt; ::= &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &lt;SPECIALCHARS&gt;)*
391&lt;SPECIALCHARS&gt; ::= [&quot;$&quot;, &quot;_&quot;, &quot;-&quot;]
392</pre></div></div>
393<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>
394<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>
395<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>
396<div class="section">
397<h5><a name="Examples"></a>Examples</h5>
398
399<div class="source">
400<div class="source">
401<pre>dataset SalesDV.Customers
402dataset Customers
403dataset(&quot;Customers&quot;)
404</pre></div></div></div></div>
405<div class="section">
406<h4><a name="Constructors"></a>Constructors</h4>
407
408<div class="source">
409<div class="source">
410<pre>ListConstructor ::= ( OrderedListConstructor | UnorderedListConstructor )
411OrderedListConstructor ::= &quot;[&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;]&quot;
412UnorderedListConstructor ::= &quot;{{&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;}}&quot;
413ObjectConstructor ::= &quot;{&quot; ( FieldBinding ( &quot;,&quot; FieldBinding )* )? &quot;}&quot;
414FieldBinding ::= Expression &quot;:&quot; Expression
415</pre></div></div>
416<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>
417<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>
418<div class="section">
419<h5><a name="Examples"></a>Examples</h5>
420
421<div class="source">
422<div class="source">
423<pre>[ &quot;a&quot;, &quot;b&quot;, &quot;c&quot; ]
424
425{{ 42, &quot;forty-two&quot;, &quot;AsterixDB!&quot;, 3.14f }}
426
427{
428 &quot;project name&quot;: &quot;AsterixDB&quot;
429 &quot;project members&quot;: {{ &quot;vinayakb&quot;, &quot;dtabass&quot;, &quot;chenli&quot; }}
430}
431</pre></div></div></div>
432<div class="section">
433<h5><a name="Note"></a>Note</h5>
434<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>
435<div class="section">
436<h3><a name="Path_Expressions"></a>Path Expressions</h3>
437
438<div class="source">
439<div class="source">
440<pre>ValueExpr ::= PrimaryExpr ( Field | Index )*
441Field ::= &quot;.&quot; Identifier
442Index ::= &quot;[&quot; ( Expression | &quot;?&quot; ) &quot;]&quot;
443</pre></div></div>
444<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>
445<p>The following examples illustrate field access for a object, index-based element access for an ordered list, and also a composition thereof.</p>
446<div class="section">
447<div class="section">
448<h5><a name="Examples"></a>Examples</h5>
449
450<div class="source">
451<div class="source">
452<pre>({&quot;list&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).list
453
454([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])[2]
455
456({ &quot;list&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).list[2]
457</pre></div></div></div></div></div>
458<div class="section">
459<h3><a name="Logical_Expressions"></a>Logical Expressions</h3>
460
461<div class="source">
462<div class="source">
463<pre>OperatorExpr ::= AndExpr ( &quot;or&quot; AndExpr )*
464AndExpr ::= RelExpr ( &quot;and&quot; RelExpr )*
465</pre></div></div>
466<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>
467<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>
468<div class="section">
469<div class="section">
470<h5><a name="Example"></a>Example</h5>
471
472<div class="source">
473<div class="source">
474<pre>$a &gt; 3 and $a &lt; 5
475</pre></div></div></div></div></div>
476<div class="section">
477<h3><a name="Comparison_Expressions"></a>Comparison Expressions</h3>
478
479<div class="source">
480<div class="source">
481<pre>RelExpr ::= AddExpr ( ( &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;&lt;=&quot; | &quot;&gt;=&quot; | &quot;=&quot; | &quot;!=&quot; | &quot;~=&quot; ) AddExpr )?
482</pre></div></div>
483<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>
484<p>An example comparison expression (which yields the boolean value true) is shown below.</p>
485<div class="section">
486<div class="section">
487<h5><a name="Example"></a>Example</h5>
488
489<div class="source">
490<div class="source">
491<pre>5 &gt; 3
492</pre></div></div></div></div></div>
493<div class="section">
494<h3><a name="Arithmetic_Expressions"></a>Arithmetic Expressions</h3>
495
496<div class="source">
497<div class="source">
498<pre>AddExpr ::= MultExpr ( ( &quot;+&quot; | &quot;-&quot; ) MultExpr )*
499MultExpr ::= UnaryExpr ( ( &quot;*&quot; | &quot;/&quot; | &quot;%&quot; | &quot;^&quot;| &quot;idiv&quot; ) UnaryExpr )*
500UnaryExpr ::= ( ( &quot;+&quot; | &quot;-&quot; ) )? ValueExpr
501</pre></div></div>
502<p>AQL also supports the usual cast of characters for arithmetic expressions. The example below evaluates to 25.</p>
503<div class="section">
504<div class="section">
505<h5><a name="Example"></a>Example</h5>
506
507<div class="source">
508<div class="source">
509<pre>3 ^ 2 + 4 ^ 2
510</pre></div></div></div></div></div>
511<div class="section">
512<h3><a name="FLWOR_Expression"></a>FLWOR Expression</h3>
513
514<div class="source">
515<div class="source">
516<pre>FLWOR ::= ( ForClause | LetClause ) ( Clause )* (&quot;return&quot;|&quot;select&quot;) Expression
517Clause ::= ForClause | LetClause | WhereClause | OrderbyClause
518 | GroupClause | LimitClause | DistinctClause
519ForClause ::= (&quot;for&quot;|&quot;from&quot;) Variable ( &quot;at&quot; Variable )? &quot;in&quot; ( Expression )
520LetClause ::= (&quot;let&quot;|&quot;with&quot;) Variable &quot;:=&quot; Expression
521WhereClause ::= &quot;where&quot; Expression
522OrderbyClause ::= &quot;order&quot; &quot;by&quot; Expression ( ( &quot;asc&quot; ) | ( &quot;desc&quot; ) )?
523 ( &quot;,&quot; Expression ( ( &quot;asc&quot; ) | ( &quot;desc&quot; ) )? )*
524GroupClause ::= &quot;group&quot; &quot;by&quot; ( Variable &quot;:=&quot; )? Expression ( &quot;,&quot; ( Variable &quot;:=&quot; )? Expression )*
525 (&quot;with&quot;|&quot;keeping&quot;) VariableRef ( &quot;,&quot; VariableRef )*
526LimitClause ::= &quot;limit&quot; Expression ( &quot;offset&quot; Expression )?
527DistinctClause ::= &quot;distinct&quot; &quot;by&quot; Expression ( &quot;,&quot; Expression )*
528Variable ::= &lt;VARIABLE&gt;
529</pre></div></div>
530<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>
531<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>
532<p>The following example shows a FLWOR expression that selects and returns one user from the dataset FacebookUsers.</p>
533<div class="section">
534<div class="section">
535<h5><a name="Example"></a>Example</h5>
536
537<div class="source">
538<div class="source">
539<pre>for $user in dataset FacebookUsers
540where $user.id = 8
541return $user
542</pre></div></div>
543<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>
544<div class="section">
545<h5><a name="Example"></a>Example</h5>
546
547<div class="source">
548<div class="source">
549<pre>for $user in dataset FacebookUsers
550for $message in dataset FacebookMessages
551where $message.author-id = $user.id
552return
553 {
554 &quot;uname&quot;: $user.name,
555 &quot;message&quot;: $message.message
556 };
557</pre></div></div>
558<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>
559<div class="section">
560<h5><a name="Example"></a>Example</h5>
561
562<div class="source">
563<div class="source">
564<pre>for $user in dataset FacebookUsers
565let $messages :=
566 for $message in dataset FacebookMessages
567 where $message.author-id = $user.id
568 return $message.message
569return
570 {
571 &quot;uname&quot;: $user.name,
572 &quot;messages&quot;: $messages
573 };
574</pre></div></div>
575<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>
576<div class="section">
577<h5><a name="Example"></a>Example</h5>
578
579<div class="source">
580<div class="source">
581<pre> for $user in dataset TwitterUsers
582 order by $user.followers_count desc, $user.lang asc
583 return $user
584</pre></div></div>
585<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>
586<div class="section">
587<h5><a name="Example"></a>Example</h5>
588
589<div class="source">
590<div class="source">
591<pre> for $x in dataset FacebookMessages
592 let $messages := $x.message
593 group by $loc := $x.sender-location with $messages
594 return
595 {
596 &quot;location&quot; : $loc,
597 &quot;message&quot; : $messages
598 }
599</pre></div></div>
600<p>The use of the <tt>limit</tt> clause is illustrated in the next example.</p></div>
601<div class="section">
602<h5><a name="Example"></a>Example</h5>
603
604<div class="source">
605<div class="source">
606<pre> for $user in dataset TwitterUsers
607 order by $user.followers_count desc
608 limit 2
609 return $user
610</pre></div></div>
611<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>
612<div class="section">
613<h5><a name="Example"></a>Example</h5>
614
615<div class="source">
616<div class="source">
617<pre> for $x in dataset FacebookMessages
618 distinct by $x.sender-location
619 return
620 {
621 &quot;location&quot; : $x.sender-location,
622 &quot;message&quot; : $x.message
623 }
624</pre></div></div>
625<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>
626<div class="section">
627<h5><a name="Example"></a>Example</h5>
628
629<div class="source">
630<div class="source">
631<pre> from $x in dataset FacebookMessages
632 with $messages := $x.message
633 group by $loc := $x.sender-location keeping $messages
634 select
635 {
636 &quot;location&quot; : $loc,
637 &quot;message&quot; : $messages
638 }
639</pre></div></div></div></div></div>
640<div class="section">
641<h3><a name="Conditional_Expression"></a>Conditional Expression</h3>
642
643<div class="source">
644<div class="source">
645<pre>IfThenElse ::= &quot;if&quot; &quot;(&quot; Expression &quot;)&quot; &quot;then&quot; Expression &quot;else&quot; Expression
646</pre></div></div>
647<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>
648<p>The following example illustrates the form of a conditional expression.</p>
649<div class="section">
650<div class="section">
651<h5><a name="Example"></a>Example</h5>
652
653<div class="source">
654<div class="source">
655<pre>if (2 &lt; 3) then &quot;yes&quot; else &quot;no&quot;
656</pre></div></div></div></div></div>
657<div class="section">
658<h3><a name="Quantified_Expressions"></a>Quantified Expressions</h3>
659
660<div class="source">
661<div class="source">
662<pre>QuantifiedExpression ::= ( ( &quot;some&quot; ) | ( &quot;every&quot; ) ) Variable &quot;in&quot; Expression
663 ( &quot;,&quot; Variable &quot;in&quot; Expression )* &quot;satisfies&quot; Expression
664</pre></div></div>
665<p>Quantified expressions are used for expressing existential or universal predicates involving the elements of a collection.</p>
666<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>
667<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>
668<div class="section">
669<div class="section">
670<h5><a name="Examples"></a>Examples</h5>
671
672<div class="source">
673<div class="source">
674<pre>every $x in [ 1, 2, 3 ] satisfies $x &lt; 3
675some $x in [ 1, 2, 3 ] satisfies $x &lt; 3
676</pre></div></div></div></div></div></div>
677<div class="section">
678<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>
679
680<div class="source">
681<div class="source">
682<pre>Statement ::= ( SingleStatement ( &quot;;&quot; )? )* &lt;EOF&gt;
683SingleStatement ::= DataverseDeclaration
684 | FunctionDeclaration
685 | CreateStatement
686 | DropStatement
687 | LoadStatement
688 | SetStatement
689 | InsertStatement
690 | DeleteStatement
691 | UpsertStatement
692 | Query
693</pre></div></div>
694<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>
695<p>This section details the statements supported in the AQL language.</p>
696<div class="section">
697<h3><a name="Declarations"></a>Declarations</h3>
698
699<div class="source">
700<div class="source">
701<pre>DataverseDeclaration ::= &quot;use&quot; &quot;dataverse&quot; Identifier
702</pre></div></div>
703<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>
704<p>As an example, the following statement sets the default dataverse to be TinySocial.</p>
705<div class="section">
706<div class="section">
707<h5><a name="Example"></a>Example</h5>
708
709<div class="source">
710<div class="source">
711<pre>use dataverse TinySocial;
712</pre></div></div>
713<p>The set statement in AQL is used to control aspects of the expression evalation context for queries.</p>
714
715<div class="source">
716<div class="source">
717<pre>SetStatement ::= &quot;set&quot; Identifier StringLiteral
718</pre></div></div>
719<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>
720<div class="section">
721<h5><a name="Example"></a>Example</h5>
722
723<div class="source">
724<div class="source">
725<pre>set simfunction &quot;jaccard&quot;;
726set simthreshold &quot;0.6f&quot;;
727</pre></div></div>
728<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>
729
730<div class="source">
731<div class="source">
732<pre>FunctionDeclaration ::= &quot;declare&quot; &quot;function&quot; Identifier ParameterList &quot;{&quot; Expression &quot;}&quot;
733ParameterList ::= &quot;(&quot; ( &lt;VARIABLE&gt; ( &quot;,&quot; &lt;VARIABLE&gt; )* )? &quot;)&quot;
734</pre></div></div>
735<p>The following is a very simple example of a temporary AQL function definition.</p></div>
736<div class="section">
737<h5><a name="Example"></a>Example</h5>
738
739<div class="source">
740<div class="source">
741<pre>declare function add($a, $b) {
742 $a + $b
743};
744</pre></div></div></div></div></div>
745<div class="section">
746<h3><a name="Lifecycle_Management_Statements"></a>Lifecycle Management Statements</h3>
747
748<div class="source">
749<div class="source">
750<pre>CreateStatement ::= &quot;create&quot; ( DataverseSpecification
751 | TypeSpecification
752 | DatasetSpecification
753 | IndexSpecification
754 | FunctionSpecification )
755
756QualifiedName ::= Identifier ( &quot;.&quot; Identifier )?
757DoubleQualifiedName ::= Identifier &quot;.&quot; Identifier ( &quot;.&quot; Identifier )?
758</pre></div></div>
759<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>
760<div class="section">
761<h4><a name="Dataverses"></a>Dataverses</h4>
762
763<div class="source">
764<div class="source">
765<pre>DataverseSpecification ::= &quot;dataverse&quot; Identifier IfNotExists ( &quot;with format&quot; StringLiteral )?
766</pre></div></div>
767<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>
768<p>The following example creates a dataverse named TinySocial.</p>
769<div class="section">
770<h5><a name="Example"></a>Example</h5>
771
772<div class="source">
773<div class="source">
774<pre>create dataverse TinySocial;
775</pre></div></div></div></div>
776<div class="section">
777<h4><a name="Types"></a>Types</h4>
778
779<div class="source">
780<div class="source">
781<pre>TypeSpecification ::= &quot;type&quot; FunctionOrTypeName IfNotExists &quot;as&quot; TypeExpr
782FunctionOrTypeName ::= QualifiedName
783IfNotExists ::= ( &quot;if not exists&quot; )?
784TypeExpr ::= ObjectTypeDef | TypeReference | OrderedListTypeDef | UnorderedListTypeDef
785ObjectTypeDef ::= ( &quot;closed&quot; | &quot;open&quot; )? &quot;{&quot; ( ObjectField ( &quot;,&quot; ObjectField )* )? &quot;}&quot;
786ObjectField ::= Identifier &quot;:&quot; ( TypeExpr ) ( &quot;?&quot; )?
787NestedField ::= Identifier ( &quot;.&quot; Identifier )*
788IndexField ::= NestedField ( &quot;:&quot; TypeReference )?
789TypeReference ::= Identifier
790OrderedListTypeDef ::= &quot;[&quot; ( TypeExpr ) &quot;]&quot;
791UnorderedListTypeDef ::= &quot;{{&quot; ( TypeExpr ) &quot;}}&quot;
792</pre></div></div>
793<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>
794<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>
795<div class="section">
796<h5><a name="Example"></a>Example</h5>
797
798<div class="source">
799<div class="source">
800<pre>create type FacebookUserType as closed {
801 &quot;id&quot; : int32,
802 &quot;alias&quot; : string,
803 &quot;name&quot; : string,
804 &quot;user-since&quot; : datetime,
805 &quot;friend-ids&quot; : {{ int32 }},
806 &quot;employment&quot; : [ EmploymentType ]
807}
808</pre></div></div>
809<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>
810<div class="section">
811<h5><a name="Example"></a>Example</h5>
812
813<div class="source">
814<div class="source">
815<pre>create type FbUserType as closed {
816 &quot;id&quot; : uuid,
817 &quot;alias&quot; : string,
818 &quot;name&quot; : string
819}
820</pre></div></div></div></div>
821<div class="section">
822<h4><a name="Datasets"></a>Datasets</h4>
823
824<div class="source">
825<div class="source">
826<pre>DatasetSpecification ::= &quot;internal&quot;? &quot;dataset&quot; QualifiedName &quot;(&quot; QualifiedName &quot;)&quot; IfNotExists
827 PrimaryKey ( &quot;on&quot; Identifier )? ( &quot;hints&quot; Properties )?
828 ( &quot;using&quot; &quot;compaction&quot; &quot;policy&quot; CompactionPolicy ( Configuration )? )?
829 ( &quot;with filter on&quot; Identifier )?
830 | &quot;external&quot; &quot;dataset&quot; QualifiedName &quot;(&quot; QualifiedName &quot;)&quot; IfNotExists
831 &quot;using&quot; AdapterName Configuration ( &quot;hints&quot; Properties )?
832 ( &quot;using&quot; &quot;compaction&quot; &quot;policy&quot; CompactionPolicy ( Configuration )? )?
833AdapterName ::= Identifier
834Configuration ::= &quot;(&quot; ( KeyValuePair ( &quot;,&quot; KeyValuePair )* )? &quot;)&quot;
835KeyValuePair ::= &quot;(&quot; StringLiteral &quot;=&quot; StringLiteral &quot;)&quot;
836Properties ::= ( &quot;(&quot; Property ( &quot;,&quot; Property )* &quot;)&quot; )?
837Property ::= Identifier &quot;=&quot; ( StringLiteral | IntegerLiteral )
838FunctionSignature ::= FunctionOrTypeName &quot;@&quot; IntegerLiteral
839PrimaryKey ::= &quot;primary&quot; &quot;key&quot; NestedField ( &quot;,&quot; NestedField )* ( &quot;autogenerated &quot;)?
840CompactionPolicy ::= Identifier
841PrimaryKey ::= &quot;primary&quot; &quot;key&quot; Identifier ( &quot;,&quot; Identifier )* ( &quot;autogenerated &quot;)?
842</pre></div></div>
843<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>
844<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>
845<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>
846<p>The following example creates an internal dataset for storing FacefookUserType objects. It specifies that their id field is their primary key.</p>
847<div class="section">
848<h5><a name="Example"></a>Example</h5>
849
850<div class="source">
851<div class="source">
852<pre>create internal dataset FacebookUsers(FacebookUserType) primary key id;
853</pre></div></div>
854<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>
855<div class="section">
856<h5><a name="Example"></a>Example</h5>
857
858<div class="source">
859<div class="source">
860<pre>create internal dataset FbMsgs(FbUserType) primary key id autogenerated;
861</pre></div></div>
862<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>
863<div class="section">
864<h5><a name="Example"></a>Example</h5>
865
866<div class="source">
867<div class="source">
868<pre>create external dataset Lineitem('LineitemType) using hdfs (
869 (&quot;hdfs&quot;=&quot;hdfs://HOST:PORT&quot;),
870 (&quot;path&quot;=&quot;HDFS_PATH&quot;),
871 (&quot;input-format&quot;=&quot;text-input-format&quot;),
872 (&quot;format&quot;=&quot;delimited-text&quot;),
873 (&quot;delimiter&quot;=&quot;|&quot;));
874</pre></div></div></div></div>
875<div class="section">
876<h4><a name="Indices"></a>Indices</h4>
877
878<div class="source">
879<div class="source">
880<pre>IndexSpecification ::= &quot;index&quot; Identifier IfNotExists &quot;on&quot; QualifiedName
881 &quot;(&quot; ( IndexField ) ( &quot;,&quot; IndexField )* &quot;)&quot; ( &quot;type&quot; IndexType )? ( &quot;enforced&quot; )?
882IndexType ::= &quot;btree&quot;
883 | &quot;rtree&quot;
884 | &quot;keyword&quot;
885 | &quot;ngram&quot; &quot;(&quot; IntegerLiteral &quot;)&quot;
886 | &quot;fulltext&quot;
887</pre></div></div>
888<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>
889<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>
890<div class="section">
891<h5><a name="Example"></a>Example</h5>
892
893<div class="source">
894<div class="source">
895<pre>create index fbAuthorIdx on FacebookMessages(author-id) type btree;
896</pre></div></div>
897<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>
898<div class="section">
899<h5><a name="Example"></a>Example</h5>
900
901<div class="source">
902<div class="source">
903<pre>create index fbSendTimeIdx on FacebookMessages(send-time:datetime) type btree enforced;
904</pre></div></div>
905<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>
906<div class="section">
907<h5><a name="Example"></a>Example</h5>
908
909<div class="source">
910<div class="source">
911<pre>create index twUserScrNameIdx on TweetMessages(user.screen-name) type btree;
912</pre></div></div>
913<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>
914<div class="section">
915<h5><a name="Example"></a>Example</h5>
916
917<div class="source">
918<div class="source">
919<pre>create index fbSenderLocIndex on FacebookMessages(sender-location) type rtree;
920</pre></div></div>
921<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>
922<div class="section">
923<h5><a name="Example"></a>Example</h5>
924
925<div class="source">
926<div class="source">
927<pre>create index fbUserIdx on FacebookUsers(name) type ngram(3);
928</pre></div></div>
929<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>
930<div class="section">
931<h5><a name="Example"></a>Example</h5>
932
933<div class="source">
934<div class="source">
935<pre>create index fbMessageIdx on FacebookMessages(message) type keyword;
936</pre></div></div>
937<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>
938<div class="section">
939<h5><a name="Example"></a>Example</h5>
940
941<div class="source">
942<div class="source">
943<pre>create index fbMessageIdx on FacebookMessages(message) type fulltext;
944</pre></div></div></div></div>
945<div class="section">
946<h4><a name="Functions"></a>Functions</h4>
947<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>
948
949<div class="source">
950<div class="source">
951<pre>FunctionSpecification ::= &quot;function&quot; FunctionOrTypeName IfNotExists ParameterList &quot;{&quot; Expression &quot;}&quot;
952</pre></div></div>
953<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>
954<div class="section">
955<h5><a name="Example"></a>Example</h5>
956
957<div class="source">
958<div class="source">
959<pre>create function add($a, $b) {
960 $a + $b
961};
962</pre></div></div></div></div>
963<div class="section">
964<h4><a name="Removal"></a>Removal</h4>
965
966<div class="source">
967<div class="source">
968<pre>DropStatement ::= &quot;drop&quot; ( &quot;dataverse&quot; Identifier IfExists
969 | &quot;type&quot; FunctionOrTypeName IfExists
970 | &quot;dataset&quot; QualifiedName IfExists
971 | &quot;index&quot; DoubleQualifiedName IfExists
972 | &quot;function&quot; FunctionSignature IfExists )
973IfExists ::= ( &quot;if&quot; &quot;exists&quot; )?
974</pre></div></div>
975<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>
976<p>The following examples illustrate uses of the drop statement.</p>
977<div class="section">
978<h5><a name="Example"></a>Example</h5>
979
980<div class="source">
981<div class="source">
982<pre>drop dataset FacebookUsers if exists;
983
984drop index FacebookUsers.fbSenderLocIndex;
985
986drop type FacebookUserType;
987
988drop dataverse TinySocial;
989
990drop function add;
991</pre></div></div></div></div></div>
992<div class="section">
993<h3><a name="ImportExport_Statements"></a>Import/Export Statements</h3>
994
995<div class="source">
996<div class="source">
997<pre>LoadStatement ::= &quot;load&quot; &quot;dataset&quot; QualifiedName &quot;using&quot; AdapterName Configuration ( &quot;pre-sorted&quot; )?
998</pre></div></div>
999<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>
1000<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>
1001<div class="section">
1002<div class="section">
1003<h5><a name="Example"></a>Example</h5>
1004
1005<div class="source">
1006<div class="source">
1007<pre>load dataset FacebookUsers using localfs
1008((&quot;path&quot;=&quot;localhost:///Users/zuck/AsterixDB/load/fbu.adm&quot;),(&quot;format&quot;=&quot;adm&quot;));
1009</pre></div></div></div></div></div>
1010<div class="section">
1011<h3><a name="Modification_Statements"></a>Modification Statements</h3>
1012<div class="section">
1013<h4><a name="Insert"></a>Insert</h4>
1014
1015<div class="source">
1016<div class="source">
1017<pre>InsertStatement ::= &quot;insert&quot; &quot;into&quot; &quot;dataset&quot; QualifiedName ( &quot;as&quot; Variable )? Query ( &quot;returning&quot; Query )?
1018</pre></div></div>
1019<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>
1020<p>The following example illustrates a query-based insertion.</p>
1021<div class="section">
1022<h5><a name="Example"></a>Example</h5>
1023
1024<div class="source">
1025<div class="source">
1026<pre>insert into dataset UsersCopy as $inserted (for $user in dataset FacebookUsers return $user ) returning $inserted.screen-name
1027</pre></div></div></div></div>
1028<div class="section">
1029<h4><a name="Delete"></a>Delete</h4>
1030
1031<div class="source">
1032<div class="source">
1033<pre>DeleteStatement ::= &quot;delete&quot; Variable &quot;from&quot; &quot;dataset&quot; QualifiedName ( &quot;where&quot; Expression )?
1034</pre></div></div>
1035<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>
1036<p>The following example illustrates a single-object deletion.</p>
1037<div class="section">
1038<h5><a name="Example"></a>Example</h5>
1039
1040<div class="source">
1041<div class="source">
1042<pre>delete $user from dataset FacebookUsers where $user.id = 8;
1043</pre></div></div></div></div>
1044<div class="section">
1045<h4><a name="Upsert"></a>Upsert</h4>
1046
1047<div class="source">
1048<div class="source">
1049<pre>UpsertStatement ::= &quot;upsert&quot; &quot;into&quot; &quot;dataset&quot; QualifiedName Query
1050</pre></div></div>
1051<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>
1052<p>The following example illustrates a query-based upsertion.</p>
1053<div class="section">
1054<h5><a name="Example"></a>Example</h5>
1055
1056<div class="source">
1057<div class="source">
1058<pre>upsert into dataset Users (for $user in dataset FacebookUsers return $user)
1059</pre></div></div>
1060<p>We close this guide to AQL with one final example of a query expression.</p></div>
1061<div class="section">
1062<h5><a name="Example"></a>Example</h5>
1063
1064<div class="source">
1065<div class="source">
1066<pre>for $praise in {{ &quot;great&quot;, &quot;brilliant&quot;, &quot;awesome&quot; }}
1067return
1068 string-concat([&quot;AsterixDB is &quot;, $praise])
1069</pre></div></div></div></div></div></div>
1070 </div>
1071 </div>
1072 </div>
1073
1074 <hr/>
1075
1076 <footer>
1077 <div class="container-fluid">
1078 <div class="row span12">Copyright &copy; 2017
1079 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
1080 All Rights Reserved.
1081
1082 </div>
1083
1084 <?xml version="1.0" encoding="UTF-8"?>
1085<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
1086 feather logo, and the Apache AsterixDB project logo are either
1087 registered trademarks or trademarks of The Apache Software
1088 Foundation in the United States and other countries.
1089 All other marks mentioned may be trademarks or registered
1090 trademarks of their respective owners.</div>
1091
1092
1093 </div>
1094 </footer>
1095 </body>
1096</html>