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