Skip to content

Commit 2b9b4c0

Browse files
committed
[DOC] Extend SQL docs
Add overview section Add data type section Improve function section
1 parent 3faef73 commit 2b9b4c0

File tree

6 files changed

+167
-31
lines changed

6 files changed

+167
-31
lines changed

x-pack/docs/en/sql/functions/index.asciidoc

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[[sql-functions]]
22
== Functions and Operators
33

4+
{es-sql} provides a number of built-in operators and functions.
5+
46
=== Comparison Operators
57

6-
Elasticsearch SQL supports the following comparison operators:
8+
{es-sql} supports the following comparison operators:
79

810
* Equality (`=`)
911

@@ -12,7 +14,7 @@ Elasticsearch SQL supports the following comparison operators:
1214
include-tagged::{sql-specs}/filter.sql-spec[whereFieldEquality]
1315
--------------------------------------------------
1416

15-
* Inequality (`<>` or `!=`)
17+
* Inequality (`<>` or `!=` or `<=>`)
1618

1719
["source","sql",subs="attributes,callouts,macros"]
1820
--------------------------------------------------
@@ -43,7 +45,7 @@ include-tagged::{sql-specs}/filter.sql-spec[whereIsNotNullAndIsNull]
4345

4446
=== Logical Operators
4547

46-
Elasticsearch SQL supports the following logical operators:
48+
{es-sql} supports the following logical operators:
4749

4850
* `AND`
4951

@@ -69,7 +71,7 @@ include-tagged::{sql-specs}/filter.sql-spec[whereFieldEqualityNot]
6971

7072
=== Math Operators
7173

72-
Elasticsearch SQL supports the following math operators:
74+
{es-sql} supports the following math operators:
7375

7476
* Add (`+`)
7577

@@ -106,7 +108,7 @@ include-tagged::{sql-specs}/arithmetic.sql-spec[multiply]
106108
include-tagged::{sql-specs}/arithmetic.sql-spec[divide]
107109
--------------------------------------------------
108110

109-
* https://en.wikipedia.org/wiki/Modulo_operation[Modulo] (`%`)
111+
* https://en.wikipedia.org/wiki/Modulo_operation[Modulo] or Reminder(`%`)
110112

111113
["source","sql",subs="attributes,callouts,macros"]
112114
--------------------------------------------------
@@ -115,25 +117,49 @@ include-tagged::{sql-specs}/arithmetic.sql-spec[mod]
115117

116118

117119
=== Math Functions
118-
==== Basic
119120

120-
* https://en.wikipedia.org/wiki/Absolute_value[Absolute value] (`ABS`)
121+
All math and trigonometric functions require their input (where applicable)
122+
to be numeric.
123+
124+
==== Generic
125+
126+
* `ABS`
127+
128+
https://en.wikipedia.org/wiki/Absolute_value[Absolute value], returns \[same type as input]
121129

122130
["source","sql",subs="attributes,callouts,macros"]
123131
--------------------------------------------------
124132
include-tagged::{sql-specs}/math.sql-spec[abs]
125133
--------------------------------------------------
126134

127-
* https://en.wikipedia.org/wiki/Rounding#Round_half_up[Round] (`ROUND`)
135+
* `CBRT`
136+
137+
https://en.wikipedia.org/wiki/Cube_root[Cube root], returns `double`
128138

129139
// TODO make the example in the tests presentable
130140

131-
NOTE: This rounds "half up" meaning that `ROUND(-1.5)` results in `-1`.
141+
* `CEIL`
142+
143+
https://en.wikipedia.org/wiki/Floor_and_ceiling_functions[Ceiling], returns `double`
132144

133-
* https://en.wikipedia.org/wiki/Floor_and_ceiling_functions[Ceiling] (`CEIL`)
145+
* `CEILING`
146+
147+
Same as `CEIL`
134148

135149
// TODO make the example in the tests presentable
136150

