blob: 1978ea11dbbe6f17c9dca4e5265dc662b6993e7f [file] [log] [blame]
Ian Maxond5b11d82017-01-25 10:48:05 -08001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia at 2017-01-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="20170125" />
11 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB &#x2013; The SQL++ Query Language</title>
13 <link rel="stylesheet" href="../css/apache-maven-fluido-1.3.0.min.css" />
14 <link rel="stylesheet" href="../css/site.css" />
15 <link rel="stylesheet" href="../css/print.css" media="print" />
16
17
18 <script type="text/javascript" src="../js/apache-maven-fluido-1.3.0.min.js"></script>
19
20
21
Ian Maxond5b11d82017-01-25 10:48:05 -080022
Ian Maxond5b11d82017-01-25 10:48:05 -080023
24 </head>
25 <body class="topBarDisabled">
26
27
28
29
30 <div class="container-fluid">
31 <div id="banner">
32 <div class="pull-left">
33 <a href=".././" id="bannerLeft">
34 <img src="../images/asterixlogo.png" alt="AsterixDB"/>
35 </a>
36 </div>
37 <div class="pull-right"> </div>
38 <div class="clear"><hr/></div>
39 </div>
40
41 <div id="breadcrumbs">
42 <ul class="breadcrumb">
43
44
45 <li id="publishDate">Last Published: 2017-01-25</li>
46
47
48
49 <li id="projectVersion" class="pull-right">Version: 0.9.0</li>
50
51 <li class="divider pull-right">|</li>
52
53 <li class="pull-right"> <a href="../index.html" title="Documentation Home">
54 Documentation Home</a>
55 </li>
56
57 </ul>
58 </div>
59
60
61 <div class="row-fluid">
62 <div id="leftColumn" class="span3">
63 <div class="well sidebar-nav">
64
65
66 <ul class="nav nav-list">
67 <li class="nav-header">Get Started - Installation</li>
68
69 <li>
70
71 <a href="../ncservice.html" title="Option 1: using NCService">
72 <i class="none"></i>
73 Option 1: using NCService</a>
74 </li>
75
76 <li>
77
78 <a href="../install.html" title="Option 2: using Managix">
79 <i class="none"></i>
80 Option 2: using Managix</a>
81 </li>
82
83 <li>
84
85 <a href="../yarn.html" title="Option 3: using YARN">
86 <i class="none"></i>
87 Option 3: using YARN</a>
88 </li>
89 <li class="nav-header">AsterixDB Primer</li>
90
91 <li>
92
93 <a href="../sqlpp/primer-sqlpp.html" title="Option 1: using SQL++">
94 <i class="none"></i>
95 Option 1: using SQL++</a>
96 </li>
97
98 <li>
99
100 <a href="../aql/primer.html" title="Option 2: using AQL">
101 <i class="none"></i>
102 Option 2: using AQL</a>
103 </li>
104 <li class="nav-header">Data Model</li>
105
106 <li>
107
108 <a href="../datamodel.html" title="The Asterix Data Model">
109 <i class="none"></i>
110 The Asterix Data Model</a>
111 </li>
112 <li class="nav-header">Queries - SQL++</li>
113
114 <li class="active">
115
116 <a href="#"><i class="none"></i>The SQL++ Query Language</a>
117 </li>
118
119 <li>
120
121 <a href="../sqlpp/builtins.html" title="Builtin Functions">
122 <i class="none"></i>
123 Builtin Functions</a>
124 </li>
125 <li class="nav-header">Queries - AQL</li>
126
127 <li>
128
129 <a href="../aql/manual.html" title="The Asterix Query Language (AQL)">
130 <i class="none"></i>
131 The Asterix Query Language (AQL)</a>
132 </li>
133
134 <li>
135
136 <a href="../aql/builtins.html" title="Builtin Functions">
137 <i class="none"></i>
138 Builtin Functions</a>
139 </li>
140 <li class="nav-header">Advanced Features</li>
141
142 <li>
143
144 <a href="../aql/similarity.html" title="Support of Similarity Queries">
145 <i class="none"></i>
146 Support of Similarity Queries</a>
147 </li>
148
149 <li>
150
151 <a href="../aql/fulltext.html" title="Support of Full-text Queries">
152 <i class="none"></i>
153 Support of Full-text Queries</a>
154 </li>
155
156 <li>
157
158 <a href="../aql/externaldata.html" title="Accessing External Data">
159 <i class="none"></i>
160 Accessing External Data</a>
161 </li>
162
163 <li>
164
165 <a href="../feeds/tutorial.html" title="Support for Data Ingestion">
166 <i class="none"></i>
167 Support for Data Ingestion</a>
168 </li>
169
170 <li>
171
172 <a href="../udf.html" title="User Defined Functions">
173 <i class="none"></i>
174 User Defined Functions</a>
175 </li>
176
177 <li>
178
179 <a href="../aql/filters.html" title="Filter-Based LSM Index Acceleration">
180 <i class="none"></i>
181 Filter-Based LSM Index Acceleration</a>
182 </li>
183 <li class="nav-header">API/SDK</li>
184
185 <li>
186
187 <a href="../api.html" title="HTTP API">
188 <i class="none"></i>
189 HTTP API</a>
190 </li>
191 </ul>
192
193
194
195 <hr class="divider" />
196
197 <div id="poweredBy">
198 <div class="clear"></div>
199 <div class="clear"></div>
200 <div class="clear"></div>
201 <a href=".././" title="AsterixDB" class="builtBy">
202 <img class="builtBy" alt="AsterixDB" src="../images/asterixlogo.png" />
203 </a>
204 </div>
205 </div>
206 </div>
207
208
209 <div id="bodyColumn" class="span9" >
210
211 <!-- ! Licensed to the Apache Software Foundation (ASF) under one
212 ! or more contributor license agreements. See the NOTICE file
213 ! distributed with this work for additional information
214 ! regarding copyright ownership. The ASF licenses this file
215 ! to you under the Apache License, Version 2.0 (the
216 ! "License"); you may not use this file except in compliance
217 ! with the License. You may obtain a copy of the License at
218 !
219 ! http://www.apache.org/licenses/LICENSE-2.0
220 !
221 ! Unless required by applicable law or agreed to in writing,
222 ! software distributed under the License is distributed on an
223 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
224 ! KIND, either express or implied. See the License for the
225 ! specific language governing permissions and limitations
226 ! under the License.
227 ! --><h1>The SQL++ Query Language</h1>
228<div class="section">
229<h2><a name="Table_of_Contents"></a><a name="toc" id="toc">Table of Contents</a></h2>
230
231<ul>
232
233<li><a href="#Introduction">1. Introduction</a></li>
234
235<li><a href="#Expressions">2. Expressions</a>
236
237<ul>
238
239<li><a href="#Operator_expressions">Operator expressions</a>
240
241<ul>
242
243<li><a href="#Arithmetic_operators">Arithmetic operators</a></li>
244
245<li><a href="#Collection_operators">Collection operators</a></li>
246
247<li><a href="#Comparison_operators">Comparison operators</a></li>
248
249<li><a href="#Logical_operators">Logical operators</a></li>
250 </ul></li>
251
252<li><a href="#Case_expressions">Case expressions</a></li>
253
254<li><a href="#Quantified_expressions">Quantified expressions</a></li>
255
256<li><a href="#Path_expressions">Path expressions</a></li>
257
258<li><a href="#Primary_expressions">Primary expressions</a>
259
260<ul>
261
262<li><a href="#Literals">Literals</a></li>
263
264<li><a href="#Variable_references">Variable references</a></li>
265
266<li><a href="#Parenthesized_expressions">Parenthesized expressions</a></li>
267
268<li><a href="#Function_call_expressions">Function call expressions</a></li>
269
270<li><a href="#Constructors">Constructors</a></li>
271 </ul></li>
272 </ul></li>
273
274<li><a href="#Queries">3. Queries</a>
275
276<ul>
277
278<li><a href="#SELECT_statements">SELECT statements</a></li>
279
280<li><a href="#Select_clauses">SELECT clauses</a>
281
282<ul>
283
284<li><a href="#Select_element">Select element/value/raw</a></li>
285
286<li><a href="#SQL_select">SQL-style select</a></li>
287
288<li><a href="#Select_star">Select *</a></li>
289
290<li><a href="#Select_distinct">Select distinct</a></li>
291
292<li><a href="#Unnamed_projections">Unnamed projections</a></li>
293
294<li><a href="#Abbreviatory_field_access_expressions">Abbreviatory field access expressions</a></li>
295 </ul></li>
296
297<li><a href="#Unnest_clauses">UNNEST clauses</a>
298
299<ul>
300
301<li><a href="#Inner_unnests">Inner unnests</a></li>
302
303<li><a href="#Left_outer_unnests">Left outer unnests</a></li>
304
305<li><a href="#Expressing_joins_using_unnests">Expressing joins using unnests</a></li>
306 </ul></li>
307
308<li><a href="#From_clauses">FROM clauses</a>
309
310<ul>
311
312<li><a href="#Binding_expressions">Binding expressions</a></li>
313
314<li><a href="#Multiple_from_terms">Multiple from terms</a></li>
315
316<li><a href="#Expressing_joins_using_from_terms">Expressing joins using from terms</a></li>
317
318<li><a href="#Implicit_binding_variables">Implicit binding variables</a></li>
319 </ul></li>
320
321<li><a href="#Join_clauses">JOIN clauses</a>
322
323<ul>
324
325<li><a href="#Inner_joins">Inner joins</a></li>
326
327<li><a href="#Left_outer_joins">Left outer joins</a></li>
328 </ul></li>
329
330<li><a href="#Group_By_clauses">GROUP BY clauses</a>
331
332<ul>
333
334<li><a href="#Group_variables">Group variables</a></li>
335
336<li><a href="#Implicit_group_key_variables">Implicit group key variables</a></li>
337
338<li><a href="#Implicit_group_variables">Implicit group variables</a></li>
339
340<li><a href="#Aggregation_functions">Aggregation functions</a></li>
341
342<li><a href="#SQL-92_aggregation_functions">SQL-92 aggregation functions</a></li>
343
344<li><a href="#SQL-92_compliant_gby">SQL-92 compliant GROUP BY aggregations</a></li>
345
346<li><a href="#Column_aliases">Column aliases</a></li>
347 </ul></li>
348
349<li><a href="#Where_having_clauses">WHERE clauases and HAVING clauses</a></li>
350
351<li><a href="#Order_By_clauses">ORDER BY clauses</a></li>
352
353<li><a href="#Limit_clauses">LIMIT clauses</a></li>
354
355<li><a href="#With_clauses">WITH clauses</a></li>
356
357<li><a href="#Let_clauses">LET clauses</a></li>
358
359<li><a href="#Union_all">UNION ALL</a></li>
360
361<li><a href="#Vs_SQL-92">SQL++ Vs. SQL-92</a></li>
362 </ul></li>
363
364<li><a href="#Errors">4. Errors</a>
365
366<ul>
367
368<li><a href="#Syntax_errors">Syntax errors</a></li>
369
370<li><a href="#Parsing_errors">Identifier resolution errors</a></li>
371
372<li><a href="#Type_errors">Type errors</a></li>
373
374<li><a href="#Resource_errors">Resource errors</a></li>
375 </ul></li>
376
377<li><a href="#DDL_and_DML_statements">5. DDL and DML statements</a>
378
379<ul>
380
381<li><a href="#Declarations">Declarations</a></li>
382
383<li><a href="#Lifecycle_management_statements">Lifecycle management statements</a>
384
385<ul>
386
387<li><a href="#Dataverses">Dataverses</a></li>
388
389<li><a href="#Datasets">Datasets</a></li>
390
391<li><a href="#Types">Types</a></li>
392
393<li><a href="#Functions">Functions</a></li>
394 </ul></li>
395
396<li><a href="#Modification_statements">Modification statements</a>
397
398<ul>
399
400<li><a href="#Inserts">Inserts</a></li>
401
402<li><a href="#Upserts">Upserts</a></li>
403
404<li><a href="#Deletes">Deletes</a></li>
405 </ul></li>
406 </ul></li>
407
408<li><a href="#Reserved_keywords">Appendix 1. Reserved keywords</a></li>
409</ul>
410<!-- ! Licensed to the Apache Software Foundation (ASF) under one
411 ! or more contributor license agreements. See the NOTICE file
412 ! distributed with this work for additional information
413 ! regarding copyright ownership. The ASF licenses this file
414 ! to you under the Apache License, Version 2.0 (the
415 ! "License"); you may not use this file except in compliance
416 ! with the License. You may obtain a copy of the License at
417 !
418 ! http://www.apache.org/licenses/LICENSE-2.0
419 !
420 ! Unless required by applicable law or agreed to in writing,
421 ! software distributed under the License is distributed on an
422 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
423 ! KIND, either express or implied. See the License for the
424 ! specific language governing permissions and limitations
425 ! under the License.
426 ! -->
427<h1><a name="Introduction" id="Introduction">1. Introduction</a><font size="3" /></h1>
428<p>This document is intended as a reference guide to the full syntax and semantics of the SQL++ Query Language, a SQL-inspired language for working with semistructured data. SQL++ has much in common with SQL, but some differences do exist due to the different data models that the two languages were designed to serve. SQL was designed in the 1970&#x2019;s for interacting with the flat, schema-ified world of relational databases, while SQL++ is much newer and targets the nested, schema-optional (or even schema-less) world of modern NoSQL systems.</p>
429<p>In the context of Apache AsterixDB, SQL++ is intended for working with the Asterix Data Model (<a href="../datamodel.html">ADM</a>),a data model based on a superset of JSON with an enriched and flexible type system. New AsterixDB users are encouraged to read and work through the (much friendlier) guide &#x201c;<a href="primer-sqlpp.html">AsterixDB 101: An ADM and SQL++ Primer</a>&#x201d; before attempting to make use of this document. In addition, readers are advised to read through the <a href="../datamodel.html">Asterix Data Model (ADM) reference guide</a> first as well, as an understanding of the data model is a prerequisite to understanding SQL++.</p>
430<p>In what follows, we detail the features of the SQL++ language in a grammar-guided manner. We list and briefly explain each of the productions in the SQL++ grammar, offering examples (and results) for clarity.</p>
431<!-- ! Licensed to the Apache Software Foundation (ASF) under one
432 ! or more contributor license agreements. See the NOTICE file
433 ! distributed with this work for additional information
434 ! regarding copyright ownership. The ASF licenses this file
435 ! to you under the Apache License, Version 2.0 (the
436 ! "License"); you may not use this file except in compliance
437 ! with the License. You may obtain a copy of the License at
438 !
439 ! http://www.apache.org/licenses/LICENSE-2.0
440 !
441 ! Unless required by applicable law or agreed to in writing,
442 ! software distributed under the License is distributed on an
443 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
444 ! KIND, either express or implied. See the License for the
445 ! specific language governing permissions and limitations
446 ! under the License.
447 ! -->
448<h1><a name="Expressions" id="Expressions">2. Expressions</a></h1>
449
450<div class="source">
451<div class="source">
452<pre>Expression ::= OperatorExpression | CaseExpression | QuantifiedExpression
453</pre></div></div>
454<p>SQL++ is a highly composable expression language. Each SQL++ expression returns zero or more data model instances. There are three major kinds of expressions in SQL++. At the topmost level, a SQL++ expression can be an OperatorExpression (similar to a mathematical expression), an ConditionalExpression (to choose between alternative values), or a QuantifiedExpression (which yields a boolean value). Each will be detailed as we explore the full SQL++ grammar.</p>
455<p>Note that in the following text, words enclosed in angle brackets denote keywords that are not case-sensitive.</p></div>
456<div class="section">
457<h2><a name="Operator_expressions" id="Operator_expressions">Operator expressions</a></h2>
458<p>Operators perform a specific operation on the input values or expressions. The syntax of an operator expression is as follows:</p>
459
460<div class="source">
461<div class="source">
462<pre>OperatorExpression ::= PathExpression
463 | Operator OperatorExpression
464 | OperatorExpression Operator (OperatorExpression)?
465 | OperatorExpression &lt;BETWEEN&gt; OperatorExpression &lt;AND&gt; OperatorExpression
466</pre></div></div>
467<p>SQL++ provides a full set of operators that you can use within its statements. Here are the categories of operators:</p>
468
469<ul>
470
471<li><a href="#Arithmetic_operators">Arithmetic operators</a>, to perform basic mathematical operations;</li>
472
473<li><a href="#Collection_operators">Collection operators</a>, to evaluate expressions on collections or objects;</li>
474
475<li><a href="#Comparison_operators">Comparison operators</a>, to compare two expressions;</li>
476
477<li><a href="#Logical_operators">Logical Operators</a>, to combine operators using Boolean logic.</li>
478</ul>
479<p>The following table summarizes the precedence order (from higher to lower) of the major unary and binary operators:</p>
480
481<table border="0" class="table table-striped">
482 <thead>
483
484<tr class="a">
485
486<th>Operator </th>
487
488<th>Operation </th>
489 </tr>
490 </thead>
491 <tbody>
492
493<tr class="b">
494
495<td>EXISTS, NOT EXISTS </td>
496
497<td>collection emptiness testing </td>
498 </tr>
499
500<tr class="a">
501
502<td>^ </td>
503
504<td>exponentiation </td>
505 </tr>
506
507<tr class="b">
508
509<td>*, /, % </td>
510
511<td>multiplication, division, modulo </td>
512 </tr>
513
514<tr class="a">
515
516<td>+, - </td>
517
518<td>addition, subtraction </td>
519 </tr>
520
521<tr class="b">
522
523<td>|| </td>
524
525<td>string concatenation </td>
526 </tr>
527
528<tr class="a">
529
530<td>IS NULL, IS NOT NULL, IS MISSING, IS NOT MISSING, <br />IS UNKNOWN, IS NOT UNKNOWN</td>
531
532<td>unknown value comparison </td>
533 </tr>
534
535<tr class="b">
536
537<td>BETWEEN, NOT BETWEEN </td>
538
539<td>range comparison (inclusive on both sides) </td>
540 </tr>
541
542<tr class="a">
543
544<td>=, !=, &lt;, &gt;, &lt;=, &gt;=, LIKE, NOT LIKE, IN, NOT IN </td>
545
546<td>comparison </td>
547 </tr>
548
549<tr class="b">
550
551<td>NOT </td>
552
553<td>logical negation </td>
554 </tr>
555
556<tr class="a">
557
558<td>AND </td>
559
560<td>conjunction </td>
561 </tr>
562
563<tr class="b">
564
565<td>OR </td>
566
567<td>disjunction </td>
568 </tr>
569 </tbody>
570</table>
571<p>In general, if any operand evaluates to a <tt>MISSING</tt> value, the enclosing operator will return <tt>MISSING</tt>; if none of operands evaluates to a <tt>MISSING</tt> value but there is an operand evaluates to a <tt>NULL</tt> value, the encolosing operator will return <tt>NULL</tt>. However, there are a few exceptions listed in <a href="#Comparison_operators">comparison operators</a> and <a href="#Logical_operators">logical operators</a>.</p>
572<div class="section">
573<h3><a name="Arithmetic_operators" id="Arithmetic_operators">Arithmetic operators</a></h3>
574<p>Arithemtic operators are used to exponentiate, add, subtract, multiply, and divide numeric values, or concatenate string values.</p>
575
576<table border="0" class="table table-striped">
577 <thead>
578
579<tr class="a">
580
581<th>Operator </th>
582
583<th>Purpose </th>
584
585<th>Example </th>
586 </tr>
587 </thead>
588 <tbody>
589
590<tr class="b">
591
592<td>+, - </td>
593
594<td>As unary operators, they denote a <br />positive or negative expression </td>
595
596<td>SELECT VALUE -1; </td>
597 </tr>
598
599<tr class="a">
600
601<td>+, - </td>
602
603<td>As binary operators, they add or subtract </td>
604
605<td>SELECT VALUE 1 + 2; </td>
606 </tr>
607
608<tr class="b">
609
610<td>*, / </td>
611
612<td>Multiply, divide </td>
613
614<td>SELECT VALUE 4 / 2.0; </td>
615 </tr>
616
617<tr class="a">
618
619<td>^ </td>
620
621<td>Exponentiation </td>
622
623<td>SELECT VALUE 2^3; </td>
624 </tr>
625
626<tr class="b">
627
628<td>|| </td>
629
630<td>String concatenation </td>
631
632<td>SELECT VALUE &#x201c;ab&#x201d;||&#x201c;c&#x201d;||&#x201c;d&#x201d;; </td>
633 </tr>
634 </tbody>
635</table></div>
636<div class="section">
637<h3><a name="Collection_operators" id="Collection_operators">Collection operators</a></h3>
638<p>Collection operators are used for membership tests (IN, NOT IN) or empty collection tests (EXISTS, NOT EXISTS).</p>
639
640<table border="0" class="table table-striped">
641 <thead>
642
643<tr class="a">
644
645<th>Operator </th>
646
647<th>Purpose </th>
648
649<th>Example </th>
650 </tr>
651 </thead>
652 <tbody>
653
654<tr class="b">
655
656<td>IN </td>
657
658<td>Membership test </td>
659
660<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.lang IN [&#x201c;en&#x201d;, &#x201c;de&#x201d;]; </td>
661 </tr>
662
663<tr class="a">
664
665<td>NOT IN </td>
666
667<td>Non-membership test </td>
668
669<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.lang NOT IN [&#x201c;en&#x201d;]; </td>
670 </tr>
671
672<tr class="b">
673
674<td>EXISTS </td>
675
676<td>Check whether a collection is not empty </td>
677
678<td>SELECT * FROM ChirpMessages cm <br />WHERE EXISTS cm.referredTopics; </td>
679 </tr>
680
681<tr class="a">
682
683<td>NOT EXISTS </td>
684
685<td>Check whether a collection is empty </td>
686
687<td>SELECT * FROM ChirpMessages cm <br />WHERE NOT EXISTS cm.referredTopics; </td>
688 </tr>
689 </tbody>
690</table></div>
691<div class="section">
692<h3><a name="Comparison_operators" id="Comparison_operators">Comparison operators</a></h3>
693<p>Comparison operators are used to compare values. The comparison operators fall into one of two sub-categories: missing value comparisons and regular value comparisons. SQL++ (and JSON) has two ways of representing missing information in a object - the presence of the field with a NULL for its value (as in SQL), and the absence of the field (which JSON permits). For example, the first of the following objects represents Jack, whose friend is Jill. In the other examples, Jake is friendless a la SQL, with a friend field that is NULL, while Joe is friendless in a more natural (for JSON) way, i.e., by not having a friend field.</p>
694<div class="section">
695<div class="section">
696<h5><a name="Examples"></a>Examples</h5>
697<p>{&#x201c;name&#x201d;: &#x201c;Jack&#x201d;, &#x201c;friend&#x201d;: &#x201c;Jill&#x201d;}</p>
698<p>{&#x201c;name&#x201d;: &#x201c;Jake&#x201d;, &#x201c;friend&#x201d;: NULL}</p>
699<p>{&#x201c;name&#x201d;: &#x201c;Joe&#x201d;}</p>
700<p>The following table enumerates all of SQL++&#x2019;s comparison operators.</p>
701
702<table border="0" class="table table-striped">
703 <thead>
704
705<tr class="a">
706
707<th>Operator </th>
708
709<th>Purpose </th>
710
711<th>Example </th>
712 </tr>
713 </thead>
714 <tbody>
715
716<tr class="b">
717
718<td>IS NULL </td>
719
720<td>Test if a value is NULL </td>
721
722<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name IS NULL; </td>
723 </tr>
724
725<tr class="a">
726
727<td>IS NOT NULL </td>
728
729<td>Test if a value is not NULL </td>
730
731<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name IS NOT NULL; </td>
732 </tr>
733
734<tr class="b">
735
736<td>IS MISSING </td>
737
738<td>Test if a value is MISSING </td>
739
740<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name IS MISSING; </td>
741 </tr>
742
743<tr class="a">
744
745<td>IS NOT MISSING </td>
746
747<td>Test if a value is not MISSING </td>
748
749<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name IS NOT MISSING;</td>
750 </tr>
751
752<tr class="b">
753
754<td>IS UNKNOWN </td>
755
756<td>Test if a value is NULL or MISSING </td>
757
758<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name IS UNKNOWN; </td>
759 </tr>
760
761<tr class="a">
762
763<td>IS NOT UNKNOWN </td>
764
765<td>Test if a value is neither NULL nor MISSING </td>
766
767<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name IS NOT UNKNOWN;</td>
768 </tr>
769
770<tr class="b">
771
772<td>BETWEEN </td>
773
774<td>Test if a value is between a start value and <br />a end value. The comparison is inclusive <br />to both start and end values. </td>
775
776<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId BETWEEN 10 AND 20;</td>
777 </tr>
778
779<tr class="a">
780
781<td>= </td>
782
783<td>Equality test </td>
784
785<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId=10; </td>
786 </tr>
787
788<tr class="b">
789
790<td>!= </td>
791
792<td>Inequality test </td>
793
794<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId!=10;</td>
795 </tr>
796
797<tr class="a">
798
799<td>&lt; </td>
800
801<td>Less than </td>
802
803<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId&lt;10; </td>
804 </tr>
805
806<tr class="b">
807
808<td>&gt; </td>
809
810<td>Greater than </td>
811
812<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId&gt;10; </td>
813 </tr>
814
815<tr class="a">
816
817<td>&lt;= </td>
818
819<td>Less than or equal to </td>
820
821<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId&lt;=10; </td>
822 </tr>
823
824<tr class="b">
825
826<td>&gt;= </td>
827
828<td>Greater than or equal to </td>
829
830<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.chirpId&gt;=10; </td>
831 </tr>
832
833<tr class="a">
834
835<td>LIKE </td>
836
837<td>Test if the left side matches a<br /> pattern defined on the right<br /> side; in the pattern, &#x201c;%&#x201d; matches <br />any string while &#x201c;_&#x201d; matches <br /> any character. </td>
838
839<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name LIKE &#x201c;%Giesen%&#x201d;;</td>
840 </tr>
841
842<tr class="b">
843
844<td>NOT LIKE </td>
845
846<td>Test if the left side does not <br />match a pattern defined on the right<br /> side; in the pattern, &#x201c;%&#x201d; matches <br />any string while &#x201c;_&#x201d; matches <br /> any character. </td>
847
848<td>SELECT * FROM ChirpMessages cm <br />WHERE cm.user.name NOT LIKE &#x201c;%Giesen%&#x201d;;</td>
849 </tr>
850 </tbody>
851</table>
852<p>The following table summarizes how the missing value comparison operators work.</p>
853
854<table border="0" class="table table-striped">
855 <thead>
856
857<tr class="a">
858
859<th>Operator </th>
860
861<th>Non-NULL/Non-MISSING value </th>
862
863<th>NULL </th>
864
865<th>MISSING </th>
866 </tr>
867 </thead>
868 <tbody>
869
870<tr class="b">
871
872<td>IS NULL </td>
873
874<td>FALSE </td>
875
876<td>TRUE </td>
877
878<td>MISSING </td>
879 </tr>
880
881<tr class="a">
882
883<td>IS NOT NULL </td>
884
885<td>TRUE </td>
886
887<td>FALSE </td>
888
889<td>MISSING </td>
890 </tr>
891
892<tr class="b">
893
894<td>IS MISSING </td>
895
896<td>FALSE </td>
897
898<td>FALSE </td>
899
900<td>TRUE </td>
901 </tr>
902
903<tr class="a">
904
905<td>IS NOT MISSING </td>
906
907<td>TRUE </td>
908
909<td>TRUE </td>
910
911<td>FALSE </td>
912 </tr>
913
914<tr class="b">
915
916<td>IS UNKNOWN </td>
917
918<td>FALSE </td>
919
920<td>TRUE </td>
921
922<td>TRUE </td>
923 </tr>
924
925<tr class="a">
926
927<td>IS NOT UNKNOWN </td>
928
929<td>TRUE </td>
930
931<td>FALSE </td>
932
933<td>FALSE</td>
934 </tr>
935 </tbody>
936</table></div></div></div>
937<div class="section">
938<h3><a name="Logical_operators" id="Logical_operators">Logical operators</a></h3>
939<p>Logical operators perform logical <tt>NOT</tt>, <tt>AND</tt>, and <tt>OR</tt> operations over Boolean values (<tt>TRUE</tt> and <tt>FALSE</tt>) plus <tt>NULL</tt> and <tt>MISSING</tt>.</p>
940
941<table border="0" class="table table-striped">
942 <thead>
943
944<tr class="a">
945
946<th>Operator </th>
947
948<th>Purpose </th>
949
950<th>Example </th>
951 </tr>
952 </thead>
953 <tbody>
954
955<tr class="b">
956
957<td>NOT </td>
958
959<td>Returns true if the following condition is false, otherwise returns false </td>
960
961<td>SELECT VALUE NOT TRUE; </td>
962 </tr>
963
964<tr class="a">
965
966<td>AND </td>
967
968<td>Returns true if both branches are true, otherwise returns false </td>
969
970<td>SELECT VALUE TRUE AND FALSE; </td>
971 </tr>
972
973<tr class="b">
974
975<td>OR </td>
976
977<td>Returns true if one branch is true, otherwise returns false </td>
978
979<td>SELECT VALUE FALSE OR FALSE; </td>
980 </tr>
981 </tbody>
982</table>
983<p>The following table is the truth table for <tt>AND</tt> and <tt>OR</tt>.</p>
984
985<table border="0" class="table table-striped">
986 <thead>
987
988<tr class="a">
989
990<th>A </th>
991
992<th>B </th>
993
994<th>A AND B </th>
995
996<th>A OR B </th>
997 </tr>
998 </thead>
999 <tbody>
1000
1001<tr class="b">
1002
1003<td>TRUE </td>
1004
1005<td>TRUE </td>
1006
1007<td>TRUE </td>
1008
1009<td>TRUE </td>
1010 </tr>
1011
1012<tr class="a">
1013
1014<td>TRUE </td>
1015
1016<td>FALSE </td>
1017
1018<td>FALSE </td>
1019
1020<td>TRUE </td>
1021 </tr>
1022
1023<tr class="b">
1024
1025<td>TRUE </td>
1026
1027<td>NULL </td>
1028
1029<td>NULL </td>
1030
1031<td>TRUE </td>
1032 </tr>
1033
1034<tr class="a">
1035
1036<td>TRUE </td>
1037
1038<td>MISSING </td>
1039
1040<td>MISSING </td>
1041
1042<td>TRUE </td>
1043 </tr>
1044
1045<tr class="b">
1046
1047<td>FALSE </td>
1048
1049<td>FALSE </td>
1050
1051<td>FALSE </td>
1052
1053<td>FALSE </td>
1054 </tr>
1055
1056<tr class="a">
1057
1058<td>FALSE </td>
1059
1060<td>NULL </td>
1061
1062<td>FALSE </td>
1063
1064<td>NULL </td>
1065 </tr>
1066
1067<tr class="b">
1068
1069<td>FALSE </td>
1070
1071<td>MISSING </td>
1072
1073<td>FALSE </td>
1074
1075<td>MISSING </td>
1076 </tr>
1077
1078<tr class="a">
1079
1080<td>NULL </td>
1081
1082<td>NULL </td>
1083
1084<td>NULL </td>
1085
1086<td>NULL </td>
1087 </tr>
1088
1089<tr class="b">
1090
1091<td>NULL </td>
1092
1093<td>MISSING </td>
1094
1095<td>MISSING </td>
1096
1097<td>NULL </td>
1098 </tr>
1099
1100<tr class="a">
1101
1102<td>MISSING </td>
1103
1104<td>MISSING </td>
1105
1106<td>MISSING </td>
1107
1108<td>MISSING </td>
1109 </tr>
1110 </tbody>
1111</table>
1112<p>The following table demonstrates the results of <tt>NOT</tt> on all possible inputs.</p>
1113
1114<table border="0" class="table table-striped">
1115 <thead>
1116
1117<tr class="a">
1118
1119<th>A </th>
1120
1121<th>NOT A </th>
1122 </tr>
1123 </thead>
1124 <tbody>
1125
1126<tr class="b">
1127
1128<td>TRUE </td>
1129
1130<td>FALSE </td>
1131 </tr>
1132
1133<tr class="a">
1134
1135<td>FALSE </td>
1136
1137<td>TRUE </td>
1138 </tr>
1139
1140<tr class="b">
1141
1142<td>NULL </td>
1143
1144<td>NULL </td>
1145 </tr>
1146
1147<tr class="a">
1148
1149<td>MISSING </td>
1150
1151<td>MISSING </td>
1152 </tr>
1153 </tbody>
1154</table></div></div>
1155<div class="section">
1156<h2><a name="Case_expressions" id="Case_expressions">Case expressions</a></h2>
1157
1158<div class="source">
1159<div class="source">
1160<pre>CaseExpression ::= SimpleCaseExpression | SearchedCaseExpression
1161SimpleCaseExpression ::= &lt;CASE&gt; Expression ( &lt;WHEN&gt; Expression &lt;THEN&gt; Expression )+ ( &lt;ELSE&gt; Expression )? &lt;END&gt;
1162SearchedCaseExpression ::= &lt;CASE&gt; ( &lt;WHEN&gt; Expression &lt;THEN&gt; Expression )+ ( &lt;ELSE&gt; Expression )? &lt;END&gt;
1163</pre></div></div>
1164<p>In a simple <tt>CASE</tt> expression, the query evaluator searches for the first <tt>WHEN</tt> &#x2026; <tt>THEN</tt> pair in which the <tt>WHEN</tt> expression is equal to the expression following <tt>CASE</tt> and returns the expression following <tt>THEN</tt>. If none of the <tt>WHEN</tt> &#x2026; <tt>THEN</tt> pairs meet this condition, and an <tt>ELSE</tt> branch exists, it returns the <tt>ELSE</tt> expression. Otherwise, <tt>NULL</tt> is returned.</p>
1165<p>In a searched CASE expression, the query evaluator searches from left to right until it finds a <tt>WHEN</tt> expression that is evaluated to <tt>TRUE</tt>, and then returns its corresponding <tt>THEN</tt> expression. If no condition is found to be <tt>TRUE</tt>, and an <tt>ELSE</tt> branch exists, it returns the <tt>ELSE</tt> expression. Otherwise, it returns <tt>NULL</tt>.</p>
1166<p>The following example illustrates the form of a case expression.</p>
1167<div class="section">
1168<div class="section">
1169<div class="section">
1170<h5><a name="Example"></a>Example</h5>
1171
1172<div class="source">
1173<div class="source">
1174<pre>CASE (2 &lt; 3) WHEN true THEN &quot;yes&quot; ELSE &quot;no&quot; END
1175</pre></div></div></div></div></div></div>
1176<div class="section">
1177<h2><a name="Quantified_expressions" id="Quantified_expressions">Quantified expressions</a></h2>
1178
1179<div class="source">
1180<div class="source">
1181<pre>QuantifiedExpression ::= ( (&lt;ANY&gt;|&lt;SOME&gt;) | &lt;EVERY&gt; ) Variable &lt;IN&gt; Expression ( &quot;,&quot; Variable &quot;in&quot; Expression )*
1182 &lt;SATISFIES&gt; Expression (&lt;END&gt;)?
1183</pre></div></div>
1184<p>Quantified expressions are used for expressing existential or universal predicates involving the elements of a collection.</p>
1185<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>
1186<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>
1187<p>A quantified expression will return a <tt>NULL</tt> (or <tt>MISSING</tt>) if the first expression in it evaluates to <tt>NULL</tt> (or <tt>MISSING</tt>). A type error will be raised if the first expression in a quantified expression does not return a collection.</p>
1188<div class="section">
1189<div class="section">
1190<div class="section">
1191<h5><a name="Examples"></a>Examples</h5>
1192
1193<div class="source">
1194<div class="source">
1195<pre>EVERY x IN [ 1, 2, 3 ] SATISFIES x &lt; 3
1196SOME x IN [ 1, 2, 3 ] SATISFIES x &lt; 3
1197</pre></div></div></div></div></div></div>
1198<div class="section">
1199<h2><a name="Path_expressions" id="Path_expressions">Path expressions</a></h2>
1200
1201<div class="source">
1202<div class="source">
1203<pre>PathExpression ::= PrimaryExpression ( Field | Index )*
1204Field ::= &quot;.&quot; Identifier
1205Index ::= &quot;[&quot; ( Expression | &quot;?&quot; ) &quot;]&quot;
1206</pre></div></div>
1207<p>Components of complex types in the data model are accessed via path expressions. Path access can be applied to the result of a SQL++ expression that yields an instance of a complex type, e.g., a object or array instance. For objects, path access is based on field names. For arrays, path access is based on (zero-based) array-style indexing. SQL++ also supports an &#x201c;I&#x2019;m feeling lucky&#x201d; style index accessor, [?], for selecting an arbitrary element from an array. Attempts to access non-existent fields or out-of-bound array elements produce the special value <tt>MISSING</tt>. Type errors will be raised for inappropriate use of a path expression, such as applying a field accessor to a numeric value.</p>
1208<p>The following examples illustrate field access for a object, index-based element access for an array, and also a composition thereof.</p>
1209<div class="section">
1210<div class="section">
1211<div class="section">
1212<h5><a name="Examples"></a>Examples</h5>
1213
1214<div class="source">
1215<div class="source">
1216<pre>({&quot;name&quot;: &quot;MyABCs&quot;, &quot;array&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).array
1217
1218([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;])[2]
1219
1220({&quot;name&quot;: &quot;MyABCs&quot;, &quot;array&quot;: [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]}).array[2]
1221</pre></div></div></div></div></div></div>
1222<div class="section">
1223<h2><a name="Primary_Expressions"></a><a name="Primary_expressions" id="Primary_expressions">Primary Expressions</a></h2>
1224
1225<div class="source">
1226<div class="source">
1227<pre>PrimaryExpr ::= Literal
1228 | VariableReference
1229 | ParenthesizedExpression
1230 | FunctionCallExpression
1231 | Constructor
1232</pre></div></div>
1233<p>The most basic building block for any SQL++ expression is PrimaryExpression. This can be a simple literal (constant) value, a reference to a query variable that is in scope, a parenthesized expression, a function call, or a newly constructed instance of the data model (such as a newly constructed object, array, or multiset of data model instances).</p>
1234<div class="section">
1235<h3><a name="Literals" id="Literals">Literals</a></h3>
1236
1237<div class="source">
1238<div class="source">
1239<pre>Literal ::= StringLiteral
1240 | IntegerLiteral
1241 | FloatLiteral
1242 | DoubleLiteral
1243 | &lt;NULL&gt;
1244 | &lt;MISSING&gt;
1245 | &lt;TRUE&gt;
1246 | &lt;FALSE&gt;
1247StringLiteral ::= &quot;\&quot;&quot; (
1248 &lt;EscapeQuot&gt;
1249 | &lt;EscapeBslash&gt;
1250 | &lt;EscapeSlash&gt;
1251 | &lt;EscapeBspace&gt;
1252 | &lt;EscapeFormf&gt;
1253 | &lt;EscapeNl&gt;
1254 | &lt;EscapeCr&gt;
1255 | &lt;EscapeTab&gt;
1256 | ~[&quot;\&quot;&quot;,&quot;\\&quot;])*
1257 &quot;\&quot;&quot;
1258 | &quot;\'&quot;(
1259 &lt;EscapeApos&gt;
1260 | &lt;EscapeBslash&gt;
1261 | &lt;EscapeSlash&gt;
1262 | &lt;EscapeBspace&gt;
1263 | &lt;EscapeFormf&gt;
1264 | &lt;EscapeNl&gt;
1265 | &lt;EscapeCr&gt;
1266 | &lt;EscapeTab&gt;
1267 | ~[&quot;\'&quot;,&quot;\\&quot;])*
1268 &quot;\'&quot;
1269&lt;ESCAPE_Apos&gt; ::= &quot;\\\'&quot;
1270&lt;ESCAPE_Quot&gt; ::= &quot;\\\&quot;&quot;
1271&lt;EscapeBslash&gt; ::= &quot;\\\\&quot;
1272&lt;EscapeSlash&gt; ::= &quot;\\/&quot;
1273&lt;EscapeBspace&gt; ::= &quot;\\b&quot;
1274&lt;EscapeFormf&gt; ::= &quot;\\f&quot;
1275&lt;EscapeNl&gt; ::= &quot;\\n&quot;
1276&lt;EscapeCr&gt; ::= &quot;\\r&quot;
1277&lt;EscapeTab&gt; ::= &quot;\\t&quot;
1278
1279IntegerLiteral ::= &lt;DIGITS&gt;
1280&lt;DIGITS&gt; ::= [&quot;0&quot; - &quot;9&quot;]+
1281FloatLiteral ::= &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
1282 | &lt;DIGITS&gt; ( &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; ) )?
1283 | &quot;.&quot; &lt;DIGITS&gt; ( &quot;f&quot; | &quot;F&quot; )
1284DoubleLiteral ::= &lt;DIGITS&gt; &quot;.&quot; &lt;DIGITS&gt;
1285 | &quot;.&quot; &lt;DIGITS&gt;
1286</pre></div></div>
1287<p>Literals (constants) in SQL++ can be strings, integers, floating point values, double values, boolean constants, or special constant values like <tt>NULL</tt> and <tt>MISSING</tt>. The <tt>NULL</tt> value is like a <tt>NULL</tt> in SQL; it is used to represent an unknown field value. The specialy value <tt>MISSING</tt> is only meaningful in the context of SQL++ field accesses; it occurs when the accessed field simply does not exist at all in a object being accessed.</p>
1288<p>The following are some simple examples of SQL++ literals.</p>
1289<div class="section">
1290<div class="section">
1291<h5><a name="Examples"></a>Examples</h5>
1292
1293<div class="source">
1294<div class="source">
1295<pre>'a string'
1296&quot;test string&quot;
129742
1298</pre></div></div>
1299<p>Different from standard SQL, double quotes play the same role as single quotes and may be used for string literals in SQL++.</p></div></div></div>
1300<div class="section">
1301<h3><a name="Variable_References"></a><a name="Variable_references" id="Variable_references">Variable References</a></h3>
1302
1303<div class="source">
1304<div class="source">
1305<pre>VariableReference ::= &lt;IDENTIFIER&gt;|&lt;DelimitedIdentifier&gt;
1306&lt;IDENTIFIER&gt; ::= &lt;LETTER&gt; (&lt;LETTER&gt; | &lt;DIGIT&gt; | &quot;_&quot; | &quot;$&quot;)*
1307&lt;LETTER&gt; ::= [&quot;A&quot; - &quot;Z&quot;, &quot;a&quot; - &quot;z&quot;]
1308DelimitedIdentifier ::= &quot;`&quot; (&lt;EscapeQuot&gt;
1309 | &lt;EscapeBslash&gt;
1310 | &lt;EscapeSlash&gt;
1311 | &lt;EscapeBspace&gt;
1312 | &lt;EscapeFormf&gt;
1313 | &lt;EscapeNl&gt;
1314 | &lt;EscapeCr&gt;
1315 | &lt;EscapeTab&gt;
1316 | ~[&quot;`&quot;,&quot;\\&quot;])*
1317 &quot;`&quot;
1318</pre></div></div>
1319<p>A variable in SQL++ can be bound to any legal data model 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 <tt>FROM</tt>, <tt>WITH</tt> or <tt>LET</tt> clauses of a <tt>SELECT</tt> statement or from an input parameter in the context of a function body.) Backticks, e.g., `id`, are used for delimited identifiers. Delimiting is needed when a variable&#x2019;s desired name clashes with a SQL++ keyword or includes characters not allowed in regular identifiers.</p>
1320<div class="section">
1321<div class="section">
1322<h5><a name="Examples"></a>Examples</h5>
1323
1324<div class="source">
1325<div class="source">
1326<pre>tweet
1327id
1328`SELECT`
1329`my-function`
1330</pre></div></div></div></div></div>
1331<div class="section">
1332<h3><a name="Parenthesized_expressions" id="Parenthesized_expressions">Parenthesized expressions</a></h3>
1333
1334<div class="source">
1335<div class="source">
1336<pre>ParenthesizedExpression ::= &quot;(&quot; Expression &quot;)&quot; | Subquery
1337</pre></div></div>
1338<p>An expression can be parenthesized to control the precedence order or otherwise clarify a query. In SQL++, for composability, a subquery is also an parenthesized expression.</p>
1339<p>The following expression evaluates to the value 2.</p>
1340<div class="section">
1341<div class="section">
1342<h5><a name="Example"></a>Example</h5>
1343
1344<div class="source">
1345<div class="source">
1346<pre>( 1 + 1 )
1347</pre></div></div></div></div></div>
1348<div class="section">
1349<h3><a name="Function_call_expressions" id="Function_call_expressions">Function call expressions</a></h3>
1350
1351<div class="source">
1352<div class="source">
1353<pre>FunctionCallExpression ::= FunctionName &quot;(&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;)&quot;
1354</pre></div></div>
1355<p>Functions are included in SQL++, like most languages, as a way to package useful functionality or to componentize complicated or reusable SQL++ computations. A function call is a legal SQL++ query expression that represents the value resulting from the evaluation of its body expression with the given parameter bindings; the parameter value bindings can themselves be any SQL++ expressions.</p>
1356<p>The following example is a (built-in) function call expression whose value is 8.</p>
1357<div class="section">
1358<div class="section">
1359<h5><a name="Example"></a>Example</h5>
1360
1361<div class="source">
1362<div class="source">
1363<pre>length('a string')
1364</pre></div></div></div></div></div>
1365<div class="section">
1366<h3><a name="Constructors" id="Constructors">Constructors</a></h3>
1367
1368<div class="source">
1369<div class="source">
1370<pre>Constructor ::= ArrayConstructor | MultisetConstructor | ObjectConstructor
1371ArrayConstructor ::= &quot;[&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;]&quot;
1372MultisetConstructor ::= &quot;{{&quot; ( Expression ( &quot;,&quot; Expression )* )? &quot;}}&quot;
1373ObjectConstructor ::= &quot;{&quot; ( FieldBinding ( &quot;,&quot; FieldBinding )* )? &quot;}&quot;
1374FieldBinding ::= Expression &quot;:&quot; Expression
1375</pre></div></div>
1376<p>A major feature of SQL++ is its ability to construct new data model instances. This is accomplished using its constructors for each of the model&#x2019;s complex object structures, namely arrays, multisets, and objects. Arrays are like JSON arrays, while multisets have bag semantics. Objects are built from fields that are field-name/field-value pairs, again like JSON.</p>
1377<p>The following examples illustrate how to construct a new array with 4 items, a new object with 2 fields, and a new multiset with 5 items, respectively. Array elements or multiset elements can be homogeneous (as in the first example), which is the common case, or they may be heterogeneous (as in the third example). The data values and field name values used to construct arrays, multisets, and objects in constructors are all simply SQL++ expressions. Thus, the collection elements, field names, and field values used in constructors can be simple literals or they can come from query variable references or even arbitrarily complex SQL++ expressions (subqueries). Type errors will be raised if the field names in a record must be strings, and duplicate field errors will be raised if they are not distinct.</p>
1378<div class="section">
1379<div class="section">
1380<h5><a name="Examples"></a>Examples</h5>
1381
1382<div class="source">
1383<div class="source">
1384<pre>[ 'a', 'b', 'c', 'c' ]
1385
1386{
1387 'project name': 'Hyracks',
1388 'project members': [ 'vinayakb', 'dtabass', 'chenli', 'tsotras', 'tillw' ]
1389}
1390
1391{{ 42, &quot;forty-two!&quot;, { &quot;rank&quot;: &quot;Captain&quot;, &quot;name&quot;: &quot;America&quot; }, 3.14159, 42 }}
1392</pre></div></div>
1393<!-- ! Licensed to the Apache Software Foundation (ASF) under one
1394 ! or more contributor license agreements. See the NOTICE file
1395 ! distributed with this work for additional information
1396 ! regarding copyright ownership. The ASF licenses this file
1397 ! to you under the Apache License, Version 2.0 (the
1398 ! "License"); you may not use this file except in compliance
1399 ! with the License. You may obtain a copy of the License at
1400 !
1401 ! http://www.apache.org/licenses/LICENSE-2.0
1402 !
1403 ! Unless required by applicable law or agreed to in writing,
1404 ! software distributed under the License is distributed on an
1405 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1406 ! KIND, either express or implied. See the License for the
1407 ! specific language governing permissions and limitations
1408 ! under the License.
1409 ! -->
1410<h1><a name="Queries" id="Queries">3. Queries</a></h1>
1411<p>A SQL++ query can be any legal SQL++ expression or <tt>SELECT</tt> statement. A SQL++ query always ends with a semicolon.</p>
1412
1413<div class="source">
1414<div class="source">
1415<pre>Query ::= (Expression | SelectStatement) &quot;;&quot;
1416</pre></div></div></div></div></div></div>
1417<div class="section">
1418<h2><a name="SELECT_statements" id="SELECT_statements">SELECT statements</a></h2>
1419<p>The following shows the (rich) grammar for the <tt>SELECT</tt> statement in SQL++.</p>
1420
1421<div class="source">
1422<div class="source">
1423<pre>SelectStatement ::= ( WithClause )?
1424 SelectSetOperation (OrderbyClause )? ( LimitClause )?
1425SelectSetOperation ::= SelectBlock (&lt;UNION&gt; &lt;ALL&gt; ( SelectBlock | Subquery ) )*
1426Subquery ::= &quot;(&quot; SelectStatement &quot;)&quot;
1427
1428SelectBlock ::= SelectClause
1429 ( FromClause ( LetClause )?)?
1430 ( WhereClause )?
1431 ( GroupbyClause ( LetClause )? ( HavingClause )? )?
1432 |
1433 FromClause ( LetClause )?
1434 ( WhereClause )?
1435 ( GroupbyClause ( LetClause )? ( HavingClause )? )?
1436 SelectClause
1437
1438SelectClause ::= &lt;SELECT&gt; ( &lt;ALL&gt; | &lt;DISTINCT&gt; )? ( SelectRegular | SelectValue )
1439SelectRegular ::= Projection ( &quot;,&quot; Projection )*
1440SelectValue ::= ( &lt;VALUE&gt; | &lt;ELEMENT&gt; | &lt;RAW&gt; ) Expression
1441Projection ::= ( Expression ( &lt;AS&gt; )? Identifier | &quot;*&quot; )
1442
1443FromClause ::= &lt;FROM&gt; FromTerm ( &quot;,&quot; FromTerm )*
1444FromTerm ::= Expression (( &lt;AS&gt; )? Variable)?
1445 ( ( JoinType )? ( JoinClause | UnnestClause ) )*
1446
1447JoinClause ::= &lt;JOIN&gt; Expression (( &lt;AS&gt; )? Variable)? &lt;ON&gt; Expression
1448UnnestClause ::= ( &lt;UNNEST&gt; | &lt;CORRELATE&gt; | &lt;FLATTEN&gt; ) Expression
1449 ( &lt;AS&gt; )? Variable ( &lt;AT&gt; Variable )?
1450JoinType ::= ( &lt;INNER&gt; | &lt;LEFT&gt; ( &lt;OUTER&gt; )? )
1451
1452WithClause ::= &lt;WITH&gt; WithElement ( &quot;,&quot; WithElement )*
1453LetClause ::= (&lt;LET&gt; | &lt;LETTING&gt;) LetElement ( &quot;,&quot; LetElement )*
1454LetElement ::= Variable &quot;=&quot; Expression
1455WithElement ::= Variable &lt;AS&gt; Expression
1456
1457WhereClause ::= &lt;WHERE&gt; Expression
1458
1459GroupbyClause ::= &lt;GROUP&gt; &lt;BY&gt; ( Expression ( (&lt;AS&gt;)? Variable )? ( &quot;,&quot; Expression ( (&lt;AS&gt;)? Variable )? )*
1460 ( &lt;GROUP&gt; &lt;AS&gt; Variable
1461 (&quot;(&quot; Variable &lt;AS&gt; VariableReference (&quot;,&quot; Variable &lt;AS&gt; VariableReference )* &quot;)&quot;)?
1462 )?
1463HavingClause ::= &lt;HAVING&gt; Expression
1464
1465OrderbyClause ::= &lt;ORDER&gt; &lt;BY&gt; Expression ( &lt;ASC&gt; | &lt;DESC&gt; )? ( &quot;,&quot; Expression ( &lt;ASC&gt; | &lt;DESC&gt; )? )*
1466LimitClause ::= &lt;LIMIT&gt; Expression ( &lt;OFFSET&gt; Expression )?
1467</pre></div></div>
1468<p>In this section, we will make use of two stored collections of objects (datasets), <tt>GleambookUsers</tt> and <tt>GleambookMessages</tt>, in a series of running examples to explain <tt>SELECT</tt> queries. The contents of the example collections are as follows:</p>
1469<p><tt>GleambookUsers</tt> collection (or, dataset):</p>
1470
1471<div class="source">
1472<div class="source">
1473<pre>{&quot;id&quot;:1,&quot;alias&quot;:&quot;Margarita&quot;,&quot;name&quot;:&quot;MargaritaStoddard&quot;,&quot;nickname&quot;:&quot;Mags&quot;,&quot;userSince&quot;:&quot;2012-08-20T10:10:00&quot;,&quot;friendIds&quot;:[2,3,6,10],&quot;employment&quot;:[{&quot;organizationName&quot;:&quot;Codetechno&quot;,&quot;start-date&quot;:&quot;2006-08-06&quot;},{&quot;organizationName&quot;:&quot;geomedia&quot;,&quot;start-date&quot;:&quot;2010-06-17&quot;,&quot;end-date&quot;:&quot;2010-01-26&quot;}],&quot;gender&quot;:&quot;F&quot;}
1474{&quot;id&quot;:2,&quot;alias&quot;:&quot;Isbel&quot;,&quot;name&quot;:&quot;IsbelDull&quot;,&quot;nickname&quot;:&quot;Izzy&quot;,&quot;userSince&quot;:&quot;2011-01-22T10:10:00&quot;,&quot;friendIds&quot;:[1,4],&quot;employment&quot;:[{&quot;organizationName&quot;:&quot;Hexviafind&quot;,&quot;startDate&quot;:&quot;2010-04-27&quot;}]}
1475{&quot;id&quot;:3,&quot;alias&quot;:&quot;Emory&quot;,&quot;name&quot;:&quot;EmoryUnk&quot;,&quot;userSince&quot;:&quot;2012-07-10T10:10:00&quot;,&quot;friendIds&quot;:[1,5,8,9],&quot;employment&quot;:[{&quot;organizationName&quot;:&quot;geomedia&quot;,&quot;startDate&quot;:&quot;2010-06-17&quot;,&quot;endDate&quot;:&quot;2010-01-26&quot;}]}
1476</pre></div></div>
1477<p><tt>GleambookMessages</tt> collection (or, dataset):</p>
1478
1479<div class="source">
1480<div class="source">
1481<pre>{&quot;messageId&quot;:2,&quot;authorId&quot;:1,&quot;inResponseTo&quot;:4,&quot;senderLocation&quot;:[41.66,80.87],&quot;message&quot;:&quot; dislike iphone its touch-screen is horrible&quot;}
1482{&quot;messageId&quot;:3,&quot;authorId&quot;:2,&quot;inResponseTo&quot;:4,&quot;senderLocation&quot;:[48.09,81.01],&quot;message&quot;:&quot; like samsung the plan is amazing&quot;}
1483{&quot;messageId&quot;:4,&quot;authorId&quot;:1,&quot;inResponseTo&quot;:2,&quot;senderLocation&quot;:[37.73,97.04],&quot;message&quot;:&quot; can't stand at&amp;t the network is horrible:(&quot;}
1484{&quot;messageId&quot;:6,&quot;authorId&quot;:2,&quot;inResponseTo&quot;:1,&quot;senderLocation&quot;:[31.5,75.56],&quot;message&quot;:&quot; like t-mobile its platform is mind-blowing&quot;}
1485{&quot;messageId&quot;:8,&quot;authorId&quot;:1,&quot;inResponseTo&quot;:11,&quot;senderLocation&quot;:[40.33,80.87],&quot;message&quot;:&quot; like verizon the 3G is awesome:)&quot;}
1486{&quot;messageId&quot;:10,&quot;authorId&quot;:1,&quot;inResponseTo&quot;:12,&quot;senderLocation&quot;:[42.5,70.01],&quot;message&quot;:&quot; can't stand motorola the touch-screen is terrible&quot;}
1487{&quot;messageId&quot;:11,&quot;authorId&quot;:1,&quot;inResponseTo&quot;:1,&quot;senderLocation&quot;:[38.97,77.49],&quot;message&quot;:&quot; can't stand at&amp;t its plan is terrible&quot;}
1488</pre></div></div></div>
1489<div class="section">
1490<h2><a name="SELECT_Clause"></a><a name="Select_clauses" id="Select_clauses">SELECT Clause</a></h2>
1491<p>The SQL++ <tt>SELECT</tt> clause always returns a collection value as its result (even if the result is empty or a singleton).</p>
1492<div class="section">
1493<h3><a name="SELECT_VALUE_Clause"></a><a name="Select_element" id="Select_element">SELECT VALUE Clause</a></h3>
1494<p>The <tt>SELECT VALUE</tt> clause in SQL++ returns a collection that contains the results of evaluating the <tt>VALUE</tt> expression, with one evaluation being performed per &#x201c;binding tuple&#x201d; (i.e., per <tt>FROM</tt> clause item) satisfying the statement&#x2019;s selection criteria. For historical reasons SQL++ also allows the keywords <tt>ELEMENT</tt> or <tt>RAW</tt> to be used in place of <tt>VALUE</tt> (not recommended).</p>
1495<p>The following example shows a standard-alone <tt>SELECT VALUE</tt>, which wraps a value into an array.</p>
1496<div class="section">
1497<div class="section">
1498<h5><a name="Example"></a>Example</h5>
1499
1500<div class="source">
1501<div class="source">
1502<pre>SELECT VALUE 1;
1503</pre></div></div>
1504<p>This query return:</p>
1505
1506<div class="source">
1507<div class="source">
1508<pre>[
1509 1
1510]
1511</pre></div></div>
1512<p>The following example shows a query that selects one user from the GleambookUsers collection.</p></div>
1513<div class="section">
1514<h5><a name="Example"></a>Example</h5>
1515
1516<div class="source">
1517<div class="source">
1518<pre>SELECT VALUE user
1519FROM GleambookUsers user
1520WHERE user.id = 1;
1521</pre></div></div>
1522<p>This query returns:</p>
1523
1524<div class="source">
1525<div class="source">
1526<pre>[{
1527 &quot;userSince&quot;: &quot;2012-08-20T10:10:00.000Z&quot;,
1528 &quot;friendIds&quot;: [
1529 2,
1530 3,
1531 6,
1532 10
1533 ],
1534 &quot;gender&quot;: &quot;F&quot;,
1535 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
1536 &quot;nickname&quot;: &quot;Mags&quot;,
1537 &quot;alias&quot;: &quot;Margarita&quot;,
1538 &quot;id&quot;: 1,
1539 &quot;employment&quot;: [
1540 {
1541 &quot;organizationName&quot;: &quot;Codetechno&quot;,
1542 &quot;start-date&quot;: &quot;2006-08-06&quot;
1543 },
1544 {
1545 &quot;end-date&quot;: &quot;2010-01-26&quot;,
1546 &quot;organizationName&quot;: &quot;geomedia&quot;,
1547 &quot;start-date&quot;: &quot;2010-06-17&quot;
1548 }
1549 ]
1550} ]
1551</pre></div></div></div></div></div>
1552<div class="section">
1553<h3><a name="SQL-style_SELECT"></a><a name="SQL_select" id="SQL_select">SQL-style SELECT</a></h3>
1554<p>In SQL++, the traditional SQL-style <tt>SELECT</tt> syntax is also supported. This syntax can also be reformulated in a <tt>SELECT VALUE</tt> based manner in SQL++. (E.g., <tt>SELECT expA AS fldA, expB AS fldB</tt> is syntactic sugar for <tt>SELECT VALUE { 'fldA': expA, 'fldB': expB }</tt>.) Unlike in SQL, the result of an SQL++ query does not preserve the order of expressions in the <tt>SELECT</tt> clause.</p>
1555<div class="section">
1556<div class="section">
1557<h5><a name="Example"></a>Example</h5>
1558
1559<div class="source">
1560<div class="source">
1561<pre>SELECT user.alias user_alias, user.name user_name
1562FROM GleambookUsers user
1563WHERE user.id = 1;
1564</pre></div></div>
1565<p>Returns:</p>
1566
1567<div class="source">
1568<div class="source">
1569<pre>[ {
1570 &quot;user_name&quot;: &quot;MargaritaStoddard&quot;,
1571 &quot;user_alias&quot;: &quot;Margarita&quot;
1572} ]
1573</pre></div></div></div></div></div>
1574<div class="section">
1575<h3><a name="SELECT_"></a><a name="Select_star" id="Select_star">SELECT *</a></h3>
1576<p>In SQL++, <tt>SELECT *</tt> returns a object with a nested field for each input tuple. Each field has as its field name the name of a binding variable generated by either the <tt>FROM</tt> clause or <tt>GROUP BY</tt> clause in the current enclosing <tt>SELECT</tt> statement, and its field value is the value of that binding variable.</p>
1577<div class="section">
1578<div class="section">
1579<h5><a name="Example"></a>Example</h5>
1580
1581<div class="source">
1582<div class="source">
1583<pre>SELECT *
1584FROM GleambookUsers user;
1585</pre></div></div>
1586<p>Since <tt>user</tt> is the only binding variable generated in the <tt>FROM</tt> clause, this query returns:</p>
1587
1588<div class="source">
1589<div class="source">
1590<pre>[ {
1591 &quot;user&quot;: {
1592 &quot;userSince&quot;: &quot;2012-08-20T10:10:00.000Z&quot;,
1593 &quot;friendIds&quot;: [
1594 2,
1595 3,
1596 6,
1597 10
1598 ],
1599 &quot;gender&quot;: &quot;F&quot;,
1600 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
1601 &quot;nickname&quot;: &quot;Mags&quot;,
1602 &quot;alias&quot;: &quot;Margarita&quot;,
1603 &quot;id&quot;: 1,
1604 &quot;employment&quot;: [
1605 {
1606 &quot;organizationName&quot;: &quot;Codetechno&quot;,
1607 &quot;start-date&quot;: &quot;2006-08-06&quot;
1608 },
1609 {
1610 &quot;end-date&quot;: &quot;2010-01-26&quot;,
1611 &quot;organizationName&quot;: &quot;geomedia&quot;,
1612 &quot;start-date&quot;: &quot;2010-06-17&quot;
1613 }
1614 ]
1615 }
1616}, {
1617 &quot;user&quot;: {
1618 &quot;userSince&quot;: &quot;2011-01-22T10:10:00.000Z&quot;,
1619 &quot;friendIds&quot;: [
1620 1,
1621 4
1622 ],
1623 &quot;name&quot;: &quot;IsbelDull&quot;,
1624 &quot;nickname&quot;: &quot;Izzy&quot;,
1625 &quot;alias&quot;: &quot;Isbel&quot;,
1626 &quot;id&quot;: 2,
1627 &quot;employment&quot;: [
1628 {
1629 &quot;organizationName&quot;: &quot;Hexviafind&quot;,
1630 &quot;startDate&quot;: &quot;2010-04-27&quot;
1631 }
1632 ]
1633 }
1634}, {
1635 &quot;user&quot;: {
1636 &quot;userSince&quot;: &quot;2012-07-10T10:10:00.000Z&quot;,
1637 &quot;friendIds&quot;: [
1638 1,
1639 5,
1640 8,
1641 9
1642 ],
1643 &quot;name&quot;: &quot;EmoryUnk&quot;,
1644 &quot;alias&quot;: &quot;Emory&quot;,
1645 &quot;id&quot;: 3,
1646 &quot;employment&quot;: [
1647 {
1648 &quot;organizationName&quot;: &quot;geomedia&quot;,
1649 &quot;endDate&quot;: &quot;2010-01-26&quot;,
1650 &quot;startDate&quot;: &quot;2010-06-17&quot;
1651 }
1652 ]
1653 }
1654} ]
1655</pre></div></div></div>
1656<div class="section">
1657<h5><a name="Example"></a>Example</h5>
1658
1659<div class="source">
1660<div class="source">
1661<pre>SELECT *
1662FROM GleambookUsers u, GleambookMessages m
1663WHERE m.authorId = u.id and u.id = 2;
1664</pre></div></div>
1665<p>This query does an inner join that we will discuss in <a href="#Multiple_from_terms">multiple from terms</a>. Since both <tt>u</tt> and <tt>m</tt> are binding variable generated in the <tt>FROM</tt> clause, this query returns:</p>
1666
1667<div class="source">
1668<div class="source">
1669<pre>[ {
1670 &quot;u&quot;: {
1671 &quot;userSince&quot;: &quot;2011-01-22T10:10:00&quot;,
1672 &quot;friendIds&quot;: [
1673 1,
1674 4
1675 ],
1676 &quot;name&quot;: &quot;IsbelDull&quot;,
1677 &quot;nickname&quot;: &quot;Izzy&quot;,
1678 &quot;alias&quot;: &quot;Isbel&quot;,
1679 &quot;id&quot;: 2,
1680 &quot;employment&quot;: [
1681 {
1682 &quot;organizationName&quot;: &quot;Hexviafind&quot;,
1683 &quot;startDate&quot;: &quot;2010-04-27&quot;
1684 }
1685 ]
1686 },
1687 &quot;m&quot;: {
1688 &quot;senderLocation&quot;: [
1689 31.5,
1690 75.56
1691 ],
1692 &quot;inResponseTo&quot;: 1,
1693 &quot;messageId&quot;: 6,
1694 &quot;authorId&quot;: 2,
1695 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
1696 }
1697}, {
1698 &quot;u&quot;: {
1699 &quot;userSince&quot;: &quot;2011-01-22T10:10:00&quot;,
1700 &quot;friendIds&quot;: [
1701 1,
1702 4
1703 ],
1704 &quot;name&quot;: &quot;IsbelDull&quot;,
1705 &quot;nickname&quot;: &quot;Izzy&quot;,
1706 &quot;alias&quot;: &quot;Isbel&quot;,
1707 &quot;id&quot;: 2,
1708 &quot;employment&quot;: [
1709 {
1710 &quot;organizationName&quot;: &quot;Hexviafind&quot;,
1711 &quot;startDate&quot;: &quot;2010-04-27&quot;
1712 }
1713 ]
1714 },
1715 &quot;m&quot;: {
1716 &quot;senderLocation&quot;: [
1717 48.09,
1718 81.01
1719 ],
1720 &quot;inResponseTo&quot;: 4,
1721 &quot;messageId&quot;: 3,
1722 &quot;authorId&quot;: 2,
1723 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
1724 }
1725} ]
1726</pre></div></div></div></div></div>
1727<div class="section">
1728<h3><a name="SELECT_DISTINCT"></a><a name="Select_distinct" id="Select_distinct">SELECT DISTINCT</a></h3>
1729<p>SQL++&#x2019;s <tt>DISTINCT</tt> keyword is used to eliminate duplicate items in results. The following example shows how it works.</p>
1730<div class="section">
1731<div class="section">
1732<h5><a name="Example"></a>Example</h5>
1733
1734<div class="source">
1735<div class="source">
1736<pre>SELECT DISTINCT * FROM [1, 2, 2, 3] AS foo;
1737</pre></div></div>
1738<p>This query returns:</p>
1739
1740<div class="source">
1741<div class="source">
1742<pre>[ {
1743 &quot;foo&quot;: 1
1744}, {
1745 &quot;foo&quot;: 2
1746}, {
1747 &quot;foo&quot;: 3
1748} ]
1749</pre></div></div></div>
1750<div class="section">
1751<h5><a name="Example"></a>Example</h5>
1752
1753<div class="source">
1754<div class="source">
1755<pre>SELECT DISTINCT VALUE foo FROM [1, 2, 2, 3] AS foo;
1756</pre></div></div>
1757<p>This version of the query returns:</p>
1758
1759<div class="source">
1760<div class="source">
1761<pre>[ 1
1762, 2
1763, 3
1764 ]
1765</pre></div></div></div></div></div>
1766<div class="section">
1767<h3><a name="Unnamed_projections" id="Unnamed_projections">Unnamed projections</a></h3>
1768<p>Similar to standard SQL, SQL++ supports unnamed projections (a.k.a, unnamed <tt>SELECT</tt> clause items), for which names are generated. Name generation has three cases:</p>
1769
1770<ul>
1771
1772<li>If a projection expression is a variable reference expression, its generated name is the name of the variable.</li>
1773
1774<li>If a projection expression is a field access expression, its generated name is the last identifier in the expression.</li>
1775
1776<li>For all other cases, the query processor will generate a unique name.</li>
1777</ul>
1778<div class="section">
1779<div class="section">
1780<h5><a name="Example"></a>Example</h5>
1781
1782<div class="source">
1783<div class="source">
1784<pre>SELECT substr(user.name, 10), user.alias
1785FROM GleambookUsers user
1786WHERE user.id = 1;
1787</pre></div></div>
1788<p>This query outputs:</p>
1789
1790<div class="source">
1791<div class="source">
1792<pre>[ {
1793 &quot;alias&quot;: &quot;Margarita&quot;,
1794 &quot;$1&quot;: &quot;Stoddard&quot;
1795} ]
1796</pre></div></div>
1797<p>In the result, <tt>$1</tt> is the generated name for <tt>substr(user.name, 1)</tt>, while <tt>alias</tt> is the generated name for <tt>user.alias</tt>.</p></div></div></div>
1798<div class="section">
1799<h3><a name="Abbreviated_Field_Access_Expressions"></a><a name="Abbreviatory_field_access_expressions" id="Abbreviatory_field_access_expressions">Abbreviated Field Access Expressions</a></h3>
1800<p>As in standard SQL, SQL++ field access expressions can be abbreviated (not recommended) when there is no ambiguity. In the next example, the variable <tt>user</tt> is the only possible variable reference for fields <tt>id</tt>, <tt>name</tt> and <tt>alias</tt> and thus could be omitted in the query.</p>
1801<div class="section">
1802<div class="section">
1803<h5><a name="Example"></a>Example</h5>
1804
1805<div class="source">
1806<div class="source">
1807<pre>SELECT substr(name, 10) AS lname, alias
1808FROM GleambookUsers user
1809WHERE id = 1;
1810</pre></div></div>
1811<p>Outputs:</p>
1812
1813<div class="source">
1814<div class="source">
1815<pre>[ {
1816 &quot;lname&quot;: &quot;Stoddard&quot;,
1817 &quot;alias&quot;: &quot;Margarita&quot;
1818} ]
1819</pre></div></div></div></div></div></div>
1820<div class="section">
1821<h2><a name="UNNEST_Clause"></a><a name="Unnest_clauses" id="Unnest_clauses">UNNEST Clause</a></h2>
1822<p>For each of its input tuples, the <tt>UNNEST</tt> clause flattens a collection-valued expression into individual items, producing multiple tuples, each of which is one of the expression&#x2019;s original input tuples augmented with a flattened item from its collection.</p>
1823<div class="section">
1824<h3><a name="Inner_UNNEST"></a><a name="Inner_unnests" id="Inner_unnests">Inner UNNEST</a></h3>
1825<p>The following example is a query that retrieves the names of the organizations that a selected user has worked for. It uses the <tt>UNNEST</tt> clause to unnest the nested collection <tt>employment</tt> in the user&#x2019;s object.</p>
1826<div class="section">
1827<div class="section">
1828<h5><a name="Example"></a>Example</h5>
1829
1830<div class="source">
1831<div class="source">
1832<pre>SELECT u.id AS userId, e.organizationName AS orgName
1833FROM GleambookUsers u
1834UNNEST u.employment e
1835WHERE u.id = 1;
1836</pre></div></div>
1837<p>This query returns:</p>
1838
1839<div class="source">
1840<div class="source">
1841<pre>[ {
1842 &quot;orgName&quot;: &quot;Codetechno&quot;,
1843 &quot;userId&quot;: 1
1844}, {
1845 &quot;orgName&quot;: &quot;geomedia&quot;,
1846 &quot;userId&quot;: 1
1847} ]
1848</pre></div></div>
1849<p>Note that <tt>UNNEST</tt> has SQL&#x2019;s inner join semantics &#x2014; that is, if a user has no employment history, no tuple corresponding to that user will be emitted in the result.</p></div></div></div>
1850<div class="section">
1851<h3><a name="Left_outer_UNNEST"></a><a name="Left_outer_unnests" id="Left_outer_unnests">Left outer UNNEST</a></h3>
1852<p>As an alternative, the <tt>LEFT OUTER UNNEST</tt> clause offers SQL&#x2019;s left outer join semantics. For example, no collection-valued field named <tt>hobbies</tt> exists in the object for the user whose id is 1, but the following query&#x2019;s result still includes user 1.</p>
1853<div class="section">
1854<div class="section">
1855<h5><a name="Example"></a>Example</h5>
1856
1857<div class="source">
1858<div class="source">
1859<pre>SELECT u.id AS userId, h.hobbyName AS hobby
1860FROM GleambookUsers u
1861LEFT OUTER UNNEST u.hobbies h
1862WHERE u.id = 1;
1863</pre></div></div>
1864<p>Returns:</p>
1865
1866<div class="source">
1867<div class="source">
1868<pre>[ {
1869 &quot;userId&quot;: 1
1870} ]
1871</pre></div></div>
1872<p>Note that if <tt>u.hobbies</tt> is an empty collection or leads to a <tt>MISSING</tt> (as above) or <tt>NULL</tt> value for a given input tuple, there is no corresponding binding value for variable <tt>h</tt> for an input tuple. A <tt>MISSING</tt> value will be generated for <tt>h</tt> so that the input tuple can still be propagated.</p></div></div></div>
1873<div class="section">
1874<h3><a name="Expressing_joins_using_UNNEST"></a><a name="Expressing_joins_using_unnests" id="Expressing_joins_using_unnests">Expressing joins using UNNEST</a></h3>
1875<p>The SQL++ <tt>UNNEST</tt> clause is similar to SQL&#x2019;s <tt>JOIN</tt> clause except that it allows its right argument to be correlated to its left argument, as in the examples above &#x2014; i.e., think &#x201c;correlated cross-product&#x201d;. The next example shows this via a query that joins two data sets, GleambookUsers and GleambookMessages, returning user/message pairs. The results contain one object per pair, with result objects containing the user&#x2019;s name and an entire message. The query can be thought of as saying &#x201c;for each Gleambook user, unnest the <tt>GleambookMessages</tt> collection and filter the output with the condition <tt>message.authorId = user.id</tt>&#x201d;.</p>
1876<div class="section">
1877<div class="section">
1878<h5><a name="Example"></a>Example</h5>
1879
1880<div class="source">
1881<div class="source">
1882<pre>SELECT u.name AS uname, m.message AS message
1883FROM GleambookUsers u
1884UNNEST GleambookMessages m
1885WHERE m.authorId = u.id;
1886</pre></div></div>
1887<p>This returns:</p>
1888
1889<div class="source">
1890<div class="source">
1891<pre>[ {
1892 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
1893 &quot;message&quot;: &quot; can't stand at&amp;t its plan is terrible&quot;
1894}, {
1895 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
1896 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
1897}, {
1898 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
1899 &quot;message&quot;: &quot; can't stand at&amp;t the network is horrible:(&quot;
1900}, {
1901 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
1902 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
1903}, {
1904 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
1905 &quot;message&quot;: &quot; can't stand motorola the touch-screen is terrible&quot;
1906}, {
1907 &quot;uname&quot;: &quot;IsbelDull&quot;,
1908 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
1909}, {
1910 &quot;uname&quot;: &quot;IsbelDull&quot;,
1911 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
1912} ]
1913</pre></div></div>
1914<p>Similarly, the above query can also be expressed as the <tt>UNNEST</tt>ing of a correlated SQL++ subquery:</p></div>
1915<div class="section">
1916<h5><a name="Example"></a>Example</h5>
1917
1918<div class="source">
1919<div class="source">
1920<pre>SELECT u.name AS uname, m.message AS message
1921FROM GleambookUsers u
1922UNNEST (
1923 SELECT VALUE msg
1924 FROM GleambookMessages msg
1925 WHERE msg.authorId = u.id
1926) AS m;
1927</pre></div></div></div></div></div></div>
1928<div class="section">
1929<h2><a name="FROM_clauses"></a><a name="From_clauses" id="From_clauses">FROM clauses</a></h2>
1930<p>A <tt>FROM</tt> clause is used for enumerating (i.e., conceptually iterating over) the contents of collections, as in SQL.</p>
1931<div class="section">
1932<h3><a name="Binding_expressions" id="Binding_expressions">Binding expressions</a></h3>
1933<p>In SQL++, in addition to stored collections, a <tt>FROM</tt> clause can iterate over any intermediate collection returned by a valid SQL++ expression. In the tuple stream generated by a <tt>FROM</tt> clause, the ordering of the input tuples are not guaranteed to be preserved.</p>
1934<div class="section">
1935<div class="section">
1936<h5><a name="Example"></a>Example</h5>
1937
1938<div class="source">
1939<div class="source">
1940<pre>SELECT VALUE foo
1941FROM [1, 2, 2, 3] AS foo
1942WHERE foo &gt; 2;
1943</pre></div></div>
1944<p>Returns:</p>
1945
1946<div class="source">
1947<div class="source">
1948<pre>[
1949 3
1950]
1951</pre></div></div></div></div></div>
1952<div class="section">
1953<h3><a name="Multiple_FROM_terms"></a><a name="Multiple_from_terms" id="Multiple_from_terms">Multiple FROM terms</a></h3>
1954<p>SQL++ permits correlations among <tt>FROM</tt> terms. Specifically, a <tt>FROM</tt> binding expression can refer to variables defined to its left in the given <tt>FROM</tt> clause. Thus, the first unnesting example above could also be expressed as follows:</p>
1955<div class="section">
1956<div class="section">
1957<h5><a name="Example"></a>Example</h5>
1958
1959<div class="source">
1960<div class="source">
1961<pre>SELECT u.id AS userId, e.organizationName AS orgName
1962FROM GleambookUsers u, u.employment e
1963WHERE u.id = 1;
1964</pre></div></div></div></div></div>
1965<div class="section">
1966<h3><a name="Expressing_joins_using_FROM_terms"></a><a name="Expressing_joins_using_from_terms" id="Expressing_joins_using_from_terms">Expressing joins using FROM terms</a></h3>
1967<p>Similarly, the join intentions of the other <tt>UNNEST</tt>-based join examples above could be expressed as:</p>
1968<div class="section">
1969<div class="section">
1970<h5><a name="Example"></a>Example</h5>
1971
1972<div class="source">
1973<div class="source">
1974<pre>SELECT u.name AS uname, m.message AS message
1975FROM GleambookUsers u, GleambookMessages m
1976WHERE m.authorId = u.id;
1977</pre></div></div></div>
1978<div class="section">
1979<h5><a name="Example"></a>Example</h5>
1980
1981<div class="source">
1982<div class="source">
1983<pre>SELECT u.name AS uname, m.message AS message
1984FROM GleambookUsers u,
1985 (
1986 SELECT VALUE msg
1987 FROM GleambookMessages msg
1988 WHERE msg.authorId = u.id
1989 ) AS m;
1990</pre></div></div>
1991<p>Note that the first alternative is one of the SQL-92 approaches to expressing a join.</p></div></div></div>
1992<div class="section">
1993<h3><a name="Implicit_binding_variables" id="Implicit_binding_variables">Implicit binding variables</a></h3>
1994<p>Similar to standard SQL, SQL++ supports implicit <tt>FROM</tt> binding variables (i.e., aliases), for which a binding variable is generated. SQL++ variable generation falls into three cases:</p>
1995
1996<ul>
1997
1998<li>If the binding expression is a variable reference expression, the generated variable&#x2019;s name will be the name of the referenced variable itself.</li>
1999
2000<li>If the binding expression is a field access expression (or a fully qualified name for a dataset), the generated variable&#x2019;s name will be the last identifier (or the dataset name) in the expression.</li>
2001
2002<li>For all other cases, a compilation error will be raised.</li>
2003</ul>
2004<p>The next two examples show queries that do not provide binding variables in their <tt>FROM</tt> clauses.</p>
2005<div class="section">
2006<div class="section">
2007<h5><a name="Example"></a>Example</h5>
2008
2009<div class="source">
2010<div class="source">
2011<pre>SELECT GleambookUsers.name, GleambookMessages.message
2012FROM GleambookUsers, GleambookMessages
2013WHERE GleambookMessages.authorId = GleambookUsers.id;
2014</pre></div></div>
2015<p>Returns:</p>
2016
2017<div class="source">
2018<div class="source">
2019<pre>[ {
2020 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2021 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2022}, {
2023 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2024 &quot;message&quot;: &quot; can't stand motorola the touch-screen is terrible&quot;
2025}, {
2026 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2027 &quot;message&quot;: &quot; can't stand at&amp;t its plan is terrible&quot;
2028}, {
2029 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2030 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
2031}, {
2032 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2033 &quot;message&quot;: &quot; can't stand at&amp;t the network is horrible:(&quot;
2034}, {
2035 &quot;name&quot;: &quot;IsbelDull&quot;,
2036 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2037}, {
2038 &quot;name&quot;: &quot;IsbelDull&quot;,
2039 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2040} ]
2041</pre></div></div></div>
2042<div class="section">
2043<h5><a name="Example"></a>Example</h5>
2044
2045<div class="source">
2046<div class="source">
2047<pre>SELECT GleambookUsers.name, GleambookMessages.message
2048FROM GleambookUsers,
2049 (
2050 SELECT VALUE GleambookMessages
2051 FROM GleambookMessages
2052 WHERE GleambookMessages.authorId = GleambookUsers.id
2053 );
2054</pre></div></div>
2055<p>Returns:</p>
2056
2057<div class="source">
2058<div class="source">
2059<pre>Error: &quot;Syntax error: Need an alias for the enclosed expression:\n(select element GleambookMessages\n from GleambookMessages as GleambookMessages\n where (GleambookMessages.authorId = GleambookUsers.id)\n )&quot;,
2060 &quot;query_from_user&quot;: &quot;use TinySocial;\n\nSELECT GleambookUsers.name, GleambookMessages.message\n FROM GleambookUsers,\n (\n SELECT VALUE GleambookMessages\n FROM GleambookMessages\n WHERE GleambookMessages.authorId = GleambookUsers.id\n );&quot;
2061</pre></div></div></div></div></div></div>
2062<div class="section">
2063<h2><a name="JOIN_clauses"></a><a name="Join_clauses" id="Join_clauses">JOIN clauses</a></h2>
2064<p>The join clause in SQL++ supports both inner joins and left outer joins from standard SQL.</p>
2065<div class="section">
2066<h3><a name="Inner_joins" id="Inner_joins">Inner joins</a></h3>
2067<p>Using a <tt>JOIN</tt> clause, the inner join intent from the preceeding examples can also be expressed as follows:</p>
2068<div class="section">
2069<div class="section">
2070<h5><a name="Example"></a>Example</h5>
2071
2072<div class="source">
2073<div class="source">
2074<pre>SELECT u.name AS uname, m.message AS message
2075FROM GleambookUsers u JOIN GleambookMessages m ON m.authorId = u.id;
2076</pre></div></div></div></div></div>
2077<div class="section">
2078<h3><a name="Left_outer_joins" id="Left_outer_joins">Left outer joins</a></h3>
2079<p>SQL++ supports SQL&#x2019;s notion of left outer join. The following query is an example:</p>
2080
2081<div class="source">
2082<div class="source">
2083<pre>SELECT u.name AS uname, m.message AS message
2084FROM GleambookUsers u LEFT OUTER JOIN GleambookMessages m ON m.authorId = u.id;
2085</pre></div></div>
2086<p>Returns:</p>
2087
2088<div class="source">
2089<div class="source">
2090<pre>[ {
2091 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
2092 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2093}, {
2094 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
2095 &quot;message&quot;: &quot; can't stand motorola the touch-screen is terrible&quot;
2096}, {
2097 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
2098 &quot;message&quot;: &quot; can't stand at&amp;t its plan is terrible&quot;
2099}, {
2100 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
2101 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
2102}, {
2103 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
2104 &quot;message&quot;: &quot; can't stand at&amp;t the network is horrible:(&quot;
2105}, {
2106 &quot;uname&quot;: &quot;IsbelDull&quot;,
2107 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2108}, {
2109 &quot;uname&quot;: &quot;IsbelDull&quot;,
2110 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2111}, {
2112 &quot;uname&quot;: &quot;EmoryUnk&quot;
2113} ]
2114</pre></div></div>
2115<p>For non-matching left-side tuples, SQL++ produces <tt>MISSING</tt> values for the right-side binding variables; that is why the last object in the above result doesn&#x2019;t have a <tt>message</tt> field. Note that this is slightly different from standard SQL, which instead would fill in <tt>NULL</tt> values for the right-side fields. The reason for this difference is that, for non-matches in its join results, SQL++ views fields from the right-side as being &#x201c;not there&#x201d; (a.k.a. <tt>MISSING</tt>) instead of as being &#x201c;there but unknown&#x201d; (i.e., <tt>NULL</tt>).</p>
2116<p>The left-outer join query can also be expressed using <tt>LEFT OUTER UNNEST</tt>:</p>
2117
2118<div class="source">
2119<div class="source">
2120<pre>SELECT u.name AS uname, m.message AS message
2121FROM GleambookUsers u
2122LEFT OUTER UNNEST (
2123 SELECT VALUE message
2124 FROM GleambookMessages message
2125 WHERE message.authorId = u.id
2126 ) m;
2127</pre></div></div>
2128<p>In general, in SQL++, SQL-style join queries can also be expressed by <tt>UNNEST</tt> clauses and left outer join queries can be expressed by <tt>LEFT OUTER UNNESTs</tt>.</p></div></div>
2129<div class="section">
2130<h2><a name="GROUP_BY_clauses"></a><a name="Group_By_clauses" id="Group_By_clauses">GROUP BY clauses</a></h2>
2131<p>The SQL++ <tt>GROUP BY</tt> clause generalizes standard SQL&#x2019;s grouping and aggregation semantics, but it also retains backward compatibility with the standard (relational) SQL <tt>GROUP BY</tt> and aggregation features.</p>
2132<div class="section">
2133<h3><a name="Group_variables" id="Group_variables">Group variables</a></h3>
2134<p>In a <tt>GROUP BY</tt> clause, in addition to the binding variable(s) defined for the grouping key(s), SQL++ allows a user to define a <i>group variable</i> by using the clause&#x2019;s <tt>GROUP AS</tt> extension to denote the resulting group. After grouping, then, the query&#x2019;s in-scope variables include the grouping key&#x2019;s binding variables as well as this group variable which will be bound to one collection value for each group. This per-group collection (i.e., multiset) value will be a set of nested objects in which each field of the object is the result of a renamed variable defined in parentheses following the group variable&#x2019;s name. The <tt>GROUP AS</tt> syntax is as follows:</p>
2135
2136<div class="source">
2137<div class="source">
2138<pre>&lt;GROUP&gt; &lt;AS&gt; Variable (&quot;(&quot; Variable &lt;AS&gt; VariableReference (&quot;,&quot; Variable &lt;AS&gt; VariableReference )* &quot;)&quot;)?
2139</pre></div></div>
2140<div class="section">
2141<div class="section">
2142<h5><a name="Example"></a>Example</h5>
2143
2144<div class="source">
2145<div class="source">
2146<pre>SELECT *
2147FROM GleambookMessages message
2148GROUP BY message.authorId AS uid GROUP AS msgs(message AS msg);
2149</pre></div></div>
2150<p>This first example query returns:</p>
2151
2152<div class="source">
2153<div class="source">
2154<pre>[ {
2155 &quot;msgs&quot;: [
2156 {
2157 &quot;msg&quot;: {
2158 &quot;senderLocation&quot;: [
2159 38.97,
2160 77.49
2161 ],
2162 &quot;inResponseTo&quot;: 1,
2163 &quot;messageId&quot;: 11,
2164 &quot;authorId&quot;: 1,
2165 &quot;message&quot;: &quot; can't stand at&amp;t its plan is terrible&quot;
2166 }
2167 },
2168 {
2169 &quot;msg&quot;: {
2170 &quot;senderLocation&quot;: [
2171 41.66,
2172 80.87
2173 ],
2174 &quot;inResponseTo&quot;: 4,
2175 &quot;messageId&quot;: 2,
2176 &quot;authorId&quot;: 1,
2177 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
2178 }
2179 },
2180 {
2181 &quot;msg&quot;: {
2182 &quot;senderLocation&quot;: [
2183 37.73,
2184 97.04
2185 ],
2186 &quot;inResponseTo&quot;: 2,
2187 &quot;messageId&quot;: 4,
2188 &quot;authorId&quot;: 1,
2189 &quot;message&quot;: &quot; can't stand at&amp;t the network is horrible:(&quot;
2190 }
2191 },
2192 {
2193 &quot;msg&quot;: {
2194 &quot;senderLocation&quot;: [
2195 40.33,
2196 80.87
2197 ],
2198 &quot;inResponseTo&quot;: 11,
2199 &quot;messageId&quot;: 8,
2200 &quot;authorId&quot;: 1,
2201 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2202 }
2203 },
2204 {
2205 &quot;msg&quot;: {
2206 &quot;senderLocation&quot;: [
2207 42.5,
2208 70.01
2209 ],
2210 &quot;inResponseTo&quot;: 12,
2211 &quot;messageId&quot;: 10,
2212 &quot;authorId&quot;: 1,
2213 &quot;message&quot;: &quot; can't stand motorola the touch-screen is terrible&quot;
2214 }
2215 }
2216 ],
2217 &quot;uid&quot;: 1
2218}, {
2219 &quot;msgs&quot;: [
2220 {
2221 &quot;msg&quot;: {
2222 &quot;senderLocation&quot;: [
2223 31.5,
2224 75.56
2225 ],
2226 &quot;inResponseTo&quot;: 1,
2227 &quot;messageId&quot;: 6,
2228 &quot;authorId&quot;: 2,
2229 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2230 }
2231 },
2232 {
2233 &quot;msg&quot;: {
2234 &quot;senderLocation&quot;: [
2235 48.09,
2236 81.01
2237 ],
2238 &quot;inResponseTo&quot;: 4,
2239 &quot;messageId&quot;: 3,
2240 &quot;authorId&quot;: 2,
2241 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2242 }
2243 }
2244 ],
2245 &quot;uid&quot;: 2
2246} ]
2247</pre></div></div>
2248<p>As we can see from the above query result, each group in the example query&#x2019;s output has an associated group variable value called <tt>msgs</tt> that appears in the <tt>SELECT *</tt>&#x2019;s result. This variable contains a collection of objects associated with the group; each of the group&#x2019;s <tt>message</tt> values appears in the <tt>msg</tt> field of the objects in the <tt>msgs</tt> collection.</p>
2249<p>The group variable in SQL++ makes more complex, composable, nested subqueries over a group possible, which is important given the more complex data model of SQL++ (relative to SQL). As a simple example of this, as we really just want the messages associated with each user, we might wish to avoid the &#x201c;extra wrapping&#x201d; of each message as the <tt>msg</tt> field of a object. (That wrapping is useful in more complex cases, but is essentially just in the way here.) We can use a subquery in the <tt>SELECT</tt> clase to tunnel through the extra nesting and produce the desired result.</p></div>
2250<div class="section">
2251<h5><a name="Example"></a>Example</h5>
2252
2253<div class="source">
2254<div class="source">
2255<pre>SELECT uid, (SELECT VALUE g.msg FROM g) AS msgs
2256FROM GleambookMessages gbm
2257GROUP BY gbm.authorId AS uid
2258GROUP AS g(gbm as msg);
2259</pre></div></div>
2260<p>This variant of the example query returns:</p>
2261
2262<div class="source">
2263<div class="source">
2264<pre> [ {
2265 &quot;msgs&quot;: [
2266 {
2267 &quot;senderLocation&quot;: [
2268 38.97,
2269 77.49
2270 ],
2271 &quot;inResponseTo&quot;: 1,
2272 &quot;messageId&quot;: 11,
2273 &quot;authorId&quot;: 1,
2274 &quot;message&quot;: &quot; can't stand at&amp;t its plan is terrible&quot;
2275 },
2276 {
2277 &quot;senderLocation&quot;: [
2278 41.66,
2279 80.87
2280 ],
2281 &quot;inResponseTo&quot;: 4,
2282 &quot;messageId&quot;: 2,
2283 &quot;authorId&quot;: 1,
2284 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
2285 },
2286 {
2287 &quot;senderLocation&quot;: [
2288 37.73,
2289 97.04
2290 ],
2291 &quot;inResponseTo&quot;: 2,
2292 &quot;messageId&quot;: 4,
2293 &quot;authorId&quot;: 1,
2294 &quot;message&quot;: &quot; can't stand at&amp;t the network is horrible:(&quot;
2295 },
2296 {
2297 &quot;senderLocation&quot;: [
2298 40.33,
2299 80.87
2300 ],
2301 &quot;inResponseTo&quot;: 11,
2302 &quot;messageId&quot;: 8,
2303 &quot;authorId&quot;: 1,
2304 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2305 },
2306 {
2307 &quot;senderLocation&quot;: [
2308 42.5,
2309 70.01
2310 ],
2311 &quot;inResponseTo&quot;: 12,
2312 &quot;messageId&quot;: 10,
2313 &quot;authorId&quot;: 1,
2314 &quot;message&quot;: &quot; can't stand motorola the touch-screen is terrible&quot;
2315 }
2316 ],
2317 &quot;uid&quot;: 1
2318 }, {
2319 &quot;msgs&quot;: [
2320 {
2321 &quot;senderLocation&quot;: [
2322 31.5,
2323 75.56
2324 ],
2325 &quot;inResponseTo&quot;: 1,
2326 &quot;messageId&quot;: 6,
2327 &quot;authorId&quot;: 2,
2328 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2329 },
2330 {
2331 &quot;senderLocation&quot;: [
2332 48.09,
2333 81.01
2334 ],
2335 &quot;inResponseTo&quot;: 4,
2336 &quot;messageId&quot;: 3,
2337 &quot;authorId&quot;: 2,
2338 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2339 }
2340 ],
2341 &quot;uid&quot;: 2
2342 } ]
2343</pre></div></div>
2344<p>Because this is a fairly common case, a third variant with output identical to the second variant is also possible:</p></div>
2345<div class="section">
2346<h5><a name="Example"></a>Example</h5>
2347
2348<div class="source">
2349<div class="source">
2350<pre>SELECT uid, msg AS msgs
2351FROM GleambookMessages gbm
2352GROUP BY gbm.authorId AS uid
2353GROUP AS g(gbm as msg);
2354</pre></div></div>
2355<p>This variant of the query exploits a bit of SQL-style &#x201c;syntactic sugar&#x201d; that SQL++ offers to shorten some user queries. In particular, in the <tt>SELECT</tt> list, the reference to the <tt>GROUP</tt> variable field <tt>msg</tt> &#x2013; because it references a field of the group variable &#x2013; is allowed but is &#x201c;pluralized&#x201d;. As a result, the <tt>msg</tt> reference in the <tt>SELECT</tt> list is implicitly rewritten into the second variant&#x2019;s <tt>SELECT VALUE</tt> subquery.</p>
2356<p>The next example shows a more interesting case involving the use of a subquery in the <tt>SELECT</tt> list. Here the subquery further processes the groups.</p></div>
2357<div class="section">
2358<h5><a name="Example"></a>Example</h5>
2359
2360<div class="source">
2361<div class="source">
2362<pre>SELECT uid,
2363 (SELECT VALUE g.msg
2364 FROM g
2365 WHERE g.msg.message LIKE '% like%'
2366 ORDER BY g.msg.messageId
2367 LIMIT 2) AS msgs
2368FROM GleambookMessages gbm
2369GROUP BY gbm.authorId AS uid
2370GROUP AS g(gbm as msg);
2371</pre></div></div>
2372<p>This example query returns:</p>
2373
2374<div class="source">
2375<div class="source">
2376<pre>[ {
2377 &quot;msgs&quot;: [
2378 {
2379 &quot;senderLocation&quot;: [
2380 40.33,
2381 80.87
2382 ],
2383 &quot;inResponseTo&quot;: 11,
2384 &quot;messageId&quot;: 8,
2385 &quot;authorId&quot;: 1,
2386 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2387 }
2388 ],
2389 &quot;uid&quot;: 1
2390}, {
2391 &quot;msgs&quot;: [
2392 {
2393 &quot;senderLocation&quot;: [
2394 48.09,
2395 81.01
2396 ],
2397 &quot;inResponseTo&quot;: 4,
2398 &quot;messageId&quot;: 3,
2399 &quot;authorId&quot;: 2,
2400 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2401 },
2402 {
2403 &quot;senderLocation&quot;: [
2404 31.5,
2405 75.56
2406 ],
2407 &quot;inResponseTo&quot;: 1,
2408 &quot;messageId&quot;: 6,
2409 &quot;authorId&quot;: 2,
2410 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2411 }
2412 ],
2413 &quot;uid&quot;: 2
2414} ]
2415</pre></div></div></div></div></div>
2416<div class="section">
2417<h3><a name="Implicit_grouping_key_variables"></a><a name="Implicit_group_key_variables" id="Implicit_group_key_variables">Implicit grouping key variables</a></h3>
2418<p>In the SQL++ syntax, providing named binding variables for <tt>GROUP BY</tt> key expressions is optional. If a grouping key is missing a user-provided binding variable, the underlying compiler will generate one. Automatic grouping key variable naming falls into three cases in SQL++, much like the treatment of unnamed projections:</p>
2419
2420<ul>
2421
2422<li>If the grouping key expression is a variable reference expression, the generated variable gets the same name as the referred variable;</li>
2423
2424<li>If the grouping key expression is a field access expression, the generated variable gets the same name as the last identifier in the expression;</li>
2425
2426<li>For all other cases, the compiler generates a unique variable (but the user query is unable to refer to this generated variable).</li>
2427</ul>
2428<p>The next example illustrates a query that doesn&#x2019;t provide binding variables for its grouping key expressions.</p>
2429<div class="section">
2430<div class="section">
2431<h5><a name="Example"></a>Example</h5>
2432
2433<div class="source">
2434<div class="source">
2435<pre>SELECT authorId,
2436 (SELECT VALUE g.msg
2437 FROM g
2438 WHERE g.msg.message LIKE '% like%'
2439 ORDER BY g.msg.messageId
2440 LIMIT 2) AS msgs
2441FROM GleambookMessages gbm
2442GROUP BY gbm.authorId
2443GROUP AS g(gbm as msg);
2444</pre></div></div>
2445<p>This query returns:</p>
2446
2447<div class="source">
2448<div class="source">
2449<pre> [ {
2450 &quot;msgs&quot;: [
2451 {
2452 &quot;senderLocation&quot;: [
2453 40.33,
2454 80.87
2455 ],
2456 &quot;inResponseTo&quot;: 11,
2457 &quot;messageId&quot;: 8,
2458 &quot;authorId&quot;: 1,
2459 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2460 }
2461 ],
2462 &quot;authorId&quot;: 1
2463}, {
2464 &quot;msgs&quot;: [
2465 {
2466 &quot;senderLocation&quot;: [
2467 48.09,
2468 81.01
2469 ],
2470 &quot;inResponseTo&quot;: 4,
2471 &quot;messageId&quot;: 3,
2472 &quot;authorId&quot;: 2,
2473 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2474 },
2475 {
2476 &quot;senderLocation&quot;: [
2477 31.5,
2478 75.56
2479 ],
2480 &quot;inResponseTo&quot;: 1,
2481 &quot;messageId&quot;: 6,
2482 &quot;authorId&quot;: 2,
2483 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2484 }
2485 ],
2486 &quot;authorId&quot;: 2
2487} ]
2488</pre></div></div>
2489<p>Based on the three variable generation rules, the generated variable for the grouping key expression <tt>message.authorId</tt> is <tt>authorId</tt> (which is how it is referred to in the example&#x2019;s <tt>SELECT</tt> clause).</p></div></div></div>
2490<div class="section">
2491<h3><a name="Implicit_group_variables" id="Implicit_group_variables">Implicit group variables</a></h3>
2492<p>The group variable itself is also optional in SQL++&#x2019;s <tt>GROUP BY</tt> syntax. If a user&#x2019;s query does not declare the name and structure of the group variable using <tt>GROUP AS</tt>, the query compiler will generate a unique group variable whose fields include all of the binding variables defined in the <tt>FROM</tt> clause of the current enclosing <tt>SELECT</tt> statement. (In this case the user&#x2019;s query will not be able to refer to the generated group variable.)</p>
2493<div class="section">
2494<div class="section">
2495<h5><a name="Example"></a>Example</h5>
2496
2497<div class="source">
2498<div class="source">
2499<pre>SELECT uid,
2500 (SELECT m.message
2501 FROM message m
2502 WHERE m.message LIKE '% like%'
2503 ORDER BY m.messageId
2504 LIMIT 2) AS msgs
2505FROM GleambookMessages message
2506GROUP BY message.authorId AS uid;
2507</pre></div></div>
2508<p>This query returns:</p>
2509
2510<div class="source">
2511<div class="source">
2512<pre>[ {
2513 &quot;msgs&quot;: [
2514 {
2515 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
2516 }
2517 ],
2518 &quot;uid&quot;: 1
2519}, {
2520 &quot;msgs&quot;: [
2521 {
2522 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
2523 },
2524 {
2525 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
2526 }
2527 ],
2528 &quot;uid&quot;: 2
2529} ]
2530</pre></div></div>
2531<p>Note that in the query above, in principle, <tt>message</tt> is not an in-scope variable in the <tt>SELECT</tt> clause. However, the query above is a syntactically-sugared simplification of the following query and it is thus legal, executable, and returns the same result:</p>
2532
2533<div class="source">
2534<div class="source">
2535<pre>SELECT uid,
2536 (SELECT g.msg.message
2537 FROM g
2538 WHERE g.msg.message LIKE '% like%'
2539 ORDER BY g.msg.messageId
2540 LIMIT 2) AS msgs
2541FROM GleambookMessages gbm
2542GROUP BY gbm.authorId AS uid GROUP AS g(gbm as msg);
2543</pre></div></div></div></div></div>
2544<div class="section">
2545<h3><a name="Aggregation_functions" id="Aggregation_functions">Aggregation functions</a></h3>
2546<p>In traditional SQL, which doesn&#x2019;t support nested data, grouping always also involves the use of aggregation to compute properties of the groups (e.g., the average number of messages per user rather than the actual set of messages per user). Each aggregation function in SQL++ takes a collection (e.g., the group of messages) as its input and produces a scalar value as its output. These aggregation functions, being truly functional in nature (unlike in SQL), can be used anywhere in a query where an expression is allowed. The following table catalogs the SQL++ built-in aggregation functions and also indicates how each one handles <tt>NULL</tt>/<tt>MISSING</tt> values in the input collection or a completely empty input collection:</p>
2547
2548<table border="0" class="table table-striped">
2549 <thead>
2550
2551<tr class="a">
2552
2553<th>Function </th>
2554
2555<th>NULL </th>
2556
2557<th>MISSING </th>
2558
2559<th>Empty Collection </th>
2560 </tr>
2561 </thead>
2562 <tbody>
2563
2564<tr class="b">
2565
2566<td>COLL_COUNT </td>
2567
2568<td>counted </td>
2569
2570<td>counted </td>
2571
2572<td>0 </td>
2573 </tr>
2574
2575<tr class="a">
2576
2577<td>COLL_SUM </td>
2578
2579<td>returns NULL </td>
2580
2581<td>returns NULL </td>
2582
2583<td>returns NULL </td>
2584 </tr>
2585
2586<tr class="b">
2587
2588<td>COLL_MAX </td>
2589
2590<td>returns NULL </td>
2591
2592<td>returns NULL </td>
2593
2594<td>returns NULL </td>
2595 </tr>
2596
2597<tr class="a">
2598
2599<td>COLL_MIN </td>
2600
2601<td>returns NULL </td>
2602
2603<td>returns NULL </td>
2604
2605<td>returns NULL </td>
2606 </tr>
2607
2608<tr class="b">
2609
2610<td>COLL_AVG </td>
2611
2612<td>returns NULL </td>
2613
2614<td>returns NULL </td>
2615
2616<td>returns NULL </td>
2617 </tr>
2618
2619<tr class="a">
2620
2621<td>ARRAY_COUNT </td>
2622
2623<td>not counted </td>
2624
2625<td>not counted </td>
2626
2627<td>0 </td>
2628 </tr>
2629
2630<tr class="b">
2631
2632<td>ARRAY_SUM </td>
2633
2634<td>ignores NULL </td>
2635
2636<td>ignores NULL </td>
2637
2638<td>returns NULL </td>
2639 </tr>
2640
2641<tr class="a">
2642
2643<td>ARRAY_MAX </td>
2644
2645<td>ignores NULL </td>
2646
2647<td>ignores NULL </td>
2648
2649<td>returns NULL </td>
2650 </tr>
2651
2652<tr class="b">
2653
2654<td>ARRAY_MIN </td>
2655
2656<td>ignores NULL </td>
2657
2658<td>ignores NULL </td>
2659
2660<td>returns NULL </td>
2661 </tr>
2662
2663<tr class="a">
2664
2665<td>ARRAY_AVG </td>
2666
2667<td>ignores NULL </td>
2668
2669<td>ignores NULL </td>
2670
2671<td>returns NULL </td>
2672 </tr>
2673 </tbody>
2674</table>
2675<p>Notice that SQL++ has twice as many functions listed above as there are aggregate functions in SQL-92. This is because SQL++ offers two versions of each &#x2013; one that handles <tt>UNKNOWN</tt> values in a semantically strict fashion, where unknown values in the input result in unknown values in the output &#x2013; and one that handles them in the ad hoc &#x201c;just ignore the unknown values&#x201d; fashion that the SQL standard chose to adopt.</p>
2676<div class="section">
2677<div class="section">
2678<h5><a name="Example"></a>Example</h5>
2679
2680<div class="source">
2681<div class="source">
2682<pre>ARRAY_AVG(
2683 (
2684 SELECT VALUE ARRAY_COUNT(friendIds) FROM GleambookUsers
2685 )
2686);
2687</pre></div></div>
2688<p>This example returns:</p>
2689
2690<div class="source">
2691<div class="source">
2692<pre>3.3333333333333335
2693</pre></div></div></div>
2694<div class="section">
2695<h5><a name="Example"></a>Example</h5>
2696
2697<div class="source">
2698<div class="source">
2699<pre>SELECT uid AS uid, ARRAY_COUNT(grp) AS msgCnt
2700FROM GleambookMessages message
2701GROUP BY message.authorId AS uid GROUP AS grp(message AS msg);
2702</pre></div></div>
2703<p>This query returns:</p>
2704
2705<div class="source">
2706<div class="source">
2707<pre>[ {
2708 &quot;uid&quot;: 1,
2709 &quot;msgCnt&quot;: 5
2710}, {
2711 &quot;uid&quot;: 2,
2712 &quot;msgCnt&quot;: 2
2713} ]
2714</pre></div></div>
2715<p>Notice how the query forms groups where each group involves a message author and their messages. (SQL cannot do this because the grouped intermediate result is non-1NF in nature.) The query then uses the collection aggregate function ARRAY_COUNT to get the cardinality of each group of messages.</p></div></div></div>
2716<div class="section">
2717<h3><a name="SQL-92_aggregation_functions" id="SQL-92_aggregation_functions">SQL-92 aggregation functions</a></h3>
2718<p>For compatibility with the traditional SQL aggregation functions, SQL++ also offers SQL-92&#x2019;s aggregation function symbols (<tt>COUNT</tt>, <tt>SUM</tt>, <tt>MAX</tt>, <tt>MIN</tt>, and <tt>AVG</tt>) as supported syntactic sugar. The SQL++ compiler rewrites queries that utilize these function symbols into SQL++ queries that only use the SQL++ collection aggregate functions. The following example uses the SQL-92 syntax approach to compute a result that is identical to that of the more explicit SQL++ example above:</p>
2719<div class="section">
2720<div class="section">
2721<h5><a name="Example"></a>Example</h5>
2722
2723<div class="source">
2724<div class="source">
2725<pre>SELECT uid, COUNT(*) AS msgCnt
2726FROM GleambookMessages msg
2727GROUP BY msg.authorId AS uid;
2728</pre></div></div>
2729<p>It is important to realize that <tt>COUNT</tt> is actually <b>not</b> a SQL++ built-in aggregation function. Rather, the <tt>COUNT</tt> query above is using a special &#x201c;sugared&#x201d; function symbol that the SQL++ compiler will rewrite as follows:</p>
2730
2731<div class="source">
2732<div class="source">
2733<pre>SELECT uid AS uid, ARRAY_COUNT( (SELECT VALUE 1 FROM `$1` as g) ) AS msgCnt
2734FROM GleambookMessages msg
2735GROUP BY msg.authorId AS uid GROUP AS `$1`(msg AS msg);
2736</pre></div></div>
2737<p>The same sort of rewritings apply to the function symbols <tt>SUM</tt>, <tt>MAX</tt>, <tt>MIN</tt>, and <tt>AVG</tt>. In contrast to the SQL++ collection aggregate functions, these special SQL-92 function symbols can only be used in the same way they are in standard SQL (i.e., with the same restrictions).</p></div></div></div>
2738<div class="section">
2739<h3><a name="SQL-92_compliant_GROUP_BY_aggregations"></a><a name="SQL-92_compliant_gby" id="SQL-92_compliant_gby">SQL-92 compliant GROUP BY aggregations</a></h3>
2740<p>SQL++ provides full support for SQL-92 <tt>GROUP BY</tt> aggregation queries. The following query is such an example:</p>
2741<div class="section">
2742<div class="section">
2743<h5><a name="Example"></a>Example</h5>
2744
2745<div class="source">
2746<div class="source">
2747<pre>SELECT msg.authorId, COUNT(msg)
2748FROM GleambookMessages msg
2749GROUP BY msg.authorId;
2750</pre></div></div>
2751<p>This query outputs:</p>
2752
2753<div class="source">
2754<div class="source">
2755<pre>[ {
2756 &quot;authorId&quot;: 1,
2757 &quot;$1&quot;: 5
2758}, {
2759 &quot;authorId&quot;: 2,
2760 &quot;$1&quot;: 2
2761} ]
2762</pre></div></div>
2763<p>In principle, a <tt>msg</tt> reference in the query&#x2019;s <tt>SELECT</tt> clause would be &#x201c;sugarized&#x201d; as a collection (as described in <a href="#Implicit_group_variables">Implicit group variables</a>). However, since the SELECT expression <tt>msg.authorId</tt> is syntactically identical to a GROUP BY key expression, it will be internally replaced by the generated group key variable. The following is the equivalent rewritten query that will be generated by the compiler for the query above:</p>
2764
2765<div class="source">
2766<div class="source">
2767<pre>SELECT authorId AS authorId, ARRAY_COUNT( (SELECT g.msg FROM `$1` AS g) )
2768FROM GleambookMessages msg
2769GROUP BY msg.authorId AS authorId GROUP AS `$1`(msg AS msg);
2770</pre></div></div></div></div></div>
2771<div class="section">
2772<h3><a name="Column_aliases" id="Column_aliases">Column aliases</a></h3>
2773<p>SQL++ also allows column aliases to be used as <tt>GROUP BY</tt> keys or <tt>ORDER BY</tt> keys.</p>
2774<div class="section">
2775<div class="section">
2776<h5><a name="Example"></a>Example</h5>
2777
2778<div class="source">
2779<div class="source">
2780<pre>SELECT msg.authorId AS aid, COUNT(msg)
2781FROM GleambookMessages msg
2782GROUP BY aid;
2783</pre></div></div>
2784<p>This query returns:</p>
2785
2786<div class="source">
2787<div class="source">
2788<pre>[ {
2789 &quot;$1&quot;: 5,
2790 &quot;aid&quot;: 1
2791}, {
2792 &quot;$1&quot;: 2,
2793 &quot;aid&quot;: 2
2794} ]
2795</pre></div></div></div></div></div></div>
2796<div class="section">
2797<h2><a name="WHERE_clauses_and_HAVING_clauses"></a><a name="Where_having_clauses" id="Where_having_clauses">WHERE clauses and HAVING clauses</a></h2>
2798<p>Both <tt>WHERE</tt> clauses and <tt>HAVING</tt> clauses are used to filter input data based on a condition expression. Only tuples for which the condition expression evaluates to <tt>TRUE</tt> are propagated. Note that if the condition expression evaluates to <tt>NULL</tt> or <tt>MISSING</tt> the input tuple will be disgarded.</p></div>
2799<div class="section">
2800<h2><a name="ORDER_BY_clauses"></a><a name="Order_By_clauses" id="Order_By_clauses">ORDER BY clauses</a></h2>
2801<p>The <tt>ORDER BY</tt> clause is used to globally sort data in either ascending order (i.e., <tt>ASC</tt>) or descending order (i.e., <tt>DESC</tt>). During ordering, <tt>MISSING</tt> and <tt>NULL</tt> are treated as being smaller than any other value if they are encountered in the ordering key(s). <tt>MISSING</tt> is treated as smaller than <tt>NULL</tt> if both occur in the data being sorted. The following example returns all <tt>GleambookUsers</tt> in descending order by their number of friends.</p>
2802<div class="section">
2803<div class="section">
2804<div class="section">
2805<h5><a name="Example"></a>Example</h5>
2806
2807<div class="source">
2808<div class="source">
2809<pre> SELECT VALUE user
2810 FROM GleambookUsers AS user
2811 ORDER BY ARRAY_COUNT(user.friendIds) DESC;
2812</pre></div></div>
2813<p>This query returns:</p>
2814
2815<div class="source">
2816<div class="source">
2817<pre> [ {
2818 &quot;userSince&quot;: &quot;2012-08-20T10:10:00.000Z&quot;,
2819 &quot;friendIds&quot;: [
2820 2,
2821 3,
2822 6,
2823 10
2824 ],
2825 &quot;gender&quot;: &quot;F&quot;,
2826 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2827 &quot;nickname&quot;: &quot;Mags&quot;,
2828 &quot;alias&quot;: &quot;Margarita&quot;,
2829 &quot;id&quot;: 1,
2830 &quot;employment&quot;: [
2831 {
2832 &quot;organizationName&quot;: &quot;Codetechno&quot;,
2833 &quot;start-date&quot;: &quot;2006-08-06&quot;
2834 },
2835 {
2836 &quot;end-date&quot;: &quot;2010-01-26&quot;,
2837 &quot;organizationName&quot;: &quot;geomedia&quot;,
2838 &quot;start-date&quot;: &quot;2010-06-17&quot;
2839 }
2840 ]
2841 }, {
2842 &quot;userSince&quot;: &quot;2012-07-10T10:10:00.000Z&quot;,
2843 &quot;friendIds&quot;: [
2844 1,
2845 5,
2846 8,
2847 9
2848 ],
2849 &quot;name&quot;: &quot;EmoryUnk&quot;,
2850 &quot;alias&quot;: &quot;Emory&quot;,
2851 &quot;id&quot;: 3,
2852 &quot;employment&quot;: [
2853 {
2854 &quot;organizationName&quot;: &quot;geomedia&quot;,
2855 &quot;endDate&quot;: &quot;2010-01-26&quot;,
2856 &quot;startDate&quot;: &quot;2010-06-17&quot;
2857 }
2858 ]
2859 }, {
2860 &quot;userSince&quot;: &quot;2011-01-22T10:10:00.000Z&quot;,
2861 &quot;friendIds&quot;: [
2862 1,
2863 4
2864 ],
2865 &quot;name&quot;: &quot;IsbelDull&quot;,
2866 &quot;nickname&quot;: &quot;Izzy&quot;,
2867 &quot;alias&quot;: &quot;Isbel&quot;,
2868 &quot;id&quot;: 2,
2869 &quot;employment&quot;: [
2870 {
2871 &quot;organizationName&quot;: &quot;Hexviafind&quot;,
2872 &quot;startDate&quot;: &quot;2010-04-27&quot;
2873 }
2874 ]
2875 } ]
2876</pre></div></div></div></div></div></div>
2877<div class="section">
2878<h2><a name="LIMIT_clauses"></a><a name="Limit_clauses" id="Limit_clauses">LIMIT clauses</a></h2>
2879<p>The <tt>LIMIT</tt> clause is used to limit the result set to a specified constant size. The use of the <tt>LIMIT</tt> clause is illustrated in the next example.</p>
2880<div class="section">
2881<div class="section">
2882<div class="section">
2883<h5><a name="Example"></a>Example</h5>
2884
2885<div class="source">
2886<div class="source">
2887<pre> SELECT VALUE user
2888 FROM GleambookUsers AS user
2889 ORDER BY len(user.friendIds) DESC
2890 LIMIT 1;
2891</pre></div></div>
2892<p>This query returns:</p>
2893
2894<div class="source">
2895<div class="source">
2896<pre> [ {
2897 &quot;userSince&quot;: &quot;2012-08-20T10:10:00.000Z&quot;,
2898 &quot;friendIds&quot;: [
2899 2,
2900 3,
2901 6,
2902 10
2903 ],
2904 &quot;gender&quot;: &quot;F&quot;,
2905 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2906 &quot;nickname&quot;: &quot;Mags&quot;,
2907 &quot;alias&quot;: &quot;Margarita&quot;,
2908 &quot;id&quot;: 1,
2909 &quot;employment&quot;: [
2910 {
2911 &quot;organizationName&quot;: &quot;Codetechno&quot;,
2912 &quot;start-date&quot;: &quot;2006-08-06&quot;
2913 },
2914 {
2915 &quot;end-date&quot;: &quot;2010-01-26&quot;,
2916 &quot;organizationName&quot;: &quot;geomedia&quot;,
2917 &quot;start-date&quot;: &quot;2010-06-17&quot;
2918 }
2919 ]
2920 } ]
2921</pre></div></div></div></div></div></div>
2922<div class="section">
2923<h2><a name="WITH_clauses"></a><a name="With_clauses" id="With_clauses">WITH clauses</a></h2>
2924<p>As in standard SQL, <tt>WITH</tt> clauses are available to improve the modularity of a query. The next query shows an example.</p>
2925<div class="section">
2926<div class="section">
2927<div class="section">
2928<h5><a name="Example"></a>Example</h5>
2929
2930<div class="source">
2931<div class="source">
2932<pre>WITH avgFriendCount AS (
2933 SELECT VALUE AVG(ARRAY_COUNT(user.friendIds))
2934 FROM GleambookUsers AS user
2935)[0]
2936SELECT VALUE user
2937FROM GleambookUsers user
2938WHERE ARRAY_COUNT(user.friendIds) &gt; avgFriendCount;
2939</pre></div></div>
2940<p>This query returns:</p>
2941
2942<div class="source">
2943<div class="source">
2944<pre>[ {
2945 &quot;userSince&quot;: &quot;2012-08-20T10:10:00.000Z&quot;,
2946 &quot;friendIds&quot;: [
2947 2,
2948 3,
2949 6,
2950 10
2951 ],
2952 &quot;gender&quot;: &quot;F&quot;,
2953 &quot;name&quot;: &quot;MargaritaStoddard&quot;,
2954 &quot;nickname&quot;: &quot;Mags&quot;,
2955 &quot;alias&quot;: &quot;Margarita&quot;,
2956 &quot;id&quot;: 1,
2957 &quot;employment&quot;: [
2958 {
2959 &quot;organizationName&quot;: &quot;Codetechno&quot;,
2960 &quot;start-date&quot;: &quot;2006-08-06&quot;
2961 },
2962 {
2963 &quot;end-date&quot;: &quot;2010-01-26&quot;,
2964 &quot;organizationName&quot;: &quot;geomedia&quot;,
2965 &quot;start-date&quot;: &quot;2010-06-17&quot;
2966 }
2967 ]
2968}, {
2969 &quot;userSince&quot;: &quot;2012-07-10T10:10:00.000Z&quot;,
2970 &quot;friendIds&quot;: [
2971 1,
2972 5,
2973 8,
2974 9
2975 ],
2976 &quot;name&quot;: &quot;EmoryUnk&quot;,
2977 &quot;alias&quot;: &quot;Emory&quot;,
2978 &quot;id&quot;: 3,
2979 &quot;employment&quot;: [
2980 {
2981 &quot;organizationName&quot;: &quot;geomedia&quot;,
2982 &quot;endDate&quot;: &quot;2010-01-26&quot;,
2983 &quot;startDate&quot;: &quot;2010-06-17&quot;
2984 }
2985 ]
2986} ]
2987</pre></div></div>
2988<p>The query is equivalent to the following, more complex, inlined form of the query:</p>
2989
2990<div class="source">
2991<div class="source">
2992<pre>SELECT *
2993FROM GleambookUsers user
2994WHERE ARRAY_COUNT(user.friendIds) &gt;
2995 ( SELECT VALUE AVG(ARRAY_COUNT(user.friendIds))
2996 FROM GleambookUsers AS user
2997 ) [0];
2998</pre></div></div>
2999<p>WITH can be particularly useful when a value needs to be used several times in a query.</p>
3000<p>Before proceeding further, notice that both the WITH query and its equivalent inlined variant include the syntax &#x201c;[0]&#x201d; &#x2013; this is due to a noteworthy difference between SQL++ and SQL-92. In SQL-92, whenever a scalar value is expected and it is being produced by a query expression, the SQL-92 query processor will evaluate the expression, check that there is only one row and column in the result at runtime, and then coerce the one-row/one-column tabular result into a scalar value. SQL++, being designed to deal with nested data and schema-less data, does not (and should not) do this. Collection-valued data is perfectly legal in most SQL++ contexts, and its data is schema-less, so a query processor rarely knows exactly what to expect where and such automatic conversion is often not desirable. Thus, in the queries above, the use of &#x201c;[0]&#x201d; extracts the first (i.e., 0th) element of an array-valued query expression&#x2019;s result; this is needed above, even though the result is an array of one element, to extract the only element in the singleton array and obtain the desired scalar for the comparison.</p></div></div></div></div>
3001<div class="section">
3002<h2><a name="LET_clauses"></a><a name="Let_clauses" id="Let_clauses">LET clauses</a></h2>
3003<p>Similar to <tt>WITH</tt> clauses, <tt>LET</tt> clauses can be useful when a (complex) expression is used several times within a query, allowing it to be written once to make the query more concise. The next query shows an example.</p>
3004<div class="section">
3005<div class="section">
3006<div class="section">
3007<h5><a name="Example"></a>Example</h5>
3008
3009<div class="source">
3010<div class="source">
3011<pre>SELECT u.name AS uname, messages AS messages
3012FROM GleambookUsers u
3013LET messages = (SELECT VALUE m
3014 FROM GleambookMessages m
3015 WHERE m.authorId = u.id)
3016WHERE EXISTS messages;
3017</pre></div></div>
3018<p>This query lists <tt>GleambookUsers</tt> that have posted <tt>GleambookMessages</tt> and shows all authored messages for each listed user. It returns:</p>
3019
3020<div class="source">
3021<div class="source">
3022<pre>[ {
3023 &quot;uname&quot;: &quot;MargaritaStoddard&quot;,
3024 &quot;messages&quot;: [
3025 {
3026 &quot;senderLocation&quot;: [
3027 38.97,
3028 77.49
3029 ],
3030 &quot;inResponseTo&quot;: 1,
3031 &quot;messageId&quot;: 11,
3032 &quot;authorId&quot;: 1,
3033 &quot;message&quot;: &quot; can't stand at&amp;t its plan is terrible&quot;
3034 },
3035 {
3036 &quot;senderLocation&quot;: [
3037 41.66,
3038 80.87
3039 ],
3040 &quot;inResponseTo&quot;: 4,
3041 &quot;messageId&quot;: 2,
3042 &quot;authorId&quot;: 1,
3043 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
3044 },
3045 {
3046 &quot;senderLocation&quot;: [
3047 37.73,
3048 97.04
3049 ],
3050 &quot;inResponseTo&quot;: 2,
3051 &quot;messageId&quot;: 4,
3052 &quot;authorId&quot;: 1,
3053 &quot;message&quot;: &quot; can't stand at&amp;t the network is horrible:(&quot;
3054 },
3055 {
3056 &quot;senderLocation&quot;: [
3057 40.33,
3058 80.87
3059 ],
3060 &quot;inResponseTo&quot;: 11,
3061 &quot;messageId&quot;: 8,
3062 &quot;authorId&quot;: 1,
3063 &quot;message&quot;: &quot; like verizon the 3G is awesome:)&quot;
3064 },
3065 {
3066 &quot;senderLocation&quot;: [
3067 42.5,
3068 70.01
3069 ],
3070 &quot;inResponseTo&quot;: 12,
3071 &quot;messageId&quot;: 10,
3072 &quot;authorId&quot;: 1,
3073 &quot;message&quot;: &quot; can't stand motorola the touch-screen is terrible&quot;
3074 }
3075 ]
3076}, {
3077 &quot;uname&quot;: &quot;IsbelDull&quot;,
3078 &quot;messages&quot;: [
3079 {
3080 &quot;senderLocation&quot;: [
3081 31.5,
3082 75.56
3083 ],
3084 &quot;inResponseTo&quot;: 1,
3085 &quot;messageId&quot;: 6,
3086 &quot;authorId&quot;: 2,
3087 &quot;message&quot;: &quot; like t-mobile its platform is mind-blowing&quot;
3088 },
3089 {
3090 &quot;senderLocation&quot;: [
3091 48.09,
3092 81.01
3093 ],
3094 &quot;inResponseTo&quot;: 4,
3095 &quot;messageId&quot;: 3,
3096 &quot;authorId&quot;: 2,
3097 &quot;message&quot;: &quot; like samsung the plan is amazing&quot;
3098 }
3099 ]
3100} ]
3101</pre></div></div>
3102<p>This query is equivalent to the following query that does not use the <tt>LET</tt> clause:</p>
3103
3104<div class="source">
3105<div class="source">
3106<pre>SELECT u.name AS uname, ( SELECT VALUE m
3107 FROM GleambookMessages m
3108 WHERE m.authorId = u.id
3109 ) AS messages
3110FROM GleambookUsers u
3111WHERE EXISTS ( SELECT VALUE m
3112 FROM GleambookMessages m
3113 WHERE m.authorId = u.id
3114 );
3115</pre></div></div></div></div></div></div>
3116<div class="section">
3117<h2><a name="UNION_ALL"></a><a name="Union_all" id="Union_all">UNION ALL</a></h2>
3118<p>UNION ALL can be used to combine two input streams into one. As in SQL, there is no ordering guarantee on the contents of the output stream. However, unlike SQL, SQL++ does not constrain what the data looks like on the input streams; in particular, it allows heterogenity on the input and output streams. A type error will be raised if one of the inputs is not a collection. The following odd but legal query is an example:</p>
3119<div class="section">
3120<div class="section">
3121<div class="section">
3122<h5><a name="Example"></a>Example</h5>
3123
3124<div class="source">
3125<div class="source">
3126<pre>SELECT u.name AS uname
3127FROM GleambookUsers u
3128WHERE u.id = 2
3129 UNION ALL
3130SELECT VALUE m.message
3131FROM GleambookMessages m
3132WHERE authorId=2;
3133</pre></div></div>
3134<p>This query returns:</p>
3135
3136<div class="source">
3137<div class="source">
3138<pre>[
3139 &quot; like t-mobile its platform is mind-blowing&quot;
3140 , {
3141 &quot;uname&quot;: &quot;IsbelDull&quot;
3142}, &quot; like samsung the plan is amazing&quot;
3143 ]
3144</pre></div></div></div></div></div></div>
3145<div class="section">
3146<h2><a name="Subqueries" id="Subqueries">Subqueries</a></h2>
3147<p>In SQL++, an arbitrary subquery can appear anywhere that an expression can appear. Unlike SQL-92, as was just alluded to, the subqueries in a SELECT list or a boolean predicate need not return singleton, single-column relations. Instead, they may return arbitrary collections. For example, the following query is a variant of the prior group-by query examples; it retrieves an array of up to two &#x201c;dislike&#x201d; messages per user.</p>
3148<div class="section">
3149<div class="section">
3150<div class="section">
3151<h5><a name="Example"></a>Example</h5>
3152
3153<div class="source">
3154<div class="source">
3155<pre>SELECT uid,
3156 (SELECT VALUE m.msg
3157 FROM msgs m
3158 WHERE m.msg.message LIKE '%dislike%'
3159 ORDER BY m.msg.messageId
3160 LIMIT 2) AS msgs
3161FROM GleambookMessages message
3162GROUP BY message.authorId AS uid GROUP AS msgs(message AS msg);
3163</pre></div></div>
3164<p>For our sample data set, this query returns:</p>
3165
3166<div class="source">
3167<div class="source">
3168<pre>[ {
3169 &quot;msgs&quot;: [
3170 {
3171 &quot;senderLocation&quot;: [
3172 41.66,
3173 80.87
3174 ],
3175 &quot;inResponseTo&quot;: 4,
3176 &quot;messageId&quot;: 2,
3177 &quot;authorId&quot;: 1,
3178 &quot;message&quot;: &quot; dislike iphone its touch-screen is horrible&quot;
3179 }
3180 ],
3181 &quot;uid&quot;: 1
3182}, {
3183 &quot;msgs&quot;: [
3184
3185 ],
3186 &quot;uid&quot;: 2
3187} ]
3188</pre></div></div>
3189<p>Note that a subquery, like a top-level <tt>SELECT</tt> statment, always returns a collection &#x2013; regardless of where within a query the subquery occurs &#x2013; and again, its result is never automatically cast into a scalar.</p></div></div></div></div>
3190<div class="section">
3191<h2><a name="SQL_vs._SQL-92"></a><a name="Vs_SQL-92" id="Vs_SQL-92">SQL++ vs. SQL-92</a></h2>
3192<p>SQL++ offers the following additional features beyond SQL-92 (hence the &#x201c;++&#x201d; in its name):</p>
3193
3194<ul>
3195
3196<li>Fully composable and functional: A subquery can iterate over any intermediate collection and can appear anywhere in a query.</li>
3197
3198<li>Schema-free: The query language does not assume the existence of a static schema for any data that it processes.</li>
3199
3200<li>Correlated FROM terms: A right-side FROM term expression can refer to variables defined by FROM terms on its left.</li>
3201
3202<li>Powerful GROUP BY: In addition to a set of aggregate functions as in standard SQL, the groups created by the <tt>GROUP BY</tt> clause are directly usable in nested queries and/or to obtain nested results.</li>
3203
3204<li>Generalized SELECT clause: A SELECT clause can return any type of collection, while in SQL-92, a <tt>SELECT</tt> clause has to return a (homogeneous) collection of objects.</li>
3205</ul>
3206<p>The following matrix is a quick &#x201c;SQL-92 compatibility cheat sheet&#x201d; for SQL++.</p>
3207
3208<table border="0" class="table table-striped">
3209 <thead>
3210
3211<tr class="a">
3212
3213<th>Feature </th>
3214
3215<th>SQL++ </th>
3216
3217<th>SQL-92 </th>
3218
3219<th>Why different? </th>
3220 </tr>
3221 </thead>
3222 <tbody>
3223
3224<tr class="b">
3225
3226<td>SELECT * </td>
3227
3228<td>Returns nested objects </td>
3229
3230<td>Returns flattened concatenated objects </td>
3231
3232<td>Nested collections are 1st class citizens </td>
3233 </tr>
3234
3235<tr class="a">
3236
3237<td>SELECT list </td>
3238
3239<td>order not preserved </td>
3240
3241<td>order preserved </td>
3242
3243<td>Fields in a JSON object is not ordered </td>
3244 </tr>
3245
3246<tr class="b">
3247
3248<td>Subquery </td>
3249
3250<td>Returns a collection </td>
3251
3252<td>The returned collection is cast into a scalar value if the subquery appears in a SELECT list or on one side of a comparison or as input to a function </td>
3253
3254<td>Nested collections are 1st class citizens </td>
3255 </tr>
3256
3257<tr class="a">
3258
3259<td>LEFT OUTER JOIN </td>
3260
3261<td>Fills in <tt>MISSING</tt>(s) for non-matches </td>
3262
3263<td>Fills in <tt>NULL</tt>(s) for non-matches </td>
3264
3265<td>&#x201c;Absence&#x201d; is more appropriate than &#x201c;unknown&#x201d; here. </td>
3266 </tr>
3267
3268<tr class="b">
3269
3270<td>UNION ALL </td>
3271
3272<td>Allows heterogeneous inputs and output </td>
3273
3274<td>Input streams must be UNION-compatible and output field names are drawn from the first input stream </td>
3275
3276<td>Heterogenity and nested collections are common </td>
3277 </tr>
3278
3279<tr class="a">
3280
3281<td>IN constant_expr </td>
3282
3283<td>The constant expression has to be an array or multiset, i.e., [..,..,&#x2026;] </td>
3284
3285<td>The constant collection can be represented as comma-separated items in a paren pair </td>
3286
3287<td>Nested collections are 1st class citizens </td>
3288 </tr>
3289
3290<tr class="b">
3291
3292<td>String literal </td>
3293
3294<td>Double quotes or single quotes </td>
3295
3296<td>Single quotes only </td>
3297
3298<td>Double quoted strings are pervasive </td>
3299 </tr>
3300
3301<tr class="a">
3302
3303<td>Delimited identifiers </td>
3304
3305<td>Backticks </td>
3306
3307<td>Double quotes </td>
3308
3309<td>Double quoted strings are pervasive </td>
3310 </tr>
3311 </tbody>
3312</table>
3313<p>The following SQL-92 features are not implemented yet. However, SQL++ does not conflict those features:</p>
3314
3315<ul>
3316
3317<li>CROSS JOIN, NATURAL JOIN, UNION JOIN</li>
3318
3319<li>RIGHT and FULL OUTER JOIN</li>
3320
3321<li>INTERSECT, EXCEPT, UNION with set semantics</li>
3322
3323<li>CAST expression</li>
3324
3325<li>NULLIF expression</li>
3326
3327<li>COALESCE expression</li>
3328
3329<li>ALL and SOME predicates for linking to subqueries</li>
3330
3331<li>UNIQUE predicate (tests a collection for duplicates)</li>
3332
3333<li>MATCH predicate (tests for referential integrity)</li>
3334
3335<li>Row and Table constructors</li>
3336
3337<li>DISTINCT aggregates</li>
3338
3339<li>Preserved order for expressions in a SELECT list</li>
3340</ul>
3341<h1><a name="Errors" id="Errors">4. Errors</a></h1>
3342<p>A SQL++ query can potentially result in one of the following errors:</p>
3343
3344<ul>
3345
3346<li>syntax error,</li>
3347
3348<li>identifier resolution error,</li>
3349
3350<li>type error,</li>
3351
3352<li>resource error.</li>
3353</ul>
3354<p>If the query processor runs into any error, it will terminate the ongoing processing of the query and immediately return an error message to the client.</p></div>
3355<div class="section">
3356<h2><a name="Syntax_Errors"></a><a name="Syntax_errors" id="Syntax_errors">Syntax Errors</a></h2>
3357<p>An valid SQL++ query must satisfy the SQL++ grammar rules. Otherwise, a syntax error will be raised.</p>
3358<div class="section">
3359<div class="section">
3360<div class="section">
3361<h5><a name="Example"></a>Example</h5>
3362
3363<div class="source">
3364<div class="source">
3365<pre>SELECT *
3366FROM GleambookUsers user
3367</pre></div></div>
3368<p>Since the ending semi-colon is mandatory for any SQL++ query, we will get a syntax error as follows:</p>
3369
3370<div class="source">
3371<div class="source">
3372<pre>Error: Syntax error: In line 2 &gt;&gt;FROM GleambookUsers user&lt;&lt; Encountered &lt;EOF&gt; at column 24.
3373==&gt; FROM GleambookUsers user
3374</pre></div></div></div>
3375<div class="section">
3376<h5><a name="Example"></a>Example</h5>
3377
3378<div class="source">
3379<div class="source">
3380<pre>SELECT *
3381FROM GleambookUsers user
3382WHERE type=&quot;advertiser&quot;;
3383</pre></div></div>
3384<p>Since &#x201c;type&#x201d; a <a href="#Reserved_keywords">reserved keyword</a> in the SQL++ parser, we will get a syntax error as follows:</p>
3385
3386<div class="source">
3387<div class="source">
3388<pre>Error: Syntax error: In line 3 &gt;&gt;WHERE type=&quot;advertiser&quot;;&lt;&lt; Encountered 'type' &quot;type&quot; at column 7.
3389==&gt; WHERE type=&quot;advertiser&quot;;
3390</pre></div></div></div></div></div></div>
3391<div class="section">
3392<h2><a name="Identifier_Resolution_Errors"></a><a name="Identifier_resolution_errors" id="Identifier_resolution_errors">Identifier Resolution Errors</a></h2>
3393<p>Referring an undefined identifier can cause an error if the identifier cannot be successfully resolved as a valid field access.</p>
3394<div class="section">
3395<div class="section">
3396<div class="section">
3397<h5><a name="Example"></a>Example</h5>
3398
3399<div class="source">
3400<div class="source">
3401<pre>SELECT *
3402FROM GleambookUser user;
3403</pre></div></div>
3404<p>Assume we have a typo in &#x201c;GleambookUser&#x201d; which misses the ending &#x201c;s&#x201d;, we will get an identifier resolution error as follows:</p>
3405
3406<div class="source">
3407<div class="source">
3408<pre>Error: Cannot find dataset GleambookUser in dataverse Default nor an alias with name GleambookUser!
3409</pre></div></div></div>
3410<div class="section">
3411<h5><a name="Example"></a>Example</h5>
3412
3413<div class="source">
3414<div class="source">
3415<pre>SELECT name, message
3416FROM GleambookUsers u JOIN GleambookMessages m ON m.authorId = u.id;
3417</pre></div></div>
3418<p>If the compiler cannot figure out all possible fields in <tt>GleambookUsers</tt> and <tt>GleambookMessages</tt>, we will get an identifier resolution error as follows:</p>
3419
3420<div class="source">
3421<div class="source">
3422<pre>Error: Cannot resolve ambiguous alias reference for undefined identifier name
3423</pre></div></div></div></div></div></div>
3424<div class="section">
3425<h2><a name="Type_Errors"></a><a name="Type_errors" id="Type_errors">Type Errors</a></h2>
3426<p>The SQL++ compiler does type checks based on its available type information. In addition, the SQL++ runtime also reports type errors if a data model instance it processes does not satisfy the type requirement.</p>
3427<div class="section">
3428<div class="section">
3429<div class="section">
3430<h5><a name="Example"></a>Example</h5>
3431
3432<div class="source">
3433<div class="source">
3434<pre>abs(&quot;123&quot;);
3435</pre></div></div>
3436<p>Since function <tt>abs</tt> can only process numeric input values, we will get a type error as follows:</p>
3437
3438<div class="source">
3439<div class="source">
3440<pre>Error: Arithmetic operations are not implemented for string
3441</pre></div></div></div></div></div></div>
3442<div class="section">
3443<h2><a name="Resource_Errors"></a><a name="Resource_errors" id="Resource_errors">Resource Errors</a></h2>
3444<p>A query can potentially exhaust system resources, such as the number of open files and disk spaces. For instance, the following two resource errors could be potentially be seen when running the system:</p>
3445
3446<div class="source">
3447<div class="source">
3448<pre>Error: no space left on device
3449Error: too many open files
3450</pre></div></div>
3451<p>The &#x201c;no space left on device&#x201d; issue usually can be fixed by cleaning up disk spaces and reserving more disk spaces for the system. The &#x201c;too many open files&#x201d; issue usually can be fixed by a system administrator, following the instructions <a class="externalLink" href="https://easyengine.io/tutorials/linux/increase-open-files-limit/">here</a>.</p>
3452<!-- ! Licensed to the Apache Software Foundation (ASF) under one
3453 ! or more contributor license agreements. See the NOTICE file
3454 ! distributed with this work for additional information
3455 ! regarding copyright ownership. The ASF licenses this file
3456 ! to you under the Apache License, Version 2.0 (the
3457 ! "License"); you may not use this file except in compliance
3458 ! with the License. You may obtain a copy of the License at
3459 !
3460 ! http://www.apache.org/licenses/LICENSE-2.0
3461 !
3462 ! Unless required by applicable law or agreed to in writing,
3463 ! software distributed under the License is distributed on an
3464 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
3465 ! KIND, either express or implied. See the License for the
3466 ! specific language governing permissions and limitations
3467 ! under the License.
3468 ! -->
3469<h1><a name="DDL_and_DML_statements" id="DDL_and_DML_statements">4. DDL and DML statements</a></h1>
3470
3471<div class="source">
3472<div class="source">
3473<pre>Statement ::= ( SingleStatement ( &quot;;&quot; )? )* &lt;EOF&gt;
3474SingleStatement ::= DatabaseDeclaration
3475 | FunctionDeclaration
3476 | CreateStatement
3477 | DropStatement
3478 | LoadStatement
3479 | SetStatement
3480 | InsertStatement
3481 | DeleteStatement
3482 | Query &quot;;&quot;
3483</pre></div></div>
3484<p>In addition to queries, an implementation of SQL++ needs to support statements for data definition and manipulation purposes as well as controlling the context to be used in evaluating SQL++ expressions. This section details the DDL and DML statements supported in the SQL++ language as realized today in Apache AsterixDB.</p></div>
3485<div class="section">
3486<h2><a name="Declarations" id="Declarations">Declarations</a></h2>
3487
3488<div class="source">
3489<div class="source">
3490<pre>DatabaseDeclaration ::= &quot;USE&quot; Identifier
3491</pre></div></div>
3492<p>At the uppermost level, the world of data is organized into data namespaces called <b>dataverses</b>. To set the default dataverse for a series of statements, the USE statement is provided in SQL++.</p>
3493<p>As an example, the following statement sets the default dataverse to be &#x201c;TinySocial&#x201d;.</p>
3494<div class="section">
3495<div class="section">
3496<div class="section">
3497<h5><a name="Example"></a>Example</h5>
3498
3499<div class="source">
3500<div class="source">
3501<pre>USE TinySocial;
3502</pre></div></div>
3503<p>When writing a complex SQL++ 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. In general, the function body (expression) can be any legal SQL++ query expression.</p>
3504
3505<div class="source">
3506<div class="source">
3507<pre>FunctionDeclaration ::= &quot;DECLARE&quot; &quot;FUNCTION&quot; Identifier ParameterList &quot;{&quot; Expression &quot;}&quot;
3508ParameterList ::= &quot;(&quot; ( &lt;VARIABLE&gt; ( &quot;,&quot; &lt;VARIABLE&gt; )* )? &quot;)&quot;
3509</pre></div></div>
3510<p>The following is a simple example of a temporary SQL++ function definition and its use.</p></div>
3511<div class="section">
3512<h5><a name="Example"></a>Example</h5>
3513
3514<div class="source">
3515<div class="source">
3516<pre>DECLARE FUNCTION friendInfo(userId) {
3517 (SELECT u.id, u.name, len(u.friendIds) AS friendCount
3518 FROM GleambookUsers u
3519 WHERE u.id = userId)[0]
3520 };
3521
3522SELECT VALUE friendInfo(2);
3523</pre></div></div>
3524<p>For our sample data set, this returns:</p>
3525
3526<div class="source">
3527<div class="source">
3528<pre>[
3529 { &quot;id&quot;: 2, &quot;name&quot;: &quot;IsbelDull&quot;, &quot;friendCount&quot;: 2 }
3530]
3531</pre></div></div></div></div></div></div>
3532<div class="section">
3533<h2><a name="Lifecycle_management_statements" id="Lifecycle_management_statements">Lifecycle management statements</a></h2>
3534
3535<div class="source">
3536<div class="source">
3537<pre>CreateStatement ::= &quot;CREATE&quot; ( DatabaseSpecification
3538 | TypeSpecification
3539 | DatasetSpecification
3540 | IndexSpecification
3541 | FunctionSpecification )
3542
3543QualifiedName ::= Identifier ( &quot;.&quot; Identifier )?
3544DoubleQualifiedName ::= Identifier &quot;.&quot; Identifier ( &quot;.&quot; Identifier )?
3545</pre></div></div>
3546<p>The CREATE statement in SQL++ is used for creating dataverses as well as other persistent artifacts in a dataverse. It can be used to create new dataverses, datatypes, datasets, indexes, and user-defined SQL++ functions.</p>
3547<div class="section">
3548<h3><a name="Dataverses" id="Dataverses"> Dataverses</a></h3>
3549
3550<div class="source">
3551<div class="source">
3552<pre>DatabaseSpecification ::= &quot;DATAVERSE&quot; Identifier IfNotExists
3553</pre></div></div>
3554<p>The CREATE DATAVERSE statement is used to create new dataverses. To ease the authoring of reusable SQL++ scripts, an optional IF NOT EXISTS clause is included to allow creation to be requested either unconditionally or only if the dataverse does not already exist. If this clause is absent, an error is returned if a dataverse with the indicated name already exists.</p>
3555<p>The following example creates a new dataverse named TinySocial if one does not already exist.</p>
3556<div class="section">
3557<div class="section">
3558<h5><a name="Example"></a>Example</h5>
3559
3560<div class="source">
3561<div class="source">
3562<pre>CREATE DATAVERSE TinySocial IF NOT EXISTS;
3563</pre></div></div></div></div></div>
3564<div class="section">
3565<h3><a name="Types" id="Types"> Types</a></h3>
3566
3567<div class="source">
3568<div class="source">
3569<pre>TypeSpecification ::= &quot;TYPE&quot; FunctionOrTypeName IfNotExists &quot;AS&quot; ObjectTypeDef
3570FunctionOrTypeName ::= QualifiedName
3571IfNotExists ::= ( &lt;IF&gt; &lt;NOT&gt; &lt;EXISTS&gt; )?
3572TypeExpr ::= ObjectTypeDef | TypeReference | ArrayTypeDef | MultisetTypeDef
3573ObjectTypeDef ::= ( &lt;CLOSED&gt; | &lt;OPEN&gt; )? &quot;{&quot; ( ObjectField ( &quot;,&quot; ObjectField )* )? &quot;}&quot;
3574ObjectField ::= Identifier &quot;:&quot; ( TypeExpr ) ( &quot;?&quot; )?
3575NestedField ::= Identifier ( &quot;.&quot; Identifier )*
3576IndexField ::= NestedField ( &quot;:&quot; TypeReference )?
3577TypeReference ::= Identifier
3578ArrayTypeDef ::= &quot;[&quot; ( TypeExpr ) &quot;]&quot;
3579MultisetTypeDef ::= &quot;{{&quot; ( TypeExpr ) &quot;}}&quot;
3580</pre></div></div>
3581<p>The CREATE TYPE statement is used to create a new named datatype. This type can then be used to create stored collections or utilized when defining one or more other datatypes. Much more information about the data model is available in the <a href="datamodel.html">data model reference guide</a>. A new type can be a object type, a renaming of another type, an array type, or a multiset type. A object type can be defined as being either open or closed. Instances of a closed object type are not permitted to contain fields other than those specified in the create type statement. Instances of an open object type may carry additional fields, and open is the default for new types if neither option is specified.</p>
3582<p>The following example creates a new object type called GleambookUser type. Since it is defined as (defaulting to) being an open type, instances will be permitted to contain more than what is specified in the type definition. The first four fields are essentially traditional typed name/value pairs (much like SQL fields). The friendIds field is a multiset of integers. The employment field is an array of instances of another named object type, EmploymentType.</p>
3583<div class="section">
3584<div class="section">
3585<h5><a name="Example"></a>Example</h5>
3586
3587<div class="source">
3588<div class="source">
3589<pre>CREATE TYPE GleambookUserType AS {
3590 id: int,
3591 alias: string,
3592 name: string,
3593 userSince: datetime,
3594 friendIds: {{ int }},
3595 employment: [ EmploymentType ]
3596};
3597</pre></div></div>
3598<p>The next example creates a new object type, closed this time, called MyUserTupleType. Instances of this closed type will not be permitted to have extra fields, although the alias field is marked as optional and may thus be NULL or MISSING in legal instances of the type. Note that the type of the id field in the example is UUID. This field type can be used if you want to have this field be an autogenerated-PK field. (Refer to the Datasets section later for more details on such fields.)</p></div>
3599<div class="section">
3600<h5><a name="Example"></a>Example</h5>
3601
3602<div class="source">
3603<div class="source">
3604<pre>CREATE TYPE MyUserTupleType AS CLOSED {
3605 id: uuid,
3606 alias: string?,
3607 name: string
3608};
3609</pre></div></div></div></div></div>
3610<div class="section">
3611<h3><a name="Datasets" id="Datasets"> Datasets</a></h3>
3612
3613<div class="source">
3614<div class="source">
3615<pre>DatasetSpecification ::= ( &lt;INTERNAL&gt; )? &lt;DATASET&gt; QualifiedName &quot;(&quot; QualifiedName &quot;)&quot; IfNotExists
3616 PrimaryKey ( &lt;ON&gt; Identifier )? ( &lt;HINTS&gt; Properties )?
3617 ( &quot;USING&quot; &quot;COMPACTION&quot; &quot;POLICY&quot; CompactionPolicy ( Configuration )? )?
3618 ( &lt;WITH&gt; &lt;FILTER&gt; &lt;ON&gt; Identifier )?
3619 |
3620 &lt;EXTERNAL&gt; &lt;DATASET&gt; QualifiedName &quot;(&quot; QualifiedName &quot;)&quot; IfNotExists &lt;USING&gt; AdapterName
3621 Configuration ( &lt;HINTS&gt; Properties )?
3622 ( &lt;USING&gt; &lt;COMPACTION&gt; &lt;POLICY&gt; CompactionPolicy ( Configuration )? )?
3623AdapterName ::= Identifier
3624Configuration ::= &quot;(&quot; ( KeyValuePair ( &quot;,&quot; KeyValuePair )* )? &quot;)&quot;
3625KeyValuePair ::= &quot;(&quot; StringLiteral &quot;=&quot; StringLiteral &quot;)&quot;
3626Properties ::= ( &quot;(&quot; Property ( &quot;,&quot; Property )* &quot;)&quot; )?
3627Property ::= Identifier &quot;=&quot; ( StringLiteral | IntegerLiteral )
3628FunctionSignature ::= FunctionOrTypeName &quot;@&quot; IntegerLiteral
3629PrimaryKey ::= &lt;PRIMARY&gt; &lt;KEY&gt; NestedField ( &quot;,&quot; NestedField )* ( &lt;AUTOGENERATED&gt; )?
3630CompactionPolicy ::= Identifier
3631</pre></div></div>
3632<p>The CREATE DATASET statement is used to create a new dataset. Datasets are named, multisets of object type instances; they are where data lives persistently and are the usual targets for SQL++ queries. Datasets are typed, and the system ensures that their contents conform to their type definitions. An Internal dataset (the default kind) is a dataset whose content lives within and is managed by the system. It is required to have a specified unique primary key field which uniquely identifies the contained objects. (The primary key is also used in secondary indexes to identify the indexed primary data objects.)</p>
3633<p>Internal datasets contain several advanced options that can be specified when appropriate. One such option is that 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, unlike other non-optional fields, a value for the auto-generated PK field should not be provided at insertion time by the user since each object&#x2019;s primary key field value will be auto-generated by the system.</p>
3634<p>Another advanced option, when creating an Internal dataset, is to specify the merge policy to control which of the underlying LSM storage components to be merged. (The system supports Log-Structured Merge tree based physical storage for Internal datasets.) Currently the system supports four different component merging policies that can be chosen per dataset: no-merge, constant, prefix, and correlated-prefix. The no-merge policy simply never merges disk components. The constant policy merges disk components when the number of components reaches a constant number k that can be configured by the user. The prefix policy relies on both component sizes and the number of components to decide which components to merge. 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 exists, the components in the sequence are merged together to form a single component. Finally, the correlated-prefix policy 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 correlated-prefix 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 system&#x2019;s default policy is the prefix policy except when there is a filter on a dataset, where the preferred policy for filters is the correlated-prefix.</p>
3635<p>Another advanced option shown in the syntax above, related to performance and mentioned above, is that a <b>filter</b> can optionally be created on a field to further optimize range queries with predicates on the filter&#x2019;s field. Filters allow some range queries to avoid searching all LSM components when the query conditions match the filter. (Refer to <a href="filters.html">Filter-Based LSM Index Acceleration</a> for more information about filters.)</p>
3636<p>An External dataset, in contrast to an Internal dataset, has data stored outside of the system&#x2019;s control. Files living in HDFS or in the local filesystem(s) of a cluster&#x2019;s nodes are currently supported. External dataset support allows SQL++ queries to treat foreign data as though it were stored in the system, making it possible to query &#x201c;legacy&#x201d; file data (e.g., Hive data) without having to physically import it. When defining an External dataset, an appropriate adapter type must be selected for the desired external data. (See the <a href="externaldata.html">Guide to External Data</a> for more information on the available adapters.)</p>
3637<p>The following example creates an Internal dataset for storing FacefookUserType objects. It specifies that their id field is their primary key.</p>
3638<div class="section">
3639<h4><a name="Example"></a>Example</h4>
3640
3641<div class="source">
3642<div class="source">
3643<pre>CREATE INTERNAL DATASET GleambookUsers(GleambookUserType) PRIMARY KEY id;
3644</pre></div></div>
3645<p>The next example creates another Internal dataset (the default kind when no dataset kind is specified) for storing MyUserTupleType objects. It specifies that the id field should be used as the primary key for the dataset. It also specifies that the id field is an auto-generated field, meaning that a randomly generated UUID value should be assigned to each incoming object by the system. (A user should therefore not attempt to provide a value for this field.) Note that the id field&#x2019;s declared type must be UUID in this case.</p></div>
3646<div class="section">
3647<h4><a name="Example"></a>Example</h4>
3648
3649<div class="source">
3650<div class="source">
3651<pre>CREATE DATASET MyUsers(MyUserTupleType) PRIMARY KEY id AUTOGENERATED;
3652</pre></div></div>
3653<p>The next example creates an External dataset for querying LineItemType objects. The choice of the <tt>hdfs</tt> adapter means that this dataset&#x2019;s data actually resides in HDFS. The example CREATE statement also 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>
3654<div class="section">
3655<h4><a name="Example"></a>Example</h4>
3656
3657<div class="source">
3658<div class="source">
3659<pre>CREATE EXTERNAL DATASET LineItem(LineItemType) USING hdfs (
3660 (&quot;hdfs&quot;=&quot;hdfs://HOST:PORT&quot;),
3661 (&quot;path&quot;=&quot;HDFS_PATH&quot;),
3662 (&quot;input-format&quot;=&quot;text-input-format&quot;),
3663 (&quot;format&quot;=&quot;delimited-text&quot;),
3664 (&quot;delimiter&quot;=&quot;|&quot;));
3665</pre></div></div></div>
3666<div class="section">
3667<h4><a name="Indices"></a>Indices</h4>
3668
3669<div class="source">
3670<div class="source">
3671<pre>IndexSpecification ::= &lt;INDEX&gt; Identifier IfNotExists &lt;ON&gt; QualifiedName
3672 &quot;(&quot; ( IndexField ) ( &quot;,&quot; IndexField )* &quot;)&quot; ( &quot;type&quot; IndexType &quot;?&quot;)?
3673 ( &lt;ENFORCED&gt; )?
3674IndexType ::= &lt;BTREE&gt; | &lt;RTREE&gt; | &lt;KEYWORD&gt; | &lt;NGRAM&gt; &quot;(&quot; IntegerLiteral &quot;)&quot;
3675</pre></div></div>
3676<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.</p>
3677<p>An indexed field is not required to be part of the datatype associated with a dataset if the dataset&#x2019;s datatype is declared as open <b>and</b> if the field&#x2019;s type is provided along with its name and if the <tt>ENFORCED</tt> keyword is specified at the end of the index definition. <tt>ENFORCING</tt> an open field introduces a check that makes sure that the actual type of the indexed field (if the optional field exists in the object) always matches this specified (open) field type.</p>
3678<p>The following example creates a btree index called gbAuthorIdx on the authorId field of the GleambookMessages dataset. This index can be useful for accelerating exact-match queries, range search queries, and joins involving the author-id field.</p></div>
3679<div class="section">
3680<h4><a name="Example"></a>Example</h4>
3681
3682<div class="source">
3683<div class="source">
3684<pre>CREATE INDEX gbAuthorIdx ON GleambookMessages(authorId) TYPE BTREE;
3685</pre></div></div>
3686<p>The following example creates an open btree index called gbSendTimeIdx on the (non-predeclared) sendTime field of the GleambookMessages dataset having datetime type. This index can be useful for accelerating exact-match queries, range search queries, and joins involving the sendTime field.</p></div>
3687<div class="section">
3688<h4><a name="Example"></a>Example</h4>
3689
3690<div class="source">
3691<div class="source">
3692<pre>CREATE INDEX gbSendTimeIdx ON GleambookMessages(sendTime: datetime?) TYPE BTREE ENFORCED;
3693</pre></div></div>
3694<p>The following example creates a btree index called crpUserScrNameIdx on screenName, a nested field residing within a object-valued user field in the ChirpMessages dataset. This index can be useful for accelerating exact-match queries, range search queries, and joins involving the nested screenName field. Such nested fields must be singular, i.e., one cannot index through (or on) an array-valued field.</p></div>
3695<div class="section">
3696<h4><a name="Example"></a>Example</h4>
3697
3698<div class="source">
3699<div class="source">
3700<pre>CREATE INDEX crpUserScrNameIdx ON ChirpMessages(user.screenName) TYPE BTREE;
3701</pre></div></div>
3702<p>The following example creates an rtree index called gbSenderLocIdx on the sender-location field of the GleambookMessages 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>
3703<div class="section">
3704<h4><a name="Example"></a>Example</h4>
3705
3706<div class="source">
3707<div class="source">
3708<pre>CREATE INDEX gbSenderLocIndex ON GleambookMessages(&quot;sender-location&quot;) TYPE RTREE;
3709</pre></div></div>
3710<p>The following example creates a 3-gram index called fbUserIdx on the name field of the GleambookUsers dataset. This index can be used to accelerate some similarity or substring maching queries on the name field. For details refer to the document on <a href="similarity.html#NGram_Index">similarity queries</a>.</p></div>
3711<div class="section">
3712<h4><a name="Example"></a>Example</h4>
3713
3714<div class="source">
3715<div class="source">
3716<pre>CREATE INDEX fbUserIdx ON GleambookUsers(name) TYPE NGRAM(3);
3717</pre></div></div>
3718<p>The following example creates a keyword index called fbMessageIdx on the message field of the GleambookMessages dataset. This keyword index can be used to optimize queries with token-based similarity predicates on the message field. For details refer to the document on <a href="similarity.html#Keyword_Index">similarity queries</a>.</p></div>
3719<div class="section">
3720<h4><a name="Example"></a>Example</h4>
3721
3722<div class="source">
3723<div class="source">
3724<pre>CREATE INDEX fbMessageIdx ON GleambookMessages(message) TYPE KEYWORD;
3725</pre></div></div></div></div>
3726<div class="section">
3727<h3><a name="Functions" id="Functions"> Functions</a></h3>
3728<p>The create function statement creates a <b>named</b> function that can then be used and reused in SQL++ queries. The body of a function can be any SQL++ expression involving the function&#x2019;s parameters.</p>
3729
3730<div class="source">
3731<div class="source">
3732<pre>FunctionSpecification ::= &quot;FUNCTION&quot; FunctionOrTypeName IfNotExists ParameterList &quot;{&quot; Expression &quot;}&quot;
3733</pre></div></div>
3734<p>The following is an example of a CREATE FUNCTION statement which is similar to our earlier DECLARE FUNCTION example. It differs from that example in that it results in a function that is persistently registered by name in the specified dataverse (the current dataverse being used, if not otherwise specified).</p>
3735<div class="section">
3736<div class="section">
3737<h5><a name="Example"></a>Example</h5>
3738
3739<div class="source">
3740<div class="source">
3741<pre>CREATE FUNCTION friendInfo(userId) {
3742 (SELECT u.id, u.name, len(u.friendIds) AS friendCount
3743 FROM GleambookUsers u
3744 WHERE u.id = userId)[0]
3745 };
3746</pre></div></div></div></div>
3747<div class="section">
3748<h4><a name="Removal"></a>Removal</h4>
3749
3750<div class="source">
3751<div class="source">
3752<pre>DropStatement ::= &quot;DROP&quot; ( &quot;DATAVERSE&quot; Identifier IfExists
3753 | &quot;TYPE&quot; FunctionOrTypeName IfExists
3754 | &quot;DATASET&quot; QualifiedName IfExists
3755 | &quot;INDEX&quot; DoubleQualifiedName IfExists
3756 | &quot;FUNCTION&quot; FunctionSignature IfExists )
3757IfExists ::= ( &quot;IF&quot; &quot;EXISTS&quot; )?
3758</pre></div></div>
3759<p>The DROP statement in SQL++ is the inverse of the CREATE statement. It can be used to drop dataverses, datatypes, datasets, indexes, and functions.</p>
3760<p>The following examples illustrate some uses of the DROP statement.</p>
3761<div class="section">
3762<h5><a name="Example"></a>Example</h5>
3763
3764<div class="source">
3765<div class="source">
3766<pre>DROP DATASET GleambookUsers IF EXISTS;
3767
3768DROP INDEX GleambookMessages.gbSenderLocIndex;
3769
3770DROP TYPE TinySocial2.GleambookUserType;
3771
3772DROP FUNCTION friendInfo@1;
3773
3774DROP DATAVERSE TinySocial;
3775</pre></div></div>
3776<p>When an artifact is dropped, it will be droppped from the current dataverse if none is specified (see the DROP DATASET example above) or from the specified dataverse (see the DROP TYPE example above) if one is specified by fully qualifying the artifact name in the DROP statement. When specifying an index to drop, the index name must be qualified by the dataset that it indexes. When specifying a function to drop, since SQL++ allows functions to be overloaded by their number of arguments, the identifying name of the function to be dropped must explicitly include that information. (<tt>friendInfo@1</tt> above denotes the 1-argument function named friendInfo in the current dataverse.)</p></div></div></div>
3777<div class="section">
3778<h3><a name="ImportExport_Statements"></a>Import/Export Statements</h3>
3779
3780<div class="source">
3781<div class="source">
3782<pre>LoadStatement ::= &lt;LOAD&gt; &lt;DATASET&gt; QualifiedName &lt;USING&gt; AdapterName Configuration ( &lt;PRE-SORTED&gt; )?
3783</pre></div></div>
3784<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 discussed earlier for 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, the file to be imported should not include that field in it.</p>
3785<p>The following example shows how to bulk load the GleambookUsers dataset from an external file containing data that has been prepared in ADM (Asterix Data Model) format.</p>
3786<div class="section">
3787<div class="section">
3788<h5><a name="Example"></a>Example</h5>
3789
3790<div class="source">
3791<div class="source">
3792<pre> LOAD DATASET GleambookUsers USING localfs
3793 ((&quot;path&quot;=&quot;127.0.0.1:///Users/bignosqlfan/tinysocialnew/gbu.adm&quot;),(&quot;format&quot;=&quot;adm&quot;));
3794</pre></div></div></div></div></div></div>
3795<div class="section">
3796<h2><a name="Modification_statements" id="Modification_statements">Modification statements</a></h2>
3797<div class="section">
3798<h3><a name="INSERTs"></a><a name="Inserts" id="Inserts">INSERTs</a></h3>
3799
3800<div class="source">
3801<div class="source">
3802<pre>InsertStatement ::= &lt;INSERT&gt; &lt;INTO&gt; QualifiedName Query
3803</pre></div></div>
3804<p>The SQL++ INSERT statement is used to insert new data into a dataset. The data to be inserted comes from a SQL++ query expression. This expression can be as simple as a constant expression, or in general it can be any legal SQL++ query. If the target dataset has an auto-generated primary key field, the insert statement should not include a value for that field in it. (The system will automatically extend the provided object with this additional field and a corresponding value.) Insertion will fail if the dataset already has data with the primary key value(s) being inserted.</p>
3805<p>Inserts are processed transactionally by the system. The transactional scope of each insert transaction is 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 will be a single, atomic transaction. If the query part returns multiple objects, each object being inserted will be treated as a separate tranaction. The following example illustrates a query-based insertion.</p>
3806<div class="section">
3807<div class="section">
3808<h5><a name="Example"></a>Example</h5>
3809
3810<div class="source">
3811<div class="source">
3812<pre>INSERT INTO UsersCopy (SELECT VALUE user FROM GleambookUsers user)
3813</pre></div></div></div></div></div>
3814<div class="section">
3815<h3><a name="UPSERTs"></a><a name="Upserts" id="Upserts">UPSERTs</a></h3>
3816
3817<div class="source">
3818<div class="source">
3819<pre>UpsertStatement ::= &lt;UPSERT&gt; &lt;INTO&gt; QualifiedName Query
3820</pre></div></div>
3821<p>The SQL++ UPSERT statement syntactically mirrors the INSERT statement discussed above. The difference lies in its semantics, which for UPSERT are &#x201c;add or replace&#x201d; instead of the INSERT &#x201c;add if not present, else error&#x201d; semantics. Whereas an INSERT can fail if another object already exists with the specified key, the analogous UPSERT will replace the previous object&#x2019;s value with that of the new object in such cases.</p>
3822<p>The following example illustrates a query-based upsert operation.</p>
3823<div class="section">
3824<div class="section">
3825<h5><a name="Example"></a>Example</h5>
3826
3827<div class="source">
3828<div class="source">
3829<pre>UPSERT INTO UsersCopy (SELECT VALUE user FROM GleambookUsers user)
3830</pre></div></div>
3831<p>*Editor&#x2019;s note: Upserts currently work in AQL but are not yet enabled (at the moment) in SQL++.</p></div></div></div>
3832<div class="section">
3833<h3><a name="DELETEs"></a><a name="Deletes" id="Deletes">DELETEs</a></h3>
3834
3835<div class="source">
3836<div class="source">
3837<pre>DeleteStatement ::= &lt;DELETE&gt; &lt;FROM&gt; QualifiedName ( ( &lt;AS&gt; )? Variable )? ( &lt;WHERE&gt; Expression )?
3838</pre></div></div>
3839<p>The SQL++ 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.</p>
3840<p>Deletes are processed transactionally by the system. The transactional scope of each delete transaction is 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 as a separate transaction.</p>
3841<p>The following examples illustrate single-object deletions.</p>
3842<div class="section">
3843<div class="section">
3844<h5><a name="Example"></a>Example</h5>
3845
3846<div class="source">
3847<div class="source">
3848<pre>DELETE FROM GleambookUsers user WHERE user.id = 8;
3849</pre></div></div></div>
3850<div class="section">
3851<h5><a name="Example"></a>Example</h5>
3852
3853<div class="source">
3854<div class="source">
3855<pre>DELETE FROM GleambookUsers WHERE id = 5;
3856</pre></div></div>
3857<h1><a name="Reserved_keywords" id="Reserved_keywords">Appendix 1. Reserved keywords</a></h1>
3858<p>All reserved keywords are listed in the following table:</p>
3859
3860<table border="0" class="table table-striped">
3861 <thead>
3862
3863<tr class="a">
3864
3865<th> </th>
3866
3867<th> </th>
3868
3869<th> </th>
3870
3871<th> </th>
3872
3873<th> </th>
3874
3875<th> </th>
3876 </tr>
3877 </thead>
3878 <tbody>
3879
3880<tr class="b">
3881
3882<td>AND </td>
3883
3884<td>ANY </td>
3885
3886<td>APPLY </td>
3887
3888<td>AS </td>
3889
3890<td>ASC </td>
3891
3892<td>AT </td>
3893 </tr>
3894
3895<tr class="a">
3896
3897<td>AUTOGENERATED </td>
3898
3899<td>BETWEEN </td>
3900
3901<td>BTREE </td>
3902
3903<td>BY </td>
3904
3905<td>CASE </td>
3906
3907<td>CLOSED </td>
3908 </tr>
3909
3910<tr class="b">
3911
3912<td>CREATE </td>
3913
3914<td>COMPACTION </td>
3915
3916<td>COMPACT </td>
3917
3918<td>CONNECT </td>
3919
3920<td>CORRELATE </td>
3921
3922<td>DATASET </td>
3923 </tr>
3924
3925<tr class="a">
3926
3927<td>COLLECTION </td>
3928
3929<td>DATAVERSE </td>
3930
3931<td>DECLARE </td>
3932
3933<td>DEFINITION </td>
3934
3935<td>DECLARE </td>
3936
3937<td>DEFINITION </td>
3938 </tr>
3939
3940<tr class="b">
3941
3942<td>DELETE </td>
3943
3944<td>DESC </td>
3945
3946<td>DISCONNECT </td>
3947
3948<td>DISTINCT </td>
3949
3950<td>DROP </td>
3951
3952<td>ELEMENT </td>
3953 </tr>
3954
3955<tr class="a">
3956
3957<td>ELEMENT </td>
3958
3959<td>EXPLAIN </td>
3960
3961<td>ELSE </td>
3962
3963<td>ENFORCED </td>
3964
3965<td>END </td>
3966
3967<td>EVERY </td>
3968 </tr>
3969
3970<tr class="b">
3971
3972<td>EXCEPT </td>
3973
3974<td>EXIST </td>
3975
3976<td>EXTERNAL </td>
3977
3978<td>FEED </td>
3979
3980<td>FILTER </td>
3981
3982<td>FLATTEN </td>
3983 </tr>
3984
3985<tr class="a">
3986
3987<td>FOR </td>
3988
3989<td>FROM </td>
3990
3991<td>FULL </td>
3992
3993<td>FUNCTION </td>
3994
3995<td>GROUP </td>
3996
3997<td>HAVING </td>
3998 </tr>
3999
4000<tr class="b">
4001
4002<td>HINTS </td>
4003
4004<td>IF </td>
4005
4006<td>INTO </td>
4007
4008<td>IN </td>
4009
4010<td>INDEX </td>
4011
4012<td>INGESTION </td>
4013 </tr>
4014
4015<tr class="a">
4016
4017<td>INNER </td>
4018
4019<td>INSERT </td>
4020
4021<td>INTERNAL </td>
4022
4023<td>INTERSECT </td>
4024
4025<td>IS </td>
4026
4027<td>JOIN </td>
4028 </tr>
4029
4030<tr class="b">
4031
4032<td>KEYWORD </td>
4033
4034<td>LEFT </td>
4035
4036<td>LETTING </td>
4037
4038<td>LET </td>
4039
4040<td>LIKE </td>
4041
4042<td>LIMIT </td>
4043 </tr>
4044
4045<tr class="a">
4046
4047<td>LOAD </td>
4048
4049<td>NODEGROUP </td>
4050
4051<td>NGRAM </td>
4052
4053<td>NOT </td>
4054
4055<td>OFFSET </td>
4056
4057<td>ON </td>
4058 </tr>
4059
4060<tr class="b">
4061
4062<td>OPEN </td>
4063
4064<td>OR </td>
4065
4066<td>ORDER </td>
4067
4068<td>OUTER </td>
4069
4070<td>OUTPUT </td>
4071
4072<td>PATH </td>
4073 </tr>
4074
4075<tr class="a">
4076
4077<td>POLICY </td>
4078
4079<td>PRE-SORTED </td>
4080
4081<td>PRIMARY </td>
4082
4083<td>RAW </td>
4084
4085<td>REFRESH </td>
4086
4087<td>RETURN </td>
4088 </tr>
4089
4090<tr class="b">
4091
4092<td>RTREE </td>
4093
4094<td>RUN </td>
4095
4096<td>SATISFIES </td>
4097
4098<td>SECONDARY </td>
4099
4100<td>SELECT </td>
4101
4102<td>SET </td>
4103 </tr>
4104
4105<tr class="a">
4106
4107<td>SOME </td>
4108
4109<td>TEMPORARY </td>
4110
4111<td>THEN </td>
4112
4113<td>TYPE </td>
4114
4115<td>UNKNOWN </td>
4116
4117<td>UNNEST </td>
4118 </tr>
4119
4120<tr class="b">
4121
4122<td>UPDATE </td>
4123
4124<td>USE </td>
4125
4126<td>USING </td>
4127
4128<td>VALUE </td>
4129
4130<td>WHEN </td>
4131
4132<td>WHERE </td>
4133 </tr>
4134
4135<tr class="a">
4136
4137<td>WITH </td>
4138
4139<td>WRITE </td>
4140
4141<td> </td>
4142
4143<td> </td>
4144
4145<td> </td>
4146
4147<td> </td>
4148 </tr>
4149 </tbody>
4150</table></div></div></div></div>
4151 </div>
4152 </div>
4153 </div>
4154
4155 <hr/>
4156
4157 <footer>
4158 <div class="container-fluid">
4159 <div class="row span12">Copyright &copy; 2017
4160 <a href="https://www.apache.org/">The Apache Software Foundation</a>.
4161 All Rights Reserved.
4162
4163 </div>
4164
4165 <?xml version="1.0" encoding="UTF-8"?>
4166<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
4167 feather logo, and the Apache AsterixDB project logo are either
4168 registered trademarks or trademarks of The Apache Software
4169 Foundation in the United States and other countries.
4170 All other marks mentioned may be trademarks or registered
4171 trademarks of their respective owners.</div>
4172
4173
4174 </div>
4175 </footer>
4176 </body>
4177</html>