-
Notifications
You must be signed in to change notification settings - Fork 7
/
ADQL.tex
3051 lines (2501 loc) · 104 KB
/
ADQL.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[11pt,a4paper]{ivoa}
\input tthdefs
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{mathtools}
\usepackage{makecell}
\usepackage{listings}
\lstloadlanguages{XML,sh}
\lstset{flexiblecolumns=true,numberstyle=\small,numbers=left}
\usepackage{hyperref}
% Macros for referring to IVOA standards and notes.
\input{ivoa-cite.tex}
\title{Astronomical Data Query Language}
\ivoagroup{Data Access Layer Working Group}
%\author[http://wiki.ivoa.net/twiki/bin/view/IVOA/IvoaDAL]{The IVOA Data Access Layer (DAL) and Virtual Observatory Query Language (VOQL) working group members}
\author{Markus Demleitner}
\author{Pat Dowler}
\author{Jeff Lusted}
\author{Grégory Mantelet}
\author{Dave Morris}
\author{Maria A. Nieto-Santisteban}
\author{Masatoshi Ohishi}
\author{William O’Mullane}
\author{Inaki Ortiz}
\author{Pedro Osuna}
\author{Yuji Shirasaki}
\author{Alexander Szalay}
\editor[http://wiki.ivoa.net/twiki/bin/view/IVOA/GregoryMantelet]{Grégory Mantelet}
\editor[http://wiki.ivoa.net/twiki/bin/view/IVOA/DaveMorris]{Dave Morris}
\previousversion[http://www.ivoa.net/Documents/ADQL/2.0]{ADQL-2.0}
\begin{document}
\begin{abstract}
This document describes the Astronomical Data Query Language (ADQL).
ADQL has been developed based on SQL92.
This document describes the subset of the SQL grammar supported by ADQL.
Special restrictions and extensions to SQL92 have been defined in order
to support generic and astronomy specific operations.
\end{abstract}
\section*{Acknowledgements}
The authors would like to acknowledge the contributors to this standard by
the members of the IVOA Data Access Layer (DAL) and Virtual Observatory
Query Language (VOQL) working groups.
\section*{Conformance-related definitions}
The words ``MUST'', ``SHALL'', ``SHOULD'', ``MAY'', ``RECOMMENDED'' and
``OPTIONAL'' (in upper or lower case) used in this document are to be
interpreted as described in the
\href{https://www.ietf.org/}{Internet Engineering Task Force (IETF)}
standard, \citet{std:RFC2119}.
The \emph{Virtual Observatory (VO)} is a general term for a collection of
federated resources that can be used to conduct astronomical research,
education and outreach. The
\href{http://www.ivoa.net}{International Virtual Observatory Alliance (IVOA)}
is a global collaboration of separately funded
projects to develop standards and infrastructure that enable VO applications.
\clearpage % section cut
\section{Introduction}
\label{sec:introduction}
The Astronomical Data Query Language (ADQL) is the language used by the
IVOA to represent astronomy queries posted to VO services.
The IVOA has developed several standardized protocols to access astronomical
data, e.g., Simple Image Access (SIA) protocol and Simple Spectral Access (SSA)
protocol for image and spectral data respectively.
These protocols might be satisfied using a single
table query. However, different VO services have different needs in terms
of query complexity and ADQL arises in this context.
ADQL is based on the Structured Query Language (SQL), especially on SQL
92 \citep{std:SQL1992}\footnote{see also
\url{https://en.wikipedia.org/wiki/SQL-92},
\url{http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt}.}.
The VO has a number of tabular data sets and many of them are stored in
relational databases, making SQL a convenient access means. A subset of the SQL
grammar has been extended to support queries that are specific to astronomy.
This specification provides a complete definition of the ADQL through a single
Backus Naur Form (BNF) based language definition. It includes both mandatory
and optional features. This BNF aims to help implementing ADQL core
features as well as the defined optional ones.
\clearpage % to have the VOArch figure along its description and subsection title
\subsection{Role within the VO architecture}
\label{sec:role}
\begin{figure}[th]
\centering
\includegraphics[width=0.9\textwidth]{role_diagram.pdf}
\caption{Architecture diagram for this document}
\label{fig:archdiag}
\end{figure}
Figure \ref{fig:archdiag} shows the role this document plays within the
IVOA architecture \VOArch{}.
It is the only mandatory query language within the Table Access Protocol
\TAPSpec{}; it is thus an important element in
enabling interoperable queries against remote astronomical databases.
TAP services communicate their metadata, and hence metadata about the
specific version of ADQL they implement, in capabilities of the
VOResource type \xmlel{tr:TableAccess} defined in TAPRegExt
\citep{2012ivoa.spec.0827D}. Within these TAPRegExt records, the
language defined in this document, ADQL 2.1, has the IVOA identifier $$
\hbox{\nolinkurl{ivo://ivoa.net/std/adql#v2.1}}.$$ Clients should note
that IVOA identifiers need to be compared case-insensitively.
DALI \citep{2017ivoa.spec.0517D} defines conventions for many of the
types ADQL is talking about, in particular geometry objects, timestamps,
and intervals.
\subsection{Extended functionality}
\label{sec:extending}
% Requested by Alberto Micol at ESO.
% Explicit permission for services that provide additional functionality.
This document defines the minimum set of functions, operators and data types
that a service MUST implement in order to register as a service that
implements this version of the ADQL specification. Optional features defined in
this specification MAY be implemented. However, if a service does provide one of
them, it MUST be implemented according to this specification.
Of course, service implementations are free to extend this functionality by
providing additional functions, operators or data types beyond those defined in
this specification, as long as the extended functionality does not conflict
with anything defined in this specification.
\clearpage % section cut
\section{Language structure}
\label{sec:language}
This section describes the ADQL language structure. We will define in
subsequent sections the syntax for the special characters, reserved and non-
reserved words, identifiers and literals and then, finally, the syntax for
the query expression.
The formal notation for syntax of computing languages is often expressed
in BNF. This syntax is used by popular tools for
producing parsers. Appendix A to this document provides the full BNF grammar
for ADQL. The following conventions are used through this document:
\begin{itemize}
\item Optional items are enclosed in meta symbols \verb:[: and \verb:]:
\item A group of items is enclosed in meta symbols \verb:{: and \verb:}:
\item Repetitive item (zero or more times) are followed by \verb:...:
\item Terminal symbols are enclosed by \verb:<: and \verb:>:
\item Terminals of meta-symbol characters (\verb!:=[]{}<>."!) are surrounded by quotes (\verb:“:) to distinguish them from meta-symbols
\item Case-insensitive unless otherwise stated.
\end{itemize}
\subsection{Characters, keywords, identifiers and literals}
\subsubsection{Characters}
\label{sec:characters}
The language allows simple Latin letters (lower and upper case, i.e.
\verb:{aA-zZ}):, digits (\verb:{0-9}:) and the following special characters:
\begin{itemize}
\item space
\item single quote \verb:':
\item double quote \verb:":
\item percent \verb:%:
\item left and right parenthesis \verb:():
\item asterisk \verb:*:
\item plus sign \verb:+:
\item minus sign \verb:-:
\item comma \verb:,:
\item period \verb:.:
\item solidus \verb:/:
\item colon \verb.:.
\item semicolon \verb:;:
\item less than operator \verb:<:
\item equals operator \verb:=:
\item greater than operator \verb:>:
\item underscore \verb:_:
\item ampersand \verb:&:
\item question mark \verb:?:
\item circumflex \verb:^:
\item tilde \verb:~:
\item vertical bar \verb:|:
\end{itemize}
\subsubsection{Keywords and identifiers}
\label{sec:keywords}
Besides the character set, the language provides a list of reserved keywords
plus the syntax description for regular identifiers.
A reserved keyword has a special meaning in ADQL and cannot be used as
an identifier unless it is isolated using the ADQL escape syntax defined
in \SectionRef{sec:adql.escape}.
The ADQL specification extends the list of SQL92 reserved keywords to accommodate
those useful for astronomical purposes and/or present in a subset of vendor
specific languages only (e.g. \verb:TOP:).
Although the following lists are all in UPPERCASE, the matching of keywords
is case-insensitive.
\subsubsection{SQL reserved keywords}
\label{sec:adql.keywords}
\begin{raggedright}
\noindent
\texttt{ABSOLUTE,} \texttt{ACTION,} \texttt{ADD,} \texttt{ALL,}
\texttt{ALLOCATE,} \texttt{ALTER,} \texttt{AND,} \texttt{ANY,}
\texttt{ARE,} \texttt{AS,} \texttt{ASC,} \texttt{ASSERTION,}
\texttt{AT,} \texttt{AUTHORIZATION,} \texttt{AVG,} \texttt{BEGIN,}
\texttt{BETWEEN,} \texttt{BIT,} \texttt{BIT\_LENGTH,} \texttt{BOTH,}
\texttt{BY,} \texttt{CASCADE,} \texttt{CASCADED,} \texttt{CASE,}
\texttt{CAST,} \texttt{CATALOG,} \texttt{CHAR,} \texttt{CHARACTER,}
\texttt{CHAR\_LENGTH,} \texttt{CHARACTER\_LENGTH,} \texttt{CHECK,}
\texttt{CLOSE,} \texttt{COALESCE,} \texttt{COLLATE,}
\texttt{COLLATION,} \texttt{COLUMN,} \texttt{COMMIT,}
\texttt{CONNECT,} \texttt{CONNECTION,} \texttt{CONSTRAINT,}
\texttt{CONSTRAINTS,} \texttt{CONTINUE,} \texttt{CONVERT,}
\texttt{CORRESPONDING,} \texttt{COUNT,} \texttt{CREATE,}
\texttt{CROSS,} \texttt{CURRENT,} \texttt{CURRENT\_DATE,}
\texttt{CURRENT\_TIME,} \texttt{CURRENT\_TIMESTAMP,}
\texttt{CURRENT\_USER,} \texttt{CURSOR,} \texttt{DATE,} \texttt{DAY,}
\texttt{DEALLOCATE,} \texttt{DECIMAL,} \texttt{DECLARE,}
\texttt{DEFAULT,} \texttt{DEFERRABLE,} \texttt{DEFERRED,}
\texttt{DELETE,} \texttt{DESC,} \texttt{DESCRIBE,}
\texttt{DESCRIPTOR,} \texttt{DIAGNOSTICS,} \texttt{DISCONNECT,}
\texttt{DISTINCT,} \texttt{DOMAIN,} \texttt{DOUBLE,} \texttt{DROP,}
\texttt{ELSE,} \texttt{END,} \texttt{END-EXEC,} \texttt{ESCAPE,}
\texttt{EXCEPT,} \texttt{EXCEPTION,} \texttt{EXEC,} \texttt{EXECUTE,}
\texttt{EXISTS,} \texttt{EXTERNAL,} \texttt{EXTRACT,} \texttt{FALSE,}
\texttt{FETCH,} \texttt{FIRST,} \texttt{FLOAT,} \texttt{FOR,}
\texttt{FOREIGN,} \texttt{FOUND,} \texttt{FROM,} \texttt{FULL,}
\texttt{GET,} \texttt{GLOBAL,} \texttt{GO,} \texttt{GOTO,}
\texttt{GRANT,} \texttt{GROUP,} \texttt{HAVING,} \texttt{HOUR,}
\texttt{IDENTITY,} \texttt{IMMEDIATE,} \texttt{IN,}
\texttt{INDICATOR,} \texttt{INITIALLY,} \texttt{INNER,}
\texttt{INPUT,} \texttt{INSENSITIVE,} \texttt{INSERT,} \texttt{INT,}
\texttt{INTEGER,} \texttt{INTERSECT,} \texttt{INTERVAL,}
\texttt{INTO,} \texttt{IS,} \texttt{ISOLATION,} \texttt{JOIN,}
\texttt{KEY,} \texttt{LANGUAGE,} \texttt{LAST,} \texttt{LEADING,}
\texttt{LEFT,} \texttt{LEVEL,} \texttt{LIKE,} \texttt{LOCAL,} \texttt{LOWER,}
\texttt{MATCH,} \texttt{MAX,} \texttt{MIN,}
\texttt{MINUTE,} \texttt{MODULE,} \texttt{MONTH,} \texttt{NAMES,}
\texttt{NATIONAL,} \texttt{NATURAL,} \texttt{NCHAR,} \texttt{NEXT,}
\texttt{NO,} \texttt{NOT,} \texttt{NULL,} \texttt{NULLIF,}
\texttt{NUMERIC,} \texttt{OCTET\_LENGTH,} \texttt{OF,} \texttt{ON,}
\texttt{ONLY,} \texttt{OPEN,} \texttt{OPTION,} \texttt{OR,}
\texttt{ORDER,} \texttt{OUTER,} \texttt{OUTPUT,} \texttt{OVERLAPS,}
\texttt{PAD,} \texttt{PARTIAL,} \texttt{POSITION,}
\texttt{PRECISION,} \texttt{PREPARE,} \texttt{PRESERVE,}
\texttt{PRIMARY,} \texttt{PRIOR,} \texttt{PRIVILEGES,}
\texttt{PROCEDURE,} \texttt{PUBLIC,} \texttt{READ,} \texttt{REAL,}
\texttt{REFERENCES,} \texttt{RELATIVE,} \texttt{RESTRICT,}
\texttt{REVOKE,} \texttt{RIGHT,} \texttt{ROLLBACK,} \texttt{ROWS,}
\texttt{SCHEMA,} \texttt{SCROLL,} \texttt{SECOND,} \texttt{SECTION,}
\texttt{SELECT,} \texttt{SESSION,} \texttt{SESSION\_USER,}
\texttt{SET,} \texttt{SIZE,} \texttt{SMALLINT,} \texttt{SOME,}
\texttt{SPACE,} \texttt{SQL,} \texttt{SQLCODE,} \texttt{SQLERROR,}
\texttt{SQLSTATE,} \texttt{SUBSTRING,} \texttt{SUM,}
\texttt{SYSTEM\_USER,} \texttt{TABLE,} \texttt{TEMPORARY,}
\texttt{THEN,} \texttt{TIME,} \texttt{TIMESTAMP,}
\texttt{TIMEZONE\_HOUR,} \texttt{TIMEZONE\_MINUTE,} \texttt{TO,}
\texttt{TRAILING,} \texttt{TRANSACTION,} \texttt{TRANSLATE,}
\texttt{TRANSLATION,} \texttt{TRIM,} \texttt{TRUE,} \texttt{UNION,}
\texttt{UNIQUE,} \texttt{UNKNOWN,} \texttt{UPDATE,} \texttt{UPPER,}
\texttt{USAGE,} \texttt{USER,} \texttt{USING,} \texttt{VALUE,}
\texttt{VALUES,} \texttt{VARCHAR,} \texttt{VARYING,} \texttt{VIEW,}
\texttt{WHEN,} \texttt{WHENEVER,} \texttt{WHERE,} \texttt{WITH,}
\texttt{WORK,} \texttt{WRITE,} \texttt{YEAR,} \texttt{ZONE}
\end{raggedright}
\subsubsection{ADQL reserved keywords}
\label{sec:adql.reswords}
\begin{raggedright}
\paragraph{Mathematical functions and operators}
\texttt{ABS,} \texttt{ACOS,} \texttt{ASIN,} \texttt{ATAN,}
\texttt{ATAN2,} \texttt{CEILING,} \texttt{COS,} \texttt{COT,}
\texttt{DEGREES,} \texttt{EXP,} \texttt{FLOOR,} \texttt{LOG,}
\texttt{LOG10,} \texttt{MOD,} \texttt{PI,} \texttt{POWER,}
\texttt{RADIANS,} \texttt{RAND,} \texttt{ROUND,} \texttt{SIN,}
\texttt{SQRT,} \texttt{TAN,} \texttt{TRUNCATE}
\end{raggedright}
\begin{raggedright}
\paragraph{Geometric functions and operators}
\texttt{AREA,} \texttt{BOX,} \texttt{CENTROID,} \texttt{CIRCLE,}
\texttt{CONTAINS,} \texttt{COORD1,} \texttt{COORD2,}
\texttt{COORDSYS,} \texttt{DISTANCE,} \texttt{INTERSECTS,}
\texttt{POINT,} \texttt{POLYGON,} \texttt{REGION}
\end{raggedright}
\begin{raggedright}
\paragraph{CAST function and datatypes}
\texttt{BIGINT}
\end{raggedright}
\begin{raggedright}
\paragraph{String functions and operators}
\texttt{ILIKE}
\end{raggedright}
\begin{raggedright}
\paragraph{Conversion functions}
\texttt{IN\_UNIT}
\end{raggedright}
\begin{raggedright}
\paragraph{Cardinality}
\texttt{OFFSET} \texttt{TOP}
\end{raggedright}
\subsubsection{Identifiers}
\label{sec:adql.identifiers}
Identifiers MUST begin with a letter
\verb:{aA-zZ}:, subsequent characters MAY be letters, underscores or
digits \verb:{0-9}: as follows:
\begin{verbatim}
<Latin_letter>... [{ <digit> | <Latin_letter> | <underscore> }...]
\end{verbatim}
\subsubsection{Delimited identifiers}
\label{sec:adql.escape}
To address reserved keyword and special character conflicts the ADQL language
provides a way to escape a non-compliant identifier by using the double
quote character \verb:": as a delimiter.
For example, to use the reserved word SIZE as a column name, in upper or lower case,
it must be isolated using double quotes.
\begin{itemize}
\item \verb:size: -- Invalid column name
\item \verb:"size": -- Valid column name
\end{itemize}
\subsubsection{Case sensitivity}
\label{sec:adql.case}
In addition to isolating keyword conflicts and special characters,
the double quote escape syntax also denotes case sensitivity.
Without double quotes, the following identifiers are all equivalent:
\begin{verbatim}
alpha == Alpha == ALPHA
\end{verbatim}
When escaped using double quotes, the same set of identifiers are not equivalent:
\begin{verbatim}
"alpha" != "Alpha" != "ALPHA"
\end{verbatim}
Data providers should avoid defining column names using delimited identifiers.
Indeed, when identifiers are mixed-case or are not written in the default case
(uppercase by SQL92, lowercase in most modern RDBMSes), users must use delimited
identifiers. This, in turn, may lead to interoperability problems between
services offering the same table, or to failing backwards compatibility when
moving between backend RDBMSes.
\subsubsection{Literals}
\label{sec:literals}
String literals are expressed as a character expression delimited by single
quotes.
\begin{verbatim}
<character_string_literal> ::=
<quote> [ <character_representation>... ] <quote>
\end{verbatim}
A single quote inside a string literal must be escaped with a second single
quote. For instance:
\begin{verbatim}
'Earth''s satellite is the Moon.'
\end{verbatim}
Numeric literals are expressed as an exact decimal value, e.g. \verb:12: or
\verb:12.3:, or a floating point number with an exponent, e.g. \verb:12.3E4:.
\begin{verbatim}
<signed_numeric_literal> ::= [<sign>] <unsigned_numeric_literal>
<unsigned_numeric_literal> ::=
<exact_numeric_literal>
| <approximate_numeric_literal>
<exact_numeric_literal> ::=
<unsigned_decimal> [<period> [<unsigned_decimal>]]
| <period><unsigned_decimal>
<approximate_numeric_literal> ::= <mantissa> E <exponent>
<mantissa> ::= <exact_numeric_literal>
<exponent> ::= <signed_decimal>
<signed_decimal> ::= [<sign>] <unsigned_decimal>
<unsigned_decimal> ::= <digit>...
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<sign> ::= <plus_sign> | <minus_sign>
\end{verbatim}
\subsubsection{Whitespace}
\label{sec:whitespace}
The rules on where whitespace is allowed and required are as in SQL-92;
essentially, any \verb:<token>: may be followed by a \verb:<separator>:.
\subsubsection{Comments}
\label{sec:comments}
As in SQL-92, comments are supported in ADQL. A comment is syntactically legal
wherever whitespace is legal and can then stand in for that whitespace. It
starts with a double minus sign and ends with the end of line.
\begin{verbatim}
<comment> ::= <comment_introducer> [ <comment_character>... ] <newline>
<comment_character> ::= <nonquote_character> | <quote>
<comment_introducer> ::= <minus_sign><minus_sign> [<minus_sign>...]
\end{verbatim}
Comments are not expected to be interpreted by ADQL parsers.
\clearpage % to keep the entire verbatim block for query syntax on the same page
\subsection{Query syntax}
\label{sec:syntax}
A more detailed definition of the \verb:SELECT: statement is given by the \verb:<query_specification>:
construct defined in \AppendixRef{sec:grammar}.
A simplified syntax for the \verb:SELECT: statement follows, showing the main constructs for
the query specification:
\begin{verbatim}
[ WITH { cte_name AS ( SELECT ... ) }, ... ]
SELECT
[ ALL | DISTINCT ]
[ TOP unsigned_decimal ]
{
*
| { value_expression [ [AS] column_name ] }, ...
}
FROM {
{
table_name [ [AS] identifier ]
| ( SELECT ...) [AS] identifier
| table_reference [NATURAL]
[ INNER | { LEFT | RIGHT | FULL [OUTER] } ]
JOIN table_reference
[ON search_condition | USING ( column_name,...) ]
},
...
}
[ WHERE search_condition ]
[ GROUP BY group_by_term, ... ]
[ HAVING search_condition ]
[ {
{ UNION | INTERSECT | EXCEPT } [ALL]
SELECT ...
}
...
]
[ ORDER BY { order_by_expression } [ ASC | DESC ], ... ]
[ OFFSET unsigned_decimal ]
\end{verbatim}
The \verb:SELECT: statement defines a query to apply to a set of tables specified
in the \verb:FROM: clause. As a result of this query, a subset of the tables
is returned.
The order of the rows MAY be arbitrary unless an \verb:ORDER BY: clause is specified.
A \verb:TOP: clause may be specified to limit the number of rows returned.
An \verb:OFFSET: clause may be specified to skip a number of rows at the start
of the results.
If both \verb:TOP: and \verb:OFFSET: are used together then \verb:OFFSET: is applied
first followed by \verb:TOP: \SectionSee{sec:offset}.
The order of the columns in the query results SHALL be the same as the
order specified in the selection list,
%or the order defined in the original table if
unless an asterisk is specified.
\subsubsection{Subqueries}
\label{sec:subqueries}
%TBD - cosmopterix tests for this
Table subqueries may be used by predicates such as \verb:IN: and \verb:EXISTS:
in the \verb:WHERE: clause of a query:
\begin{verbatim}
SELECT alpha_source.id
FROM alpha_source
WHERE alpha_sourceid >=5
AND alpha_sourceid IN (
SELECT id
FROM alpha_source
WHERE id < 10
)
\end{verbatim}
Table subqueries may be used for declaring derived tables in the \verb:FROM: clause
of a query:
\begin{verbatim}
SELECT alpha_source.id
FROM alpha_source,
(
SELECT alpha_source.id
FROM alpha_source
WHERE id < 10
) AS subsample
WHERE alpha_source.id >=5
AND alpha_source.id = subsample.id
\end{verbatim}
If supported by the implementation, table subqueries may be used for declaring
named result sets in the \verb:WITH: clause of the main query:
\begin{verbatim}
WITH subsample AS (
SELECT alpha_source.id
FROM alpha_source
WHERE id < 10
)
SELECT alpha_source.id
FROM alpha_source
INNER JOIN subsample USING(id)
WHERE alpha_source.id >= 5
\end{verbatim}
\subsubsection{Joins}
\label{sec:joins}
%TBD - cosmopterix tests for this
ADQL supports \verb:INNER: and \verb:OUTER:
(\verb:LEFT:, \verb:RIGHT: and \verb:FULL:) joins. If no type is specified, the
default is \verb:INNER:. All of these can be \verb:NATURAL: or not.
%REMOVED: The join condition does not support embedded sub joins.
%REASON: The BNF allows nested JOINs.
\subsubsection{Search condition}
\label{sec:search}
A search condition may be part of other clauses including \verb:JOIN:, \verb:HAVING: and \verb:WHERE:.
A search condition may contain the standard logical operators, \verb:AND:, \verb:OR: and \verb:NOT:.
A search condition may contain the following predicates:
\begin{itemize}
\item Standard comparison operators: \verb:=:, \verb:!=:, \verb:<>:, \verb:<:, \verb:>:, \verb:<=:, \verb:>=:
\item Range comparison: \verb:BETWEEN:
\item Membership check: \verb:IN:
\item Case-sensitive string comparison: \verb:LIKE:
\item Null value checks: \verb:IS NULL:, \verb:IS NOT NULL:
\item Non-empty subquery check: \verb:EXISTS:
\end{itemize}
In addition, some service implementations may also support the optional \verb:ILIKE:
case-insensitive string comparison operator, defined in \SectionRef{sec:string.functions.ilike}.
\begin{itemize}
\item \verb:ILIKE:
\end{itemize}
\subsection{Mathematical and Trigonometrical Functions}
\label{sec:math.functions}
ADQL declares a list of reserved keywords \SectionSee{sec:keywords} which include
the mathematical and trigonometrical function names. Their syntax,
usage and description are detailed in tables~\ref{table:math.functions}
and \ref{table:trig.functions}.
\begin{table}[tpb]\footnotesize
\begin{tabular}{|p{0.20\textwidth}|p{0.125\textwidth}|p{0.125\textwidth}|p{0.55\textwidth}|}
\hline
\textbf{Name} &
\textbf{Argument \newline datatype} &
\textbf{Return \newline datatype} &
\textbf{Description}
\tabularnewline
\hline
abs(x) &
\textit{x} double &
double &
Returns the absolute value of \textit{x}.
\tabularnewline
\hline
ceiling(x) &
\textit{x} double &
double &
Returns the smallest integer that is not less than \textit{x}.
\tabularnewline
\hline
degrees(x) &
\textit{x} double &
double &
Converts the angle \textit{x} from radians to degrees.
\tabularnewline
\hline
exp(x) &
\textit{x} double &
double &
Returns Euler’s number \textit{e} raised to the power of \textit{x}.
\tabularnewline
\hline
floor(x) &
\textit{x} double &
double &
Returns the largest integer that is not greater than \textit{x}.
\tabularnewline
\hline
log(x) &
\textit{x} double &
double &
Returns the natural logarithm (base \textit{e}) of \textit{x}. The value of \textit{x} must be greater than zero.
\tabularnewline
\hline
log10(x) &
\textit{x} double &
double &
Returns the base 10 logarithm of \textit{x}. The value of \textit{x} must be greater than zero.
\tabularnewline
\hline
mod(x,y) &
\textit{x} double,
\newline
\textit{y} double &
double &
Returns the remainder \textit{r} of \textit{x/y} as a double,
where:
\begin{itemize}
\item \textit{r} has the same sign as \textit{x}
\item |\textit{r}| is less than |\textit{y}|
\item \textit{x} = (\textit{f} * \textit{y}) + \textit{r} for a given integer \textit{f}
\end{itemize}
\tabularnewline
\hline
pi() &
&
double &
The numeric constant \(\pi\).
\tabularnewline
\hline
power(x,y) &
\textit{x} double,
\newline
\textit{y} double &
double &
Returns the value of \textit{x} raised to the power of \textit{y}.
\tabularnewline
\hline
radians(x) &
\textit{x} double &
double &
Converts the angle \textit{x} from degrees to radians.
\tabularnewline
\hline
sqrt(x) &
\textit{x} double &
double &
Returns the positive square root of \textit{x}.
\tabularnewline
\hline
rand(x) &
\textit{x} double &
double &
Returns a random value between 0.0 and 1.0.
The optional argument, \textit{x}, originally intended to provide a random seed,
has undefined semantics. Query writers are advised to omit this argument.
\tabularnewline
\hline
round(x,n) &
\textit{x} double,
\newline
\textit{n} integer &
double &
Rounds \textit{x} to \textit{n} decimal places.
The integer \textit{n} is optional and defaults to 0 if not specified.
A negative value of \textit{n} will round to the left of the decimal point.
\tabularnewline
\hline
truncate(x, n) &
\textit{x} double
\newline
\textit{n} integer &
double &
Truncates \textit{x} to \textit{n} decimal places.
The integer \textit{n} is optional and defaults to 0 if not specified.
\tabularnewline
\hline
\end{tabular}
\caption{Mathematical functions}
\label{table:math.functions}
\end{table}
\begin{table}[tbh]\footnotesize
\begin{tabular}{|p{0.20\textwidth}|p{0.125\textwidth}|p{0.125\textwidth}|p{0.55\textwidth}|}
\hline
\textbf{Name} &
\textbf{Argument \newline datatype} &
\textbf{Return \newline datatype} &
\textbf{Description}
\tabularnewline
\hline
acos(x) &
\textit{x} double &
double &
Returns the arc cosine of \textit{x}, in the range of 0 through \(\pi\) radians. The absolute value of \textit{x} must be less than or equal to 1.0.
\tabularnewline
\hline
asin(x) &
\textit{x} double &
double &
Returns the arc sine of \textit{x}, in the range of -\(\pi\)/2 through \(\pi\)/2 radians. The absolute value of \textit{x} must be less than or equal to 1.0.
\tabularnewline
\hline
atan(x) &
\textit{x} double &
double &
Returns the arc tangent of \textit{x} , in the range of -\(\pi\)/2 through \(\pi\)/2 radians.
\tabularnewline
\hline
atan2(y,x) &
\textit{x} double,
\newline
\textit{y} double &
double &
Converts rectangular coordinates \textit{x,y} to polar angle. It computes the arc tangent of \textit{y/x} in the range of –\(\pi\) through \(\pi\) radians.
\tabularnewline
\hline
cos(x) &
\textit{x} double &
double &
Returns the cosine of the angle \textit{x} in radians, in the range of -1.0 through 1.0.
\tabularnewline
\hline
sin(x) &
\textit{x} double &
double &
Returns the sine of the angle \textit{x} in radians, in the range of -1.0 through 1.0.
\tabularnewline
\hline
tan(x) &
\textit{x} double &
double &
Returns the tangent of the angle \textit{x} in radians.
\tabularnewline
\hline
\end{tabular}
\caption{Trigonometrical functions}
\label{table:trig.functions}
\end{table}
\section{Type system}
\label{sec:types}
ADQL defines no data definition language (DDL).
It is assumed that table definition and data ingestion are performed in
the underlying database's native language and type system.
However, service metadata needs to give column types in order to allow the
construction of queries that are both syntactically and semantically correct.
Examples of such metadata includes the \verb:TAP_SCHEMA: tables defined in the
\TAPSpec{} and the \verb:/tables: webservice response defined in the
\VOSISpec{}.
Services SHOULD, if at all possible, try to express their column metadata in
these terms even if the underlying database employs different types.
Services SHOULD also use the following mappings when interfacing to user data,
either by serializing result sets into VOTables or by ingesting user-provided
VOTables into ADQL-visible tables.
\subsection{Numeric types}
\label{sec:types.numeric}
\subsubsection{Numeric primitives}
\label{sec:types.numeric.primitive}
The numeric datatypes, \verb:BIT:, \verb:SMALLINT:, \verb:INTEGER:,
\verb:BIGINT:, \verb:REAL: \linebreak and \verb:DOUBLE PRECISION: map to the
corresponding datatypes defined in the \VOTableSpec{}.
\begin{table}[h]\footnotesize
\begin{tabular}
{|p{0.30\textwidth}|p{0.26\textwidth}|p{0.15\textwidth}|p{0.15\textwidth}|}
\hline
\multicolumn{1}{|c|}{\textbf{ADQL}} &
\multicolumn{3}{c|}{\textbf{VOTable}}
\tabularnewline
\hline
\textbf{type} &
\textbf{datatype} &
\textbf{arraysize} &
\textbf{xtype}
\tabularnewline
\hline
BIT &
bit &
- &
-
\tabularnewline
\hline
SMALLINT &
short &
- &
-
\tabularnewline
\hline
INTEGER &
int &
- &
-
\tabularnewline
\hline
BIGINT &
long &
- &
-
\tabularnewline
\hline
REAL &
float &
- &
-
\tabularnewline
\hline
DOUBLE PRECISION &
double &
- &
-
\tabularnewline
\hline
\end{tabular}
\caption{ADQL type mapping for numeric values}
\label{table:types.numeric.primitive}
\end{table}
Where possible ADQL numeric values SHOULD be implemented using database types
that correspond to the VOTable serialization types, e.g. \verb:SMALLINT: should
map to a 16 bit integer, \verb:INTEGER: should map to a 32 bit integer, etc.
\subsubsection{INTERVAL}
\label{sec:types.numeric.interval}
The \DALISpec{} defines \verb:INTERVAL: as a pair of integer or floating-point
numeric values which are serialized as an array of numbers.
None of the ADQL operators apply to \verb:INTERVAL: values.
However, specific implementations MAY provide user defined functions that
operate on some \verb:INTERVAL: values.
\begin{table}[h]\footnotesize
\begin{tabular}
{|p{0.26\textwidth}|p{0.30\textwidth}|p{0.15\textwidth}|p{0.15\textwidth}|}
\hline
\multicolumn{1}{|c|}{\textbf{ADQL}} &
\multicolumn{3}{|c|}{\textbf{VOTable}}
\tabularnewline
\hline
\textbf{type} &
\textbf{datatype} &
\textbf{arraysize} &
\textbf{xtype}
\tabularnewline
\hline
INTERVAL &
short, int, float, double &
2 &
interval
\tabularnewline
\hline
\end{tabular}
\caption{ADQL type mapping for INTERVAL}
\label{table:types.numeric.interval}
\end{table}
The details of how \verb:INTERVAL: values behave in ADQL are not yet
defined.
\subsection{Date and time}
\label{sec:types.datetime}
Where possible, date and time values SHOULD be implemented as described in the
\DALISpec{}.
\subsubsection{TIMESTAMP}
\label{sec:types.datetime.timestamp}
The \verb:TIMESTAMP: datatype maps to the corresponding type defined in the
\DALISpec{}.
\begin{table}[h]\footnotesize
\begin{tabular}
{|p{0.26\textwidth}|p{0.30\textwidth}|p{0.15\textwidth}|p{0.15\textwidth}|}
\hline
\multicolumn{1}{|c|}{\textbf{ADQL}} &
\multicolumn{3}{|c|}{\textbf{VOTable}}
\tabularnewline
\hline
\textbf{type} &
\textbf{datatype} &
\textbf{arraysize} &
\textbf{xtype}
\tabularnewline
\hline
TIMESTAMP &
char &
n, n*, * &
timestamp
\tabularnewline
\hline
\end{tabular}
\caption{ADQL type mapping for TIMESTAMP}
\label{table:types.datetime.timestamp}
\end{table}
\verb:TIMESTAMP:-s can be created from string literals using the \verb:CAST(): function
(if supported) described in \SectionRef{sec:type.cast}.
The basic comparison operators \verb:=:, \verb:<:, \verb:>:, \verb:<=:, \verb:>=:,
\verb:<>: and \verb:BETWEEN: can all be applied to \verb:TIMESTAMP: values.
For instance:
\begin{itemize}
\item \begin{verbatim}
obstime > CAST('2015-01-01' AS TIMESTAMP)
\end{verbatim}
\item \begin{verbatim}
obstime BETWEEN
CAST('2014-01-01' AS TIMESTAMP)
AND
CAST('2014-01-02' AS TIMESTAMP)