This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
/
value_predicate.js
292 lines (246 loc) · 8.38 KB
/
value_predicate.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
/**
* @license
* Copyright 2014 The Lovefield Project Authors. All Rights Reserved.
*
* 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
*
* http://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.
*/
goog.provide('lf.pred.ValuePredicate');
goog.require('goog.asserts');
goog.require('lf.Binder');
goog.require('lf.Exception');
goog.require('lf.Type');
goog.require('lf.eval.Registry');
goog.require('lf.eval.Type');
goog.require('lf.index.SingleKeyRange');
goog.require('lf.index.SingleKeyRangeSet');
goog.require('lf.pred.PredicateNode');
goog.require('lf.proc.Relation');
goog.require('lf.structs.set');
/**
* @constructor @struct
* @extends {lf.pred.PredicateNode}
*
* @template T
* @param {!lf.schema.Column} column
* @param {T} value
* @param {!lf.eval.Type} evaluatorType
*/
lf.pred.ValuePredicate = function(column, value, evaluatorType) {
lf.pred.ValuePredicate.base(this, 'constructor');
/** @type {!lf.schema.Column} */
this.column = column;
/** @type {T} */
this.value = value;
/** @type {!lf.eval.Type} */
this.evaluatorType = evaluatorType;
/** @private {!function(T, T):boolean} */
this.evaluatorFn_ = lf.eval.Registry.get().getEvaluator(
this.column.getType(), this.evaluatorType);
/**
* Whether this predicate should be applied reversed (return false where the
* original predicate returns true and vice versa).
* @private
*/
this.isComplement_ = false;
/** @private {T} */
this.binder_ = value;
};
goog.inherits(lf.pred.ValuePredicate, lf.pred.PredicateNode);
/** @override */
lf.pred.ValuePredicate.prototype.copy = function() {
var clone = new lf.pred.ValuePredicate(
this.column, this.value, this.evaluatorType);
clone.setBinder(this.binder_);
clone.setComplement(this.isComplement_);
clone.setId(this.getId());
return clone;
};
/** @override */
lf.pred.ValuePredicate.prototype.getColumns = function(opt_results) {
if (opt_results != null) {
opt_results.push(this.column);
return opt_results;
} else {
return [this.column];
}
};
/** @override */
lf.pred.ValuePredicate.prototype.getTables = function(opt_results) {
var tables = opt_results != null ? opt_results : lf.structs.set.create();
tables.add(this.column.getTable());
return tables;
};
/** @override */
lf.pred.ValuePredicate.prototype.setComplement = function(isComplement) {
this.isComplement_ = isComplement;
};
/** @param {T} binder */
lf.pred.ValuePredicate.prototype.setBinder = function(binder) {
this.binder_ = binder;
};
/**
* @private
* @throws {!lf.Exception}
*/
lf.pred.ValuePredicate.prototype.checkBinding_ = function() {
var bound = false;
if (!(this.value instanceof lf.Binder)) {
if (goog.isArray(this.value)) {
bound = !this.value.some(function(val) {
return val instanceof lf.Binder;
});
} else {
bound = true;
}
}
if (!bound) {
// 501: Value is not bounded.
throw new lf.Exception(501);
}
};
/** @override */
lf.pred.ValuePredicate.prototype.eval = function(relation) {
this.checkBinding_();
// Ignoring this.evaluatorFn_() for the case of the IN, in favor of a faster
// evaluation implementation.
if (this.evaluatorType == lf.eval.Type.IN) {
return this.evalAsIn_(relation);
}
var entries = relation.entries.filter(function(entry) {
return this.evaluatorFn_(
entry.getField(this.column),
this.value) != this.isComplement_;
}, this);
return new lf.proc.Relation(entries, relation.getTables());
};
/** @param {!Array<*>} values */
lf.pred.ValuePredicate.prototype.bind = function(values) {
/** @param {number} index */
var checkIndexWithinRange = function(index) {
if (values.length <= index) {
// 510: Cannot bind to given array: out of range.
throw new lf.Exception(510);
}
};
if (this.binder_ instanceof lf.Binder) {
var index = /** @type {!lf.Binder} */ (this.binder_).getIndex();
checkIndexWithinRange(index);
this.value = /** @type {T} */ (values[index]);
} else if (goog.isArray(this.binder_)) {
this.value = this.binder_.map(function(val) {
if (val instanceof lf.Binder) {
checkIndexWithinRange(val.getIndex());
return values[val.getIndex()];
} else {
return val;
}
});
}
};
/**
* Evaluates this predicate as an lf.eval.Type.IN. The execution time of this
* operation is O(N) where N is the number of entries to be evaluated.
* @param {!lf.proc.Relation} relation The relation to be checked.
* @return {!lf.proc.Relation} A relation that holds only the entries that
* satisfy the predicate.
* @private
*/
lf.pred.ValuePredicate.prototype.evalAsIn_ = function(relation) {
goog.asserts.assert(
this.evaluatorType == lf.eval.Type.IN,
'ValuePredicate#evalAsIn_() called for wrong predicate type.');
var valueSet = lf.structs.set.create(this.value);
var evaluatorFn = function(rowValue) {
return rowValue === null ? false :
(valueSet.has(rowValue) != this.isComplement_);
}.bind(this);
var entries = relation.entries.filter(function(entry) {
return evaluatorFn(entry.getField(this.column));
}, this);
return new lf.proc.Relation(entries, relation.getTables());
};
/** @override */
lf.pred.ValuePredicate.prototype.toString = function() {
return 'value_pred(' +
this.column.getNormalizedName() + ' ' +
this.evaluatorType + (this.isComplement_ ? '(complement)' : '') + ' ' +
this.value + ')';
};
/**
* @return {boolean} Whether this predicate can be converted to a KeyRange
* instance.
*/
lf.pred.ValuePredicate.prototype.isKeyRangeCompatible = function() {
this.checkBinding_();
return this.value !== null &&
(this.evaluatorType == lf.eval.Type.BETWEEN ||
this.evaluatorType == lf.eval.Type.IN ||
this.evaluatorType == lf.eval.Type.EQ ||
this.evaluatorType == lf.eval.Type.GT ||
this.evaluatorType == lf.eval.Type.GTE ||
this.evaluatorType == lf.eval.Type.LT ||
this.evaluatorType == lf.eval.Type.LTE);
};
/**
* Converts this predicate to a key range.
* NOTE: Not all predicates can be converted to a key range, callers must call
* isKeyRangeCompatible() before calling this method.
* @return {!lf.index.SingleKeyRangeSet} The key range set corresponding to
* this predicate.
*/
lf.pred.ValuePredicate.prototype.toKeyRange = function() {
goog.asserts.assert(
this.isKeyRangeCompatible(),
'Could not convert predicate to key range.');
var keyRange = null;
if (this.evaluatorType == lf.eval.Type.BETWEEN) {
keyRange = new lf.index.SingleKeyRange(
this.getValueAsKey_(this.value[0]), this.getValueAsKey_(this.value[1]),
false, false);
} else if (this.evaluatorType == lf.eval.Type.IN) {
var keyRanges = this.value.map(function(value) {
return lf.index.SingleKeyRange.only(value);
});
return new lf.index.SingleKeyRangeSet(
this.isComplement_ ?
lf.index.SingleKeyRange.complement(keyRanges) : keyRanges);
} else {
var value = this.getValueAsKey_(this.value);
if (this.evaluatorType == lf.eval.Type.EQ) {
keyRange = lf.index.SingleKeyRange.only(value);
} else if (this.evaluatorType == lf.eval.Type.GTE) {
keyRange = lf.index.SingleKeyRange.lowerBound(value);
} else if (this.evaluatorType == lf.eval.Type.GT) {
keyRange = lf.index.SingleKeyRange.lowerBound(value, true);
} else if (this.evaluatorType == lf.eval.Type.LTE) {
keyRange = lf.index.SingleKeyRange.upperBound(value);
} else {
// Must be this.evaluatorType == lf.eval.Type.LT.
keyRange = lf.index.SingleKeyRange.upperBound(value, true);
}
}
return new lf.index.SingleKeyRangeSet(
this.isComplement_ ? keyRange.complement() : [keyRange]);
};
/**
* @param {T} value
* @return {!lf.index.Index.SingleKey} The value in this predicated converted to
* an index key.
* @private
*/
lf.pred.ValuePredicate.prototype.getValueAsKey_ = function(value) {
if (this.column.getType() == lf.Type.DATE_TIME) {
return value.getTime();
}
return value;
};