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