<commit_before># -*- coding: utf-8 -*-
import os
import re
import sqlite3
import tempfile

from decimal import Decimal


# Schema:
#                           +----------------+     +=================+
#     +================+    | cell_label     |     | hierarchy       |
#     | cell           |    +----------------+     +=================+
#     +================+    | cell_label_id  |     | hierarchy_id    |--+
#  +--| cell_id        |--->| cell_id        |     | hierarchy_value |  |
#  |  | partial        |    | hierarchy_id   |<-+  | hierarchy_level |  |
#  |  +----------------+    | label_id       |<-+  +-----------------+  |
#  |                        +----------------+  |                       |
#  |   +----------------+                       |  +-----------------+  |
#  |   | property       |    +--------------+   |  | label           |  |
#  |   +----------------+    | node         |   |  +-----------------+  |
#  |   | property_id    |    +--------------+   +--| label_id        |  |
#  |   | property_key   |    | node_id      |   +--| hierarchy_id    |<-+
#  |   | property_value |    | node_hash    |      | label_value     |
#  |   | created_date   |    | created_date |      +-----------------+
#  |   +----------------+    +--------------+
#  |                                  +--------------------+
#  |          +==================+    | weight             |
#  |          | edge             |    +--------------------+
#  |          +==================+    | weight_id          |--+
#  |       +--| edge_id          |--->| edge_id            |  |
#  |       |  | edge_name        |    | weight_name        |  |
#  |       |  | edge_description |    | weight_description |  |
#  |       |  | other_node_hash  |    | weight_order       |  |
#  |       |  | other_node_name  |    | proportional       |  |
#  |       |  +------------------+    +--------------------+  |
#  |       |                                                  |
#  |       |   +---------------+     +--------------------+   |
#  |       |   | relation      |     | relation_weight    |   |
#  |       |   +---------------+     +--------------------+   |
#  |       |   | relation_id   |--+  | relation_weight_id |   |
#  |       +-->| edge_id       |  |  | weight_id          |<--+
#  |           | other_cell_id |  +->| relation_id        |
#  +---------->| cell_id       |     | weight_value       |
#              +---------------+     +--------------------+


# Register SQLite adapter/converter for Decimal type.
sqlite3.register_adapter(Decimal, str)
sqlite3.register_converter('TEXTNUM', lambda x: Decimal(x.decode('utf-8')))

_invalid_root_hierarchy = """
    SELECT hierarchy_value AS invalid_root
    FROM label
    NATURAL JOIN (SELECT hierarchy_id, hierarchy_value
                  FROM hierarchy
                  ORDER BY hierarchy_level
                  LIMIT 1)
    WHERE label_value != 'UNMAPPED'
    GROUP BY hierarchy_value
    HAVING COUNT(*) != 1
"""

_duplicate_label_sets = """
    SELECT *
    FROM (SELECT GROUP_CONCAT(label_id) AS label_set
          FROM (SELECT cell_id, label_id
                FROM cell_label
                ORDER BY cell_id, label_id)
          GROUP BY cell_id)
    GROUP BY label_set
    HAVING COUNT(*) > 1<commit_msg>Add `edge_order` column to schema diagram.<commit_after># -*- coding: utf-8 -*-
import os
import re
import sqlite3
import tempfile

from decimal import Decimal


# Schema:
#                           +----------------+     +=================+
#     +================+    | cell_label     |     | hierarchy       |
#     | cell           |    +----------------+     +=================+
#     +================+    | cell_label_id  |     | hierarchy_id    |--+
#  +--| cell_id        |--->| cell_id        |     | hierarchy_value |  |
#  |  | partial        |    | hierarchy_id   |<-+  | hierarchy_level |  |
#  |  +----------------+    | label_id       |<-+  +-----------------+  |
#  |                        +----------------+  |                       |
#  |   +----------------+                       |  +-----------------+  |
#  |   | property       |    +--------------+   |  | label           |  |
#  |   +----------------+    | node         |   |  +-----------------+  |
#  |   | property_id    |    +--------------+   +--| label_id        |  |
#  |   | property_key   |    | node_id      |   +--| hierarchy_id    |<-+
#  |   | property_value |    | node_hash    |      | label_value     |
#  |   | created_date   |    | created_date |      +-----------------+
#  |   +----------------+    +--------------+
#  |
#  |         +==================+     +--------------------+
#  |         | edge             |     | weight             |
#  |         +==================+     +--------------------+
#  |      +--| edge_id          |--+  | weight_id          |--+
#  |      |  | edge_name        |  +->| edge_id            |  |
#  |      |  | edge_description |     | weight_name        |  |
#  |      |  | edge_order       |     | weight_description |  |
#  |      |  | other_node_hash  |     | weight_order       |  |
#  |      |  | other_node_name  |     | proportional       |  |
#  |      |  +------------------+     +--------------------+  |
#  |      |                                                   |
#  |      |     +---------------+     +--------------------+  |
#  |      |     | relation      |     | relation_weight    |  |
#  |      |     +---------------+     +--------------------+  |
#  |      |     | relation_id   |--+  | relation_weight_id |  |
#  |      +---->| edge_id       |  |  | weight_id          |<-+
#  |            | other_cell_id |  +->| relation_id        |
#  +----------> | cell_id       |     | weight_value       |
#               +---------------+     +--------------------+


# Register SQLite adapter/converter for Decimal type.
sqlite3.register_adapter(Decimal, str)
sqlite3.register_converter('TEXTNUM', lambda x: Decimal(x.decode('utf-8')))

_invalid_root_hierarchy = """
    SELECT hierarchy_value AS invalid_root
    FROM label
    NATURAL JOIN (SELECT hierarchy_id, hierarchy_value
                  FROM hierarchy
                  ORDER BY hierarchy_level
                  LIMIT 1)
    WHERE label_value != 'UNMAPPED'
    GROUP BY hierarchy_value
    HAVING COUNT(*) != 1
"""

_duplicate_label_sets = """
    SELECT *
    FROM (SELECT GROUP_CONCAT(label_id) AS label_set
          FROM (SELECT cell_id, label_id
                FROM cell_label
                ORDER BY cell_id, label_id)
          GROUP BY cell_id)
    GROUP BY label_set
    HAVING COUNT(*) > 1
{'fraction_non_alphanumeric': 0.27264451524806554, 'fraction_numerical': 0.0024275527234107115, 'mean_word_length': 0.9173938336242001, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '60557', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1851, 'n_words': 265}