Skip to content

Commit 6dc2a7d

Browse files
rakhi-prathapsimon-dew
authored andcommitted
[DOC-10552] Changes to array operators and functions (#371)
1 parent 370103a commit 6dc2a7d

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed
42 Bytes
Loading

modules/n1ql/pages/n1ql-language-reference/arrayfun.adoc

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,119 @@ SELECT array_star(children).age FROM contacts WHERE fname = "Dave"
14971497
----
14981498
====
14991499

1500+
=== Empty Array Subscripts
1501+
1502+
You can use an empty array subscript (`[ ]`) to return an array that includes only defined elements.
1503+
1504+
The rules governing its use are as follows:
1505+
1506+
[cols="1a,2a"]
1507+
|====
1508+
| Expression | Description
1509+
1510+
| `field[]`
1511+
|* If `field` is not an array, it returns `NULL`.
1512+
* If `field` is an array, it returns the array as is.
1513+
1514+
1515+
| `field[][]`
1516+
|* If `field` is not an array, it returns `NULL`.
1517+
* If `field` contains only unnamed arrays, it returns `field` as is.
1518+
Otherwise, it returns `NULL`.
1519+
1520+
| `field[].field2`
1521+
|* If `field` is not an array, it returns `MISSING`.
1522+
* If `field` is an array, it extracts `field2` from each element in `field` and returns a new array with those values.
1523+
* If `field` contains unnamed arrays, they are flattened by one level.
1524+
Then from the resulting array, it extracts `field2` from each object where it is present.
1525+
1526+
| `field[][].field2`
1527+
|* If `field` is not an array, it returns `MISSING`.
1528+
* If every element in `field` is not an unnamed array, it returns `MISSING`.
1529+
* If `field` contains unnamed arrays, they are flattened by one level.
1530+
Then from the resulting array, it extracts `field2` from each object where it is present.
1531+
1532+
1533+
| `field[].field2[].field3`
1534+
|* Returns an array of `field3` values by traversing two levels of arrays.
1535+
First it extracts `field2` from each element of `field`, then extracts `field3` from each element of the resulting `field2` array.
1536+
* If `field` and `field2` contain unnamed arrays, they are flattened by one level.
1537+
Then from the resulting `field2` array, it extracts `field3` from each object where it is present.
1538+
1539+
|====
1540+
1541+
NOTE: If you use more that two empty array subscripts (for example, `field[][][]`), the function considers only the first two subscripts and ignores the rest.
1542+
1543+
==== Example
1544+
1545+
====
1546+
Given the following sample document:
1547+
1548+
[source,json]
1549+
----
1550+
{
1551+
"a": {
1552+
"airline": "AF",
1553+
"airlineid": "airline_003",
1554+
"destinationairport": "SFO",
1555+
"distance": 2481.617376098415,
1556+
"equipment": "320",
1557+
"id": 3,
1558+
"schedule": [
1559+
{
1560+
"day": 0,
1561+
"flight": "AF198",
1562+
"utc": "10:13:00"
1563+
},
1564+
{
1565+
"flight": "AF250",
1566+
"utc": "12:59:00"
1567+
},
1568+
{
1569+
"day": 2,
1570+
"flight": "AF223",
1571+
"special_flights": [
1572+
{
1573+
"a": "SA"
1574+
},
1575+
{
1576+
"b": [
1577+
[
1578+
{
1579+
"c": "SC1"
1580+
},
1581+
{
1582+
"c": "SC2"
1583+
}
1584+
],
1585+
[
1586+
{
1587+
"c": "SC3"
1588+
}
1589+
]
1590+
]
1591+
}
1592+
],
1593+
"utc": "19:41:00"
1594+
}
1595+
],
1596+
"sourceairport": "DFW"
1597+
}
1598+
}
1599+
----
1600+
1601+
Here's how different array subscripts evaluate:
1602+
1603+
* `a.airline[]`: Returns `NULL` (not an array).
1604+
* `a.schedule[]`: Equivalent to `a.schedule`, returns the array.
1605+
* `a.schedule[].day`: Returns `[0, 2]`. This is in contrast to `a.schedule[*].day`, which returns `[0, null, 2]`.
1606+
* `a.schedule[].special_flights[].a`: Returns `["SA"]`.
1607+
* `a.schedule[][].utc`: Returns `MISSING`.
1608+
* `a.schedule[].special_flights[].b[][].c`: Returns `["SC1", "SC2", "SC3"]`.
1609+
* `a.schedule[].special_flights[].b[].c`: Also returns `["SC1", "SC2", "SC3"]`, as the unnamed arrays are flattened.
1610+
1611+
====
1612+
15001613
[[fn-array-sum,ARRAY_SUM()]]
15011614
== ARRAY_SUM([.var]`expr`)
15021615

modules/n1ql/pages/n1ql-language-reference/nestedops.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ expr::
132132
An expression resolving to an array.
133133

134134
start-expr::
135-
A numeric expression specifying a start position in the array, counting from 0.
135+
[Optional] A numeric expression specifying a start position in the array, counting from 0.
136136
Negative positions are counted backwards from the end of the array.
137137

138138
end-expr::
@@ -144,6 +144,8 @@ Negative positions are counted backwards from the end of the array.
144144
A new subset of the source array, containing the elements from the start position to the end position minus 1.
145145
The element at the start position is included, while the element at the end position is not.
146146

147+
If the start position is omitted, the subset starts from the beginning of the source array.
148+
147149
If the end position is omitted, all elements from the start position to the end of the source array are included.
148150

149151
=== Example
@@ -164,4 +166,6 @@ Given the following data:
164166
The expression `revisions[1:3]` evaluates to the array value `[2012, 2011]`.
165167
166168
The expression `revisions[1:]` evaluates to the array value `[2012, 2011, 2010]`.
169+
170+
The expression `revisions[:2]` evaluates to the array value `[2013, 2012]`.
167171
====

modules/n1ql/partials/grammar/n1ql.ebnf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ element-expr ::= expr '[' position ']'
260260
/* end::element-expr[] */
261261

262262
/* tag::slice-expr[] */
263-
slice-expr ::= expr '[' start-expr ':' end-expr? ']'
263+
slice-expr ::= expr '[' start-expr? ':' end-expr? ']'
264264
/* end::slice-expr[] */
265265

266266
start-expr ::= expr

0 commit comments

Comments
 (0)