Skip to content

Commit 4abdf3f

Browse files
committed
Fixes gh-2516 New SQL data types and function changes, second commit
1 parent a070cce commit 4abdf3f

File tree

2 files changed

+107
-53
lines changed

2 files changed

+107
-53
lines changed

doc/reference/reference_sql/sql_plus_lua.rst

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Limitations: (`Issue#4659 <https://github.com/tarantool/tarantool/issues/4659>`_
151151
`Issue#4758 <https://github.com/tarantool/tarantool/issues/4758>`_) |br|
152152
SELECT with * or ORDER BY or GROUP BY from spaces that have map fields
153153
or array fields may cause errors. Any access to spaces that have hash
154-
indexes may cause severe errors.
154+
indexes may cause severe errors in Tarantool version 2.3 or earlier.
155155

156156
.. _sql_system_tables:
157157

@@ -257,7 +257,7 @@ See also: :ref:`Lua functions to make views of metadata <sql_lua_functions>`.
257257
and 2**6 possibilities for the other opts options, Tarantool supports
258258
about 736 * 2 * 5 * 64 = 471,040 different collations out of the box.
259259
In fact three of the pre-defined collations (unicode_uk_s1 unicode_uk_s2 unicode_uk_s3)
260-
re the standard CLDR variants for Ukrainian, so the above example was
260+
are the standard CLDR variants for Ukrainian, so the above example was
261261
made only to show how one makes a new one, not because there is any need to do so for this situation.
262262
263263
Limitations:
@@ -312,23 +312,25 @@ For a useful example, here is a general function for decoding a single Lua ``'ma
312312
box.schema.func.create('_DECODE',
313313
{language = 'LUA',
314314
returns = 'string',
315-
body = [[function (field, part)
316-
__GLOBAL= field
317-
return dostring("return require('msgpack').decode(__GLOBAL,1)." .. part)
315+
body = [[function (field, key)
316+
-- If Tarantool version < 2.10.1, replace next line with
317+
-- return require('msgpack').decode(field)[key]
318+
return field[key]
318319
end]],
319320
is_sandboxed = false,
320-
param_list = {'string', "string"},
321+
-- If Tarantool version < 2.10.1, replace next line with
322+
-- param_list = {'string', 'string'},
323+
param_list = {'map', 'string'},
321324
exports = {'LUA', 'SQL'},
322325
is_deterministic = true})
323326
324327
See it work with, say, the _trigger space.
325-
That space has a ``'map'`` field named opts which has a part named sql.
326-
By selecting from the space and passing the field and the part name to _DECODE,
328+
That space has a ``'map'`` field named opts which has a key named sql.
329+
By selecting from the space and passing the field and the key name to _DECODE,
327330
you can get a list of all the trigger bodies.
328331

329332
.. code-block:: lua
330333
331-
__GLOBAL = ""
332334
box.execute([[SELECT _decode("opts", 'sql') FROM "_trigger";]])
333335
334336
Remember that SQL converts :ref:`regular identifiers <sql_identifiers>` to upper case,
@@ -359,12 +361,16 @@ So our way of making the function looks like this:
359361
returns = 'boolean',
360362
body = [[function (flags)
361363
local view
362-
view = require('msgpack').decode(flags).view
364+
-- If Tarantool version < 2.10.1, replace next line with
365+
-- view = require('msgpack').decode(flags).view
366+
view = flags.view
363367
if view == nil then return false end
364368
return view
365369
end]],
366370
is_sandboxed = false,
367-
param_list = {'string'},
371+
-- If Tarantool version < 2.10.1, replace next line with
372+
-- param_list = {'string'},
373+
param_list = {'map'},
368374
exports = {'LUA', 'SQL'},
369375
is_deterministic = true})
370376
@@ -597,14 +603,17 @@ Definition of the function and the CREATE VIEW statement:
597603
returns = 'boolean',
598604
body = [[function (flags)
599605
local view
600-
view = require('msgpack').decode(flags).view
606+
-- If Tarantool version < 2.10.1, replace next line with
607+
-- view = require('msgpack').decode(flags).view
608+
view = flags.view
601609
if view == nil then return false end
602610
return view
603611
end]],
604612
is_sandboxed = false,
605-
param_list = {'string'},
613+
-- If Tarantool version < 2.10.1, replace next line with
614+
-- param_list = {'string'},
615+
param_list = {'map'},
606616
exports = {'LUA', 'SQL'},
607-
setuid = false,
608617
is_deterministic = true})
609618
box.schema.role.grant('public', 'execute', 'function', '_TABLES_IS_VIEW')
610619
pcall(function ()
@@ -769,8 +778,13 @@ Definition of the function and the CREATE VIEW statement:
769778
{language = 'LUA',
770779
returns = 'string',
771780
body = [[function (flags)
772-
return require('msgpack').decode(flags).sql end]],
773-
param_list = {'string'},
781+
-- If Tarantool version < 2.10.1, replace next line with
782+
-- return require('msgpack').decode(flags).sql
783+
return flags.sql
784+
end]],
785+
-- If Tarantool version < 2.10.1, replace next line with
786+
-- param_list = {'string'},
787+
param_list = {'map'},
774788
exports = {'LUA', 'SQL'},
775789
is_sandboxed = false,
776790
setuid = false,
@@ -824,8 +838,13 @@ Definition of the function and the CREATE VIEW statement:
824838
{language = 'LUA',
825839
returns = 'string',
826840
body = [[function (opts)
827-
return require('msgpack').decode(opts).sql end]],
828-
param_list = {'string'},
841+
-- If Tarantool version < 2.10.1, replace next line with
842+
-- return require('msgpack').decode(opts).sql
843+
return opts.sql
844+
end]],
845+
-- If Tarantool version < 2.10.1, replace next line with
846+
-- param_list = {'string'},
847+
param_list = {'map'},
829848
exports = {'LUA', 'SQL'},
830849
is_sandboxed = false,
831850
setuid = false,

