blob: 0ba96edc4ead10ac723100b560ddb7e4319057bd [file] [log] [blame]
Ian Maxoned124d82015-05-29 18:44:11 -07001<!DOCTYPE html>
2<!--
Till Westmann0817a3f2015-06-03 21:08:18 -07003 | Generated by Apache Maven Doxia at 2015-05-31
Ian Maxoned124d82015-05-29 18:44:11 -07004 | 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" />
Till Westmann0817a3f2015-06-03 21:08:18 -070010 <meta name="Date-Revision-yyyymmdd" content="20150531" />
Ian Maxoned124d82015-05-29 18:44:11 -070011 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB - </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 </head>
22 <body class="topBarDisabled">
23
24
25
26
27 <div class="container-fluid">
28 <div id="banner">
29 <div class="pull-left">
30 <a href=".././" id="bannerLeft">
31 <img src="../images/asterixlogo.png" alt="AsterixDB"/>
32 </a>
33 </div>
34 <div class="pull-right"> <a href="http://incubator.apache.org/" id="bannerRight">
35 <img src="../images/egg-logo.png" alt="Apache Software Foundation Incubator"/>
36 </a>
37 </div>
38 <div class="clear"><hr/></div>
39 </div>
40
41 <div id="breadcrumbs">
42 <ul class="breadcrumb">
43
44
Till Westmann0817a3f2015-06-03 21:08:18 -070045 <li id="publishDate">Last Published: 2015-05-31</li>
Ian Maxoned124d82015-05-29 18:44:11 -070046
47
48
49 <li id="projectVersion" class="pull-right">Version: 0.8.7-SNAPSHOT</li>
50
51 <li class="divider pull-right">|</li>
52
53 <li class="pull-right"> <a href="../index.html" title="Home">
54 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">Apache Software Foundation</li>
68
69 <li>
70
71 <a href="http://www.apache.org/" class="externalLink" title="Home">
72 <i class="none"></i>
73 Home</a>
74 </li>
75
76 <li>
77
78 <a href="http://www.apache.org/foundation/sponsorship.html" class="externalLink" title="Donate">
79 <i class="none"></i>
80 Donate</a>
81 </li>
82
83 <li>
84
85 <a href="http://www.apache.org/foundation/thanks.html" class="externalLink" title="Thanks">
86 <i class="none"></i>
87 Thanks</a>
88 </li>
89
90 <li>
91
92 <a href="http://www.apache.org/security/" class="externalLink" title="Security">
93 <i class="none"></i>
94 Security</a>
95 </li>
96 <li class="nav-header">User Documentation</li>
97
98 <li>
99
100 <a href="../install.html" title="Installing and Managing AsterixDB using Managix">
101 <i class="none"></i>
102 Installing and Managing AsterixDB using Managix</a>
103 </li>
104
105 <li>
106
107 <a href="../aql/primer.html" title="AsterixDB 101: An ADM and AQL Primer">
108 <i class="none"></i>
109 AsterixDB 101: An ADM and AQL Primer</a>
110 </li>
111
112 <li>
113
114 <a href="../aql/primer-sql-like.html" title="AsterixDB 101: An ADM and AQL Primer (For SQL Fans)">
115 <i class="none"></i>
116 AsterixDB 101: An ADM and AQL Primer (For SQL Fans)</a>
117 </li>
118
119 <li>
120
121 <a href="../aql/js-sdk.html" title="AsterixDB Javascript SDK">
122 <i class="none"></i>
123 AsterixDB Javascript SDK</a>
124 </li>
125
126 <li>
127
128 <a href="../aql/datamodel.html" title="Asterix Data Model (ADM)">
129 <i class="none"></i>
130 Asterix Data Model (ADM)</a>
131 </li>
132
133 <li class="active">
134
135 <a href="#"><i class="none"></i>Asterix Query Language (AQL)</a>
136 </li>
137
138 <li>
139
140 <a href="../aql/functions.html" title="AQL Functions">
141 <i class="none"></i>
142 AQL Functions</a>
143 </li>
144
145 <li>
146
147 <a href="../aql/allens.html" title="AQL Allen's Relations Functions">
148 <i class="none"></i>
149 AQL Allen's Relations Functions</a>
150 </li>
151
152 <li>
153
154 <a href="../aql/similarity.html" title="AQL Support of Similarity Queries">
155 <i class="none"></i>
156 AQL Support of Similarity Queries</a>
157 </li>
158
159 <li>
160
161 <a href="../aql/externaldata.html" title="Accessing External Data">
162 <i class="none"></i>
163 Accessing External Data</a>
164 </li>
165
166 <li>
167
168 <a href="../aql/filters.html" title="Filter-Based LSM Index Acceleration">
169 <i class="none"></i>
170 Filter-Based LSM Index Acceleration</a>
171 </li>
172
173 <li>
174
175 <a href="../api.html" title="REST API to AsterixDB">
176 <i class="none"></i>
177 REST API to AsterixDB</a>
178 </li>
179 </ul>
180
181
182
183 <hr class="divider" />
184
185 <div id="poweredBy">
186 <div class="clear"></div>
187 <div class="clear"></div>
188 <div class="clear"></div>
189 <a href=".././" title="Hyracks" class="builtBy">
190 <img class="builtBy" alt="Hyracks" src="../images/hyrax_ts.png" />
191 </a>
192 </div>
193 </div>
194 </div>
195
196
197 <div id="bodyColumn" class="span9" >
198
199 <h1>The Asterix Query Language, Version 1.0</h1>
200<div class="section">
201<h2><a name="toc" id="toc">Table of Contents</a><a name="Table_of_Contents"></a></h2>
202
203<ul>
204
205<li><a href="#Introduction">1. Introduction</a></li>
206
207<li><a href="#Expressions">2. Expressions</a></li>
208
209<li><a href="#Statements">3. Statements</a></li>
210</ul></div>
211<div class="section">
212<h2><a name="Introduction" id="Introduction">1. Introduction</a><font size="4"> <a href="#toc">[Back to TOC]</a></font><a name="a1._Introduction_Back_to_TOC"></a></h2>
213<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.<br />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>
214<div class="section">
215<h2><a name="Expressions" id="Expressions">2. Expressions</a> <font size="4"><a href="#toc">[Back to TOC]</a></font><a name="a2._Expressions_Back_to_TOC"></a></h2>
216
217<div class="source">
218<pre>Query ::= Expression
219</pre></div>
220<p>An AQL query can be any legal AQL expression.</p>
221
222<div class="source">
223<pre>Expression ::= ( OperatorExpr | IfThenElse | FLWOR | QuantifiedExpression )
224</pre></div>
225<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>
226<div class="section">
227<h3>Primary Expressions<a name="Primary_Expressions"></a></h3>
228
229<div class="source">
230<pre>PrimaryExpr ::= Literal
231 | VariableRef
232 | ParenthesizedExpression
233 | FunctionCallExpr
234 | DatasetAccessExpression
235 | ListConstructor
236 | RecordConstructor
237</pre></div>
238<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 record.</p>
239<div class="section">
240<h4>Literals<a name="Literals"></a></h4>
241
242<div class="source">
243<pre>Literal ::= StringLiteral
244 | IntegerLiteral
245 | FloatLiteral
246 | DoubleLiteral
247 | &quot;null&quot;
248 | &quot;true&quot;
249 | &quot;false&quot;
250StringLiteral ::= (&quot;\&quot;&quot; (&lt;ESCAPE_QUOT&gt; | ~[&quot;\&quot;&quot;])* &quot;\&quot;&quot;)
251 | (&quot;\'&quot; (&lt;ESCAPE_APOS&gt; | ~[&quot;\'&quot;])* &quot;\'&quot;)
252&lt;ESCAPE_QUOT&gt; ::= &quot;\\\&quot;&quot;
253&lt;ESCAPE_APOS&gt; ::= &quot;\\\'&quot;
254IntegerLiteral ::= &lt;DIGITS&gt;
255&lt;DIGITS&gt; ::= [&quot;0&quot; - &quot;9&quot;]+
256FloatLiteral ::= &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
257 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; ) )?
258 | &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
259DoubleLiteral ::= &lt;DIGITS&gt;
260 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; )?
261 | &quot;.&quot; &lt;DIGITS&gt;
262</pre></div>
263<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>
264<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>
265<div class="section">
266<h5>Examples<a name="Examples"></a></h5>
267
268<div class="source">
269<pre>&quot;a string&quot;
27042
271</pre></div></div></div>
272<div class="section">
273<h4>Variable References<a name="Variable_References"></a></h4>
274
275<div class="source">
276<pre>VariableRef ::= &lt;VARIABLE&gt;
277&lt;VARIABLE&gt; ::= &quot;$&quot; &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &quot;_&quot;)*
278&lt;LETTER&gt; ::= [&quot;A&quot; - &quot;Z&quot;, &quot;a&quot; - &quot;z&quot;]
279</pre></div>
280<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>
281<div class="section">
282<h5>Examples<a name="Examples"></a></h5>
283
284<div class="source">
285<pre>$tweet
286$id
287</pre></div></div></div>
288<div class="section">
289<h4>Parenthesized Expressions<a name="Parenthesized_Expressions"></a></h4>
290
291<div class="source">
292<pre>ParenthesizedExpression ::= &quot;(&quot; Expression &quot;)&quot;
293</pre></div>
294<p>As in most languages, an expression may be parenthesized.</p>
295<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>
296<div class="section">
297<h5>Example<a name="Example"></a></h5>
298
299<div class="source">
300<pre>( 1 + 1 )
301</pre></div></div></div>
302<div class="section">
303<h4>Function Calls<a name="Function_Calls"></a></h4>
304
305<div class="source">
306<pre>FunctionCallExpr ::= FunctionOrTypeName &quot;(&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;)&quot;
307</pre></div>
308<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>
309<p>The following example is a (built-in) function call expression whose value is 8.</p>
310<div class="section">
311<h5>Example<a name="Example"></a></h5>
312
313<div class="source">
314<pre>string-length(&quot;a string&quot;)
315</pre></div></div></div>
316<div class="section">
317<h4>Dataset Access<a name="Dataset_Access"></a></h4>
318
319<div class="source">
320<pre>DatasetAccessExpression ::= &quot;dataset&quot; ( ( Identifier ( &quot;.&quot; Identifier )? )
321 | ( &quot;(&quot; Expression &quot;)&quot; ) )
322Identifier ::= &lt;IDENTIFIER&gt; | StringLiteral
323&lt;IDENTIFIER&gt; ::= &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &lt;SPECIALCHARS&gt;)*
324&lt;SPECIALCHARS&gt; ::= [&quot;$&quot;, &quot;_&quot;, &quot;-&quot;]
325</pre></div>
326<p>Querying Big Data is the main point of AsterixDB and AQL. Data in AsterixDB reside in datasets (collections of ADM records), 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>
327<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>
328<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>
329<div class="section">
330<h5>Examples<a name="Examples"></a></h5>
331
332<div class="source">
333<pre>dataset SalesDV.Customers
334dataset Customers
335dataset(&quot;Customers&quot;)
336</pre></div></div></div>
337<div class="section">
338<h4>Constructors<a name="Constructors"></a></h4>
339
340<div class="source">
341<pre>ListConstructor ::= ( OrderedListConstructor | UnorderedListConstructor )
342OrderedListConstructor ::= &quot;[&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;]&quot;
343UnorderedListConstructor ::= &quot;{{&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;}}&quot;
344RecordConstructor ::= &quot;{&quot; ( FieldBinding ( &quot;,&quot; FieldBinding )* )? &quot;}&quot;
345FieldBinding ::= Expression &quot;:&quot; Expression
346</pre></div>
347<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 records. Ordered lists are like JSON arrays, while unordered lists have bag (multiset) semantics. Records 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>
348<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 record 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 records 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>
349<div class="section">
350<h5>Examples<a name="Examples"></a></h5>
351
352<div class="source">
353<pre>[ &quot;a&quot;, &quot;b&quot;, &quot;c&quot; ]
354
355{{ 42, &quot;forty-two&quot;, &quot;AsterixDB!&quot;, 3.14f }}
356
357{
358 &quot;project name&quot;: &quot;AsterixDB&quot;
359 &quot;project members&quot;: {{ &quot;vinayakb&quot;, &quot;dtabass&quot;, &quot;chenli&quot; }}
360}
361</pre></div></div>
362<div class="section">
363<h5>Note<a name="Note"></a></h5>
364<p>When constructing nested records 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>
365<div class="section">
366<h3>Path Expressions<a name="Path_Expressions"></a></h3>
367
368<div class="source">
369<pre>ValueExpr ::= PrimaryExpr ( Field | Index )*
370Field ::= &quot;.&quot; Identifier
371Index ::= &quot;[&quot; ( Expression | &quot;?&quot; ) &quot;]&quot;
372</pre></div>
373<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 record or list instance. For records, 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>
374<p>The following examples illustrate field access for a record, index-based element access for an ordered list, and also a composition thereof.</p>
375<div class="section">
376<div class="section">
377<h5>Examples<a name="Examples"></a></h5>
378
379<div class="source">
380<pre>({&quot;list&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).list
381
382([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])[2]
383
384({ &quot;list&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).list[2]
385</pre></div></div></div></div>
386<div class="section">
387<h3>Logical Expressions<a name="Logical_Expressions"></a></h3>
388
389<div class="source">
390<pre>OperatorExpr ::= AndExpr ( &quot;or&quot; AndExpr )*
391AndExpr ::= RelExpr ( &quot;and&quot; RelExpr )*
392</pre></div>
393<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>
394<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>
395<div class="section">
396<div class="section">
397<h5>Example<a name="Example"></a></h5>
398
399<div class="source">
400<pre>$a &gt; 3 and $a &lt; 5
401</pre></div></div></div></div>
402<div class="section">
403<h3>Comparison Expressions<a name="Comparison_Expressions"></a></h3>
404
405<div class="source">
406<pre>RelExpr ::= AddExpr ( ( &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;&lt;=&quot; | &quot;&gt;=&quot; | &quot;=&quot; | &quot;!=&quot; | &quot;~=&quot; ) AddExpr )?
407</pre></div>
408<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>
409<p>An example comparison expression (which yields the boolean value true) is shown below.</p>
410<div class="section">
411<div class="section">
412<h5>Example<a name="Example"></a></h5>
413
414<div class="source">
415<pre>5 &gt; 3
416</pre></div></div></div></div>
417<div class="section">
418<h3>Arithmetic Expressions<a name="Arithmetic_Expressions"></a></h3>
419
420<div class="source">
421<pre>AddExpr ::= MultExpr ( ( &quot;+&quot; | &quot;-&quot; ) MultExpr )*
422MultExpr ::= UnaryExpr ( ( &quot;*&quot; | &quot;/&quot; | &quot;%&quot; | &quot;^&quot;| &quot;idiv&quot; ) UnaryExpr )*
423UnaryExpr ::= ( ( &quot;+&quot; | &quot;-&quot; ) )? ValueExpr
424</pre></div>
425<p>AQL also supports the usual cast of characters for arithmetic expressions. The example below evaluates to 25.</p>
426<div class="section">
427<div class="section">
428<h5>Example<a name="Example"></a></h5>
429
430<div class="source">
431<pre>3 ^ 2 + 4 ^ 2
432</pre></div></div></div></div>
433<div class="section">
434<h3>FLWOR Expression<a name="FLWOR_Expression"></a></h3>
435
436<div class="source">
437<pre>FLWOR ::= ( ForClause | LetClause ) ( Clause )* (&quot;return&quot;|&quot;select&quot;) Expression
438Clause ::= ForClause | LetClause | WhereClause | OrderbyClause
439 | GroupClause | LimitClause | DistinctClause
440ForClause ::= (&quot;for&quot;|&quot;from&quot;) Variable ( &quot;at&quot; Variable )? &quot;in&quot; ( Expression )
441LetClause ::= (&quot;let&quot;|&quot;with&quot;) Variable &quot;:=&quot; Expression
442WhereClause ::= &quot;where&quot; Expression
443OrderbyClause ::= &quot;order&quot; &quot;by&quot; Expression ( ( &quot;asc&quot; ) | ( &quot;desc&quot; ) )?
444 ( &quot;,&quot; Expression ( ( &quot;asc&quot; ) | ( &quot;desc&quot; ) )? )*
445GroupClause ::= &quot;group&quot; &quot;by&quot; ( Variable &quot;:=&quot; )? Expression ( &quot;,&quot; ( Variable &quot;:=&quot; )? Expression )*
446 (&quot;with&quot;|&quot;keeping&quot;) VariableRef ( &quot;,&quot; VariableRef )*
447LimitClause ::= &quot;limit&quot; Expression ( &quot;offset&quot; Expression )?
448DistinctClause ::= &quot;distinct&quot; &quot;by&quot; Expression ( &quot;,&quot; Expression )*
449Variable ::= &lt;VARIABLE&gt;
450</pre></div>
451<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>
452<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>
453<p>The following example shows a FLWOR expression that selects and returns one user from the dataset FacebookUsers.</p>
454<div class="section">
455<div class="section">
456<h5>Example<a name="Example"></a></h5>
457
458<div class="source">
459<pre>for $user in dataset FacebookUsers
460where $user.id = 8
461return $user
462</pre></div>
463<p>The next example shows a FLWOR expression that joins two datasets, FacebookUsers and FacebookMessages, returning user/message pairs. The results contain one record per pair, with result records containing the user&#x2019;s name and an entire message.</p></div>
464<div class="section">
465<h5>Example<a name="Example"></a></h5>
466
467<div class="source">
468<pre>for $user in dataset FacebookUsers
469for $message in dataset FacebookMessages
470where $message.author-id = $user.id
471return
472 {
473 &quot;uname&quot;: $user.name,
474 &quot;message&quot;: $message.message
475 };
476</pre></div>
477<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 record per user, with result records containing the user&#x2019;s name and the set of all messages by that user.</p></div>
478<div class="section">
479<h5>Example<a name="Example"></a></h5>
480
481<div class="source">
482<pre>for $user in dataset FacebookUsers
483let $messages :=
484 for $message in dataset FacebookMessages
485 where $message.author-id = $user.id
486 return $message.message
487return
488 {
489 &quot;uname&quot;: $user.name,
490 &quot;messages&quot;: $messages
491 };
492</pre></div>
493<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>
494<div class="section">
495<h5>Example<a name="Example"></a></h5>
496
497<div class="source">
498<pre> for $user in dataset TwitterUsers
499 order by $user.followers_count desc, $user.lang asc
500 return $user
501</pre></div>
502<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>
503<div class="section">
504<h5>Example<a name="Example"></a></h5>
505
506<div class="source">
507<pre> for $x in dataset FacebookMessages
508 let $messages := $x.message
509 group by $loc := $x.sender-location with $messages
510 return
511 {
512 &quot;location&quot; : $loc,
513 &quot;message&quot; : $messages
514 }
515</pre></div>
516<p>The use of the <tt>limit</tt> clause is illustrated in the next example.</p></div>
517<div class="section">
518<h5>Example<a name="Example"></a></h5>
519
520<div class="source">
521<pre> for $user in dataset TwitterUsers
522 order by $user.followers_count desc
523 limit 2
524 return $user
525</pre></div>
526<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>
527<div class="section">
528<h5>Example<a name="Example"></a></h5>
529
530<div class="source">
531<pre> for $x in dataset FacebookMessages
532 distinct by $x.sender-location
533 return
534 {
535 &quot;location&quot; : $x.sender-location,
536 &quot;message&quot; : $x.message
537 }
538</pre></div>
539<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>
540<div class="section">
541<h5>Example<a name="Example"></a></h5>
542
543<div class="source">
544<pre> from $x in dataset FacebookMessages
545 with $messages := $x.message
546 group by $loc := $x.sender-location keeping $messages
547 select
548 {
549 &quot;location&quot; : $loc,
550 &quot;message&quot; : $messages
551 }
552</pre></div></div></div></div>
553<div class="section">
554<h3>Conditional Expression<a name="Conditional_Expression"></a></h3>
555
556<div class="source">
557<pre>IfThenElse ::= &quot;if&quot; &quot;(&quot; Expression &quot;)&quot; &quot;then&quot; Expression &quot;else&quot; Expression
558</pre></div>
559<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>
560<p>The following example illustrates the form of a conditional expression.</p>
561<div class="section">
562<div class="section">
563<h5>Example<a name="Example"></a></h5>
564
565<div class="source">
566<pre>if (2 &lt; 3) then &quot;yes&quot; else &quot;no&quot;
567</pre></div></div></div></div>
568<div class="section">
569<h3>Quantified Expressions<a name="Quantified_Expressions"></a></h3>
570
571<div class="source">
572<pre>QuantifiedExpression ::= ( ( &quot;some&quot; ) | ( &quot;every&quot; ) ) Variable &quot;in&quot; Expression
573 ( &quot;,&quot; Variable &quot;in&quot; Expression )* &quot;satisfies&quot; Expression
574</pre></div>
575<p>Quantified expressions are used for expressing existential or universal predicates involving the elements of a collection.</p>
576<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>
577<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>
578<div class="section">
579<div class="section">
580<h5>Examples<a name="Examples"></a></h5>
581
582<div class="source">
583<pre>every $x in [ 1, 2, 3 ] satisfies $x &lt; 3
584some $x in [ 1, 2, 3 ] satisfies $x &lt; 3
585</pre></div></div></div></div></div>
586<div class="section">
587<h2><a name="Statements" id="Statements">3. Statements</a> <font size="4"><a href="#toc">[Back to TOC]</a></font><a name="a3._Statements_Back_to_TOC"></a></h2>
588
589<div class="source">
590<pre>Statement ::= ( SingleStatement ( &quot;;&quot; )? )* &lt;EOF&gt;
591SingleStatement ::= DataverseDeclaration
592 | FunctionDeclaration
593 | CreateStatement
594 | DropStatement
595 | LoadStatement
596 | SetStatement
597 | InsertStatement
598 | DeleteStatement
599 | Query
600</pre></div>
601<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 record-level ACID transactions that begin and terminate implicitly for each record inserted, deleted, or searched while a given AQL statement is being executed.</p>
602<p>This section details the statements supported in the AQL language.</p>
603<div class="section">
604<h3>Declarations<a name="Declarations"></a></h3>
605
606<div class="source">
607<pre>DataverseDeclaration ::= &quot;use&quot; &quot;dataverse&quot; Identifier
608</pre></div>
609<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>
610<p>As an example, the following statement sets the default dataverse to be TinySocial.</p>
611<div class="section">
612<div class="section">
613<h5>Example<a name="Example"></a></h5>
614
615<div class="source">
616<pre>use dataverse TinySocial;
617</pre></div>
618<p>The set statement in AQL is used to control aspects of the expression evalation context for queries.</p>
619
620<div class="source">
621<pre>SetStatement ::= &quot;set&quot; Identifier StringLiteral
622</pre></div>
623<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>
624<div class="section">
625<h5>Example<a name="Example"></a></h5>
626
627<div class="source">
628<pre>set simfunction &quot;jaccard&quot;;
629set simthreshold &quot;0.6f&quot;;
630</pre></div>
631<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>
632
633<div class="source">
634<pre>FunctionDeclaration ::= &quot;declare&quot; &quot;function&quot; Identifier ParameterList &quot;{&quot; Expression &quot;}&quot;
635ParameterList ::= &quot;(&quot; ( &lt;VARIABLE&gt; ( &quot;,&quot; &lt;VARIABLE&gt; )* )? &quot;)&quot;
636</pre></div>
637<p>The following is a very simple example of a temporary AQL function definition.</p></div>
638<div class="section">
639<h5>Example<a name="Example"></a></h5>
640
641<div class="source">
642<pre>declare function add($a, $b) {
643 $a + $b
644};
645</pre></div></div></div></div>
646<div class="section">
647<h3>Lifecycle Management Statements<a name="Lifecycle_Management_Statements"></a></h3>
648
649<div class="source">
650<pre>CreateStatement ::= &quot;create&quot; ( DataverseSpecification
651 | TypeSpecification
652 | DatasetSpecification
653 | IndexSpecification
654 | FunctionSpecification )
655
656QualifiedName ::= Identifier ( &quot;.&quot; Identifier )?
657DoubleQualifiedName ::= Identifier &quot;.&quot; Identifier ( &quot;.&quot; Identifier )?
658</pre></div>
659<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>
660<div class="section">
661<h4>Dataverses<a name="Dataverses"></a></h4>
662
663<div class="source">
664<pre>DataverseSpecification ::= &quot;dataverse&quot; Identifier IfNotExists ( &quot;with format&quot; StringLiteral )?
665</pre></div>
666<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>
667<p>The following example creates a dataverse named TinySocial.</p>
668<div class="section">
669<h5>Example<a name="Example"></a></h5>
670
671<div class="source">
672<pre>create dataverse TinySocial;
673</pre></div></div></div>
674<div class="section">
675<h4>Types<a name="Types"></a></h4>
676
677<div class="source">
678<pre>TypeSpecification ::= &quot;type&quot; FunctionOrTypeName IfNotExists &quot;as&quot; TypeExpr
679FunctionOrTypeName ::= QualifiedName
680IfNotExists ::= ( &quot;if not exists&quot; )?
681TypeExpr ::= RecordTypeDef | TypeReference | OrderedListTypeDef | UnorderedListTypeDef
682RecordTypeDef ::= ( &quot;closed&quot; | &quot;open&quot; )? &quot;{&quot; ( RecordField ( &quot;,&quot; RecordField )* )? &quot;}&quot;
683RecordField ::= Identifier &quot;:&quot; ( TypeExpr ) ( &quot;?&quot; )?
684NestedField ::= Identifier ( &quot;.&quot; Identifier )*
685OpenField ::= NestedField ( &quot;:&quot; TypeReference )?
686TypeReference ::= Identifier
687OrderedListTypeDef ::= &quot;[&quot; ( TypeExpr ) &quot;]&quot;
688UnorderedListTypeDef ::= &quot;{{&quot; ( TypeExpr ) &quot;}}&quot;
689</pre></div>
690<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 record type, a renaming of another type, an ordered list type, or an unordered list type. A record type can be defined as being either open or closed. Instances of a closed record type are not permitted to contain fields other than those specified in the create type statement. Instances of an open record type may carry additional fields, and open is the default for a new type (if neither option is specified).</p>
691<p>The following example creates a new ADM record 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 record type, EmploymentType.</p>
692<div class="section">
693<h5>Example<a name="Example"></a></h5>
694
695<div class="source">
696<pre>create type FacebookUserType as closed {
697 &quot;id&quot; : int32,
698 &quot;alias&quot; : string,
699 &quot;name&quot; : string,
700 &quot;user-since&quot; : datetime,
701 &quot;friend-ids&quot; : {{ int32 }},
702 &quot;employment&quot; : [ EmploymentType ]
703}
704</pre></div></div></div>
705<div class="section">
706<h4>Datasets<a name="Datasets"></a></h4>
707
708<div class="source">
709<pre>DatasetSpecification ::= &quot;internal&quot;? &quot;dataset&quot; QualifiedName &quot;(&quot; Identifier &quot;)&quot; IfNotExists
710 PrimaryKey ( &quot;on&quot; Identifier )? ( &quot;hints&quot; Properties )?
711 ( &quot;using&quot; &quot;compaction&quot; &quot;policy&quot; CompactionPolicy ( Configuration )? )?
712 ( &quot;with filter on&quot; Identifier )?
713 | &quot;external&quot; &quot;dataset&quot; QualifiedName &quot;(&quot; Identifier &quot;)&quot; IfNotExists
714 &quot;using&quot; AdapterName Configuration ( &quot;hints&quot; Properties )?
715 ( &quot;using&quot; &quot;compaction&quot; &quot;policy&quot; CompactionPolicy ( Configuration )? )?
716AdapterName ::= Identifier
717Configuration ::= &quot;(&quot; ( KeyValuePair ( &quot;,&quot; KeyValuePair )* )? &quot;)&quot;
718KeyValuePair ::= &quot;(&quot; StringLiteral &quot;=&quot; StringLiteral &quot;)&quot;
719Properties ::= ( &quot;(&quot; Property ( &quot;,&quot; Property )* &quot;)&quot; )?
720Property ::= Identifier &quot;=&quot; ( StringLiteral | IntegerLiteral )
721FunctionSignature ::= FunctionOrTypeName &quot;@&quot; IntegerLiteral
722PrimaryKey ::= &quot;primary&quot; &quot;key&quot; NestedField ( &quot;,&quot; NestedField )*
723CompactionPolicy ::= Identifier
724PrimaryKey ::= &quot;primary&quot; &quot;key&quot; Identifier ( &quot;,&quot; Identifier )*
725</pre></div>
726<p>The create dataset statement is used to create a new dataset. Datasets are named, unordered collections of ADM record 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 records. 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>
727<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>
728<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>
729<p>The following example creates an internal dataset for storing FacefookUserType records. It specifies that their id field is their primary key.</p>
730<div class="section">
731<h5>Example<a name="Example"></a></h5>
732
733<div class="source">
734<pre>create internal dataset FacebookUsers(FacebookUserType) primary key id;
735</pre></div>
736<p>The next example creates an external dataset for storing LineitemType records. 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>
737<div class="section">
738<h5>Example<a name="Example"></a></h5>
739
740<div class="source">
741<pre>create external dataset Lineitem('LineitemType) using hdfs (
742 (&quot;hdfs&quot;=&quot;hdfs://HOST:PORT&quot;),
743 (&quot;path&quot;=&quot;HDFS_PATH&quot;),
744 (&quot;input-format&quot;=&quot;text-input-format&quot;),
745 (&quot;format&quot;=&quot;delimited-text&quot;),
746 (&quot;delimiter&quot;=&quot;|&quot;));
747</pre></div></div></div>
748<div class="section">
749<h4>Indices<a name="Indices"></a></h4>
750
751<div class="source">
752<pre>IndexSpecification ::= &quot;index&quot; Identifier IfNotExists &quot;on&quot; QualifiedName
753 &quot;(&quot; ( OpenField ) ( &quot;,&quot; OpenField )* &quot;)&quot; ( &quot;type&quot; IndexType )? ( &quot;enforced&quot; )?
754IndexType ::= &quot;btree&quot;
755 | &quot;rtree&quot;
756 | &quot;keyword&quot;
757 | &quot;ngram&quot; &quot;(&quot; IntegerLiteral &quot;)&quot;
758</pre></div>
759<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> and <tt>ngram</tt> for textual (string) data. Index could be created on arbitrary nested fields by providing valid path expression as an indexed 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, field type is provided along with it&#x2019;s type and <tt>enforced</tt> keyword is specified in the end of index definition. <tt>Enforcing</tt> an open field will introduce a load-time check, which will make sure that the actual type of an indexed field (if such field exists in the record) matches the specified field type.</p>
760<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>
761<div class="section">
762<h5>Example<a name="Example"></a></h5>
763
764<div class="source">
765<pre>create index fbAuthorIdx on FacebookMessages(author-id) type btree enforced;
766</pre></div>
767<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>
768<div class="section">
769<h5>Example<a name="Example"></a></h5>
770
771<div class="source">
772<pre>create index fbSendTimeIdx on FacebookMessages(send-time:datetime) type btree;
773</pre></div>
774<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>
775<div class="section">
776<h5>Example<a name="Example"></a></h5>
777
778<div class="source">
779<pre>create index twUserScrNameIdx on TweetMessages(user.screen-name) type btree;
780</pre></div>
781<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>
782<div class="section">
783<h5>Example<a name="Example"></a></h5>
784
785<div class="source">
786<pre>create index fbSenderLocIndex on FacebookMessages(sender-location) type rtree;
787</pre></div>
788<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>
789<div class="section">
790<h5>Example<a name="Example"></a></h5>
791
792<div class="source">
793<pre>create index fbUserIdx on FacebookUsers(name) type ngram(3);
794</pre></div>
795<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>
796<div class="section">
797<h5>Example<a name="Example"></a></h5>
798
799<div class="source">
800<pre>create index fbMessageIdx on FacebookMessages(message) type keyword;
801</pre></div></div></div>
802<div class="section">
803<h4>Functions<a name="Functions"></a></h4>
804<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>
805
806<div class="source">
807<pre>FunctionSpecification ::= &quot;function&quot; FunctionOrTypeName IfNotExists ParameterList &quot;{&quot; Expression &quot;}&quot;
808</pre></div>
809<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>
810<div class="section">
811<h5>Example<a name="Example"></a></h5>
812
813<div class="source">
814<pre>create function add($a, $b) {
815 $a + $b
816};
817</pre></div></div></div>
818<div class="section">
819<h4>Removal<a name="Removal"></a></h4>
820
821<div class="source">
822<pre>DropStatement ::= &quot;drop&quot; ( &quot;dataverse&quot; Identifier IfExists
823 | &quot;type&quot; FunctionOrTypeName IfExists
824 | &quot;dataset&quot; QualifiedName IfExists
825 | &quot;index&quot; DoubleQualifiedName IfExists
826 | &quot;function&quot; FunctionSignature IfExists )
827IfExists ::= ( &quot;if&quot; &quot;exists&quot; )?
828</pre></div>
829<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>
830<p>The following examples illustrate uses of the drop statement.</p>
831<div class="section">
832<h5>Example<a name="Example"></a></h5>
833
834<div class="source">
835<pre>drop dataset FacebookUsers if exists;
836
837drop index fbSenderLocIndex;
838
839drop type FacebookUserType;
840
841drop dataverse TinySocial;
842
843drop function add;
844</pre></div></div></div></div>
845<div class="section">
846<h3>Import/Export Statements<a name="ImportExport_Statements"></a></h3>
847
848<div class="source">
849<pre>LoadStatement ::= &quot;load&quot; &quot;dataset&quot; QualifiedName &quot;using&quot; AdapterName Configuration ( &quot;pre-sorted&quot; )?
850</pre></div>
851<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.)</p>
852<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>
853<div class="section">
854<div class="section">
855<h5>Example<a name="Example"></a></h5>
856
857<div class="source">
858<pre>load dataset FacebookUsers using localfs
859((&quot;path&quot;=&quot;localhost:///Users/zuck/AsterixDB/load/fbu.adm&quot;),(&quot;format&quot;=&quot;adm&quot;));
860</pre></div></div></div></div>
861<div class="section">
862<h3>Modification Statements<a name="Modification_Statements"></a></h3>
863<div class="section">
864<h4>Insert<a name="Insert"></a></h4>
865
866<div class="source">
867<pre>InsertStatement ::= &quot;insert&quot; &quot;into&quot; &quot;dataset&quot; QualifiedName Query
868</pre></div>
869<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.</p>
870<p>The following example illustrates a query-based insertion.</p>
871<div class="section">
872<h5>Example<a name="Example"></a></h5>
873
874<div class="source">
875<pre>insert into dataset UsersCopy (for $user in dataset FacebookUsers return $user)
876</pre></div></div></div>
877<div class="section">
878<h4>Delete<a name="Delete"></a></h4>
879
880<div class="source">
881<pre>DeleteStatement ::= &quot;delete&quot; Variable &quot;from&quot; &quot;dataset&quot; QualifiedName ( &quot;where&quot; Expression )?
882</pre></div>
883<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>
884<p>The following example illustrates a single-object deletion.</p>
885<div class="section">
886<h5>Example<a name="Example"></a></h5>
887
888<div class="source">
889<pre>delete $user from dataset FacebookUsers where $user.id = 8;
890</pre></div>
891<p>We close this guide to AQL with one final example of a query expression.</p></div>
892<div class="section">
893<h5>Example<a name="Example"></a></h5>
894
895<div class="source">
896<pre>for $praise in {{ &quot;great&quot;, &quot;brilliant&quot;, &quot;awesome&quot; }}
897return
898 string-concat([&quot;AsterixDB is &quot;, $praise])
899</pre></div></div></div></div></div>
900 </div>
901 </div>
902 </div>
903
904 <hr/>
905
906 <footer>
907 <div class="container-fluid">
908 <div class="row span12">Copyright &copy; 2015.
909 All Rights Reserved.
910
911 </div>
912
913 <?xml version="1.0" encoding="UTF-8"?>
914<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
915 feather logo, and the Apache AsterixDB project logo are either
916 registered trademarks or trademarks of The Apache Software
917 Foundation in the United States and other countries.
918 All other marks mentioned may be trademarks or registered
919 trademarks of their respective owners.</div>
920
921
922 </div>
923 </footer>
924 </body>
925</html>