Skip to content

Commit de9e56a

Browse files
authored
DOC: Add examples to the SQL docs (elastic#31633)
Significantly improve the example snippets in the documentation. The examples are part of the test suite and checked nightly. To help readability, the existing dataset was extended (test_emp renamed to emp plus library). Improve output of JDBC tests to be consistent with the CLI Add lenient flag to JDBC asserts to allow type widening (a long is equivalent to a integer as long as the value is the same).
1 parent 4108722 commit de9e56a

File tree

19 files changed

+1200
-146
lines changed

19 files changed

+1200
-146
lines changed

docs/reference/sql/language/syntax/describe-table.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ DESC table
2020
.Description
2121

2222
`DESC` and `DESCRIBE` are aliases to <<sql-syntax-show-columns>>.
23+
24+
["source","sql",subs="attributes,callouts,macros"]
25+
----
26+
include-tagged::{sql-specs}/docs.csv-spec[describeTable]
27+
----

docs/reference/sql/language/syntax/select.asciidoc

Lines changed: 161 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,26 @@ The general execution of `SELECT` is as follows:
3636

3737
As with a table, every output column of a `SELECT` has a name which can be either specified per column through the `AS` keyword :
3838

39-
[source,sql]
39+
["source","sql",subs="attributes,callouts,macros"]
4040
----
41-
SELECT column AS c
41+
include-tagged::{sql-specs}/docs.csv-spec[selectColumnAlias]
4242
----
4343

44+
Note: `AS` is an optional keyword however it helps with the readability and in some case ambiguity of the query
45+
which is why it is recommended to specify it.
46+
4447
assigned by {es-sql} if no name is given:
4548

46-
[source,sql]
49+
["source","sql",subs="attributes,callouts,macros"]
4750
----
48-
SELECT 1 + 1
51+
include-tagged::{sql-specs}/docs.csv-spec[selectInline]
4952
----
5053

5154
or if it's a simple column reference, use its name as the column name:
5255

53-
[source,sql]
56+
["source","sql",subs="attributes,callouts,macros"]
5457
----
55-
SELECT col FROM table
58+
include-tagged::{sql-specs}/docs.csv-spec[selectColumn]
5659
----
5760

5861
[[sql-syntax-select-wildcard]]
@@ -61,11 +64,11 @@ SELECT col FROM table
6164
To select all the columns in the source, one can use `*`:
6265

6366
["source","sql",subs="attributes,callouts,macros"]
64-
--------------------------------------------------
65-
include-tagged::{sql-specs}/select.sql-spec[wildcardWithOrder]
66-
--------------------------------------------------
67+
----
68+
include-tagged::{sql-specs}/docs.csv-spec[wildcardWithOrder]
69+
----
6770

68-
which essentially returns all columsn found.
71+
which essentially returns all(top-level fields, sub-fields, such as multi-fields are ignored] columns found.
6972

7073
[[sql-syntax-from]]
7174
[float]
@@ -83,17 +86,30 @@ where:
8386
`table_name`::
8487

8588
Represents the name (optionally qualified) of an existing table, either a concrete or base one (actual index) or alias.
89+
90+
8691
If the table name contains special SQL characters (such as `.`,`-`,etc...) use double quotes to escape them:
87-
[source, sql]
92+
93+
["source","sql",subs="attributes,callouts,macros"]
8894
----
89-
SELECT ... FROM "some-table"
95+
include-tagged::{sql-specs}/docs.csv-spec[fromTableQuoted]
9096
----
9197

9298
The name can be a <<multi-index, pattern>> pointing to multiple indices (likely requiring quoting as mentioned above) with the restriction that *all* resolved concrete tables have **exact mapping**.
9399

100+
["source","sql",subs="attributes,callouts,macros"]
101+
----
102+
include-tagged::{sql-specs}/docs.csv-spec[fromTablePatternQuoted]
103+
----
104+
94105
`alias`::
95106
A substitute name for the `FROM` item containing the alias. An alias is used for brevity or to eliminate ambiguity. When an alias is provided, it completely hides the actual name of the table and must be used in its place.
96107

108+
["source","sql",subs="attributes,callouts,macros"]
109+
----
110+
include-tagged::{sql-specs}/docs.csv-spec[fromTableAlias]
111+
----
112+
97113
[[sql-syntax-where]]
98114
[float]
99115
==== WHERE Clause
@@ -111,6 +127,11 @@ where:
111127

112128
Represents an expression that evaluates to a `boolean`. Only the rows that match the condition (to `true`) are returned.
113129

130+
["source","sql",subs="attributes,callouts,macros"]
131+
----
132+
include-tagged::{sql-specs}/docs.csv-spec[basicWhere]
133+
----
134+
114135
[[sql-syntax-group-by]]
115136
[float]
116137
==== GROUP BY
@@ -126,10 +147,80 @@ where:
126147

127148
`grouping_element`::
128149

129-
Represents an expression on which rows are being grouped _on_. It can be a column name, name or ordinal number of a column or an arbitrary expression of column values.
150+
Represents an expression on which rows are being grouped _on_. It can be a column name, alias or ordinal number of a column or an arbitrary expression of column values.
151+
152+
A common, group by column name:
153+
154+
["source","sql",subs="attributes,callouts,macros"]
155+
----
156+
include-tagged::{sql-specs}/docs.csv-spec[groupByColumn]
157+
----
158+
159+
Grouping by output ordinal:
160+
161+
["source","sql",subs="attributes,callouts,macros"]
162+
----
163+
include-tagged::{sql-specs}/docs.csv-spec[groupByOrdinal]
164+
----
165+
166+
Grouping by alias:
167+
168+
["source","sql",subs="attributes,callouts,macros"]
169+
----
170+
include-tagged::{sql-specs}/docs.csv-spec[groupByAlias]
171+
----
172+
173+
And grouping by column expression (typically used along-side an alias):
174+
175+
["source","sql",subs="attributes,callouts,macros"]
176+
----
177+
include-tagged::{sql-specs}/docs.csv-spec[groupByExpression]
178+
----
130179

131180
When a `GROUP BY` clause is used in a `SELECT`, _all_ output expressions must be either aggregate functions or expressions used for grouping or derivates of (otherwise there would be more than one possible value to return for each ungrouped column).
132181

182+
To wit:
183+
184+
["source","sql",subs="attributes,callouts,macros"]
185+
----
186+
include-tagged::{sql-specs}/docs.csv-spec[groupByAndAgg]
187+
----
188+
189+
Expressions over aggregates used in output:
190+
191+
["source","sql",subs="attributes,callouts,macros"]
192+
----
193+
include-tagged::{sql-specs}/docs.csv-spec[groupByAndAggExpression]
194+
----
195+
196+
Multiple aggregates used:
197+
198+
["source","sql",subs="attributes,callouts,macros"]
199+
----
200+
include-tagged::{sql-specs}/docs.csv-spec[groupByAndMultipleAggs]
201+
----
202+
203+
[[sql-syntax-group-by-implicit]]
204+
[float]
205+
===== Implicit Grouping
206+
207+
When an aggregation is used without an associated `GROUP BY`, an __implicit grouping__ is applied, meaning all selected rows are considered to form a single default, or implicit group.
208+
As such, the query emits only a single row (as there is only a single group).
209+
210+
A common example is counting the number of records:
211+
212+
["source","sql",subs="attributes,callouts,macros"]
213+
----
214+
include-tagged::{sql-specs}/docs.csv-spec[groupByImplicitCount]
215+
----
216+
217+
Of course, multiple aggregations can be applied:
218+
219+
["source","sql",subs="attributes,callouts,macros"]
220+
----
221+
include-tagged::{sql-specs}/docs.csv-spec[groupByImplicitMultipleAggs]
222+
----
223+
133224
[[sql-syntax-having]]
134225
[float]
135226
==== HAVING
@@ -147,13 +238,44 @@ where:
147238

148239
Represents an expression that evaluates to a `boolean`. Only groups that match the condition (to `true`) are returned.
149240

150-
Both `WHERE` and `HAVING` are used for filtering however there are several differences between them:
241+
Both `WHERE` and `HAVING` are used for filtering however there are several significant differences between them:
151242

152243
. `WHERE` works on individual *rows*, `HAVING` works on the *groups* created by ``GROUP BY``
153244
. `WHERE` is evaluated *before* grouping, `HAVING` is evaluated *after* grouping
154245

155-
Note that it is possible to have a `HAVING` clause without a ``GROUP BY``. In this case, an __implicit grouping__ is applied, meaning all selected rows are considered to form a single group and `HAVING` can be applied on any of the aggregate functions specified on this group. `
156-
As such a query emits only a single row (as there is only a single group), `HAVING` condition returns either one row (the group) or zero if the condition fails.
246+
["source","sql",subs="attributes,callouts,macros"]
247+
----
248+
include-tagged::{sql-specs}/docs.csv-spec[groupByHaving]
249+
----
250+
251+
Further more, one can use multiple aggregate expressions inside `HAVING` even ones that are not used in the output (`SELECT`):
252+
253+
["source","sql",subs="attributes,callouts,macros"]
254+
----
255+
include-tagged::{sql-specs}/docs.csv-spec[groupByHavingMultiple]
256+
----
257+
258+
[[sql-syntax-having-group-by-implicit]]
259+
[float]
260+
===== Implicit Grouping
261+
262+
As indicated above, it is possible to have a `HAVING` clause without a ``GROUP BY``. In this case, the so-called <<sql-syntax-group-by-implicit, __implicit grouping__>> is applied, meaning all selected rows are considered to form a single group and `HAVING` can be applied on any of the aggregate functions specified on this group. `
263+
As such, the query emits only a single row (as there is only a single group) and `HAVING` condition returns either one row (the group) or zero if the condition fails.
264+
265+
In this example, `HAVING` matches:
266+
267+
["source","sql",subs="attributes,callouts,macros"]
268+
----
269+
include-tagged::{sql-specs}/docs.csv-spec[groupByHavingImplicitMatch]
270+
----
271+
272+
//However `HAVING` can also not match, in which case an empty result is returned:
273+
//
274+
//["source","sql",subs="attributes,callouts,macros"]
275+
//----
276+
//include-tagged::{sql-specs}/docs.csv-spec[groupByHavingImplicitNoMatch]
277+
//----
278+
157279
158280
[[sql-syntax-order-by]]
159281
[float]
@@ -178,30 +300,10 @@ IMPORTANT: When used along-side, `GROUP BY` expression can point _only_ to the c
178300
179301
For example, the following query sorts by an arbitrary input field (`page_count`):
180302
181-
[source,js]
182-
--------------------------------------------------
183-
POST /_xpack/sql?format=txt
184-
{
185-
"query": "SELECT * FROM library ORDER BY page_count DESC LIMIT 5"
186-
}
187-
--------------------------------------------------
188-
// CONSOLE
189-
// TEST[setup:library]
190-
191-
which results in something like:
192-
193-
[source,text]
194-
--------------------------------------------------
195-
author | name | page_count | release_date
196-
-----------------+--------------------+---------------+------------------------
197-
Peter F. Hamilton|Pandora's Star |768 |2004-03-02T00:00:00.000Z
198-
Vernor Vinge |A Fire Upon the Deep|613 |1992-06-01T00:00:00.000Z
199-
Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
200-
Alastair Reynolds|Revelation Space |585 |2000-03-15T00:00:00.000Z
201-
James S.A. Corey |Leviathan Wakes |561 |2011-06-02T00:00:00.000Z
202-
--------------------------------------------------
203-
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
204-
// TESTRESPONSE[_cat]
303+
["source","sql",subs="attributes,callouts,macros"]
304+
----
305+
include-tagged::{sql-specs}/docs.csv-spec[orderByBasic]
306+
----
205307
206308
[[sql-syntax-order-by-score]]
207309
==== Order By Score
@@ -215,54 +317,18 @@ combined using the same rules as {es}'s
215317
216318
To sort based on the `score`, use the special function `SCORE()`:
217319
218-
[source,js]
219-
--------------------------------------------------
220-
POST /_xpack/sql?format=txt
221-
{
222-
"query": "SELECT SCORE(), * FROM library WHERE match(name, 'dune') ORDER BY SCORE() DESC"
223-
}
224-
--------------------------------------------------
225-
// CONSOLE
226-
// TEST[setup:library]
227-
228-
Which results in something like:
229-
230-
[source,text]
231-
--------------------------------------------------
232-
SCORE() | author | name | page_count | release_date
233-
---------------+---------------+-------------------+---------------+------------------------
234-
2.288635 |Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
235-
1.8893257 |Frank Herbert |Dune Messiah |331 |1969-10-15T00:00:00.000Z
236-
1.6086555 |Frank Herbert |Children of Dune |408 |1976-04-21T00:00:00.000Z
237-
1.4005898 |Frank Herbert |God Emperor of Dune|454 |1981-05-28T00:00:00.000Z
238-
--------------------------------------------------
239-
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/ s/\(/\\\(/ s/\)/\\\)/]
240-
// TESTRESPONSE[_cat]
241-
242-
Note that you can return `SCORE()` by adding it to the where clause. This
243-
is possible even if you are not sorting by `SCORE()`:
244-
245-
[source,js]
246-
--------------------------------------------------
247-
POST /_xpack/sql?format=txt
248-
{
249-
"query": "SELECT SCORE(), * FROM library WHERE match(name, 'dune') ORDER BY page_count DESC"
250-
}
251-
--------------------------------------------------
252-
// CONSOLE
253-
// TEST[setup:library]
254-
255-
[source,text]
256-
--------------------------------------------------
257-
SCORE() | author | name | page_count | release_date
258-
---------------+---------------+-------------------+---------------+------------------------
259-
2.288635 |Frank Herbert |Dune |604 |1965-06-01T00:00:00.000Z
260-
1.4005898 |Frank Herbert |God Emperor of Dune|454 |1981-05-28T00:00:00.000Z
261-
1.6086555 |Frank Herbert |Children of Dune |408 |1976-04-21T00:00:00.000Z
262-
1.8893257 |Frank Herbert |Dune Messiah |331 |1969-10-15T00:00:00.000Z
263-
--------------------------------------------------
264-
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/ s/\(/\\\(/ s/\)/\\\)/]
265-
// TESTRESPONSE[_cat]
320+
["source","sql",subs="attributes,callouts,macros"]
321+
----
322+
include-tagged::{sql-specs}/docs.csv-spec[orderByScore]
323+
----
324+
325+
Note that you can return `SCORE()` by using a full-text search predicate in the `WHERE` clause.
326+
This is possible even if `SCORE()` is not used for sorting:
327+
328+
["source","sql",subs="attributes,callouts,macros"]
329+
----
330+
include-tagged::{sql-specs}/docs.csv-spec[orderByScoreWithMatch]
331+
----
266332
267333
NOTE:
268334
Trying to return `score` from a non full-text queries will return the same value for all results, as
@@ -284,3 +350,10 @@ where
284350
count:: is a positive integer or zero indicating the maximum *possible* number of results being returned (as there might be less matches than the limit). If `0` is specified, no results are returned.
285351
286352
ALL:: indicates there is no limit and thus all results are being returned.
353+
354+
To return
355+
356+
["source","sql",subs="attributes,callouts,macros"]
357+
----
358+
include-tagged::{sql-specs}/docs.csv-spec[limitBasic]
359+
----

