/
statvar_match.go
336 lines (309 loc) · 8.78 KB
/
statvar_match.go
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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package statvar
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"hash/fnv"
"math"
"sort"
"strings"
"time"
_ "github.com/mattn/go-sqlite3" // sqlite3 used for the search database.
pb "github.com/datacommonsorg/mixer/internal/proto"
"github.com/datacommonsorg/mixer/internal/server/resource"
"github.com/datacommonsorg/mixer/internal/store"
"github.com/datacommonsorg/mixer/internal/util"
)
const defaultLimit = 5
// Table and triggers creation:
// - 'statvars' is the main table containing information about a single statistical variable.
// - 'statvars_fts_idx' is a virtual FTS5 table used for providing full-text search capabilities on the statvars' content field.
// The setup follow the "External Content Tables" in the FTS5 documentation: https://www.sqlite.org/fts5.html
const sqlCreateTables = `
CREATE TABLE statvars(
doc_id INTEGER PRIMARY KEY,
dcid TEXT,
name TEXT,
signature INTEGER UNIQUE NOT NULL,
num_constraints INTEGER,
content TEXT);
CREATE VIRTUAL TABLE statvars_fts_idx USING fts5(
content,
content="statvars",
content_rowid="doc_id",
tokenize = "ascii tokenchars '-_'");
-- Triggers to keep the FTS index up to date.
CREATE TRIGGER statvars_ai AFTER INSERT ON statvars
BEGIN
INSERT INTO statvars_fts_idx(rowid, content)
VALUES (new.doc_id, new.content);
END;
CREATE TRIGGER statvars_ad AFTER DELETE ON statvars
BEGIN
INSERT INTO statvars_fts_idx(statvars_fts_idx, rowid, content)
VALUES ('delete', old.doc_id, old.content);
END;
CREATE TRIGGER statvars_au AFTER UPDATE ON statvars
BEGIN
INSERT INTO statvars_fts_idx(statvars_fts_idx, rowid, content)
VALUES ('delete', old.doc_id, old.content);
INSERT INTO statvars_fts_idx(rowid, content)
VALUES (new.doc_id, new.content);
END;
`
// See "The 'optimize' Command" in https://www.sqlite.org/fts5.html
const sqlOptimize = `INSERT INTO statvars_fts_idx(content) VALUES('optimize');`
const sqlInsert = `
INSERT INTO statvars(
doc_id,
dcid,
name,
signature,
num_constraints,
content) VALUES (?, ?, ?, ?, ?, ?);
`
// Returns the highest scoring result.
// In case of ties, returns statvars in descending order by number of constraints.
const sqlSearch = `
WITH statvars_rows AS (
SELECT
ROWID,
*
FROM
statvars
)
SELECT
statvars_fts_idx.rank * -1 as rank,
statvars_rows.dcid AS dcid,
statvars_rows.name AS name,
"%s" || highlight(statvars_fts_idx, 0, '[', ']') AS explanation
FROM
statvars_rows
JOIN statvars_fts_idx ON statvars_rows.ROWID = statvars_fts_idx.ROWID
WHERE
statvars_fts_idx MATCH (?)
ORDER BY
statvars_fts_idx.rank,
statvars_rows.num_constraints DESC
LIMIT
%d;
`
// Exact search by signature.
const sqlSearchBySignature = `
SELECT
1.0 AS rank,
dcid,
name,
"Full match on: " || content AS explanation
FROM
statvars
WHERE
signature = (?)
LIMIT
1;
`
// A Document models a statvar indexed in the sqlite DB.
type Document struct {
// ID of the statvar.
ID string
// Title of the document. For a statvar this will be the DisplayName.
Title string
// A signature text that uniquely identifies the statvar from the key value text.
Signature int
// A key value pairs string describing the properties of a stat var.
KeyValueText string
// Number of constraints associated to a statvar.
NumConstraints int32
}
func computeSignature(terms []string) int {
sort.Strings(terms)
hasher := fnv.New32()
for _, term := range terms {
_, err := hasher.Write([]byte(term))
// Ignore the error.
if err != nil {
continue
}
}
return int(hasher.Sum32())
}
func buildSortedDocumentSet(rawSvg map[string]*pb.StatVarGroupNode) []Document {
documents := make([]Document, 0)
for _, svgData := range rawSvg {
for _, svData := range svgData.ChildStatVars {
if strings.HasPrefix(svData.Id, "dc/") {
continue
}
numConstraints := int32(strings.Count(svData.Definition, ",") + 1)
keyValueText := strings.Replace(strings.Replace(svData.Definition, ",", " ", -1), "=", " ", -1)
signature := computeSignature(strings.Split(keyValueText, " "))
documents = append(documents, Document{
ID: svData.Id,
Title: svData.DisplayName,
Signature: signature,
KeyValueText: keyValueText,
NumConstraints: numConstraints,
})
}
}
sort.Slice(documents, func(i, j int) bool {
return documents[i].ID < documents[j].ID
})
return documents
}
// BuildSQLiteIndex builds the sqlite search index for all the stat vars.
func BuildSQLiteIndex(
rawSvg map[string]*pb.StatVarGroupNode,
) (*sql.DB, error) {
defer util.TimeTrack(time.Now(), "BuildSQLiteIndex")
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
return nil, err
}
_, err = db.Exec(sqlCreateTables)
if err != nil {
return nil, err
}
tx, err := db.Begin()
if err != nil {
return nil, err
}
stmt, err := tx.Prepare(sqlInsert)
if err != nil {
return nil, err
}
defer stmt.Close()
docSet := buildSortedDocumentSet(rawSvg)
for index, doc := range docSet {
_, err = stmt.Exec(index, doc.ID, doc.Title, doc.Signature, doc.NumConstraints, doc.KeyValueText)
if err != nil {
fmt.Printf("Ignoring statvar with DCID=%s as its signature is not unique.\n", doc.ID)
continue
}
}
err = tx.Commit()
if err != nil {
return nil, err
}
_, err = db.Exec(sqlOptimize)
if err != nil {
return nil, err
}
return db, nil
}
func roundFloat(val float64, precision uint) float64 {
ratio := math.Pow(10, float64(precision))
return math.Round(val*ratio) / ratio
}
func parseSQLQueryResults(err error, rows *sql.Rows, result *pb.GetStatVarMatchResponse) error {
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var score float64
matchInfo := &pb.GetStatVarMatchResponse_MatchInfo{}
err := rows.Scan(&score, &matchInfo.StatVar, &matchInfo.StatVarName, &matchInfo.Explanation)
if err != nil {
return err
}
matchInfo.Score = roundFloat(score, 4)
result.MatchInfo = append(result.MatchInfo, matchInfo)
}
return nil
}
func searchByTerms(db *sql.DB, escapedQuery string, explanationPrefix string, limit int32, result *pb.GetStatVarMatchResponse) error {
rows, err := db.Query(fmt.Sprintf(sqlSearch, explanationPrefix, limit), escapedQuery)
return parseSQLQueryResults(err, rows, result)
}
func searchBySignature(db *sql.DB, queryTerms []string, result *pb.GetStatVarMatchResponse) error {
rows, err := db.Query(sqlSearchBySignature, computeSignature(queryTerms))
return parseSQLQueryResults(err, rows, result)
}
func escapeForSqliteSearch(terms []string) []string {
escapedTerms := make([]string, 0)
for _, token := range terms {
jsonToken, _ := json.Marshal(token)
escapedTerms = append(escapedTerms, string(jsonToken))
}
return escapedTerms
}
func searchRelatedStatvars(db *sql.DB, queryTerms []string, limit int32, result *pb.GetStatVarMatchResponse) error {
err := searchBySignature(db, queryTerms, result)
if err != nil {
return err
}
if len(result.MatchInfo) > 0 {
return nil
}
escapedTerms := escapeForSqliteSearch(queryTerms)
err = searchByTerms(db, strings.Join(escapedTerms, " "), "AND query: ", limit, result)
if err != nil {
return err
}
if len(result.MatchInfo) > 0 {
return nil
}
return searchByTerms(db, strings.Join(escapedTerms, " OR "), "OR query: ", limit, result)
}
func normalizeToken(token string) string {
if token == "measuredProperty" {
return "mp"
}
if token == "populationType" {
return "pt"
}
if token == "statType" {
return "st"
}
if token == "measurementQualifier" {
return "mq"
}
if token == "measurementDenominator" {
return "md"
}
return token
}
func tokenizeQuery(query string) []string {
tokens := make([]string, 0)
for _, token := range strings.Split(query, " ") {
tokens = append(tokens, normalizeToken(token))
}
return tokens
}
// GetStatVarMatch implements API for Mixer.GetStatVarMatch.
func GetStatVarMatch(
ctx context.Context,
in *pb.GetStatVarMatchRequest,
store *store.Store,
cache *resource.Cache,
) (*pb.GetStatVarMatchResponse, error) {
limit := in.GetLimit()
if limit == 0 {
limit = defaultLimit
}
queryTerms := tokenizeQuery(in.GetQuery())
result := &pb.GetStatVarMatchResponse{}
err := searchRelatedStatvars(cache.SQLiteDb, queryTerms, limit, result)
if err != nil {
return nil, err
}
sort.SliceStable(result.MatchInfo, func(i, j int) bool {
return result.MatchInfo[i].Score > result.MatchInfo[j].Score
})
return result, nil
}