Skip to content

Commit b8f31d6

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

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

MP SELECT Ancestors.md

+35
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,38 @@ SELECT * FROM ancestors;
8181
~~~
8282

8383
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.
84+
85+
A similar query splits provided paths directly:
86+
87+
~~~sql
88+
WITH
89+
json_nodes(ids) AS (
90+
VALUES
91+
('["tcl/compat/zlib1/", "tcl/pkgs/thread2.8.7/tcl/cmdsrv/"]')
92+
),
93+
nodes AS (
94+
SELECT "key" + 1 AS id, value AS node_id
95+
FROM json_nodes AS jn, json_each(jn.ids) AS node_ids
96+
),
97+
tops AS (SELECT id, node_id AS path FROM nodes),
98+
levels AS (
99+
SELECT id, path, length(path) - length(replace(path, '/', '')) AS depth
100+
FROM tops
101+
),
102+
json_objs AS (
103+
SELECT *, json('{"' || replace(rtrim(path, '/'), '/', '": {"') ||
104+
'":""' || replace(hex(zeroblob(depth)), '00', '}')) AS json_obj
105+
FROM levels
106+
),
107+
ancestors AS (
108+
SELECT jo.id,
109+
replace(replace(substr(fullkey, 3), '.', '/'), '^#^', '.') || '/' AS asc_path
110+
FROM
111+
json_objs AS jo,
112+
json_tree(replace(jo.json_obj, '.', '^#^')) AS terms
113+
WHERE terms.parent IS NOT NULL
114+
)
115+
SELECT * FROM ancestors;
116+
~~~
117+
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*.

0 commit comments

Comments
 (0)