forked from metrico/qryn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
303 lines (279 loc) · 7.68 KB
/
index.js
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
const AttrConditionPlanner = require('./attr_condition')
const AttrConditionEvalPlanner = require('./attr_condition_eval')
const InitIndexPlanner = require('./init')
const IndexGroupByPlanner = require('./group_by')
const AggregatorPlanner = require('./aggregator')
const IndexLimitPlanner = require('./limit')
const TracesDataPlanner = require('./traces_data')
const { th } = require('date-fns/locale')
/**
* @param script {Token}
*/
module.exports.transpile = (script) => {
return new module.exports.Planner(script).plan()
}
/**
* @param script {Token}
*/
module.exports.evaluateCmpl = (script) => {
return new module.exports.Planner(script).planEval()
}
module.exports.Planner = class Planner {
/**
*
* @param script {Token}
*/
constructor (script) {
this.script = script
this.cond = null
this.terms = {}
this.termIdx = []
this.eval = null
this.preCond = null
this.preCondTerms = {}
this.precondTermsIdx = []
this.aggregatedAttr = ''
this.cmpVal = ''
this.aggFn = ''
}
plan () {
this.check()
this.analyze()
let res = (new AttrConditionPlanner())
.withTerms(this.termIdx)
.withConditions(this.cond)
.withAggregatedAttr(this.aggregatedAttr)
.withMain((new InitIndexPlanner()).build())
if (this.preCond) {
const preCond = (new AttrConditionPlanner())
.withTerms(this.precondTermsIdx)
.withConditions(this.preCond)
.withMain((new InitIndexPlanner()).build())
res = res.withPrecondition(preCond.build())
}
res = res.build()
res = (new IndexGroupByPlanner()).withMain(res).build()
if (this.aggFn) {
res = (new AggregatorPlanner())
.withFn(this.aggFn)
.withAttr(this.aggregatedAttr)
.withCompareFn(this.script.Child('cmp').value)
.withCompareVal(this.script.Child('cmp_val').value)
.withMain(res)
.build()
}
res = (new IndexLimitPlanner()).withMain(res).build()
res = (new TracesDataPlanner()).withMain(res).build()
res = (new IndexLimitPlanner()).withMain(res).build()
return res
}
planEval () {
this.check()
this.analyze()
const res = (new AttrConditionEvalPlanner())
.withTerms(this.termIdx)
.withConditions(this.cond)
.withAggregatedAttr(this.aggregatedAttr)
.withMain((new InitIndexPlanner()).build())
.build()
return res
}
setEvaluationResult (result) {
this.eval = {}
for (const row of result) {
this.eval[row.cond] = row.count
}
}
minify () {
const subcost = {}
for (let i = 0; i < this.termIdx.length; i++) {
subcost[this.termIdx[i].value] = Object.entries(this.eval)
.find(x => parseInt(x[0]) === i + 1)
subcost[this.termIdx[i].value] = subcost[this.termIdx[i].value]
? parseInt(subcost[this.termIdx[i].value][1])
: 0
}
if (!this.isDNF(this.cond)) {
return this.estimateCost(this.cond, subcost)
}
this.preCond = this.getSimplePrecondition(this.cond, subcost)
if (this.preCond) {
this.extractTermsIdx(this.preCond, this.precondTermsIdx, this.preCondTerms)
}
return this.preCond
? this.estimateCost(this.preCond, subcost)
: this.estimateCost(this.cond, subcost)
}
check () {
if (this.script.Children('SYNTAX').length > 1) {
throw new Error('more than one selector is not supported')
}
}
analyze () {
this.terms = {}
this.cond = this.analyzeCond(this.script.Child('attr_selector_exp'))
this.analyzeAgg()
}
/**
*
* @param token {Token}
* @param tree {{root: any}}
* @param place {{ref: any}}
*/
buildExpressionTree (token, tree, place) {
if (token.name !== 'attr_selector_exp') {
throw new Error('unsupported selector')
}
let leftHand = token.tokens[0]
if (token.tokens[0].name === 'complex_head') {
const newTree = { root: { ref: null } }
this.buildExpressionTree(token.tokens[0].tokens.find(x => x.name === 'attr_selector_exp'),
newTree,
newTree.root
)
leftHand = newTree.root
}
const tail = token.tokens.find(x => x.name === 'tail')
if (!tail) {
// if we have `a`
place.ref = leftHand
return
}
const andOr = token.tokens.find(x => x.name === 'and_or').value
const newPlace = { ref: null }
switch (andOr) {
case '&&':
place.ref = ['&&', { ref: leftHand }, newPlace]
this.buildExpressionTree(tail.tokens[0], tree, newPlace)
return
case '||':
place.ref = leftHand
tree.root = ['||', { ref: tree.root }, newPlace]
this.buildExpressionTree(tail.tokens[0], tree, newPlace)
}
}
/**
*
* @param t {{ref: any} | Token | Array}
* @returns {Token | Array}
*/
minimizeTree (t) {
while (t.ref) {
t = t.ref
}
if (!Array.isArray(t)) {
return t
}
for (let i = t.length - 1; i > 0; i--) {
t[i] = this.minimizeTree(t[i])
if (Array.isArray(t[i]) && t[i][0] === t[0]) {
t.splice(i, 1, ...t[i].slice(1))
}
}
return t
}
/**
* @param t {Token | Array}
* @returns {boolean}
*/
isDNF (t) {
if (t.name) {
return true
}
const fn = t[0]
for (let i = 1; i < t.length; i++) {
if (!this.isDNF(t[i])) {
return false
}
if (Array.isArray(t[i]) && fn === '&&' && t[i][0] === '||') {
return false
}
}
return true
}
/**
*
* @param t {Token | Array}
* @param subcosts {{[key: string]: number}}
* @returns number
*/
estimateCost (t, subcosts) {
if (t.name) {
return subcosts[t.value]
}
const fn = t[0]
const costs = t.slice(1).map(x => this.estimateCost(x, subcosts))
switch (fn) {
case '&&':
return Math.min(...costs)
case '||':
return costs.reduce((a, b) => a + b)
}
throw new Error('unsupported function')
}
/**
*
* @param t {Token | Array}
* @param subcosts {{[key: string]: number}}
*/
getSimplePrecondition (t, subcosts) {
if (!this.isDNF(t)) {
return null
}
if (t.name) {
return subcosts[t.value] < 10000000 ? t : null
}
const fn = t[0]
const self = this
const simplify = x => x.length === 2 ? x[1] : x
if (fn === '&&') {
const res = t.slice(1).filter(x => self.estimateCost(x, subcosts) < 10000000)
return res.length > 0 ? simplify(['&&', ...res]) : null
}
if (fn === '||') {
const res = t.slice(1).map(x => self.getSimplePrecondition(x, subcosts)).filter(x => x)
return res.length === t.length - 1 ? simplify(['||', ...res]) : null
}
throw new Error('unsupported function')
}
/**
*
* @param token {Token}
*/
analyzeCond (token) {
const tree = { root: { ref: null } }
this.buildExpressionTree(token, tree, tree.root)
tree.root = this.minimizeTree(tree.root)
this.extractTermsIdx(tree.root, this.termIdx, this.terms)
return tree.root
}
extractTermsIdx (t, termIdx, terms) {
const self = this
if (t.name) {
if (!terms[t.value]) {
termIdx.push(t)
terms[t.value] = termIdx.length
t.termIdx = termIdx.length - 1
} else {
t.termIdx = terms[t.value] - 1
}
return
}
if (Array.isArray(t)) {
t.forEach(x => self.extractTermsIdx(x, termIdx, terms))
}
}
analyzeAgg () {
const agg = this.script.Child('aggregator')
if (!agg) {
return
}
if (['count', 'sum', 'min', 'max', 'avg'].indexOf(agg.Child('fn').value) < 0) {
return
}
this.aggFn = agg.Child('fn').value
const labelName = agg.Child('attr').Child('label_name')
this.aggregatedAttr = labelName ? labelName.value : ''
this.cmpVal = agg.Child('cmp_val').value
}
}