Skip to content

Commit e614018

Browse files
committed
Materialized Paths
1 parent be33731 commit e614018

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

MP Overview.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: default
3+
title: Overview
4+
nav_order: 1
5+
parent: Materialized Paths
6+
permalink: /mat-paths/overview
7+
---
8+
9+
10+
<a name="References"></a>
11+
### References
12+
13+
1. [Joe Celko's Trees and Hierarchies in SQL for Smarties, 2nd edition][Celko's Trees]
14+
2. [SQL Design Patterns: The Expert Guide to SQL Programming][Tropashko]
15+
3. [Nested Sets and Materialized Path SQL Trees][NS-MP]
16+
4. [Storing trees in RDBMS][Kolesnikova]
17+
5. [Django-Treebeard tree library for Django Web Framework][django-treebeard]
18+
6. [PostgreSQL tree module ltree][PostgreSQL ltree]
19+
20+
21+
<!-- References -->
22+
23+
[Celko's Trees]: https://sciencedirect.com/book/9780123877338
24+
[Tropashko]: https://vadimtropashko.wordpress.com/%22sql-design-patterns%22-book/about
25+
[NS-MP]: http://rampant-books.com/art_vadim_nested_sets_sql_trees.htm
26+
[django-treebeard]: https://django-treebeard.readthedocs.io
27+
[PostgreSQL ltree]: https://www.postgresql.org/docs/current/ltree.html
28+
[Kolesnikova]: https://bitworks.software/en/2017-10-20-storing-trees-in-rdbms.html

Patterns Fixed-Width Values.md

+2-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ permalink: /patterns/split-fwv
88

99
### Context
1010

11-
Consider a tree structure representing a hierarchical system of categories to be stored in a relational database. While the classical relational model does not mesh well with hierarchical data, there are several approaches to marrying these concepts (see [References](#References) for further information). In the Materialized/Enumerated Paths, each node record in the *nodes* table stores information about its absolute path. There are several approaches to encoding this path, which may or may not include the node itself. Let us assume that each node is assigned an ID consisting of eight randomly selected alphanumeric ASCII characters. An analog of file system path constructed from a sequence of ancestor IDs can act as the node path. Furthermore, because node ID has a fixed length, a "path separator" is not necessary, for example:
11+
Consider a tree structure representing a hierarchical system of categories to be stored in a relational database. While the classical relational model does not mesh well with hierarchical data, there are several approaches to marrying these concepts (see [References](/mat-paths/overview#References) for further information). In the Materialized/Enumerated Paths, each node record in the *nodes* table stores information about its absolute path. There are several approaches to encoding this path, which may or may not include the node itself. Let us assume that each node is assigned an ID consisting of eight randomly selected alphanumeric ASCII characters. An analog of file system path constructed from a sequence of ancestor IDs can act as the node path. Furthermore, because node ID has a fixed length, a "path separator" is not necessary, for example:
1212

1313
~~~json
1414
{"BE0A8514": "afc2e40a40CF97B4704BA7F4F4CE4D5BF5A2A524"}
@@ -52,7 +52,7 @@ SELECT * FROM json_prefixes;
5252
1. ID length is necessary to split the prefix. This value may be provided via a dedicated query parameter or hardcoded. *id_sizes* uses a third option: because this query expects both node id and prefix, it grabs node ID from the first input pair and takes its length.
5353
2. *positions* query is possibly a bit over-engineered way of creating a table containing offsets of different *prefix* components to be used by the *substr* function. To start, the query determines length(*prefix*) / length(*node ID*) = *IDs_in_prefix* ratio used to produce a dummy JSON array of the same length. First, the *hex* and *zeroblob* functions produce a zero-field string template. Then, the *replace* function inserts JSON element separators (commas) into the template. Because the *hex/zeroblob* pair produces a doubled length string, _replace_ swaps every two zeros with a single zero. There is no comma after the last "0", so *IDs_in_prefix* is reduced by one, and the last zero prefixes the closing bracket. Finally, the *json_each* table-valued function splits this template and returns a table with the "key" column containing the offsets of JSON array elements, and "key" x id_size + 1 can be used as offsets for *substr*.
5454
3. *ascii_ids* uses *substr* to generate rows containing *prefix* elements labeled with both node ID and the element position in the original string.
55-
4. *json_prefixes* collapses rows belonging to the same node ID (the *ascii_ids* is sorted on *ascii_id* and *position*), yielding the final result.
55+
4. *json_prefixes* collapses rows belonging to the same node ID (the *ascii_ids* is sorted on *ascii_id* and *position*), yielding the final result.
5656

5757
Query output:
5858

@@ -80,26 +80,3 @@ to switch to the JSON-based input format:
8080
{"ascii_id_1": "prefix_1", "ascii_id_2": "prefix_2",...}
8181
~~~
8282

83-
84-
85-
86-
<a name="References"></a>
87-
### References
88-
89-
1. [Joe Celko's Trees and Hierarchies in SQL for Smarties, 2nd edition][Celko's Trees]
90-
2. [SQL Design Patterns: The Expert Guide to SQL Programming][Tropashko]
91-
3. [Nested Sets and Materialized Path SQL Trees][NS-MP]
92-
4. [Storing trees in RDBMS][Kolesnikova]
93-
5. [Django-Treebeard tree library for Django Web Framework][django-treebeard]
94-
6. [PostgreSQL tree module ltree][PostgreSQL ltree]
95-
96-
97-
<!-- References -->
98-
99-
[Celko's Trees]: https://sciencedirect.com/book/9780123877338
100-
[Tropashko]: https://vadimtropashko.wordpress.com/%22sql-design-patterns%22-book/about
101-
[NS-MP]: http://rampant-books.com/art_vadim_nested_sets_sql_trees.htm
102-
[django-treebeard]: https://django-treebeard.readthedocs.io
103-
[PostgreSQL ltree]: https://www.postgresql.org/docs/current/ltree.html
104-
[Kolesnikova]: https://bitworks.software/en/2017-10-20-storing-trees-in-rdbms.html
105-
[DSV]: /strings/split-dsv#DSV-Query

0 commit comments

Comments
 (0)