Skip to content

Commit c475eda

Browse files
committed
Document flatten command
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 732d9c8 commit c475eda

4 files changed

Lines changed: 89 additions & 9 deletions

File tree

docs/user/ppl/cmd/appendcol.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ appendcol
1111

1212
Description
1313
============
14-
| (Experimental)
15-
| (From 3.1.0)
16-
| Using ``appendcol`` command to append the result of a sub-search and attach it alongside with the input search results (The main search).
14+
(Experimental)
15+
(From 3.1.0)
16+
17+
Use ``appendcol`` command to append the result of a sub-search and attach it alongside with the input search results (The main search).
1718

1819
Version
1920
=======

docs/user/ppl/cmd/eventstats.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ evenstats
1111

1212
Description
1313
============
14-
| (Experimental)
15-
| (From 3.1.0)
16-
| Using ``evenstats`` command to enriches your event data with calculated summary statistics. It operates by analyzing specified fields within your events, computing various statistical measures, and then appending these results as new fields to each original event.
14+
(Experimental)
15+
(From 3.1.0)
16+
17+
Use ``evenstats`` command to enriches your event data with calculated summary statistics. It operates by analyzing specified fields within your events, computing various statistical measures, and then appending these results as new fields to each original event.
1718

1819
| Key aspects of `eventstats`:
1920

docs/user/ppl/cmd/flatten.rst

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,89 @@ Description
1212
===========
1313
From 3.1.0
1414

15-
Use ``flatten`` command to flatten a nested struct field into separate fields in a document.
15+
Use ``flatten`` command to flatten a nested struct / object field into separate
16+
fields in a document.
1617

18+
The flattened fields will be ordered **lexicographically** by their original
19+
key names in the struct. I.e. if the struct has keys ``b``, ``c`` and ``Z``,
20+
the flattened fields will be ordered as ``Z``, ``b``, ``c``.
21+
22+
Note that ``flatten`` does not work on arrays. Please use ``expand`` command
23+
to expand an array field into multiple rows instead. If the field is an nested
24+
array of structs, only the first element of the array will be flattened.
1725

1826
Syntax
1927
======
2028

21-
flatten <field-list> [as <alias-list>]
29+
flatten <field> [as (<alias-list>)]
30+
31+
* field: The field to be flatten. Currently only nested struct is supported.
32+
* alias-list: (Optional) The names to use instead of the original key names.
33+
Names are separated by commas. It is advised to put the alias-list in
34+
parentheses if there is more than one alias. E.g. both
35+
``country, state, city`` and ``(country, state, city)`` are supported,
36+
but the latter is advised. Its length must match the number of keys in the
37+
struct field. Please note that the provided alias names **must** follow
38+
the alphabetical order of the corresponding original keys in the struct.
39+
40+
Example: flatten a struct field with aliases
41+
============================================
42+
43+
Given the following index ``nested``
44+
45+
.. code-block::
46+
47+
{"message":{"info":"a","author":"e","dayOfWeek":1},"myNum":1}
48+
{"message":{"info":"b","author":"f","dayOfWeek":2},"myNum":2}
49+
50+
with the following mapping:
51+
52+
.. code-block:: json
53+
54+
{
55+
"mappings": {
56+
"properties": {
57+
"message": {
58+
"type": "nested",
59+
"properties": {
60+
"info": {
61+
"type": "keyword",
62+
"index": "true"
63+
},
64+
"author": {
65+
"type": "keyword",
66+
"fields": {
67+
"keyword": {
68+
"type": "keyword",
69+
"ignore_above": 256
70+
}
71+
},
72+
"index": "true"
73+
},
74+
"dayOfWeek": {
75+
"type": "long"
76+
}
77+
}
78+
},
79+
"myNum": {
80+
"type": "long"
81+
}
82+
}
83+
}
84+
}
85+
86+
87+
The following query flattens the ``message`` field and renames the keys to
88+
``creator, dow, info``:
89+
90+
PPL query::
91+
92+
PPL> source=nested | flatten message as (creator, dow, info);
93+
fetched rows / total rows = 2/2
94+
+-----------------------------------------+--------+---------+-----+------+
95+
| message | myNum | creator | dow | info |
96+
|-----------------------------------------|--------|---------|-----|------|
97+
| {"info":"a","author":"e","dayOfWeek":1} | 1 | e | 1 | a |
98+
| {"info":"b","author":"f","dayOfWeek":2} | 2 | f | 2 | b |
99+
+-----------------------------------------+--------+---------+-----+------+
22100

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteFlattenCommandIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testFlattenWithAliases() throws Exception {
7171
}
7272

7373
@Test
74-
public void testFlattenWithMismatchedNumberOfAliasesShouldThrow() throws Exception {
74+
public void testFlattenWithMismatchedNumberOfAliasesShouldThrow() {
7575
Throwable t =
7676
expectThrows(
7777
Exception.class,

0 commit comments

Comments
 (0)