Skip to content

Commit 9c36062

Browse files
committed
Update MP SELECT Ancestors.md
1 parent b8f31d6 commit 9c36062

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

MP SELECT Ancestors.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,20 @@ WITH
8080
SELECT * FROM ancestors;
8181
~~~
8282

83-
Blocks *json_nodes* through *tops* constitute the same prologue as [before](select-desc#prologue). The *prefixes* block retrieves node prefixes from the *categories* table (alternatively, prefixes could be calculated from the input by stripping the last path parts). *json_prefixes* prepares "nested doll" JSON objects, and the *ancestors* block generates ancestor lists. The last block temporarily escapes any periods in names by replacing them with '^#^' at the beginning and performing the inverse replacement at the end.
83+
Blocks *json_nodes* through *tops* constitute the same prologue as [before](select-desc#prologue). The *prefixes* block retrieves node prefixes from the *categories* table (alternatively, the query could incorporate the [path splitting template](../patterns/split-dsv#Split-Path)). *json_prefixes* prepares "nested doll" JSON objects. The *ancestors* block generates ancestor lists, temporarily replacing any periods in names with '^#^' at the beginning and reversing it at the end. Note that if the input contains multiple paths with a shared prefix, the *asc_path* will have duplicate values.
84+
85+
**Output**
86+
87+
| ascii_id | asc_path |
88+
|----------|---------------------------|
89+
| 0FDAF2C8 | tcl/ |
90+
| 0FDAF2C8 | tcl/compat/ |
91+
| BE0A8514 | tcl/ |
92+
| BE0A8514 | tcl/pkgs/ |
93+
| BE0A8514 | tcl/pkgs/thread2.8.7/ |
94+
| BE0A8514 | tcl/pkgs/thread2.8.7/tcl/ |
95+
96+
---
8497

8598
A similar query splits provided paths directly:
8699

@@ -105,14 +118,29 @@ WITH
105118
FROM levels
106119
),
107120
ancestors AS (
108-
SELECT jo.id,
121+
SELECT min(jo.id) AS id,
109122
replace(replace(substr(fullkey, 3), '.', '/'), '^#^', '.') || '/' AS asc_path
110123
FROM
111124
json_objs AS jo,
112125
json_tree(replace(jo.json_obj, '.', '^#^')) AS terms
113126
WHERE terms.parent IS NOT NULL
127+
GROUP BY asc_path
128+
ORDER BY id, asc_path
114129
)
115130
SELECT * FROM ancestors;
116131
~~~
117132

118-
This query splits paths as provided and no longer accesses the *categories* table. The _nodes_ block also labels paths with array element id generated by _json_each_ instead of *ascii_id*.
133+
This query splits paths as provided and no longer accesses the *categories* table. Additionally, the *nodes* block labels paths with array element id (instead of *ascii_id*) generated by *json_each*, and the *ancestors* block removes duplicate terms.
134+
135+
**Output**
136+
137+
| id | asc_path |
138+
|----|----------------------------------|
139+
| 1 | tcl/ |
140+
| 1 | tcl/compat/ |
141+
| 1 | tcl/compat/zlib1/ |
142+
| 2 | tcl/pkgs/ |
143+
| 2 | tcl/pkgs/thread2.8.7/ |
144+
| 2 | tcl/pkgs/thread2.8.7/tcl/ |
145+
| 2 | tcl/pkgs/thread2.8.7/tcl/cmdsrv/ |
146+

0 commit comments

Comments
 (0)