doc/reference/reference_sql/sql_user_guide.rst

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ but for readability one would usually use spaces to separate tokens: |br|
169169
Literals
170170
********************************************************************************
171171

172-
There are eight kinds of literals: BOOLEAN INTEGER DOUBLE DECIMAL STRING VARBINARY ARRAY MAP.
172+
There are eight kinds of literals: BOOLEAN INTEGER DOUBLE DECIMAL STRING VARBINARY MAP ARRAY.
173173

174174
BOOLEAN literals: |br|
175175
TRUE | FALSE | UNKNOWN |br|
@@ -221,27 +221,20 @@ Example: ``X'414243'``, which will be displayed as ``'ABC'``. |br|
221221
A literal has :ref:`data type = VARBINARY <sql_data_type_varbinary>`
222222
("variable-length binary") if it is the letter X followed by quotes containing pairs of hexadecimal digits, representing byte values.
223223

224-
ARRAY expressions: |br|
225-
[left square bracket] [comma-separated list of values] [right square bracket] |br|
226-
Examples: ``[1,2,3,4]``, ``[1,[2,3],4]``, ``X['a','22',uuid()]`` |br|
227-
An expression has data type = ARRAY if it is a sequence of zero or more values
228-
enclosed in square brackets (``[`` and ``]``).
229-
Values may be of any type; values may be expressions; arrays may be nested.
230-
In formal terms we should say that ``[`` and ``]`` are actually "operators"
231-
and the values are "elements", but most of the examples we have used are in
232-
fact arrays of literals.
233-
234-
MAP expressions: |br|
235-
[left curly bracket] key [colon] value [right curly bracket] |br|
236-
Examples: ``{'a':1}``, ``{ "column_1" : X'1234' }`` |br|
237-
An expression has data type = MAP if it is enclosed in curly brackets
238-
(also called braces) ``{`` and ``}`` and contains a key for identification,
239-
then a colon ``:``, then a value for what the key identifies.
240-
As with ARRAY expressions, the ``{`` and ``}`` are actually operators
241-
so the key and the value are not necessarily literals themselves.
242-
It is not uncommon to have an array of maps, for example
243-
``[{'a':1},{'b':2}]``, which can be called an associative array and can
244-
be converted to a `Lua table <https://www.lua.org/pil/2.5.html>`_.
224+
MAP literals: |br|
225+
[left curly bracket] key [colon] value [right square bracket] |br|
226+
Examples: ``{'a':1}``, ``{1:'a'}`` |br|
227+
A map literal a pair of curly brackets (also called "braces")
228+
enclosing a STRING or INTEGER or UUID literal (called the map "key")
229+
followed by a colon
230+
followed by any type of literal (called the map "value").
231+
This is a minimal form of a :ref:`MAP expression <sql_map_expression>`.
232+
233+
ARRAY literals: |br|
234+
[left square bracket] [literal] [right square bracket] |br|
235+
Examples: ``[1]``, ``['a']`` |br|
236+
An ARRAY literal is a literal value which is enclosed inside square brackets.
237+
This is a minimal form of an :ref:`ARRAY expression <sql_array_expression>`.
245238

246239
Here are four ways to put non-ASCII characters,such as the Greek letter α alpha, in string literals: |br|
247240
First make sure that your shell program is set to accept characters as UTF-8. A simple way to check is |br|
@@ -495,10 +488,12 @@ and minimum / maximum literal examples.
495488
+-----------+------------+------------+----------------------+-------------------------+
496489
| SCALAR | (varies) | (none) | FALSE | maximum UUID value |
497490
+-----------+------------+------------+----------------------+-------------------------+
498-
| ARRAY | array | (none) | [] | ``many values`` |
499-
+-----------+------------+------------+----------------------+-------------------------+
500491
| MAP | map | (none) | ``{'':''}`` | ``big-key:big-value`` |
501492
+-----------+------------+------------+----------------------+-------------------------+
493+
| ARRAY | array | (none) | [] | ``[many values]`` |
494+
+-----------+------------+------------+----------------------+-------------------------+
495+
| ANY | any | (none) | FALSE | ``[many values]`` |
496+
+-----------+------------+------------+----------------------+-------------------------+
502497

