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