Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-10-06 6bac54b555d27f1a85c1e4f011a6e7bd9111b0d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
-- Hybrid chunking test
-- This test verifies the hybrid chunking strategy (Docling-style)
 
-- Test 1: Basic hybrid chunking with markdown structure
SELECT array_length(
    pgedge_vectorizer.chunk_text(
        '# Introduction
 
This is the introduction paragraph.
 
## Section 1
 
This is section 1 content.
 
## Section 2
 
This is section 2 content.',
        'hybrid',
        100,
        10
    ),
    1
) >= 1 AS hybrid_basic_works;
 
-- Test 2: Verify heading context is preserved (check any chunk has context)
SELECT bool_or(chunk LIKE '%[Context:%') AS has_context
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Main Title
 
Introduction text.
 
## Subsection
 
Subsection content here.',
        'hybrid',
        50,
        0
    )
) AS chunk;
 
-- Test 3: Code blocks should be kept together
SELECT bool_or(chunk LIKE '%```%') AS code_blocks_preserved
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Code Example
 
Here is some code:
 
```python
def hello():
    print("Hello, World!")
```
 
That was the code.',
        'hybrid',
        200,
        0
    )
) AS chunk;
 
-- Test 4: Lists should be handled properly
SELECT
    array_length(
        pgedge_vectorizer.chunk_text(
            '# Shopping List
 
- Apples
- Bananas
- Oranges
- Grapes
 
# Todo List
 
1. Wake up
2. Code
3. Sleep',
            'hybrid',
            50,
            0
        ),
        1
    ) >= 1 AS lists_handled;
 
-- Test 5: Large document chunking with structure preservation
SELECT
    array_length(
        pgedge_vectorizer.chunk_text(
            '# Chapter 1: Introduction
 
This is a long introduction that spans multiple sentences. We need to test how the hybrid chunker handles large documents with multiple sections and subsections. The goal is to maintain semantic coherence while respecting token limits.
 
## 1.1 Background
 
The background section provides context. It explains the history and motivation behind this work. Understanding the background is essential for appreciating the contributions.
 
## 1.2 Objectives
 
Our objectives are:
- First objective is to test chunking
- Second objective is to verify structure
- Third objective is to ensure quality
 
# Chapter 2: Methods
 
This chapter describes our methods.
 
## 2.1 Data Collection
 
We collected data from various sources. The data was preprocessed and cleaned before analysis.
 
## 2.2 Analysis
 
The analysis involved multiple steps including statistical tests and machine learning models.',
            'hybrid',
            100,
            10
        ),
        1
    ) >= 3 AS large_doc_chunked;
 
-- Test 6: Blockquotes handling
SELECT
    array_length(
        pgedge_vectorizer.chunk_text(
            '# Quotes
 
Here is a famous quote:
 
> To be or not to be,
> that is the question.
 
And another one:
 
> The only thing we have to fear is fear itself.',
            'hybrid',
            100,
            0
        ),
        1
    ) >= 1 AS blockquotes_handled;
 
-- Test 7: Tables handling
SELECT bool_or(chunk LIKE '%|%') AS tables_preserved
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Data Table
 
| Name | Age | City |
|------|-----|------|
| Alice | 30 | NYC |
| Bob | 25 | LA |
 
The table above shows our data.',
        'hybrid',
        200,
        0
    )
) AS chunk;
 
-- Test 8: Empty content handling
SELECT array_length(
    pgedge_vectorizer.chunk_text(
        '',
        'hybrid',
        100,
        10
    ),
    1
) IS NULL AS empty_returns_null;
 
-- Test 9: Plain text fallback works (no context prefix for plain text)
SELECT bool_and(chunk NOT LIKE '%[Context:%') AS plain_text_no_context
FROM unnest(
    pgedge_vectorizer.chunk_text(
        'This is plain text without any markdown formatting. It should still be chunked properly by the hybrid strategy falling back to token based.',
        'hybrid',
        20,
        5
    )
) AS chunk;
 
-- Test 10: Nested headings preserve full context
SELECT bool_or(chunk LIKE '%Level 1%Level 2%Level 3%') AS nested_context_preserved
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Level 1
 
Content at level 1.
 
## Level 2
 
Content at level 2.
 
### Level 3
 
