-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDDL.sql
More file actions
420 lines (389 loc) · 9.36 KB
/
DDL.sql
File metadata and controls
420 lines (389 loc) · 9.36 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
USE [Vectors]
GO
/****** Object: UserDefinedTableType [dbo].[PointType] Script Date: 28/06/2023 17:06:20 ******/
CREATE TYPE [dbo].[PointType] AS TABLE(
[ID] [bigint] NOT NULL,
[Idx] [smallint] NOT NULL,
[Value] [real] NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC,
[Idx] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
/****** Object: UserDefinedTableType [dbo].[RangeType] Script Date: 28/06/2023 17:06:20 ******/
CREATE TYPE [dbo].[RangeType] AS TABLE(
[RangeID] [bigint] NOT NULL,
[Dimension] [smallint] NULL,
[Mid] [real] NULL,
[LowRangeID] [bigint] NULL,
[HighRangeID] [bigint] NULL,
[ID] [bigint] NULL,
PRIMARY KEY CLUSTERED
(
[RangeID] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
/****** Object: UserDefinedFunction [dbo].[BuildIndex] Script Date: 28/06/2023 17:06:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
USE [Vectors]
GO
/****** Object: UserDefinedFunction [dbo].[BuildIndex] Script Date: 04/07/2023 12:54:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Builds range index for points.
create function [dbo].[BuildIndex]
(
-- a points table to build range index.
@points dbo.PointType readonly
)
returns @index table
(
RangeID bigint not null primary key,
Dimension smallint null,
Mid real null,
LowRangeID bigint null,
HighRangeID bigint null,
ID bigint null
)
as
begin
declare @ranges table
(
ID bigint,
RangeID bigint,
primary key(RangeID, ID)
);
declare @stats table
(
RangeID bigint not null primary key,
Idx smallint not null,
Mean real not null,
[Stdev] real,
Count bigint not null,
ID bigint not null
);
--raiserror(N'Level 0.', 0, 0) with nowait;
insert into @stats(RangeID, Idx, Mean, Stdev, Count, ID)
select top 1
0,
Idx,
avg(Value),
isnull(stdev(Value), 0) Stdev,
count_big(*),
avg(ID)
from
@points
group by
Idx
order by
Stdev desc
declare @next bit = @@rowcount;
if (@next != 0)
begin
insert @ranges(RangeID, ID)
select
iif(S.Stdev = 0, iif(P.ID <= S.ID, 1, 2), iif(Value < Mean, 1, 2)),
P.ID
from
@points P
join
@stats S
on
P.Idx = S.Idx and
S.Count > 1;
set @next = @@rowcount;
declare @level bigint = 0;
declare @i tinyint = 0;
while(@next != 0)
begin
--raiserror(N'Level %i.', 0, 0, @level) with nowait;
insert into @stats(RangeID, Idx, Mean, Stdev, Count, ID)
select
S.RangeID * 2 + N.I,
R.Idx,
R.Mean,
R.Stdev,
R.Count,
R.ID
from
@stats S
join
(select 1 union all select 2) N(I)
on
S.RangeID >= @level and
S.Count > 1
cross apply
(
select top 1
P.Idx,
avg(P.Value) Mean,
isnull(stdev(P.Value), 0) Stdev,
count_big(*) Count,
avg(P.ID) ID
from
@ranges R
join
@points P
on
P.ID = R.ID and
R.RangeID = S.RangeID * 2 + N.I
group by
Idx
order by
iif(@level % 2 = 1, Stdev, -Stdev) desc
) R;
set @level = @level * 2 + 1;
set @i += 1;
with R as
(
select
R.*,
R.RangeID * 2 +
case
when Value < Mean then 1
when Value > Mean then 2
when R.ID <= S.ID then 1
else 2
end NewRangeID
from
@ranges R
join
@stats S
on
S.RangeID = R.RangeID and
S.RangeID >= @level and
S.Count > 1
join
@points P
on
P.ID = R.ID and
P.Idx = S.Idx
)
update R
set
RangeID = NewRangeID;
set @next = @@rowcount;
end;
end;
insert into @index(RangeID, Dimension, Mid, LowRangeID, HighRangeID, ID)
select
RangeID,
iif(Stdev = 0, null, Idx) Dimension,
iif(Stdev = 0, null, Mean) Mid,
iif(Count = 1, null, RangeID * 2 + 1) LowRangeID,
iif(Count = 1, null, RangeID * 2 + 2) HighRangeID,
iif(Count = 1, ID, null) ID
from
@stats;
return;
end
GO
/****** Object: Table [dbo].[TextIndex] Script Date: 28/06/2023 17:06:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TextIndex](
[DocID] [bigint] NOT NULL,
[RangeID] [bigint] NOT NULL,
[Dimension] [smallint] NULL,
[Mid] [real] NULL,
[LowRangeID] [bigint] NULL,
[HighRangeID] [bigint] NULL,
[TextID] [bigint] NULL,
CONSTRAINT [PK_TextIndex] PRIMARY KEY CLUSTERED
(
[RangeID] ASC,
[DocID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [IX_TextIndex] UNIQUE NONCLUSTERED
(
[DocID] ASC,
[RangeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[Search] Script Date: 28/06/2023 17:06:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[Search]
(
-- json array of embedding vector
@point nvarchar(max),
-- a search domain.
@domain real,
-- Optional doc id.
@docId bigint = null
)
returns table
as
return
with Vector as
(
select
[key] Idx,
value - @domain MinValue,
value + @domain MaxValue
from
openjson(@point)
),
Node as
(
select
*
from
dbo.TextIndex
where
RangeID = 0 and
(@docId is null or DocID = @docId)
union all
select
I.*
from
dbo.TextIndex I
inner join
Node N
on
N.LowRangeID is not null and
I.DocID = N.DocID and
I.RangeID = N.LowRangeID and
(
N.Dimension is null or
N.Mid >= (select MinValue from Vector where Idx = N.Dimension)
)
union all
select
I.*
from
dbo.TextIndex I
inner join
Node N
on
N.HighRangeID is not null and
I.DocID = N.DocID and
I.RangeID = N.HighRangeID and
(
N.Dimension is null or
N.Mid <= (select MaxValue from Vector where Idx = N.Dimension)
)
)
select DocID, TextID from Node where TextID is not null;
GO
/****** Object: Table [dbo].[Document] Script Date: 28/06/2023 17:06:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Document](
[DocID] [bigint] NOT NULL,
[Name] [nvarchar](256) NULL,
CONSTRAINT [PK_Documents] PRIMARY KEY CLUSTERED
(
[DocID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Text] Script Date: 28/06/2023 17:06:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Text](
[DocID] [bigint] NOT NULL,
[TextID] [bigint] NOT NULL,
[Text] [nvarchar](max) NULL,
[Vector] [nvarchar](max) NULL,
CONSTRAINT [PK_Text] PRIMARY KEY CLUSTERED
(
[DocID] ASC,
[TextID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Document] ADD CONSTRAINT [DF_Document_DocID] DEFAULT (NEXT VALUE FOR [dbo].[DocumentID]) FOR [DocID]
GO
ALTER TABLE [dbo].[Text] ADD CONSTRAINT [DF_Text_TextID_1] DEFAULT (NEXT VALUE FOR [dbo].[TextID]) FOR [TextID]
GO
ALTER TABLE [dbo].[Text] WITH CHECK ADD CONSTRAINT [FK_Text_Document] FOREIGN KEY([DocID])
REFERENCES [dbo].[Document] ([DocID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Text] CHECK CONSTRAINT [FK_Text_Document]
GO
ALTER TABLE [dbo].[TextIndex] WITH CHECK ADD CONSTRAINT [FK_TextIndex_Document] FOREIGN KEY([DocID])
REFERENCES [dbo].[Document] ([DocID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[TextIndex] CHECK CONSTRAINT [FK_TextIndex_Document]
GO
/****** Object: StoredProcedure [dbo].[IndexDocument] Script Date: 28/06/2023 17:06:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[IndexDocument]
@docID bigint
as
begin
set nocount on;
declare @points dbo.PointType;
declare @index dbo.RangeType;
--raiserror(N'Start loading points.', 0, 0, @timespan) with nowait;
--set @start = current_timestamp;
insert into @points(ID, Idx, Value)
select
TextID, [key], value
from
dbo.Text
cross apply
openjson(Vector)
where
DocID = @docID;
--set @end = current_timestamp;
--set @timespan = datediff(ms, @start, @end);
--raiserror(N'Points loaded in %i milliseconds.', 0, 0, @timespan) with nowait;
--raiserror(N'Start building index.', 0, 0, @timespan) with nowait;
--set @start = current_timestamp;
--raiserror(N'Start building index.', 0, 0, @timespan) with nowait;
--set @start = current_timestamp;
insert into @index
select * from dbo.BuildIndex(@points);
--set @end = current_timestamp;
--set @timespan = datediff(ms, @start, @end);
--raiserror(N'Index built in %i milliseconds.', 0, 0, @timespan) with nowait;
-- Update index.
delete from dbo.TextIndex where DocID = @docID;
insert into dbo.TextIndex
(
DocID,
RangeID,
Dimension,
Mid,
LowRangeID,
HighRangeID,
TextID
)
select
@docID,
RangeID,
Dimension,
Mid,
LowRangeID,
HighRangeID,
ID
from
@index
end
GO