503498

504499
.. _sql_data_type_boolean:
@@ -589,27 +584,33 @@ Prior to Tarantool version 2.10.1, individual column values had
589584
one of the preceding types -- BOOLEAN, INTEGER, DOUBLE, DECIMAL, STRING, VARBINARY, or UUID.
590585
Starting in Tarantool version 2.10.1, all values have type SCALAR.
591586

592-
.. _sql_data_type_array:
587+
.. _sql_data_type_map:
593588

594-
ARRAY values are lists of values of any type, including other ARRAY values
595-
-- that is, arrays can be "nested". Tarantool does
596-
not require that all values in an array must have the same type.
597-
Arrays cannot be used in arithmetic or comparison (except ``IS [NOT] NULL``), and the only
589+
MAP values are key:value combinations which can be produced with
590+
:ref:`MAP expressions <sql_map_expression>`.
591+
Maps cannot be used in arithmetic or comparison (except ``IS [NOT] NULL``),
592+
and the only
598593
functions where they are allowed are :ref:`CAST <sql_function_cast>`,
599594
:ref:`QUOTE <sql_function_quote>`,
600595
:ref:`TYPEOF <sql_function_typeof>`, and functions involving NULL comparisons.
601596

602-
.. _sql_data_type_map:
597+
.. _sql_data_type_array:
603598

604-
MAP values are key:value combinations which can be produced with expressions
605-
like ``LUA('return {a=1}')``, although the SQL literal will look more like
606-
``{'a':1}``.
607-
Maps cannot be used in arithmetic or comparison (except ``IS [NOT] NULL``),
608-
and the only
599+
ARRAY values are lists which can be produced with
600+
:ref:`ARRAY expressions <sql_array_expression>`.
601+
Arrays cannot be used in arithmetic or comparison (except ``IS [NOT] NULL``), and the only
609602
functions where they are allowed are :ref:`CAST <sql_function_cast>`,
610603
:ref:`QUOTE <sql_function_quote>`,
611604
:ref:`TYPEOF <sql_function_typeof>`, and functions involving NULL comparisons.
612605

606+
.. _sql_data_type_any:
607+
608+
ANY can be used for
609+
:ref:`column definitions <sql_column_def_data_type>` and the individual column values have
610+
type ANY.
611+
The difference between SCALAR and ANY is:
612+
SCALAR columns may not contain MAP or ARRAY values, but ANY columns may contain them.
613+
613614
Any value of any data type may be NULL. Ordinarily NULL will be cast to the
614615
data type of any operand it is being compared to or to the data type of the
615616
column it is in. If the data type of NULL cannot be determined from context,
@@ -865,6 +866,40 @@ value IS [NOT] NULL |br|
865866
CASE ... WHEN ... THEN ... ELSE ... END |br|
866867
... for setting a series of conditions.
867868

869+
.. _sql_map_expression:
870+
871+
{ key : value } |br|
872+
... for MAP expressions. |br|
873+
Literal Examples: ``{'a':1}``, ``{ "column_1" : X'1234' }`` |br|
874+
Non-literal Examples: ``{"a":"a"}``, ``{UUID(), (SELECT 1) + 1}`` |br|
875+
An expression has data type = MAP if it is enclosed in curly brackets
876+
(also called braces) ``{`` and ``}`` and contains a key for identification,
877+
then a colon ``:``, then a value for what the key identifies.
878+
The key data type must be INTEGER or STRING or UUID.
879+
The value data type may be anything.
880+
The Lua equivalent type is 'map' but the syntax is slightly different,
881+
for example the SQL value ``{'a': 1}`` is represented in Lua as ``{a = 1}``.
882+
883+
.. _sql_array_expression:
884+
885+
[ value ... ] |br|
886+
... for ARRAY expressions. |br|
887+
Examples: ``[1,2,3,4]``, ``[1,[2,3],4]``, ``['a', "column_1", uuid()]`` |br|
888+
An expression has data type = ARRAY if it is a sequence of zero or more values
889+
enclosed in square brackets (``[`` and ``]``).
890+
Often the values in the sequence are called "elements".
891+
The element data type may be anything, including ARRAY -- that is, ARRAYs may be nested.
892+
Different elements may have different types.
893+
The Lua equivalent type is `'array' <https://www.lua.org/pil/11.1.html>`_.
894+
895+
.. _sql_array_index_expression:
896+
897+
ARRAY index expression: |br|
898+
array-value [square bracket] index [square bracket] |br|
899+
Example: ``['a', 'b', 'c'] [2]`` (this equals 'b') |br|
900+
As in other languages, an element of an array can be referenced with an
901+
integer inside square brackets.
902+
868903
See also: :ref:`subquery <sql_subquery>`.
869904

870905
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

0 commit comments

Comments
 (0)