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