151+
* `E`
152+
153+
https://en.wikipedia.org/wiki/E_%28mathematical_constant%29[Euler's number], returns `2.7182818284590452354`
154+
155+
156+
* https://en.wikipedia.org/wiki/Rounding#Round_half_up[Round] (`ROUND`)
157+
158+
// TODO make the example in the tests presentable
159+
160+
NOTE: This rounds "half up" meaning that `ROUND(-1.5)` results in `-1`.
161+
162+
137163
* https://en.wikipedia.org/wiki/Floor_and_ceiling_functions[Floor] (`FLOOR`)
138164

139165
// TODO make the example in the tests presentable
@@ -159,10 +185,6 @@ include-tagged::{sql-specs}/math.sql-spec[log10]
159185
include-tagged::{sql-specs}/math.sql-spec[sqrt]
160186
--------------------------------------------------
161187

162-
* https://en.wikipedia.org/wiki/Cube_root[Cube root] (`CBRT`)
163-
164-
// TODO make the example in the tests presentable
165-
166188
* https://en.wikipedia.org/wiki/Exponential_function[e^x^] (`EXP`)
167189

168190
["source","sql",subs="attributes,callouts,macros"]

x-pack/docs/en/sql/getting-started.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[sql-getting-started]]
22
== Getting Started with SQL
33

4-
To start using Elasticsearch SQL, create
4+
To start using {es-sql}, create
55
an index with some data to experiment with:
66

77
[source,js]

x-pack/docs/en/sql/index.asciidoc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,40 @@
66
:sql-specs: {sql-tests}/src/main/resources
77
:jdbc-tests: {sql-tests}/src/main/java/org/elasticsearch/xpack/qa/sql/jdbc
88
:security-tests: {sql-tests}/security/src/test/java/org/elasticsearch/xpack/qa/sql/security
9+
:es-sql: Elasticsearch SQL
910

1011
[partintro]
1112
--
1213

1314
experimental[]
1415

1516
X-Pack includes a SQL feature to execute SQL against Elasticsearch
16-
indices and return tabular results. There are four main components:
17+
indices and return results in tabular format.
1718

19+
<<sql-overview, Overview>>::
20+
Overview of {es-sql} and its features.
21+
<<sql-getting-started, Getting Started>>::
22+
Start using SQL right away in {es}
1823
<<sql-rest,REST API>>::
1924
Accepts SQL in a JSON document, executes it, and returns the
2025
results.
2126
<<sql-translate,Translate API>>::
2227
Accepts SQL in a JSON document and translates it into a native
2328
Elasticsearch query and returns that.
2429
<<sql-cli,CLI>>::
25-
Command line application that connects to Elasticsearch to execute
30+
Command-line application that connects to {es} to execute
2631
SQL and print tabular results.
2732
<<sql-jdbc,JDBC>>::
28-
A JDBC driver for Elasticsearch.
33+
A JDBC driver for {es}.
34+
<<sql-functions,Functions and Operators>>::
35+
List of functions and operators supported.
36+
<<sql-spec,SQL Language>>::
37+
Overview of the {es-sql} language, such as data types, syntax and
38+
reserved keywords.
39+
2940
--
3041

42+
include::overview.asciidoc[]
3143
include::getting-started.asciidoc[]
3244
include::endpoints/index.asciidoc[]
3345
include::functions/index.asciidoc[]

