Skip to content

Commit 16a3d4b

Browse files
committed
added documentation for array and object literals
1 parent 3c4ae98 commit 16a3d4b

File tree

8 files changed

+230
-63
lines changed

8 files changed

+230
-63
lines changed

CHANGES.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ Changes for Crate Data
4141
2014/05/19 0.38.0
4242
=================
4343

44+
- implemented object and array literals
45+
4446
- changed refresh table for partitioned tables.
45-
Instead of::
46-
47+
Instead of::
48+
4749
refresh table my_table PARTITION ident
4850

4951
it is now::

core/src/main/java/io/crate/core/NumberOfReplicas.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,10 @@ public NumberOfReplicas(Integer replicas) {
4242

4343
public NumberOfReplicas(String replicas) {
4444
assert replicas != null;
45-
try {
46-
int numReplicas = Integer.parseInt(replicas);
47-
} catch (NumberFormatException e) {
48-
Preconditions.checkArgument(EXPAND_REPLICA_PATTERN.matcher(replicas).matches(),
49-
"The \"number_of_replicas\" range \"%s\" isn't valid", replicas);
45+
Preconditions.checkArgument(EXPAND_REPLICA_PATTERN.matcher(replicas).matches(),
46+
"The \"number_of_replicas\" range \"%s\" isn't valid", replicas);
5047

51-
this.esSettingKey = AUTO_EXPAND_REPLICAS;
52-
this.esSettingsValue = replicas;
53-
return;
54-
}
55-
56-
this.esSettingKey = NUMBER_OF_REPLICAS;
48+
this.esSettingKey = AUTO_EXPAND_REPLICAS;
5749
this.esSettingsValue = replicas;
5850
}
5951

docs/sql/ddl.txt

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,37 @@ New columns added to ``ignored`` objects can be retrieved as result column in a
296296
but one cannot order by them or use them in a where clause. They are simply there for fetching,
297297
nothing else.
298298

299+
object literals
300+
...............
301+
302+
To insert values into object columns one can use object literals or parameters.
303+
304+
.. note:: Even though they look like `JSON` - object literals are not `JSON` compatible.
305+
306+
Object literals are given in curly brackets. Key value pairs are connected via ``=``.
307+
308+
Synopsis::
309+
310+
{ [ ident = expr [ , ... ] ] }
311+
312+
The *key* of a key-value pair is an SQL identifier. That means every unquoted identifier
313+
in an object literal key will be lowercased.
314+
315+
The *value* of a key-value pair is another literal or a parameter.
316+
317+
An object literal can contain zero or more key value pairs
318+
319+
Some examples::
320+
321+
{ "CamelCase"=123, key=['v', 'a', 'l', 'u', 'e'], param=?}
322+
323+
{}
324+
325+
{{},{}}
326+
327+
{"true"=false}
328+
329+
299330
array
300331
-----
301332

@@ -323,14 +354,33 @@ Array types are defined as follows::
323354
Currently arrays cannot be nested. Something like array(array(string))
324355
won't work.
325356

326-
.. note::
357+
array literals
358+
..............
359+
360+
Array literals are given in brackets and may contain zero or more elements.
361+
362+
Synopsis (literal brackets are given in single quotes)::
363+
364+
'[' [ element [ , ... ] ] ']'
365+
366+
Valid ``element``s are literals and parameters.
367+
368+
All array elements have to be of the type of the first element,
369+
which determines the element type. If an array contains no elements, its element
370+
type will be inferred by the context in which it occurs, if possible.
371+
372+
Some Examples::
373+
374+
[]
375+
376+
[[], []]
377+
378+
[1, 2, 3, 4, 5, 6, 7, 8]
379+
380+
[null]
327381

328-
Crash currently has no support to insert arrays. But it is possible to
329-
insert arrays using the `_sql` REST Endpoint or any of the available crate
330-
clients.
382+
['Zaphod', 'Ford', 'Arthur']
331383

332-
E.g. in the python client a python list can be used as an argument to the
333-
cursors execute method.
334384

335385
.. _sql_ddl_sharding:
336386

docs/sql/dml.txt

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ Query for a property of an inner object::
300300
SELECT 1 row in set (... sec)
301301

302302

303-
.. note::
304-
305-
It is currently not possible to insert objects using crash. In order to do
306-
that it is necessary to use one of the client libraries like
307-
`crate-python`_ or use the :doc:`rest`.
303+
Inserting objects::
308304

305+
cr> insert into locations (id, name, position, kind, race)
306+
... values ('DO', 'Dornbirn', 14, 'City', {name='Vorarlberger',
307+
... description='Very nice people with a strange accent', interests=['mountains', 'cheese', 'enzian']});
308+
INSERT OK, 1 row affected (... sec)
309309

310310
.. _sql_dml_object_arrays:
311311

@@ -331,13 +331,24 @@ Example::
331331
+-------------------+-------------------------------------------------------------------------------------------+----------------------------+
332332
SELECT 2 rows in set (... sec)
333333

334+
cr> insert into locations (id, name, position, kind, informations)
335+
... values ('B', 'Berlin', 15, 'City', [{evolution_level=6, population=3600001}, {evolution_level=42, population=1}]);
336+
INSERT OK, 1 row affected (... sec)
337+
338+
cr> refresh table locations;
339+
REFRESH OK (... sec)
340+
334341
cr> select name from locations where 4 < ANY (informations['evolution_level']);
335342
+-------------------+
336343
| name |
337344
+-------------------+
345+
| Berlin |
338346
| North West Ripple |
339347
+-------------------+
340-
SELECT 1 row in set (... sec)
348+
SELECT 2 rows in set (... sec)
349+
350+
351+
341352

342353
.. _sql_dml_aggregation:
343354

@@ -406,7 +417,7 @@ Some Examples::
406417
+----------+
407418
| count(*) |
408419
+----------+
409-
| 13 |
420+
| 15 |
410421
+----------+
411422
SELECT 1 row in set (... sec)
412423

@@ -426,7 +437,7 @@ Some Examples::
426437
+-------------+----------+
427438
| count(name) | count(*) |
428439
+-------------+----------+
429-
| 12 | 13 |
440+
| 14 | 15 |
430441
+-------------+----------+
431442
SELECT 1 row in set (... sec)
432443

@@ -456,23 +467,25 @@ Some Examples::
456467
+----------+-------------+
457468
| count(*) | kind |
458469
+----------+-------------+
470+
| 2 | City |
459471
| 4 | Galaxy |
460472
| 5 | Planet |
461473
| 4 | Star System |
462474
+----------+-------------+
463-
SELECT 3 rows in set (... sec)
475+
SELECT 4 rows in set (... sec)
464476

465477
::
466478

467479
cr> select max(position), kind from locations group by kind order by max(position) desc;
468480
+---------------+-------------+
469481
| max(position) | kind |
470482
+---------------+-------------+
483+
| 15 | City |
471484
| 6 | Galaxy |
472485
| 5 | Planet |
473486
| 4 | Star System |
474487
+---------------+-------------+
475-
SELECT 3 rows in set (... sec)
488+
SELECT 4 rows in set (... sec)
476489

477490
::
478491

@@ -482,21 +495,23 @@ Some Examples::
482495
+------------------------------------+-------------+
483496
| | Planet |
484497
| Aldebaran | Star System |
498+
| Berlin | City |
485499
| Galactic Sector QQ7 Active J Gamma | Galaxy |
486500
+------------------------------------+-------------+
487-
SELECT 3 rows in set (... sec)
501+
SELECT 4 rows in set (... sec)
488502

489503
::
490504

491505
cr> select count(*), min(name), kind from locations group by kind order by kind;
492506
+----------+------------------------------------+-------------+
493507
| count(*) | min(name) | kind |
494508
+----------+------------------------------------+-------------+
509+
| 2 | Berlin | City |
495510
| 4 | Galactic Sector QQ7 Active J Gamma | Galaxy |
496511
| 5 | | Planet |
497512
| 4 | Aldebaran | Star System |
498513
+----------+------------------------------------+-------------+
499-
SELECT 3 rows in set (... sec)
514+
SELECT 4 rows in set (... sec)
500515

501516
::
502517

@@ -507,8 +522,9 @@ Some Examples::
507522
| 10.0 | Star System |
508523
| 13.0 | Galaxy |
509524
| 15.0 | Planet |
525+
| 29.0 | City |
510526
+---------------+-------------+
511-
SELECT 3 rows in set (... sec)
527+
SELECT 4 rows in set (... sec)
512528

513529
.. _sql_dml_group_by:
514530

@@ -528,8 +544,9 @@ This is useful if used in conjunction with aggregation functions::
528544
| 5 | Planet |
529545
| 4 | Galaxy |
530546
| 4 | Star System |
547+
| 2 | City |
531548
+----------+-------------+
532-
SELECT 3 rows in set (... sec)
549+
SELECT 4 rows in set (... sec)
533550

534551
.. note::
535552

@@ -572,8 +589,8 @@ In order to get the match score of the fulltext search, an internal system colum
572589
+-----------+------------+
573590
| name | _score |
574591
+-----------+------------+
575-
| Altair | 0.56319076 |
576-
| Bartledan | 0.4590714 |
592+
| Altair | 0.5965736 |
593+
| Bartledan | 0.49279192 |
577594
+-----------+------------+
578595
SELECT 2 rows in set (... sec)
579596

@@ -586,8 +603,8 @@ Of course it is possible to change it to use an ascending order instead::
586603
+-----------+------------+
587604
| name | _score |
588605
+-----------+------------+
589-
| Bartledan | 0.4590714 |
590-
| Altair | 0.56319076 |
606+
| Bartledan | 0.49279192 |
607+
| Altair | 0.5965736 |
591608
+-----------+------------+
592609
SELECT 2 rows in set (... sec)
593610

@@ -609,14 +626,16 @@ A negative fulltext search can be done using a ``NOT`` clause::
609626
| Alpha Centauri | 1.0 |
610627
| Allosimanius Syneca | 1.0 |
611628
| NULL | 1.0 |
629+
| Berlin | 1.0 |
612630
| North West Ripple | 1.0 |
613631
| Galactic Sector QQ7 Active J Gamma | 1.0 |
614632
| Algol | 1.0 |
615633
| Argabuthon | 1.0 |
616634
| Arkintoofle Minor | 1.0 |
617635
| | 1.0 |
636+
| Dornbirn | 1.0 |
618637
+------------------------------------+--------+
619-
SELECT 11 rows in set (... sec)
638+
SELECT 13 rows in set (... sec)
620639

621640

622641
Filter by :ref:`_score <sql_ddl_system_column_score>`
@@ -632,11 +651,11 @@ Anyway let's do it here for demonstration purpose::
632651

633652
cr> select name, \"_score\" from locations where match(name_description_ft, 'time')
634653
... and \"_score\" > 0.9;
635-
+--------+-----------+
636-
| name | _score |
637-
+--------+-----------+
638-
| Altair | 0.9204767 |
639-
+--------+-----------+
654+
+--------+------------+
655+
| name | _score |
656+
+--------+------------+
657+
| Altair | 0.93670994 |
658+
+--------+------------+
640659
SELECT 1 row in set (... sec)
641660

642661
As you maybe noticed, the :ref:`_score <sql_ddl_system_column_score>` value has changed for the

sql/src/main/java/io/crate/analyze/BlobTableAnalyzer.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.crate.core.NumberOfReplicas;
2626
import io.crate.metadata.TableIdent;
2727
import io.crate.metadata.blob.BlobSchemaInfo;
28-
import io.crate.sql.tree.ArrayLiteral;
2928
import io.crate.sql.tree.Expression;
3029
import io.crate.sql.tree.GenericProperties;
3130
import io.crate.sql.tree.Table;
@@ -58,22 +57,22 @@ protected static NumberOfReplicas extractNumberOfReplicas(
5857
Map<String,Expression> properties = genericProperties.properties();
5958
Expression number_of_replicas = properties.remove("number_of_replicas");
6059

61-
NumberOfReplicas replicas = null;
62-
60+
NumberOfReplicas numberOfReplicas = null;
6361
if (number_of_replicas != null) {
64-
Preconditions.checkArgument(!(number_of_replicas instanceof ArrayLiteral),
65-
"Invalid argument to \"number_of_replicas\"");
66-
67-
Object numReplicas = ExpressionToObjectVisitor.convert(number_of_replicas, parameters);
68-
if (numReplicas != null) {
69-
replicas = new NumberOfReplicas(numReplicas.toString());
62+
try {
63+
Integer numReplicas = ExpressionToNumberVisitor.convert(number_of_replicas, parameters).intValue();
64+
numberOfReplicas = new NumberOfReplicas(numReplicas);
65+
} catch (IllegalArgumentException e) {
66+
String numReplicas = ExpressionToObjectVisitor.convert(number_of_replicas, parameters).toString();
67+
numberOfReplicas = new NumberOfReplicas(numReplicas);
7068
}
7169
}
70+
7271
if (properties.size() > 0) {
7372
throw new IllegalArgumentException(
7473
String.format(Locale.ENGLISH, "Invalid properties \"%s\" passed to [ALTER | CREATE] BLOB TABLE statement",
7574
properties.keySet()));
7675
}
77-
return replicas;
76+
return numberOfReplicas;
7877
}
7978
}

sql/src/main/java/io/crate/analyze/DataStatementAnalyzer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,9 @@ protected Symbol visitNullLiteral(NullLiteral node, T context) {
437437
@Override
438438
public Symbol visitArrayLiteral(ArrayLiteral node, T context) {
439439
// TODO: support everything that is immediately evaluable as values
440-
switch (node.values().size()) {
441-
case 0:
442-
return Literal.newLiteral(new ArrayType(NullType.INSTANCE), new Object[0]);
443-
default:
440+
if (node.values().isEmpty()) {
441+
return Literal.newLiteral(new ArrayType(NullType.INSTANCE), new Object[0]);
442+
} else {
444443
DataType innerType = null;
445444
List<Literal> literals = new ArrayList<>(node.values().size());
446445
for (Expression e : node.values()) {

0 commit comments

Comments
 (0)