docs/reference/sql/language/syntax/show-columns.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ SHOW COLUMNS [ FROM | IN ] ? table
1212
.Description
1313

1414
List the columns in table and their data type (and other attributes).
15+
16+
["source","sql",subs="attributes,callouts,macros"]
17+
----
18+
include-tagged::{sql-specs}/docs.csv-spec[showColumns]
19+
----

docs/reference/sql/language/syntax/show-functions.asciidoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,34 @@ SHOW FUNCTIONS [ LIKE? pattern<1>? ]?
1414
.Description
1515

1616
List all the SQL functions and their type. The `LIKE` clause can be used to restrict the list of names to the given pattern.
17+
18+
["source","sql",subs="attributes,callouts,macros"]
19+
----
20+
include-tagged::{sql-specs}/docs.csv-spec[showFunctions]
21+
----
22+
23+
The list of functions returned can be customized based on the pattern.
24+
25+
It can be an exact match:
26+
["source","sql",subs="attributes,callouts,macros"]
27+
----
28+
include-tagged::{sql-specs}/docs.csv-spec[showFunctionsLikeExact]
29+
----
30+
31+
A wildcard for exactly one character:
32+
["source","sql",subs="attributes,callouts,macros"]
33+
----
34+
include-tagged::{sql-specs}/docs.csv-spec[showFunctionsLikeChar]
35+
----
36+
37+
A wildcard matching zero or more characters:
38+
["source","sql",subs="attributes,callouts,macros"]
39+
----
40+
include-tagged::{sql-specs}/docs.csv-spec[showFunctionsLikeWildcard]
41+
----
42+
43+
Or of course, a variation of the above:
44+
["source","sql",subs="attributes,callouts,macros"]
45+
----
46+
include-tagged::{sql-specs}/docs.csv-spec[showFunctionsWithPattern]
47+
----

0 commit comments

Comments
 (0)