x-pack/docs/en/sql/language/data-types.asciidoc

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,87 @@
11
[[sql-data-types]]
2-
=== Data Type and Mapping
2+
=== Data Types
3+
4+
Most of {es} <<mapping-types, data types>> are available in {es-sql}, as indicated below:
5+
6+
[cols="^,^,^",options="header"]
7+
8+
|===
9+
| {es} type | SQL type | SQL precision
10+
11+
3+h| Core types
12+
13+
| <<null-value, `null`>> | `null` | 0
14+
| <<boolean, `boolean`>> | `boolean` | 1
15+
| <<number, `byte`>> | `tinyint` | 3
16+
| <<number, `short`>> | `smallint` | 5
17+
| <<number, `integer`>> | `integer` | 10
18+
| <<number, `long`>> | `long` | 19
19+
| <<number, `double`>> | `double` | 15
20+
| <<number, `float`>> | `real` | 7
21+
| <<number, `half_float`>> | `float` | 16
22+
| <<number, `scaled_float`>> | `float` | 19
23+
| <<keyword, `keyword`>> | `varchar` | based on <<ignore-above>>
24+
| <<text, `text`>> | `varchar` | 2,147,483,647
25+
| <<binary, `binary`>> | `varbinary` | 2,147,483,647
26+
| <<date, `date`>> | `timestamp` | 24
27+
28+
3+h| Complex types
29+
30+
| <<object, `object`>> | `struct` | 0
31+
| <<nested, `nested`>> | `struct` | 0
32+
33+
3+h| Unsupported types
34+
35+
| _types not mentioned above_ | `unsupported`| 0
36+
37+
|===
38+
39+
Obviously, not all types in {es} have an equivalent in SQL and vice-versa hence why, {es-sql}
40+
uses the data type _particularities_ of the former over the latter as ultimately {es} is the backing store.
41+
42+
43+
[[sql-multi-field]]
44+
[float]
45+
==== SQL and multi-fields
46+
47+
A core concept in {es} is that of an `analyzed` field, that is a full-text value that is interpreted in order
48+
to be effectively indexed. These fields are of type <<text, `text`>> and are not used for sorting or aggregations as their actual value depends on the <<analyzer, `analyzer`>> used hence why {es} also offers the <<keyword, `keyword`>> type for storing the _exact_
49+
value.
50+
51+
In most case, and the default actually, is to use both types when for strings which {es} supports through <<multi-fields, multi fields>>, that is the ability to index the same string in multiple ways; for example index it both as `text` for search but also as `keyword` for sorting and aggregations.
52+
53+
As SQL requires exact values, when encountering a `text` field {es-sql} will search for an exact multi-field that it can use for comparisons, sorting and aggregations.
54+
To do that, it will search for the first `keyword` that it can find that is _not_ normalized and use that as the original field _exact_ value.
55+
56+
Consider the following `string` mapping:
57+
58+
[source, js]
59+
----
60+
{
61+
"first_name" : {
62+
"type" : "text",
63+
"fields" : {
64+
"raw" : {
65+
"type" : "keyword"
66+
}
67+
}
68+
}
69+
}
70+
----
71+
72+
The following SQL query:
73+
74+
[source, sql]
75+
----
76+
SELECT first_name FROM index WHERE first_name = 'John'
77+
----
78+
79+
is identical to:
80+
81+
[source, sql]
82+
----
83+
SELECT first_name FROM index WHERE first_name.raw = 'John'
84+
----
85+
86+
as {es-sql} automatically _picks_ up the `raw` multi-field from `raw` for exact matching.
387

4-
// TODO finish this
5-
List of data types in SQL and how they actually map to Elasticsearch.
6-
Also mention the corner cases - multi-fields, names with dots, etc...

x-pack/docs/en/sql/overview.asciidoc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[[sql-overview]]
2+
== Overview
3+
4+
{es-sql} aims to provide a powerful yet lightweight SQL interface to {es}.
5+
6+
[[sql-introduction]]
7+
=== Introduction
8+
9+
{es-sql} is an X-Pack component that allows SQL-like queries to be executed in real-time against {es}.
10+
Whether using the REST interface, command-line or JDBC, any client can use SQL to search and aggregate data
11+
_natively_ inside {es}.
12+
One can think of {es-sql} as a _translator_, one that understands both SQL and {es} and makes it easy to read and process data in real-time, at scale by leveraging {es} capabilities.
13+
14+
[[sql-why]]
15+
=== Why {es-sql} ?
16+
17+
Native integration::
18+
19+
{es-sql} is built from the ground up for {es}. Each and every query is efficiently executed against the relevant nodes according to the underlying storage.
20+
21+
No external parts::
22+
23+
No need for additional hardware, processes, runtimes or libraries to query {es}; {es-sql} eliminates extra moving parts by running _inside_ the {es} cluster.
24+
25+
Lightweight and efficient::
26+
27+
{es-sql} does not abstract {es} and its search capabilities - on the contrary, it embrases and exposes to SQL to allow proper full-text search, in real-time, in the same declarative, succint fashion.
28+
29+
30+

x-pack/docs/en/sql/standalone.asciidoc

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)