Content at level 3 should have full heading hierarchy.',
        'hybrid',
        100,
        0
    )
) AS chunk;
 
-- Test 11: Horizontal rules create section breaks
SELECT
    array_length(
        pgedge_vectorizer.chunk_text(
            '# Section A
 
Content A.
 
---
 
# Section B
 
Content B.',
            'hybrid',
            100,
            0
        ),
        1
    ) >= 1 AS hr_handled;
 
-- Test 12: Mixed content with all element types
SELECT
    array_length(
        pgedge_vectorizer.chunk_text(
            '# Documentation
 
Welcome to the docs.
 
## Getting Started
 
Install the package:
 
```bash
npm install mypackage
```
 
## Configuration
 
Configure with these options:
 
| Option | Default | Description |
|--------|---------|-------------|
| debug | false | Enable debug mode |
| timeout | 30 | Request timeout |
 
> Note: Always set timeout in production.
 
## Features
 
Key features include:
- Easy installation
- Fast performance
- Great documentation
 
---
 
## Conclusion
 
Thanks for reading!',
            'hybrid',
            100,
            10
        ),
        1
    ) >= 2 AS mixed_content_works;
 
-- Test 13: Chunks start properly (not mid-word)
SELECT bool_and(
    substring(chunk, 1, 1) ~ '^[\[A-Za-z#>\-\*0-9`|"'']'
) AS all_chunks_start_properly
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# First Section
 
This is content that needs to be split into multiple chunks for testing purposes.
 
## Second Section
 
More content here that will also be split.',
        'hybrid',
        15,
        3
    )
) AS chunk;
 
-- ============================================================================
-- FALLBACK BEHAVIOR TESTS
-- ============================================================================
 
-- Test 14: Plain text with hybrid strategy falls back to token-based
-- (no heading context should be added since it's not markdown)
SELECT bool_and(chunk NOT LIKE '%[Context:%') AS no_context_for_plain_text
FROM unnest(
    pgedge_vectorizer.chunk_text(
        'This is plain text without any markdown syntax at all. It does not have headings or code blocks or lists. The hybrid chunker should detect this and fall back to simple token-based chunking for efficiency.',
        'hybrid',
        30,
        5
    )
) AS chunk;
 
-- Test 15: Pure markdown strategy basic test
SELECT
    array_length(
        pgedge_vectorizer.chunk_text(
            '# Markdown Document
 
This is a markdown document.
 
## Section One
 
Content for section one.
 
## Section Two
 
Content for section two.',
            'markdown',
            100,
            0
        ),
        1
    ) >= 1 AS markdown_strategy_works;
 
-- Test 16: Markdown strategy preserves heading context
SELECT bool_or(chunk LIKE '%[Context:%') AS markdown_has_context
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Title
 
Intro paragraph.
 
## Subsection
 
Subsection content.',
        'markdown',
        50,
        0
    )
) AS chunk;
 
-- Test 17: Plain text with markdown strategy falls back to token-based
SELECT bool_and(chunk NOT LIKE '%[Context:%') AS markdown_fallback_no_context
FROM unnest(
    pgedge_vectorizer.chunk_text(
        'This document has no markdown formatting whatsoever. It is purely plain text that should be chunked using the token-based strategy as a fallback.',
        'markdown',
        30,
        5
    )
) AS chunk;
 
-- Test 18: Markdown detection - single heading is enough
SELECT bool_or(chunk LIKE '%[Context:%') AS heading_triggers_markdown
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Just One Heading
 
But this document has a heading, so it should be treated as markdown and get context.',
        'hybrid',
        100,
        0
    )
) AS chunk;
 
-- Test 19: Markdown detection - code fence is enough
SELECT bool_or(chunk LIKE '%```%') AS code_fence_preserved
FROM unnest(
    pgedge_vectorizer.chunk_text(
        'Here is some text with a code block:
 
```python
print("hello")
```
 
That was the code.',
        'hybrid',
        100,
        0
    )
) AS chunk;
 
-- Test 20: Token-based strategy never adds context prefix
SELECT bool_and(chunk NOT LIKE '%[Context:%') AS token_based_no_context
FROM unnest(
    pgedge_vectorizer.chunk_text(
        '# Heading
 
Content here.',
        'token_based',
        100,
        0
    )
) AS chunk;