oak-domain 6.1.0 → 6.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/base-app-domain/ActionDefDict.d.ts +1 -0
- package/es/base-app-domain/ActionDefDict.js +3 -1
- package/es/base-app-domain/EntityDict.d.ts +86 -1
- package/es/base-app-domain/Log/Storage.js +4 -0
- package/es/base-app-domain/Log/_baseSchema.d.ts +5 -0
- package/es/base-app-domain/Storage.js +3 -1
- package/es/base-app-domain/StyleDict.js +3 -1
- package/es/base-app-domain/Version/Action.d.ts +6 -0
- package/es/base-app-domain/Version/Action.js +2 -0
- package/es/base-app-domain/Version/Schema.d.ts +19 -0
- package/es/base-app-domain/Version/Schema.js +1 -0
- package/es/base-app-domain/Version/Storage.d.ts +3 -0
- package/es/base-app-domain/Version/Storage.js +54 -0
- package/es/base-app-domain/Version/Style.d.ts +3 -0
- package/es/base-app-domain/Version/Style.js +6 -0
- package/es/base-app-domain/Version/_baseSchema.d.ts +57 -0
- package/es/base-app-domain/Version/_baseSchema.js +1 -0
- package/es/compiler/dependencyBuilder.d.ts +1 -0
- package/es/compiler/dependencyBuilder.js +173 -55
- package/es/compiler/routerBuilder.d.ts +4 -1
- package/es/compiler/routerBuilder.js +22 -19
- package/es/compiler/schemaBuilder.d.ts +5 -1
- package/es/compiler/schemaBuilder.js +10 -0
- package/es/context/BackendRuntimeContext.d.ts +9 -1
- package/es/context/BackendRuntimeContext.js +13 -1
- package/es/entities/Log.d.ts +2 -0
- package/es/entities/Log.js +2 -0
- package/es/entities/Version.d.ts +14 -0
- package/es/entities/Version.js +42 -0
- package/es/store/AsyncRowStore.d.ts +4 -1
- package/es/store/AsyncRowStore.js +7 -0
- package/es/store/CascadeStore.js +22 -0
- package/es/store/RelationAuth/authorizationFilter.d.ts +11 -0
- package/es/store/RelationAuth/authorizationFilter.js +104 -0
- package/es/store/RelationAuth/continuation.d.ts +9 -0
- package/es/store/RelationAuth/continuation.js +33 -0
- package/es/store/RelationAuth/index.d.ts +141 -0
- package/es/store/RelationAuth/index.js +1440 -0
- package/es/store/RelationAuth/model.d.ts +38 -0
- package/es/store/RelationAuth/model.js +24 -0
- package/es/store/RelationAuth/userLookup.d.ts +21 -0
- package/es/store/RelationAuth/userLookup.js +102 -0
- package/es/store/SyncRowStore.d.ts +2 -1
- package/es/store/SyncRowStore.js +3 -0
- package/es/store/filter.fix.d.ts +128 -0
- package/es/store/filter.fix.js +2862 -0
- package/es/store/filter.js +14 -9
- package/es/store/triggers.js +239 -44
- package/es/types/Context.d.ts +2 -1
- package/es/types/Demand.d.ts +11 -1
- package/es/types/Environment.d.ts +13 -2
- package/es/types/Exception.d.ts +3 -3
- package/es/types/Exception.js +25 -22
- package/es/types/Expression.d.ts +4 -1
- package/es/types/Expression.js +1 -1
- package/es/types/Storage.d.ts +15 -2
- package/es/utils/SimpleConnector.js +5 -2
- package/es/utils/decimal.js +2 -1
- package/es/utils/fetch/index.ali.d.ts +3 -0
- package/es/utils/fetch/index.ali.js +31 -0
- package/es/utils/fetch/index.d.ts +3 -0
- package/es/utils/fetch/index.js +5 -0
- package/es/utils/fetch/index.mp.d.ts +5 -3
- package/es/utils/fetch/index.mp.js +5 -280
- package/es/utils/fetch/index.wechat.d.ts +5 -0
- package/es/utils/fetch/index.wechat.js +174 -0
- package/es/utils/fetch/mini-program.shared.d.ts +37 -0
- package/es/utils/fetch/mini-program.shared.js +85 -0
- package/es/utils/geo.d.ts +1 -1
- package/es/utils/geo.js +1 -1
- package/es/utils/random/random.ali.d.ts +6 -0
- package/es/utils/random/random.ali.js +21 -0
- package/es/utils/random/random.mp.d.ts +5 -1
- package/es/utils/random/random.mp.js +11 -5
- package/es/utils/random/random.native.d.ts +1 -1
- package/es/utils/random/random.native.js +7 -4
- package/es/utils/random/random.wechat.d.ts +1 -0
- package/es/utils/random/random.wechat.js +10 -0
- package/lib/base-app-domain/ActionDefDict.d.ts +1 -0
- package/lib/base-app-domain/ActionDefDict.js +3 -1
- package/lib/base-app-domain/EntityDict.d.ts +86 -1
- package/lib/base-app-domain/Log/Storage.js +4 -0
- package/lib/base-app-domain/Log/_baseSchema.d.ts +5 -0
- package/lib/base-app-domain/Storage.js +3 -1
- package/lib/base-app-domain/StyleDict.js +3 -1
- package/lib/base-app-domain/Version/Action.d.ts +6 -0
- package/lib/base-app-domain/Version/Action.js +5 -0
- package/lib/base-app-domain/Version/Schema.d.ts +19 -0
- package/lib/base-app-domain/Version/Schema.js +4 -0
- package/lib/base-app-domain/Version/Storage.d.ts +3 -0
- package/lib/base-app-domain/Version/Storage.js +57 -0
- package/lib/base-app-domain/Version/Style.d.ts +3 -0
- package/lib/base-app-domain/Version/Style.js +9 -0
- package/lib/base-app-domain/Version/_baseSchema.d.ts +57 -0
- package/lib/base-app-domain/Version/_baseSchema.js +2 -0
- package/lib/compiler/dependencyBuilder.d.ts +1 -0
- package/lib/compiler/dependencyBuilder.js +173 -54
- package/lib/compiler/routerBuilder.d.ts +4 -1
- package/lib/compiler/routerBuilder.js +22 -19
- package/lib/compiler/schemaBuilder.d.ts +5 -1
- package/lib/compiler/schemaBuilder.js +10 -0
- package/lib/context/BackendRuntimeContext.d.ts +9 -1
- package/lib/context/BackendRuntimeContext.js +13 -1
- package/lib/entities/Log.d.ts +2 -0
- package/lib/entities/Log.js +2 -0
- package/lib/entities/Version.d.ts +14 -0
- package/lib/entities/Version.js +43 -0
- package/lib/store/AsyncRowStore.d.ts +4 -1
- package/lib/store/AsyncRowStore.js +7 -0
- package/lib/store/CascadeStore.js +22 -0
- package/lib/store/RelationAuth/authorizationFilter.d.ts +11 -0
- package/lib/store/RelationAuth/authorizationFilter.js +110 -0
- package/lib/store/RelationAuth/continuation.d.ts +9 -0
- package/lib/store/RelationAuth/continuation.js +37 -0
- package/lib/store/RelationAuth/index.d.ts +141 -0
- package/lib/store/RelationAuth/index.js +1445 -0
- package/lib/store/RelationAuth/model.d.ts +38 -0
- package/lib/store/RelationAuth/model.js +27 -0
- package/lib/store/RelationAuth/userLookup.d.ts +21 -0
- package/lib/store/RelationAuth/userLookup.js +105 -0
- package/lib/store/SyncRowStore.d.ts +2 -1
- package/lib/store/SyncRowStore.js +3 -0
- package/lib/store/filter.fix.d.ts +128 -0
- package/lib/store/filter.fix.js +2876 -0
- package/lib/store/filter.js +14 -9
- package/lib/store/triggers.js +239 -44
- package/lib/types/Context.d.ts +2 -1
- package/lib/types/Demand.d.ts +11 -1
- package/lib/types/Environment.d.ts +13 -2
- package/lib/types/Exception.d.ts +3 -3
- package/lib/types/Exception.js +25 -22
- package/lib/types/Expression.d.ts +4 -1
- package/lib/types/Expression.js +1 -1
- package/lib/types/Storage.d.ts +15 -2
- package/lib/utils/SimpleConnector.js +5 -2
- package/lib/utils/decimal.js +2 -1
- package/lib/utils/fetch/index.ali.d.ts +3 -0
- package/lib/utils/fetch/index.ali.js +36 -0
- package/lib/utils/fetch/index.d.ts +3 -0
- package/lib/utils/fetch/index.js +7 -0
- package/lib/utils/fetch/index.mp.d.ts +5 -3
- package/lib/utils/fetch/index.mp.js +5 -282
- package/lib/utils/fetch/index.wechat.d.ts +5 -0
- package/lib/utils/fetch/index.wechat.js +180 -0
- package/lib/utils/fetch/mini-program.shared.d.ts +37 -0
- package/lib/utils/fetch/mini-program.shared.js +96 -0
- package/lib/utils/geo.d.ts +1 -1
- package/lib/utils/geo.js +1 -1
- package/lib/utils/random/random.ali.d.ts +6 -0
- package/lib/utils/random/random.ali.js +24 -0
- package/lib/utils/random/random.mp.d.ts +5 -1
- package/lib/utils/random/random.mp.js +11 -5
- package/lib/utils/random/random.native.d.ts +1 -1
- package/lib/utils/random/random.native.js +7 -5
- package/lib/utils/random/random.wechat.d.ts +1 -0
- package/lib/utils/random/random.wechat.js +13 -0
- package/package.json +8 -5
- package/es/store/RelationAuth.d.ts +0 -115
- package/es/store/RelationAuth.js +0 -1340
- package/lib/store/RelationAuth.d.ts +0 -115
- package/lib/store/RelationAuth.js +0 -1345
|
@@ -0,0 +1,2862 @@
|
|
|
1
|
+
import { assert } from '../utils/assert';
|
|
2
|
+
import { EXPRESSION_PREFIX } from "../types";
|
|
3
|
+
import { uniq, pick, difference, intersection, union, cloneDeep } from '../utils/toolkit';
|
|
4
|
+
import { judgeRelation } from './relation';
|
|
5
|
+
/**
|
|
6
|
+
* 把创建数据中可直接用于过滤的标量字段投影成 filter;关系对象和结构化字段不做机械复制。
|
|
7
|
+
* 对字段值为 null 的情况,创建语义转换为“字段缺失”:data[attr] = null => {$exists: false}。
|
|
8
|
+
*/
|
|
9
|
+
export function translateCreateDataToFilter(schema, entity, data, allowUnrecoganized) {
|
|
10
|
+
const data2 = {};
|
|
11
|
+
for (const attr in data) {
|
|
12
|
+
const rel = judgeRelation(schema, entity, attr, allowUnrecoganized);
|
|
13
|
+
if (rel === 1) {
|
|
14
|
+
// 只需要记住id和各种外键属性,不这样处理有些古怪的属性比如coordinate,其作为createdata和作为filter并不同构
|
|
15
|
+
if (![
|
|
16
|
+
'geometry',
|
|
17
|
+
'geography',
|
|
18
|
+
'st_geometry',
|
|
19
|
+
'st_point',
|
|
20
|
+
'function',
|
|
21
|
+
'sequence',
|
|
22
|
+
'array',
|
|
23
|
+
'object'
|
|
24
|
+
].includes(schema[entity].attributes[attr]?.type) || entity === 'userEntityGrant') {
|
|
25
|
+
// 这里要过滤掉userEntityGrant这一特殊对象,因为其relationIds和relationEntityFilter这两个属性后面要用
|
|
26
|
+
if (data[attr] === null) {
|
|
27
|
+
data2[attr] = {
|
|
28
|
+
$exists: false,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
data2[attr] = data[attr];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return data2;
|
|
38
|
+
}
|
|
39
|
+
function isFilterRecord(value) {
|
|
40
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
/** schema-aware 校验 filter 形状;结构异常时禁止压平,保留整棵输入树。 */
|
|
43
|
+
function hasValidFilterShape(entity, schema, value, caller, ancestors = new WeakSet()) {
|
|
44
|
+
if (!isFilterRecord(value)) {
|
|
45
|
+
console.warn(`${caller}: malformed filter object; preserving the original filter`);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (ancestors.has(value)) {
|
|
49
|
+
console.warn(`${caller}: cyclic filter object; preserving the original filter`);
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
ancestors.add(value);
|
|
53
|
+
try {
|
|
54
|
+
for (const [attr, condition] of Object.entries(value)) {
|
|
55
|
+
if (['$and', '$or', '$xor'].includes(attr)) {
|
|
56
|
+
if (!Array.isArray(condition) || attr === '$xor' && condition.length === 0
|
|
57
|
+
|| condition.some(branch => !isFilterRecord(branch))) {
|
|
58
|
+
console.warn(`${caller}: malformed ${attr} filter; preserving the original filter`);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
for (const branch of condition) {
|
|
62
|
+
if (!hasValidFilterShape(entity, schema, branch, caller, ancestors)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (attr === '$not') {
|
|
69
|
+
if (!hasValidFilterShape(entity, schema, condition, caller, ancestors)) {
|
|
70
|
+
console.warn(`${caller}: malformed $not filter; preserving the original filter`);
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (attr.startsWith('#') || attr.startsWith(EXPRESSION_PREFIX) || attr.toLowerCase() === '$text') {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (attr.startsWith('$')) {
|
|
79
|
+
console.warn(`${caller}: unsupported filter operator ${attr}; preserving the original filter`);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
let relation;
|
|
83
|
+
try {
|
|
84
|
+
relation = judgeRelation(schema, entity, attr);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
console.warn(`${caller}: invalid relation attribute ${attr}; preserving the original filter`);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
let relatedEntity;
|
|
91
|
+
if (relation === 2) {
|
|
92
|
+
relatedEntity = attr;
|
|
93
|
+
}
|
|
94
|
+
else if (typeof relation === 'string') {
|
|
95
|
+
relatedEntity = relation;
|
|
96
|
+
}
|
|
97
|
+
else if (Array.isArray(relation)) {
|
|
98
|
+
relatedEntity = relation[0];
|
|
99
|
+
if (!isFilterRecord(condition)) {
|
|
100
|
+
console.warn(`${caller}: malformed relation subquery ${attr}; preserving the original filter`);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const sqp = condition['#sqp'];
|
|
104
|
+
if (sqp !== undefined && !['in', 'not in', 'all', 'not all'].includes(sqp)) {
|
|
105
|
+
console.warn(`${caller}: unsupported #sqp operator for ${attr}; preserving the original filter`);
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (relatedEntity !== undefined
|
|
110
|
+
&& !hasValidFilterShape(relatedEntity, schema, condition, caller, ancestors)) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
ancestors.delete(value);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 尽量合并外键连接并保持交集语义,减少数据库中的 join。
|
|
122
|
+
* 若每个 filter 表示集合 `F_i`,结果始终满足 `result = ⋂ F_i`;不能安全压平时保留为 `$and`。
|
|
123
|
+
* 对一对多子查询,`not in(P) ∧ not in(Q) = not in(P ∨ Q)`,
|
|
124
|
+
* `all(P) ∧ all(Q) = all(P ∧ Q)`;`in` 与 `not all` 的 AND 不能按同一方式改写,因此保留独立子句。
|
|
125
|
+
* @param entity 当前 filter 所属实体
|
|
126
|
+
* @param schema 存储关系 schema
|
|
127
|
+
* @param filters 待求交集的 filters
|
|
128
|
+
* @returns 与输入查询集合交集等价的 filter
|
|
129
|
+
*/
|
|
130
|
+
function addFilterSegment(entity, schema, ...filters) {
|
|
131
|
+
let filter;
|
|
132
|
+
let filterSafeForMerge = true;
|
|
133
|
+
// 将一个完整条件加入 AND:S(result) = S(result) ∩ S(f)。
|
|
134
|
+
const addIntoAnd = (f) => {
|
|
135
|
+
assert(filter);
|
|
136
|
+
const branch = cloneDeep(f);
|
|
137
|
+
if (Array.isArray(filter.$and)) {
|
|
138
|
+
filter.$and.push(branch);
|
|
139
|
+
}
|
|
140
|
+
else if (Object.prototype.hasOwnProperty.call(filter, '$and')) {
|
|
141
|
+
console.warn('combineFilters(AND): malformed accumulated $and; preserving it as an opaque branch');
|
|
142
|
+
filter = {
|
|
143
|
+
$and: [cloneDeep(filter), branch],
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
Object.assign(filter, {
|
|
148
|
+
$and: [branch],
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const addSingleAttr = (attr, value) => {
|
|
153
|
+
assert(filter);
|
|
154
|
+
// Own-property 检查不可改成 truthy 检查:0、false、'' 都是有效约束。
|
|
155
|
+
if (!Object.prototype.hasOwnProperty.call(filter, attr)) {
|
|
156
|
+
Object.assign(filter, {
|
|
157
|
+
[attr]: value
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
// 优化两个都等值且相等
|
|
161
|
+
else if (filter[attr] === value) {
|
|
162
|
+
}
|
|
163
|
+
// value定义的查询被当前查询包含
|
|
164
|
+
else if (contains(entity, schema, {
|
|
165
|
+
[attr]: value,
|
|
166
|
+
}, {
|
|
167
|
+
[attr]: filter[attr],
|
|
168
|
+
}) === true) {
|
|
169
|
+
Object.assign(filter, {
|
|
170
|
+
[attr]: value,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
// 当前查询被value所定义的查询包含
|
|
174
|
+
else if (contains(entity, schema, {
|
|
175
|
+
[attr]: filter[attr],
|
|
176
|
+
}, {
|
|
177
|
+
[attr]: value
|
|
178
|
+
}) == true) {
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
addIntoAnd({
|
|
182
|
+
[attr]: value,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const manyToOneFilters = {};
|
|
187
|
+
// 暂存同一多对一关系上的子 filter,稍后在目标实体层递归求交集并折叠回关系属性。
|
|
188
|
+
const addManyToOneFilter = (attr, entity2, filter) => {
|
|
189
|
+
if (manyToOneFilters[attr]) {
|
|
190
|
+
manyToOneFilters[attr].push([entity2, filter]);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
manyToOneFilters[attr] = [[entity2, filter]];
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const oneToManyFilters = {};
|
|
197
|
+
// 暂存一对多子查询;之后必须依据 #sqp 的量词语义决定能否合并。
|
|
198
|
+
const addOneToManyFilter = (attr, entity2, filter) => {
|
|
199
|
+
if (oneToManyFilters[attr]) {
|
|
200
|
+
oneToManyFilters[attr].push([entity2, filter]);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
oneToManyFilters[attr] = [[entity2, filter]];
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
const addInner = (f) => {
|
|
207
|
+
if (f !== undefined && f !== null) {
|
|
208
|
+
const inputSafeForMerge = hasValidFilterShape(entity, schema, f, 'combineFilters(AND)');
|
|
209
|
+
// 闭包内的类型收窄不会稳定保留;固定当前快照后再检查元数据冲突,避免
|
|
210
|
+
// `filter` 在后续赋值后被 TypeScript 视为可能为 undefined。
|
|
211
|
+
const currentFilter = filter;
|
|
212
|
+
const hasMetadataConflict = currentFilter !== undefined && Object.keys(f).some(attr => attr.startsWith('#')
|
|
213
|
+
&& Object.prototype.hasOwnProperty.call(currentFilter, attr)
|
|
214
|
+
&& currentFilter[attr] !== f[attr]);
|
|
215
|
+
if (hasMetadataConflict) {
|
|
216
|
+
console.warn('combineFilters(AND): conflicting metadata; preserving both filters');
|
|
217
|
+
filter = {
|
|
218
|
+
$and: [cloneDeep(currentFilter), cloneDeep(f)],
|
|
219
|
+
};
|
|
220
|
+
filterSafeForMerge = false;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (!filter) {
|
|
224
|
+
filter = inputSafeForMerge ? {} : cloneDeep(f);
|
|
225
|
+
filterSafeForMerge = inputSafeForMerge;
|
|
226
|
+
if (!inputSafeForMerge) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
else if (!inputSafeForMerge || !filterSafeForMerge) {
|
|
231
|
+
filter = {
|
|
232
|
+
$and: [cloneDeep(filter), cloneDeep(f)],
|
|
233
|
+
};
|
|
234
|
+
filterSafeForMerge = false;
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (Object.prototype.hasOwnProperty.call(f, '$or')) {
|
|
238
|
+
// (A ∧ (B ∨ C)) 不能普遍压平成各自属性;把整个 OR 保留为 AND 的一个项。
|
|
239
|
+
addIntoAnd(f);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
for (const attr in f) {
|
|
243
|
+
if (attr === '$and') {
|
|
244
|
+
f[attr].forEach((f2) => addInner(f2));
|
|
245
|
+
}
|
|
246
|
+
else if (attr.startsWith('$')) {
|
|
247
|
+
addIntoAnd({
|
|
248
|
+
[attr]: f[attr],
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
else if (attr.startsWith('#')) {
|
|
252
|
+
filter[attr] = f[attr];
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
const rel = judgeRelation(schema, entity, attr);
|
|
256
|
+
if (rel === 1) {
|
|
257
|
+
addSingleAttr(attr, f[attr]);
|
|
258
|
+
}
|
|
259
|
+
else if (rel === 2) {
|
|
260
|
+
addManyToOneFilter(attr, attr, f[attr]);
|
|
261
|
+
}
|
|
262
|
+
else if (typeof rel === 'string') {
|
|
263
|
+
addManyToOneFilter(attr, rel, f[attr]);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
assert(rel instanceof Array);
|
|
267
|
+
addOneToManyFilter(attr, rel[0], f[attr]);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
filters.forEach(ele => addInner(ele));
|
|
274
|
+
for (const attr in manyToOneFilters) {
|
|
275
|
+
const filters2 = manyToOneFilters[attr].map(ele => ele[1]);
|
|
276
|
+
const combined = addFilterSegment(manyToOneFilters[attr][0][0], schema, ...filters2);
|
|
277
|
+
addSingleAttr(attr, combined);
|
|
278
|
+
}
|
|
279
|
+
for (const attr in oneToManyFilters) {
|
|
280
|
+
const filters2 = oneToManyFilters[attr].map(ele => ele[1]);
|
|
281
|
+
const sqpOps = filters2.map(ele => ele['#sqp'] || 'in');
|
|
282
|
+
// 子查询条件之间只有在同一量词语义下存在可靠等价式时才合并;算子混用时保留原始 AND。
|
|
283
|
+
if (uniq(sqpOps).length > 1) {
|
|
284
|
+
filters2.forEach(ele => {
|
|
285
|
+
addIntoAnd({
|
|
286
|
+
[attr]: ele,
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const sqpOp = sqpOps[0];
|
|
292
|
+
if (sqpOp === 'not in') {
|
|
293
|
+
// ¬∃x:P(x) ∧ ¬∃x:Q(x) = ¬∃x:(P(x) ∨ Q(x));内层并集准确保留交集语义。
|
|
294
|
+
const unioned = unionFilterSegment(oneToManyFilters[attr][0][0], schema, ...filters2);
|
|
295
|
+
addSingleAttr(attr, Object.assign(unioned, {
|
|
296
|
+
['#sqp']: sqpOp,
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
else if (sqpOp === 'all') {
|
|
300
|
+
// ∀x:P(x) ∧ ∀x:Q(x) = ∀x:(P(x) ∧ Q(x));对空子集两侧也都为真。
|
|
301
|
+
const intersected = addFilterSegment(oneToManyFilters[attr][0][0], schema, ...filters2);
|
|
302
|
+
addSingleAttr(attr, Object.assign(intersected, {
|
|
303
|
+
['#sqp']: sqpOp,
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
filters2.forEach(ele => {
|
|
308
|
+
addIntoAnd({
|
|
309
|
+
[attr]: ele,
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return filter;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* 尽量把多个 filter 的并集压成一个 filter;无法证明形状变换等价时保留 `$or`。
|
|
319
|
+
* 标量等值集合满足 `a ∈ A ∨ a ∈ B ⇔ a ∈ (A ∪ B)`,因此可合并为 `$in`。
|
|
320
|
+
* 一对多量词仅安全支持 `in(P) ∨ in(Q) = in(P ∨ Q)` 和
|
|
321
|
+
* `not all(P) ∨ not all(Q) = not all(P ∧ Q)`;`not in` 与 `all` 不满足相应等价式,必须保留为 OR 分支。
|
|
322
|
+
*/
|
|
323
|
+
function unionFilterSegment(entity, schema, ...filters) {
|
|
324
|
+
let filter;
|
|
325
|
+
// OR 的局部合并要求两侧各只有一个可替换业务键,且元数据完全一致。
|
|
326
|
+
const possibleCombiningAttrs = (f1, f2) => {
|
|
327
|
+
let pca1s = [], pca2s = [];
|
|
328
|
+
const attributes1 = Object.keys(f1);
|
|
329
|
+
const attributes2 = Object.keys(f2);
|
|
330
|
+
for (const a of attributes1) {
|
|
331
|
+
if (a.startsWith('#')) {
|
|
332
|
+
if (f1[a] !== f2[a]) {
|
|
333
|
+
// metadata不相等,无法合并
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
pca1s.push(a);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
for (const a of attributes2) {
|
|
342
|
+
if (a.startsWith('#')) {
|
|
343
|
+
if (f1[a] !== f2[a]) {
|
|
344
|
+
// metadata不相等,无法合并
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
pca2s.push(a);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
// 只有每边恰好一个业务属性(忽略相同元数据)时才能局部替换;多属性的合取不能直接并入单属性。
|
|
353
|
+
if (pca1s.length !== 1 || pca2s.length !== 1) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
if (pca1s[0] !== pca2s[0] && pca1s[0] !== '$or' && pca2s[0] !== '$or') {
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
return [pca1s[0], pca2s[0]];
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* 尝试合并同一个属性到f1上,这里只合并等值查询和$in
|
|
363
|
+
* @param f1
|
|
364
|
+
* @param f2
|
|
365
|
+
* @param attr
|
|
366
|
+
* @param justTry
|
|
367
|
+
*/
|
|
368
|
+
// 对同一标量属性,将 x=a ∨ x=b(或两个 $in 集合)改写为 x∈A∪B。
|
|
369
|
+
const tryMergeAttributeValue = (f1, f2, attr, justTry) => {
|
|
370
|
+
const isMergeableScalar = (value) => typeof value === 'string'
|
|
371
|
+
|| typeof value === 'boolean'
|
|
372
|
+
|| typeof value === 'number' && Number.isFinite(value);
|
|
373
|
+
const getOperator = (value) => {
|
|
374
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
const keys = Object.keys(value);
|
|
378
|
+
return keys.length === 1 ? keys[0] : undefined;
|
|
379
|
+
};
|
|
380
|
+
const getValues = (value) => {
|
|
381
|
+
const operator = getOperator(value);
|
|
382
|
+
if (!operator) {
|
|
383
|
+
return isMergeableScalar(value) ? [value] : undefined;
|
|
384
|
+
}
|
|
385
|
+
if (operator === '$eq') {
|
|
386
|
+
return isMergeableScalar(value.$eq) ? [value.$eq] : undefined;
|
|
387
|
+
}
|
|
388
|
+
if (operator === '$in') {
|
|
389
|
+
return Array.isArray(value.$in) && value.$in.every(isMergeableScalar)
|
|
390
|
+
? value.$in
|
|
391
|
+
: undefined;
|
|
392
|
+
}
|
|
393
|
+
return undefined;
|
|
394
|
+
};
|
|
395
|
+
const values1 = getValues(f1[attr]);
|
|
396
|
+
const values2 = getValues(f2[attr]);
|
|
397
|
+
if (!values1 || !values2) {
|
|
398
|
+
// 数组、对象、非法数值和混合结构不能机械改写成标量 $in;保留原始 OR。
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
const types = new Set([...values1, ...values2].map(value => typeof value));
|
|
402
|
+
if (types.size > 1) {
|
|
403
|
+
// `x = 1 ∨ x = '1'` 不能在未知字段类型下安全归一化。
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
if (justTry) {
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
Object.assign(f1, {
|
|
410
|
+
[attr]: {
|
|
411
|
+
$in: [...values1, ...values2],
|
|
412
|
+
},
|
|
413
|
+
});
|
|
414
|
+
return true;
|
|
415
|
+
};
|
|
416
|
+
/**
|
|
417
|
+
* 把f2尝试combine到f1中,保持or的语义
|
|
418
|
+
* @param f1
|
|
419
|
+
* @param f2
|
|
420
|
+
* @returns
|
|
421
|
+
*/
|
|
422
|
+
const tryMergeFiltersInner = (f1, f2, justTry) => {
|
|
423
|
+
const pcaResult = possibleCombiningAttrs(f1, f2);
|
|
424
|
+
if (!pcaResult) {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
const [pca1, pca2] = pcaResult;
|
|
428
|
+
if (pca1 === '$or' && pca2 === '$or') {
|
|
429
|
+
// 仅当右侧每个分支都能与左侧某分支安全合并时,才尝试分配律式的分支合并;否则保留整体 OR。
|
|
430
|
+
// 如果双方都是or,有可能可以交叉合并,如:
|
|
431
|
+
/**
|
|
432
|
+
* {
|
|
433
|
+
$or: [
|
|
434
|
+
{
|
|
435
|
+
password: '1234',
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
ref: {
|
|
439
|
+
nickname: 'xc',
|
|
440
|
+
},
|
|
441
|
+
}
|
|
442
|
+
]
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
$or: [
|
|
446
|
+
{
|
|
447
|
+
ref: {
|
|
448
|
+
name: 'xc2',
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
password: 'dddd',
|
|
453
|
+
}
|
|
454
|
+
]
|
|
455
|
+
}
|
|
456
|
+
*/
|
|
457
|
+
for (const f21 of f2[pca2]) {
|
|
458
|
+
let success = false;
|
|
459
|
+
for (const f11 of f1[pca2]) {
|
|
460
|
+
if (tryMergeFiltersInner(f11, f21, true)) {
|
|
461
|
+
success = true;
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (!success) {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
if (justTry) {
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
for (const f21 of f2[pca2]) {
|
|
473
|
+
let merged = false;
|
|
474
|
+
for (const f11 of f1[pca2]) {
|
|
475
|
+
if (tryMergeFilters(f11, f21)) {
|
|
476
|
+
merged = true;
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (!merged) {
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
else if (pca1 === '$or') {
|
|
487
|
+
for (const f11 of f1[pca1]) {
|
|
488
|
+
const merged = justTry
|
|
489
|
+
? tryMergeFiltersInner(f11, f2, true)
|
|
490
|
+
: tryMergeFilters(f11, f2);
|
|
491
|
+
if (merged) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
else if (pca2 === '$or') {
|
|
498
|
+
for (const f21 of f2[pca2]) {
|
|
499
|
+
if (!tryMergeFiltersInner(f1, f21, true)) {
|
|
500
|
+
return false;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (justTry) {
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
for (const f12 of f2[pca2]) {
|
|
507
|
+
// 预检只证明每个分支在“原始 f1”上可合并;实际合并时 f1
|
|
508
|
+
// 已经可能被前一个分支改写,因此仍必须检查返回值。
|
|
509
|
+
// 若某一步失败,不能把部分改写后的结果当成完整并集返回。
|
|
510
|
+
if (!tryMergeFiltersInner(f1, f12)) {
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
else if (pca1 === pca2) {
|
|
517
|
+
if (pca1 === '$and') {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
else if (pca1 === '$not') {
|
|
521
|
+
// 先not后or 等于先and后not
|
|
522
|
+
if (justTry) {
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
Object.assign(f1, {
|
|
526
|
+
[pca1]: addFilterSegment(entity, schema, f1[pca1], f2[pca2]),
|
|
527
|
+
});
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
else if (pca1.startsWith('$')) {
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
// 原生属性
|
|
535
|
+
const rel = judgeRelation(schema, entity, pca1);
|
|
536
|
+
if (rel === 1) {
|
|
537
|
+
return tryMergeAttributeValue(f1, f2, pca1, justTry);
|
|
538
|
+
}
|
|
539
|
+
else if (rel === 2) {
|
|
540
|
+
if (justTry) {
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
Object.assign(f1, {
|
|
544
|
+
[pca1]: unionFilterSegment(pca1, schema, f1[pca1], f2[pca2]),
|
|
545
|
+
});
|
|
546
|
+
return true;
|
|
547
|
+
}
|
|
548
|
+
else if (typeof rel === 'string') {
|
|
549
|
+
if (justTry) {
|
|
550
|
+
return true;
|
|
551
|
+
}
|
|
552
|
+
Object.assign(f1, {
|
|
553
|
+
[pca1]: unionFilterSegment(rel, schema, f1[pca1], f2[pca2]),
|
|
554
|
+
});
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
assert(rel instanceof Array);
|
|
559
|
+
// 一对多的子查询,只有子查询的语义算子一样才实施合并
|
|
560
|
+
const sqpOp1 = f1[pca1]['#sqp'] || 'in';
|
|
561
|
+
const sqpOp2 = f2[pca1]['#sqp'] || 'in';
|
|
562
|
+
// OR 合并只允许 in 的谓词并集,以及 not all 的否定交集;
|
|
563
|
+
// not in(P)∨not in(Q)≠not in(P∨Q),all(P)∨all(Q)≠all(P∨Q)。
|
|
564
|
+
if (sqpOp1 !== sqpOp2 || ['not in', 'all'].includes(sqpOp1)) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
if (justTry) {
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
if (sqpOp1 === 'in') {
|
|
571
|
+
Object.assign(f1, {
|
|
572
|
+
[pca1]: Object.assign(unionFilterSegment(rel[0], schema, f1[pca1], f2[pca2]), {
|
|
573
|
+
['#sqp']: sqpOp1,
|
|
574
|
+
})
|
|
575
|
+
});
|
|
576
|
+
return true;
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
// not all(condition1) or not all(condition2)等价于not all(condition1 and condition2)
|
|
580
|
+
assert(sqpOp1 === 'not all');
|
|
581
|
+
Object.assign(f1, {
|
|
582
|
+
[pca1]: Object.assign(addFilterSegment(rel[0], schema, f1[pca1], f2[pca2]), {
|
|
583
|
+
['#sqp']: sqpOp1,
|
|
584
|
+
})
|
|
585
|
+
});
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return false;
|
|
592
|
+
};
|
|
593
|
+
// 先在克隆树上执行真实改写;任何递归分支失败都会整体回滚,避免留下半完成的 OR 合并。
|
|
594
|
+
const tryMergeFilters = (f1, f2) => {
|
|
595
|
+
const candidate = cloneDeep(f1);
|
|
596
|
+
if (!tryMergeFiltersInner(candidate, cloneDeep(f2))) {
|
|
597
|
+
return false;
|
|
598
|
+
}
|
|
599
|
+
for (const key of Object.keys(f1)) {
|
|
600
|
+
delete f1[key];
|
|
601
|
+
}
|
|
602
|
+
Object.assign(f1, candidate);
|
|
603
|
+
return true;
|
|
604
|
+
};
|
|
605
|
+
const addIntoOr = (f) => {
|
|
606
|
+
assert(filter);
|
|
607
|
+
// 空 filter 匹配全集 U:U ∪ F = U,而 F ∪ U = U。
|
|
608
|
+
if (Object.keys(filter).length === 0 || Object.keys(f).length === 0) {
|
|
609
|
+
filter = {};
|
|
610
|
+
}
|
|
611
|
+
else if (Array.isArray(filter.$or) && Object.keys(filter).length === 1
|
|
612
|
+
&& filter.$or.every(isFilterRecord)) {
|
|
613
|
+
if (Object.keys(f).length === 1 && Array.isArray(f.$or) && f.$or.every(isFilterRecord)) {
|
|
614
|
+
filter.$or.push(...f.$or.map((branch) => cloneDeep(branch)));
|
|
615
|
+
}
|
|
616
|
+
else {
|
|
617
|
+
filter.$or.push(cloneDeep(f));
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
else {
|
|
621
|
+
if (Object.prototype.hasOwnProperty.call(filter, '$or')
|
|
622
|
+
&& (!Array.isArray(filter.$or) || filter.$or.some((branch) => !isFilterRecord(branch)))) {
|
|
623
|
+
console.warn('combineFilters(OR): malformed accumulated $or; preserving both branches');
|
|
624
|
+
}
|
|
625
|
+
filter = {
|
|
626
|
+
$or: [cloneDeep(filter), cloneDeep(f)],
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
// 只有 tryMergeFilters 能证明等价时才改写;否则累积为 filter ∨ f。
|
|
631
|
+
let filterSafeForMerge = true;
|
|
632
|
+
const addInner = (f) => {
|
|
633
|
+
if (f !== undefined && f !== null) {
|
|
634
|
+
const inputSafeForMerge = hasValidFilterShape(entity, schema, f, 'combineFilters(OR)');
|
|
635
|
+
if (!filter) {
|
|
636
|
+
filter = cloneDeep(f);
|
|
637
|
+
filterSafeForMerge = inputSafeForMerge;
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (!inputSafeForMerge || !filterSafeForMerge) {
|
|
641
|
+
addIntoOr(f);
|
|
642
|
+
filterSafeForMerge = false;
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (tryMergeFilters(filter, f)) {
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
addIntoOr(f);
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
filters.forEach(f => addInner(f));
|
|
652
|
+
return filter;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* 按默认 AND 或 `union=true` 的 OR 组合 filters;内部合并器只在集合等价可证明时改写树形结构。
|
|
656
|
+
* 默认:`combineFilters(F_1,…,F_n) ≡ ⋂F_i`;union:`combineFilters(..., true) ≡ ⋃F_i`。
|
|
657
|
+
*/
|
|
658
|
+
export function combineFilters(entity, schema, filters, union) {
|
|
659
|
+
if (!Array.isArray(filters)) {
|
|
660
|
+
console.warn('combineFilters: filters must be an array; returning no combined filter');
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
const filters2 = filters.map(filter => filter ? cloneDeep(filter) : filter);
|
|
664
|
+
if (filters2.some(filter => filter !== undefined && filter !== null && !isFilterRecord(filter))) {
|
|
665
|
+
console.warn('combineFilters: non-object filter entry; preserving all entries without normalization');
|
|
666
|
+
return union ? { $or: filters2 } : { $and: filters2 };
|
|
667
|
+
}
|
|
668
|
+
if (union) {
|
|
669
|
+
return unionFilterSegment(entity, schema, ...filters2);
|
|
670
|
+
}
|
|
671
|
+
return addFilterSegment(entity, schema, ...filters2);
|
|
672
|
+
}
|
|
673
|
+
/** 返回本有限值域建模器支持的值类型;其他类型由通用判定路径处理。 */
|
|
674
|
+
function getFiniteValueType(value) {
|
|
675
|
+
if (typeof value === 'string') {
|
|
676
|
+
return 'string';
|
|
677
|
+
}
|
|
678
|
+
if (typeof value === 'boolean') {
|
|
679
|
+
return 'boolean';
|
|
680
|
+
}
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
/** 将字符串/布尔值上的等值、反等值和 membership 谓词归一化为集合约束。 */
|
|
684
|
+
function toFiniteConstraint(value) {
|
|
685
|
+
const constraint = {
|
|
686
|
+
includesMissing: false,
|
|
687
|
+
includesPresent: true,
|
|
688
|
+
excludedValues: [],
|
|
689
|
+
};
|
|
690
|
+
const scalarType = getFiniteValueType(value);
|
|
691
|
+
if (scalarType) {
|
|
692
|
+
constraint.valueType = scalarType;
|
|
693
|
+
constraint.includedValues = [value];
|
|
694
|
+
return constraint;
|
|
695
|
+
}
|
|
696
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
const keys = Object.keys(value);
|
|
700
|
+
if (keys.length !== 1) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const operator = keys[0];
|
|
704
|
+
const operand = value[operator];
|
|
705
|
+
if (operator === '$eq' || operator === '$ne') {
|
|
706
|
+
const operandType = getFiniteValueType(operand);
|
|
707
|
+
if (!operandType)
|
|
708
|
+
return;
|
|
709
|
+
constraint.valueType = operandType;
|
|
710
|
+
if (operator === '$eq') {
|
|
711
|
+
constraint.includedValues = [operand];
|
|
712
|
+
}
|
|
713
|
+
else {
|
|
714
|
+
constraint.excludedValues = [operand];
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
else if (operator === '$in' || operator === '$nin') {
|
|
718
|
+
if (!Array.isArray(operand))
|
|
719
|
+
return;
|
|
720
|
+
const valueTypes = [...new Set(operand.map(getFiniteValueType))];
|
|
721
|
+
if (valueTypes.includes(undefined) || valueTypes.length > 1)
|
|
722
|
+
return;
|
|
723
|
+
constraint.valueType = valueTypes[0];
|
|
724
|
+
if (operator === '$in') {
|
|
725
|
+
constraint.includedValues = operand;
|
|
726
|
+
}
|
|
727
|
+
else {
|
|
728
|
+
constraint.excludedValues = operand;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
else {
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
return constraint;
|
|
735
|
+
}
|
|
736
|
+
/** 逐项实现约束交集:包含候选取交集/较窄一侧,排除候选取并集。 */
|
|
737
|
+
function intersectFiniteConstraints(left, right) {
|
|
738
|
+
const includedValues = left.includedValues === undefined ? right.includedValues : right.includedValues === undefined ? left.includedValues :
|
|
739
|
+
left.includedValues.filter(value => right.includedValues.includes(value));
|
|
740
|
+
return {
|
|
741
|
+
includesMissing: left.includesMissing && right.includesMissing,
|
|
742
|
+
includesPresent: left.includesPresent && right.includesPresent,
|
|
743
|
+
valueType: left.valueType || right.valueType,
|
|
744
|
+
includedValues,
|
|
745
|
+
excludedValues: [...new Set([...left.excludedValues, ...right.excludedValues])],
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
function finiteConstraintContainsValue(constraint, value) {
|
|
749
|
+
return constraint.includesPresent
|
|
750
|
+
&& (constraint.valueType === undefined || typeof value === constraint.valueType)
|
|
751
|
+
&& (constraint.includedValues === undefined || constraint.includedValues.includes(value))
|
|
752
|
+
&& !constraint.excludedValues.includes(value);
|
|
753
|
+
}
|
|
754
|
+
function finiteConstraintDomain(constraint) {
|
|
755
|
+
if (constraint.valueType === 'boolean') {
|
|
756
|
+
return [false, true];
|
|
757
|
+
}
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
function finiteConstraintHasPresentValue(constraint) {
|
|
761
|
+
if (!constraint.includesPresent) {
|
|
762
|
+
return false;
|
|
763
|
+
}
|
|
764
|
+
if (constraint.includedValues !== undefined) {
|
|
765
|
+
return constraint.includedValues.some(value => finiteConstraintContainsValue(constraint, value));
|
|
766
|
+
}
|
|
767
|
+
const domain = finiteConstraintDomain(constraint);
|
|
768
|
+
return domain ? domain.some(value => finiteConstraintContainsValue(constraint, value)) : true;
|
|
769
|
+
}
|
|
770
|
+
function finiteConstraintIsEmpty(constraint) {
|
|
771
|
+
return !constraint.includesMissing && !finiteConstraintHasPresentValue(constraint);
|
|
772
|
+
}
|
|
773
|
+
function finiteConstraintFiniteValues(constraint) {
|
|
774
|
+
if (!constraint.includesPresent) {
|
|
775
|
+
return [];
|
|
776
|
+
}
|
|
777
|
+
if (constraint.includedValues !== undefined) {
|
|
778
|
+
return constraint.includedValues.filter(value => finiteConstraintContainsValue(constraint, value));
|
|
779
|
+
}
|
|
780
|
+
const domain = finiteConstraintDomain(constraint);
|
|
781
|
+
return domain?.filter(value => finiteConstraintContainsValue(constraint, value));
|
|
782
|
+
}
|
|
783
|
+
/** 判断 left ⊆ right;空集是任意集合的子集,missing/present 两个分区分别检查。 */
|
|
784
|
+
function finiteConstraintIsSubset(left, right) {
|
|
785
|
+
if (finiteConstraintIsEmpty(left)) {
|
|
786
|
+
return true;
|
|
787
|
+
}
|
|
788
|
+
if (left.includesMissing && !right.includesMissing) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
if (!finiteConstraintHasPresentValue(left)) {
|
|
792
|
+
return true;
|
|
793
|
+
}
|
|
794
|
+
if (!right.includesPresent) {
|
|
795
|
+
return false;
|
|
796
|
+
}
|
|
797
|
+
const leftValues = finiteConstraintFiniteValues(left);
|
|
798
|
+
if (leftValues !== undefined) {
|
|
799
|
+
return leftValues.every(value => finiteConstraintContainsValue(right, value));
|
|
800
|
+
}
|
|
801
|
+
if (right.valueType === 'boolean' && right.includedValues?.includes(false) && right.includedValues.includes(true)) {
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
804
|
+
if (right.includedValues !== undefined) {
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
if (right.valueType === undefined && right.excludedValues.length === 0) {
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
810
|
+
if (!left.valueType) {
|
|
811
|
+
return right.excludedValues.length === 0;
|
|
812
|
+
}
|
|
813
|
+
if (left.valueType === 'string' && right.valueType === 'string') {
|
|
814
|
+
return right.excludedValues.every(value => left.excludedValues.includes(value));
|
|
815
|
+
}
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
function toNumericConstraint(value) {
|
|
819
|
+
const constraint = {
|
|
820
|
+
includesMissing: false,
|
|
821
|
+
includesPresent: true,
|
|
822
|
+
excludedValues: [],
|
|
823
|
+
};
|
|
824
|
+
if (typeof value === 'number') {
|
|
825
|
+
if (!Number.isFinite(value)) {
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
constraint.includedValues = [value];
|
|
829
|
+
return constraint;
|
|
830
|
+
}
|
|
831
|
+
if (value instanceof Date) {
|
|
832
|
+
const timestamp = value.getTime();
|
|
833
|
+
if (!Number.isFinite(timestamp)) {
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
// Date 的顺序比较等价于其 epoch 毫秒比较:D₁ ≤ D₂ ⇔ t(D₁) ≤ t(D₂)。
|
|
837
|
+
constraint.includedValues = [timestamp];
|
|
838
|
+
return constraint;
|
|
839
|
+
}
|
|
840
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
const keys = Object.keys(value);
|
|
844
|
+
if (keys.length !== 1) {
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
const operator = keys[0];
|
|
848
|
+
const operand = value[operator];
|
|
849
|
+
if (operator === '$exists') {
|
|
850
|
+
if (typeof operand !== 'boolean') {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
constraint.includesMissing = operand === false;
|
|
854
|
+
constraint.includesPresent = operand === true;
|
|
855
|
+
return constraint;
|
|
856
|
+
}
|
|
857
|
+
const isFiniteNumber = (item) => typeof item === 'number' && Number.isFinite(item);
|
|
858
|
+
const isFiniteDate = (item) => item instanceof Date && Number.isFinite(item.getTime());
|
|
859
|
+
const toTimestamp = (item) => item instanceof Date ? item.getTime() : item;
|
|
860
|
+
if (operator === '$eq' && (isFiniteNumber(operand) || isFiniteDate(operand))) {
|
|
861
|
+
constraint.includedValues = [toTimestamp(operand)];
|
|
862
|
+
}
|
|
863
|
+
else if (operator === '$ne' && (isFiniteNumber(operand) || isFiniteDate(operand))) {
|
|
864
|
+
constraint.excludedValues = [toTimestamp(operand)];
|
|
865
|
+
}
|
|
866
|
+
else if (operator === '$gt' || operator === '$gte') {
|
|
867
|
+
if (!isFiniteNumber(operand) && !isFiniteDate(operand))
|
|
868
|
+
return;
|
|
869
|
+
constraint.lower = { value: toTimestamp(operand), inclusive: operator === '$gte' };
|
|
870
|
+
}
|
|
871
|
+
else if (operator === '$lt' || operator === '$lte') {
|
|
872
|
+
if (!isFiniteNumber(operand) && !isFiniteDate(operand))
|
|
873
|
+
return;
|
|
874
|
+
constraint.upper = { value: toTimestamp(operand), inclusive: operator === '$lte' };
|
|
875
|
+
}
|
|
876
|
+
else if (operator === '$between') {
|
|
877
|
+
if (!Array.isArray(operand) || operand.length !== 2)
|
|
878
|
+
return;
|
|
879
|
+
const bothNumbers = operand.every(isFiniteNumber);
|
|
880
|
+
const bothDates = operand.every(isFiniteDate);
|
|
881
|
+
if (!bothNumbers && !bothDates)
|
|
882
|
+
return;
|
|
883
|
+
constraint.lower = { value: toTimestamp(operand[0]), inclusive: true };
|
|
884
|
+
constraint.upper = { value: toTimestamp(operand[1]), inclusive: true };
|
|
885
|
+
}
|
|
886
|
+
else if (operator === '$in' || operator === '$nin') {
|
|
887
|
+
if (!Array.isArray(operand))
|
|
888
|
+
return;
|
|
889
|
+
const allNumbers = operand.every(isFiniteNumber);
|
|
890
|
+
const allDates = operand.every(isFiniteDate);
|
|
891
|
+
if (!allNumbers && !allDates)
|
|
892
|
+
return;
|
|
893
|
+
const timestamps = operand.map(toTimestamp);
|
|
894
|
+
if (operator === '$in') {
|
|
895
|
+
constraint.includedValues = timestamps;
|
|
896
|
+
}
|
|
897
|
+
else {
|
|
898
|
+
constraint.excludedValues = timestamps;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
return constraint;
|
|
905
|
+
}
|
|
906
|
+
/** 区间下界取较大者、上界取较小者;端点相等时只有双方都闭合才保留该点。 */
|
|
907
|
+
function intersectNumericConstraints(left, right) {
|
|
908
|
+
const lower = !left.lower ? right.lower : !right.lower ? left.lower :
|
|
909
|
+
left.lower.value > right.lower.value ? left.lower : right.lower.value > left.lower.value ? right.lower : {
|
|
910
|
+
value: left.lower.value,
|
|
911
|
+
inclusive: left.lower.inclusive && right.lower.inclusive,
|
|
912
|
+
};
|
|
913
|
+
const upper = !left.upper ? right.upper : !right.upper ? left.upper :
|
|
914
|
+
left.upper.value < right.upper.value ? left.upper : right.upper.value < left.upper.value ? right.upper : {
|
|
915
|
+
value: left.upper.value,
|
|
916
|
+
inclusive: left.upper.inclusive && right.upper.inclusive,
|
|
917
|
+
};
|
|
918
|
+
const includedValues = left.includedValues === undefined ? right.includedValues : right.includedValues === undefined ? left.includedValues :
|
|
919
|
+
left.includedValues.filter(value => right.includedValues.includes(value));
|
|
920
|
+
return {
|
|
921
|
+
includesMissing: left.includesMissing && right.includesMissing,
|
|
922
|
+
includesPresent: left.includesPresent && right.includesPresent,
|
|
923
|
+
lower,
|
|
924
|
+
upper,
|
|
925
|
+
includedValues,
|
|
926
|
+
excludedValues: [...new Set([...left.excludedValues, ...right.excludedValues])],
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
function numericConstraintContainsValue(constraint, value) {
|
|
930
|
+
return constraint.includesPresent
|
|
931
|
+
&& (!constraint.lower || value > constraint.lower.value || constraint.lower.inclusive && value === constraint.lower.value)
|
|
932
|
+
&& (!constraint.upper || value < constraint.upper.value || constraint.upper.inclusive && value === constraint.upper.value)
|
|
933
|
+
&& (constraint.includedValues === undefined || constraint.includedValues.includes(value))
|
|
934
|
+
&& !constraint.excludedValues.includes(value);
|
|
935
|
+
}
|
|
936
|
+
/** 判断可见数值域是否非空;仅当边界反向或等点未同时闭合时判为空。 */
|
|
937
|
+
function numericConstraintHasPresentValue(constraint) {
|
|
938
|
+
if (!constraint.includesPresent) {
|
|
939
|
+
return false;
|
|
940
|
+
}
|
|
941
|
+
if (constraint.lower && constraint.upper) {
|
|
942
|
+
if (constraint.lower.value > constraint.upper.value || constraint.lower.value === constraint.upper.value
|
|
943
|
+
&& !(constraint.lower.inclusive && constraint.upper.inclusive)) {
|
|
944
|
+
return false;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
if (constraint.includedValues !== undefined) {
|
|
948
|
+
return constraint.includedValues.some(value => numericConstraintContainsValue(constraint, value));
|
|
949
|
+
}
|
|
950
|
+
if (constraint.lower && constraint.upper && constraint.lower.value === constraint.upper.value) {
|
|
951
|
+
return numericConstraintContainsValue(constraint, constraint.lower.value);
|
|
952
|
+
}
|
|
953
|
+
return true;
|
|
954
|
+
}
|
|
955
|
+
function numericConstraintIsEmpty(constraint) {
|
|
956
|
+
return !constraint.includesMissing && !numericConstraintHasPresentValue(constraint);
|
|
957
|
+
}
|
|
958
|
+
function numericConstraintFiniteValues(constraint) {
|
|
959
|
+
if (!constraint.includesPresent) {
|
|
960
|
+
return [];
|
|
961
|
+
}
|
|
962
|
+
if (constraint.includedValues !== undefined) {
|
|
963
|
+
return constraint.includedValues.filter(value => numericConstraintContainsValue(constraint, value));
|
|
964
|
+
}
|
|
965
|
+
if (constraint.lower && constraint.upper && constraint.lower.value === constraint.upper.value
|
|
966
|
+
&& constraint.lower.inclusive && constraint.upper.inclusive) {
|
|
967
|
+
return numericConstraintContainsValue(constraint, constraint.lower.value) ? [constraint.lower.value] : [];
|
|
968
|
+
}
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
/** 通过检查 left 是否落在 right 的区间外或命中 right 的排除点,证明 left ⊆ right。 */
|
|
972
|
+
function numericConstraintIsSubset(left, right) {
|
|
973
|
+
if (numericConstraintIsEmpty(left)) {
|
|
974
|
+
return true;
|
|
975
|
+
}
|
|
976
|
+
if (left.includesMissing && !right.includesMissing) {
|
|
977
|
+
return false;
|
|
978
|
+
}
|
|
979
|
+
if (!numericConstraintHasPresentValue(left)) {
|
|
980
|
+
return true;
|
|
981
|
+
}
|
|
982
|
+
if (!right.includesPresent) {
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
if (right.includedValues !== undefined) {
|
|
986
|
+
const leftValues = numericConstraintFiniteValues(left);
|
|
987
|
+
return leftValues !== undefined && leftValues.every(value => numericConstraintContainsValue(right, value));
|
|
988
|
+
}
|
|
989
|
+
if (right.lower) {
|
|
990
|
+
const belowLower = intersectNumericConstraints(left, {
|
|
991
|
+
includesMissing: false,
|
|
992
|
+
includesPresent: true,
|
|
993
|
+
upper: { value: right.lower.value, inclusive: !right.lower.inclusive },
|
|
994
|
+
excludedValues: [],
|
|
995
|
+
});
|
|
996
|
+
if (numericConstraintHasPresentValue(belowLower)) {
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
if (right.upper) {
|
|
1001
|
+
const aboveUpper = intersectNumericConstraints(left, {
|
|
1002
|
+
includesMissing: false,
|
|
1003
|
+
includesPresent: true,
|
|
1004
|
+
lower: { value: right.upper.value, inclusive: !right.upper.inclusive },
|
|
1005
|
+
excludedValues: [],
|
|
1006
|
+
});
|
|
1007
|
+
if (numericConstraintHasPresentValue(aboveUpper)) {
|
|
1008
|
+
return false;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return !right.excludedValues.some(value => numericConstraintContainsValue(left, value));
|
|
1012
|
+
}
|
|
1013
|
+
function toStringConstraint(value) {
|
|
1014
|
+
if (typeof value === 'string') {
|
|
1015
|
+
return {
|
|
1016
|
+
includedValues: [value],
|
|
1017
|
+
excludedValues: [],
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
const keys = Object.keys(value);
|
|
1024
|
+
if (keys.length !== 1) {
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
const operator = keys[0];
|
|
1028
|
+
const operand = value[operator];
|
|
1029
|
+
if (operator === '$eq' || operator === '$ne') {
|
|
1030
|
+
if (typeof operand !== 'string') {
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
return operator === '$eq'
|
|
1034
|
+
? { includedValues: [operand], excludedValues: [] }
|
|
1035
|
+
: { excludedValues: [operand] };
|
|
1036
|
+
}
|
|
1037
|
+
if (operator === '$in' || operator === '$nin') {
|
|
1038
|
+
if (!Array.isArray(operand) || !operand.every((item) => typeof item === 'string')) {
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
return operator === '$in'
|
|
1042
|
+
? { includedValues: operand, excludedValues: [] }
|
|
1043
|
+
: { excludedValues: operand };
|
|
1044
|
+
}
|
|
1045
|
+
if (operator === '$gt' || operator === '$gte') {
|
|
1046
|
+
if (typeof operand !== 'string')
|
|
1047
|
+
return;
|
|
1048
|
+
return {
|
|
1049
|
+
excludedValues: [],
|
|
1050
|
+
lower: { value: operand, inclusive: operator === '$gte' },
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
if (operator === '$lt' || operator === '$lte') {
|
|
1054
|
+
if (typeof operand !== 'string')
|
|
1055
|
+
return;
|
|
1056
|
+
return {
|
|
1057
|
+
excludedValues: [],
|
|
1058
|
+
upper: { value: operand, inclusive: operator === '$lte' },
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
if (operator === '$between') {
|
|
1062
|
+
if (!Array.isArray(operand) || operand.length !== 2 || !operand.every((item) => typeof item === 'string')) {
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
return {
|
|
1066
|
+
excludedValues: [],
|
|
1067
|
+
lower: { value: operand[0], inclusive: true },
|
|
1068
|
+
upper: { value: operand[1], inclusive: true },
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
if (['$startsWith', '$endsWith', '$includes'].includes(operator) && typeof operand === 'string') {
|
|
1072
|
+
return {
|
|
1073
|
+
excludedValues: [],
|
|
1074
|
+
pattern: {
|
|
1075
|
+
operator: operator,
|
|
1076
|
+
value: operand,
|
|
1077
|
+
},
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
function stringPatternMatches(pattern, value) {
|
|
1083
|
+
if (pattern.operator === '$startsWith') {
|
|
1084
|
+
return value.startsWith(pattern.value);
|
|
1085
|
+
}
|
|
1086
|
+
if (pattern.operator === '$endsWith') {
|
|
1087
|
+
return value.endsWith(pattern.value);
|
|
1088
|
+
}
|
|
1089
|
+
return value.includes(pattern.value);
|
|
1090
|
+
}
|
|
1091
|
+
function stringBoundAllowsLower(value, bound) {
|
|
1092
|
+
return bound.inclusive ? value >= bound.value : value > bound.value;
|
|
1093
|
+
}
|
|
1094
|
+
function stringBoundAllowsUpper(value, bound) {
|
|
1095
|
+
return bound.inclusive ? value <= bound.value : value < bound.value;
|
|
1096
|
+
}
|
|
1097
|
+
function stringRangeHasValue(lower, upper) {
|
|
1098
|
+
if (!lower) {
|
|
1099
|
+
return !upper || upper.value !== '' || upper.inclusive;
|
|
1100
|
+
}
|
|
1101
|
+
const firstAllowedValue = lower.inclusive ? lower.value : `${lower.value}\u0000`;
|
|
1102
|
+
if (!upper) {
|
|
1103
|
+
return true;
|
|
1104
|
+
}
|
|
1105
|
+
return upper.inclusive ? firstAllowedValue <= upper.value : firstAllowedValue < upper.value;
|
|
1106
|
+
}
|
|
1107
|
+
function stringConstraintHasBoundIntersection(left, right) {
|
|
1108
|
+
const lower = !left.lower ? right.lower : !right.lower ? left.lower :
|
|
1109
|
+
left.lower.value > right.lower.value ? left.lower : right.lower.value > left.lower.value ? right.lower : {
|
|
1110
|
+
value: left.lower.value,
|
|
1111
|
+
inclusive: left.lower.inclusive && right.lower.inclusive,
|
|
1112
|
+
};
|
|
1113
|
+
const upper = !left.upper ? right.upper : !right.upper ? left.upper :
|
|
1114
|
+
left.upper.value < right.upper.value ? left.upper : right.upper.value < left.upper.value ? right.upper : {
|
|
1115
|
+
value: left.upper.value,
|
|
1116
|
+
inclusive: left.upper.inclusive && right.upper.inclusive,
|
|
1117
|
+
};
|
|
1118
|
+
return stringRangeHasValue(lower, upper);
|
|
1119
|
+
}
|
|
1120
|
+
function stringConstraintLowerContains(left, right) {
|
|
1121
|
+
if (!right.lower)
|
|
1122
|
+
return true;
|
|
1123
|
+
if (!left.lower)
|
|
1124
|
+
return false;
|
|
1125
|
+
if (left.lower.value > right.lower.value)
|
|
1126
|
+
return true;
|
|
1127
|
+
if (left.lower.value < right.lower.value) {
|
|
1128
|
+
// 在字典序字符串域中,`x > a` 的最小候选是 `a\u0000`。
|
|
1129
|
+
// 因而 `(a, +∞) ⊆ [a\u0000, +∞)`,即只有紧邻后继且右端闭合时可跨过原始端点。
|
|
1130
|
+
return !left.lower.inclusive
|
|
1131
|
+
&& right.lower.inclusive
|
|
1132
|
+
&& `${left.lower.value}\u0000` === right.lower.value;
|
|
1133
|
+
}
|
|
1134
|
+
return !left.lower.inclusive || right.lower.inclusive;
|
|
1135
|
+
}
|
|
1136
|
+
function stringConstraintUpperContains(left, right) {
|
|
1137
|
+
if (!right.upper)
|
|
1138
|
+
return true;
|
|
1139
|
+
if (!left.upper)
|
|
1140
|
+
return false;
|
|
1141
|
+
if (left.upper.value < right.upper.value)
|
|
1142
|
+
return true;
|
|
1143
|
+
if (left.upper.value > right.upper.value) {
|
|
1144
|
+
// 对称地,`x < a\u0000` 与 `x ≤ a` 表示同一组可见字符串边界。
|
|
1145
|
+
return !left.upper.inclusive
|
|
1146
|
+
&& right.upper.inclusive
|
|
1147
|
+
&& left.upper.value === `${right.upper.value}\u0000`;
|
|
1148
|
+
}
|
|
1149
|
+
return !left.upper.inclusive || right.upper.inclusive;
|
|
1150
|
+
}
|
|
1151
|
+
function stringConstraintMatches(constraint, value) {
|
|
1152
|
+
if (constraint.lower && !stringBoundAllowsLower(value, constraint.lower)) {
|
|
1153
|
+
return false;
|
|
1154
|
+
}
|
|
1155
|
+
if (constraint.upper && !stringBoundAllowsUpper(value, constraint.upper)) {
|
|
1156
|
+
return false;
|
|
1157
|
+
}
|
|
1158
|
+
if (constraint.includedValues !== undefined && !constraint.includedValues.includes(value)) {
|
|
1159
|
+
return false;
|
|
1160
|
+
}
|
|
1161
|
+
if (constraint.pattern && !stringPatternMatches(constraint.pattern, value)) {
|
|
1162
|
+
return false;
|
|
1163
|
+
}
|
|
1164
|
+
return !constraint.excludedValues.includes(value);
|
|
1165
|
+
}
|
|
1166
|
+
function stringConstraintIsEmpty(constraint) {
|
|
1167
|
+
if (!stringRangeHasValue(constraint.lower, constraint.upper)) {
|
|
1168
|
+
return true;
|
|
1169
|
+
}
|
|
1170
|
+
if (constraint.includedValues !== undefined) {
|
|
1171
|
+
return !constraint.includedValues.some(value => stringConstraintMatches(constraint, value));
|
|
1172
|
+
}
|
|
1173
|
+
const firstAllowedValue = constraint.lower
|
|
1174
|
+
? constraint.lower.inclusive ? constraint.lower.value : `${constraint.lower.value}\u0000`
|
|
1175
|
+
: '';
|
|
1176
|
+
if (constraint.upper?.inclusive && firstAllowedValue === constraint.upper.value) {
|
|
1177
|
+
return !stringConstraintMatches(constraint, firstAllowedValue);
|
|
1178
|
+
}
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
/** 对可判定的前缀/后缀/子串模式证明语言包含;未覆盖的模式组合保守不声称包含。 */
|
|
1182
|
+
function stringPatternIsSubset(left, right) {
|
|
1183
|
+
if (right.value === '') {
|
|
1184
|
+
return true;
|
|
1185
|
+
}
|
|
1186
|
+
if (left.value === '') {
|
|
1187
|
+
return false;
|
|
1188
|
+
}
|
|
1189
|
+
if (left.operator === right.operator) {
|
|
1190
|
+
if (left.operator === '$startsWith') {
|
|
1191
|
+
return left.value.startsWith(right.value);
|
|
1192
|
+
}
|
|
1193
|
+
if (left.operator === '$endsWith') {
|
|
1194
|
+
return left.value.endsWith(right.value);
|
|
1195
|
+
}
|
|
1196
|
+
return left.value.includes(right.value);
|
|
1197
|
+
}
|
|
1198
|
+
if (right.operator === '$includes' && ['$startsWith', '$endsWith'].includes(left.operator)) {
|
|
1199
|
+
return left.value.includes(right.value);
|
|
1200
|
+
}
|
|
1201
|
+
return false;
|
|
1202
|
+
}
|
|
1203
|
+
/** 只有前缀或后缀必然冲突时才断言模式集合不相交;其他组合可能有交集。 */
|
|
1204
|
+
function stringPatternIsDisjoint(left, right) {
|
|
1205
|
+
if (left.value === '' || right.value === '') {
|
|
1206
|
+
return false;
|
|
1207
|
+
}
|
|
1208
|
+
if (left.operator === '$startsWith' && right.operator === '$startsWith') {
|
|
1209
|
+
return !left.value.startsWith(right.value) && !right.value.startsWith(left.value);
|
|
1210
|
+
}
|
|
1211
|
+
if (left.operator === '$endsWith' && right.operator === '$endsWith') {
|
|
1212
|
+
return !left.value.endsWith(right.value) && !right.value.endsWith(left.value);
|
|
1213
|
+
}
|
|
1214
|
+
return false;
|
|
1215
|
+
}
|
|
1216
|
+
function stringConstraintIsSubset(left, right) {
|
|
1217
|
+
if (stringConstraintIsEmpty(left)) {
|
|
1218
|
+
return true;
|
|
1219
|
+
}
|
|
1220
|
+
if (left.includedValues !== undefined) {
|
|
1221
|
+
return left.includedValues.every(value => stringConstraintMatches(right, value));
|
|
1222
|
+
}
|
|
1223
|
+
if (right.includedValues !== undefined) {
|
|
1224
|
+
return false;
|
|
1225
|
+
}
|
|
1226
|
+
if (left.pattern) {
|
|
1227
|
+
if (!right.pattern || right.lower || right.upper || right.excludedValues.length > 0) {
|
|
1228
|
+
return false;
|
|
1229
|
+
}
|
|
1230
|
+
return stringPatternIsSubset(left.pattern, right.pattern);
|
|
1231
|
+
}
|
|
1232
|
+
if (right.pattern) {
|
|
1233
|
+
return false;
|
|
1234
|
+
}
|
|
1235
|
+
if (!stringConstraintLowerContains(left, right) || !stringConstraintUpperContains(left, right)) {
|
|
1236
|
+
return false;
|
|
1237
|
+
}
|
|
1238
|
+
return right.excludedValues.every(value => !stringConstraintMatches(left, value));
|
|
1239
|
+
}
|
|
1240
|
+
function stringConstraintIsDisjoint(left, right) {
|
|
1241
|
+
if (stringConstraintIsEmpty(left) || stringConstraintIsEmpty(right)) {
|
|
1242
|
+
return true;
|
|
1243
|
+
}
|
|
1244
|
+
if (left.includedValues !== undefined) {
|
|
1245
|
+
return left.includedValues.every(value => !stringConstraintMatches(right, value));
|
|
1246
|
+
}
|
|
1247
|
+
if (right.includedValues !== undefined) {
|
|
1248
|
+
return right.includedValues.every(value => !stringConstraintMatches(left, value));
|
|
1249
|
+
}
|
|
1250
|
+
if (!stringConstraintHasBoundIntersection(left, right)) {
|
|
1251
|
+
return true;
|
|
1252
|
+
}
|
|
1253
|
+
if (left.pattern && right.pattern) {
|
|
1254
|
+
return stringPatternIsDisjoint(left.pattern, right.pattern);
|
|
1255
|
+
}
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
/**
|
|
1259
|
+
* 对单属性谓词求集合关系:`contained=true` 检查 `S(value1) ⊆ S(value2)`;否则检查 `S(value1) ∩ S(value2) = ∅`。
|
|
1260
|
+
* 返回 `undefined` 表示当前算子组合不足以证明包含或相斥,不能当作 true/false 使用。
|
|
1261
|
+
*/
|
|
1262
|
+
export function judgeValueRelation(value1, value2, contained) {
|
|
1263
|
+
if (value1 === null || value1 === undefined || value2 === null || value2 === undefined) {
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
const isFiniteNumber = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
1267
|
+
const isComparableScalar = (value) => typeof value === 'string' || typeof value === 'boolean' || isFiniteNumber(value)
|
|
1268
|
+
|| value instanceof Date && Number.isFinite(value.getTime());
|
|
1269
|
+
const supportedOperators = [
|
|
1270
|
+
'$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in', '$nin', '$between',
|
|
1271
|
+
'$exists', '$startsWith', '$endsWith', '$includes',
|
|
1272
|
+
];
|
|
1273
|
+
const getUnsupportedOperator = (value) => {
|
|
1274
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
if (value instanceof Date) {
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
const keys = Object.keys(value);
|
|
1281
|
+
if (keys.length !== 1 || !supportedOperators.includes(keys[0])) {
|
|
1282
|
+
return keys.join(',') || '<empty predicate>';
|
|
1283
|
+
}
|
|
1284
|
+
return;
|
|
1285
|
+
};
|
|
1286
|
+
const unsupportedOperator = getUnsupportedOperator(value1) || getUnsupportedOperator(value2);
|
|
1287
|
+
if (unsupportedOperator) {
|
|
1288
|
+
console.warn(`judgeValueRelation: unsupported filter operator ${unsupportedOperator}`);
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
const hasMalformedOperand = (value) => {
|
|
1292
|
+
if (typeof value === 'number') {
|
|
1293
|
+
return !Number.isFinite(value);
|
|
1294
|
+
}
|
|
1295
|
+
if (value instanceof Date) {
|
|
1296
|
+
return !Number.isFinite(value.getTime());
|
|
1297
|
+
}
|
|
1298
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
1299
|
+
return Array.isArray(value);
|
|
1300
|
+
}
|
|
1301
|
+
const operator = Object.keys(value)[0];
|
|
1302
|
+
const operand = value[operator];
|
|
1303
|
+
if (['$in', '$nin'].includes(operator)) {
|
|
1304
|
+
if (!Array.isArray(operand)) {
|
|
1305
|
+
return true;
|
|
1306
|
+
}
|
|
1307
|
+
const operandTypes = new Set(operand.map((item) => typeof item));
|
|
1308
|
+
return operand.some((item) => !isComparableScalar(item)) || operandTypes.size > 1;
|
|
1309
|
+
}
|
|
1310
|
+
if (operator === '$between') {
|
|
1311
|
+
if (!Array.isArray(operand) || operand.length !== 2) {
|
|
1312
|
+
return true;
|
|
1313
|
+
}
|
|
1314
|
+
const allNumbers = operand.every(isFiniteNumber);
|
|
1315
|
+
const allDates = operand.every((item) => item instanceof Date && Number.isFinite(item.getTime()));
|
|
1316
|
+
const allStrings = operand.every((item) => typeof item === 'string');
|
|
1317
|
+
return !allNumbers && !allDates && !allStrings;
|
|
1318
|
+
}
|
|
1319
|
+
if (['$eq', '$ne'].includes(operator)) {
|
|
1320
|
+
return !isComparableScalar(operand);
|
|
1321
|
+
}
|
|
1322
|
+
if (['$gt', '$gte', '$lt', '$lte'].includes(operator)) {
|
|
1323
|
+
return typeof operand !== 'string'
|
|
1324
|
+
&& !isFiniteNumber(operand)
|
|
1325
|
+
&& !(operand instanceof Date && Number.isFinite(operand.getTime()));
|
|
1326
|
+
}
|
|
1327
|
+
if (['$startsWith', '$endsWith', '$includes'].includes(operator)) {
|
|
1328
|
+
return typeof operand !== 'string';
|
|
1329
|
+
}
|
|
1330
|
+
if (operator === '$exists') {
|
|
1331
|
+
return typeof operand !== 'boolean';
|
|
1332
|
+
}
|
|
1333
|
+
return false;
|
|
1334
|
+
};
|
|
1335
|
+
if (hasMalformedOperand(value1) || hasMalformedOperand(value2)) {
|
|
1336
|
+
console.warn('judgeValueRelation: malformed filter operand');
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
const getConstraintEmptiness = (value) => {
|
|
1340
|
+
const finiteConstraint = toFiniteConstraint(value);
|
|
1341
|
+
if (finiteConstraint) {
|
|
1342
|
+
return finiteConstraintIsEmpty(finiteConstraint);
|
|
1343
|
+
}
|
|
1344
|
+
const numericConstraint = toNumericConstraint(value);
|
|
1345
|
+
if (numericConstraint) {
|
|
1346
|
+
return numericConstraintIsEmpty(numericConstraint);
|
|
1347
|
+
}
|
|
1348
|
+
const stringConstraint = toStringConstraint(value);
|
|
1349
|
+
if (stringConstraint) {
|
|
1350
|
+
return stringConstraintIsEmpty(stringConstraint);
|
|
1351
|
+
}
|
|
1352
|
+
return;
|
|
1353
|
+
};
|
|
1354
|
+
const value1IsEmpty = getConstraintEmptiness(value1);
|
|
1355
|
+
const value2IsEmpty = getConstraintEmptiness(value2);
|
|
1356
|
+
// ∅ ⊆ A 且 ∅ ∩ A = ∅,先处理空谓词,不能被无 schema 的类型差异降级成 unknown。
|
|
1357
|
+
if (value1IsEmpty === true) {
|
|
1358
|
+
return true;
|
|
1359
|
+
}
|
|
1360
|
+
if (value2IsEmpty === true) {
|
|
1361
|
+
if (!contained) {
|
|
1362
|
+
return true;
|
|
1363
|
+
}
|
|
1364
|
+
return value1IsEmpty === false ? false : undefined;
|
|
1365
|
+
}
|
|
1366
|
+
const getScalarType = (value) => {
|
|
1367
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
1368
|
+
return 'number';
|
|
1369
|
+
}
|
|
1370
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) {
|
|
1371
|
+
return 'date';
|
|
1372
|
+
}
|
|
1373
|
+
if (typeof value === 'string') {
|
|
1374
|
+
return 'string';
|
|
1375
|
+
}
|
|
1376
|
+
if (typeof value === 'boolean') {
|
|
1377
|
+
return 'boolean';
|
|
1378
|
+
}
|
|
1379
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
1380
|
+
return;
|
|
1381
|
+
}
|
|
1382
|
+
const [operator] = Object.keys(value);
|
|
1383
|
+
const operand = value[operator];
|
|
1384
|
+
const getOperandType = (item) => {
|
|
1385
|
+
if (typeof item === 'string') {
|
|
1386
|
+
return 'string';
|
|
1387
|
+
}
|
|
1388
|
+
if (item instanceof Date && Number.isFinite(item.getTime())) {
|
|
1389
|
+
return 'date';
|
|
1390
|
+
}
|
|
1391
|
+
if (typeof item === 'boolean') {
|
|
1392
|
+
return 'boolean';
|
|
1393
|
+
}
|
|
1394
|
+
if (isFiniteNumber(item)) {
|
|
1395
|
+
return 'number';
|
|
1396
|
+
}
|
|
1397
|
+
return;
|
|
1398
|
+
};
|
|
1399
|
+
if (['$eq', '$ne', '$gt', '$gte', '$lt', '$lte'].includes(operator)) {
|
|
1400
|
+
return getOperandType(operand);
|
|
1401
|
+
}
|
|
1402
|
+
if (['$in', '$nin'].includes(operator) && operand.length > 0) {
|
|
1403
|
+
return getOperandType(operand[0]);
|
|
1404
|
+
}
|
|
1405
|
+
if (operator === '$between') {
|
|
1406
|
+
return getOperandType(operand[0]);
|
|
1407
|
+
}
|
|
1408
|
+
if (['$startsWith', '$endsWith', '$includes'].includes(operator)) {
|
|
1409
|
+
return 'string';
|
|
1410
|
+
}
|
|
1411
|
+
return;
|
|
1412
|
+
};
|
|
1413
|
+
const valueType1 = getScalarType(value1);
|
|
1414
|
+
const valueType2 = getScalarType(value2);
|
|
1415
|
+
if (valueType1 && valueType2 && valueType1 !== valueType2) {
|
|
1416
|
+
// 没有字段 schema 时,跨类型约束不能安全地推断为包含或相斥。
|
|
1417
|
+
return;
|
|
1418
|
+
}
|
|
1419
|
+
const finiteConstraint1 = toFiniteConstraint(value1);
|
|
1420
|
+
const finiteConstraint2 = toFiniteConstraint(value2);
|
|
1421
|
+
if (finiteConstraint1 && finiteConstraint2) {
|
|
1422
|
+
const intersectionConstraint = intersectFiniteConstraints(finiteConstraint1, finiteConstraint2);
|
|
1423
|
+
if (contained) {
|
|
1424
|
+
if (finiteConstraintIsSubset(finiteConstraint1, finiteConstraint2)) {
|
|
1425
|
+
return true;
|
|
1426
|
+
}
|
|
1427
|
+
if (finiteConstraintIsEmpty(intersectionConstraint)) {
|
|
1428
|
+
return false;
|
|
1429
|
+
}
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
return finiteConstraintIsEmpty(intersectionConstraint);
|
|
1433
|
+
}
|
|
1434
|
+
const numericConstraint1 = toNumericConstraint(value1);
|
|
1435
|
+
const numericConstraint2 = toNumericConstraint(value2);
|
|
1436
|
+
if (numericConstraint1 && numericConstraint2) {
|
|
1437
|
+
const intersectionConstraint = intersectNumericConstraints(numericConstraint1, numericConstraint2);
|
|
1438
|
+
if (contained) {
|
|
1439
|
+
if (numericConstraintIsSubset(numericConstraint1, numericConstraint2)) {
|
|
1440
|
+
return true;
|
|
1441
|
+
}
|
|
1442
|
+
if (numericConstraintIsEmpty(intersectionConstraint)) {
|
|
1443
|
+
return false;
|
|
1444
|
+
}
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
return numericConstraintIsEmpty(intersectionConstraint);
|
|
1448
|
+
}
|
|
1449
|
+
const stringConstraint1 = toStringConstraint(value1);
|
|
1450
|
+
const stringConstraint2 = toStringConstraint(value2);
|
|
1451
|
+
if (stringConstraint1 && stringConstraint2) {
|
|
1452
|
+
if (contained) {
|
|
1453
|
+
if (stringConstraintIsSubset(stringConstraint1, stringConstraint2)) {
|
|
1454
|
+
return true;
|
|
1455
|
+
}
|
|
1456
|
+
if (stringConstraintIsDisjoint(stringConstraint1, stringConstraint2)) {
|
|
1457
|
+
return false;
|
|
1458
|
+
}
|
|
1459
|
+
return;
|
|
1460
|
+
}
|
|
1461
|
+
return stringConstraintIsDisjoint(stringConstraint1, stringConstraint2);
|
|
1462
|
+
}
|
|
1463
|
+
const getPresence = (value) => {
|
|
1464
|
+
if (value instanceof Date) {
|
|
1465
|
+
return {
|
|
1466
|
+
includesMissing: false,
|
|
1467
|
+
includesPresent: true,
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
1471
|
+
const predicate = value;
|
|
1472
|
+
const keys = Object.keys(predicate);
|
|
1473
|
+
if (keys.length !== 1) {
|
|
1474
|
+
return;
|
|
1475
|
+
}
|
|
1476
|
+
const operator = keys[0];
|
|
1477
|
+
if (operator === '$exists') {
|
|
1478
|
+
return {
|
|
1479
|
+
includesMissing: predicate.$exists === false,
|
|
1480
|
+
includesPresent: predicate.$exists === true,
|
|
1481
|
+
presenceOnly: true,
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
if (operator === '$in' && Array.isArray(predicate.$in) && predicate.$in.length === 0) {
|
|
1485
|
+
return {
|
|
1486
|
+
includesMissing: false,
|
|
1487
|
+
includesPresent: false,
|
|
1488
|
+
};
|
|
1489
|
+
}
|
|
1490
|
+
if (supportedOperators.includes(operator)) {
|
|
1491
|
+
return {
|
|
1492
|
+
includesMissing: false,
|
|
1493
|
+
includesPresent: true,
|
|
1494
|
+
presenceOnly: false,
|
|
1495
|
+
};
|
|
1496
|
+
}
|
|
1497
|
+
return;
|
|
1498
|
+
}
|
|
1499
|
+
return {
|
|
1500
|
+
includesMissing: false,
|
|
1501
|
+
includesPresent: true,
|
|
1502
|
+
};
|
|
1503
|
+
};
|
|
1504
|
+
const presence1 = getPresence(value1);
|
|
1505
|
+
const presence2 = getPresence(value2);
|
|
1506
|
+
const hasExistsPredicate = value1 && typeof value1 === 'object'
|
|
1507
|
+
&& Object.prototype.hasOwnProperty.call(value1, '$exists')
|
|
1508
|
+
|| value2 && typeof value2 === 'object'
|
|
1509
|
+
&& Object.prototype.hasOwnProperty.call(value2, '$exists');
|
|
1510
|
+
if (hasExistsPredicate && presence1 && presence2) {
|
|
1511
|
+
const isSubset = (!presence1.includesMissing || presence2.includesMissing)
|
|
1512
|
+
&& (!presence1.includesPresent || presence2.includesPresent);
|
|
1513
|
+
const isDisjoint = !(presence1.includesMissing && presence2.includesMissing)
|
|
1514
|
+
&& !(presence1.includesPresent && presence2.includesPresent);
|
|
1515
|
+
if (contained) {
|
|
1516
|
+
if (isSubset && (!presence1.presenceOnly || presence2.presenceOnly)) {
|
|
1517
|
+
return true;
|
|
1518
|
+
}
|
|
1519
|
+
if (isDisjoint) {
|
|
1520
|
+
return false;
|
|
1521
|
+
}
|
|
1522
|
+
return;
|
|
1523
|
+
}
|
|
1524
|
+
return isDisjoint;
|
|
1525
|
+
}
|
|
1526
|
+
// 结构化字段、模式与其他尚未建模的组合不做猜测;错误输入已在上面发出 warning。
|
|
1527
|
+
return;
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* 判断filter条件对compared条件上的attr键值的条件是否相容或相斥
|
|
1531
|
+
* @param entity
|
|
1532
|
+
* @param schema
|
|
1533
|
+
* @param attr
|
|
1534
|
+
* @param filter
|
|
1535
|
+
* @param compared
|
|
1536
|
+
* @param contained
|
|
1537
|
+
* @returns 返回true说明肯定相容(相斥),返回false说明肯定不相容(相斥),返回undefined说明无法判定相容(相斥),返回DeducedFilterCombination说明需要进一步判断此推断的条件
|
|
1538
|
+
*/
|
|
1539
|
+
/** 将 filter 中与目标属性相关的逻辑分支递归比较;无法静态证明的关系收集成可查询的推导组合。 */
|
|
1540
|
+
const maxXorExpansionBranches = 7;
|
|
1541
|
+
function expandXorFilter(branches, caller) {
|
|
1542
|
+
if (!Array.isArray(branches)) {
|
|
1543
|
+
console.warn(`${caller}: malformed $xor filter; relation is unknown`);
|
|
1544
|
+
return;
|
|
1545
|
+
}
|
|
1546
|
+
if (branches.length > maxXorExpansionBranches) {
|
|
1547
|
+
console.warn(`${caller}: $xor expansion limit reached; relation is unknown`);
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
if (branches.some(branch => !branch || typeof branch !== 'object' || Array.isArray(branch))) {
|
|
1551
|
+
console.warn(`${caller}: malformed $xor branch; relation is unknown`);
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
if (branches.length === 0) {
|
|
1555
|
+
console.warn(`${caller}: empty $xor filter; relation is unknown`);
|
|
1556
|
+
return;
|
|
1557
|
+
}
|
|
1558
|
+
if (branches.length === 1) {
|
|
1559
|
+
return cloneDeep(branches[0]);
|
|
1560
|
+
}
|
|
1561
|
+
// Odd parity is the union of assignments with an odd number of true branches:
|
|
1562
|
+
// XOR(P₁,…,Pₙ) = ⋃_{|S| odd}(⋂_{i∈S} Pᵢ ∩ ⋂_{i∉S} ¬Pᵢ).
|
|
1563
|
+
const alternatives = [];
|
|
1564
|
+
const assignmentCount = 1 << branches.length;
|
|
1565
|
+
for (let mask = 1; mask < assignmentCount; mask++) {
|
|
1566
|
+
let selectedCount = 0;
|
|
1567
|
+
for (let bits = mask; bits !== 0; bits &= bits - 1) {
|
|
1568
|
+
selectedCount++;
|
|
1569
|
+
}
|
|
1570
|
+
if (selectedCount % 2 === 0) {
|
|
1571
|
+
continue;
|
|
1572
|
+
}
|
|
1573
|
+
const conditions = branches.map((branch, index) => mask & (1 << index)
|
|
1574
|
+
? cloneDeep(branch)
|
|
1575
|
+
: { $not: cloneDeep(branch) });
|
|
1576
|
+
alternatives.push({ $and: conditions });
|
|
1577
|
+
}
|
|
1578
|
+
return { $or: alternatives };
|
|
1579
|
+
}
|
|
1580
|
+
function judgeFilterSingleAttrRelation(entity, schema, attr, filter, compared, contained) {
|
|
1581
|
+
const comparedFilterAttrValue = compared[attr];
|
|
1582
|
+
const orDeducedFilters = [];
|
|
1583
|
+
for (const attr2 in filter) {
|
|
1584
|
+
if (['$and', '$or', '$xor', '$not'].includes(attr2)) {
|
|
1585
|
+
switch (attr2) {
|
|
1586
|
+
case '$and':
|
|
1587
|
+
case '$or': {
|
|
1588
|
+
if (!Array.isArray(filter[attr2])) {
|
|
1589
|
+
console.warn(`judgeFilterSingleAttrRelation: malformed ${attr2} filter; relation is unknown`);
|
|
1590
|
+
return;
|
|
1591
|
+
}
|
|
1592
|
+
const logicQueries = filter[attr2];
|
|
1593
|
+
const results = logicQueries.map((logicQuery) => judgeFilterSingleAttrRelation(entity, schema, attr, logicQuery, compared, contained));
|
|
1594
|
+
const deduced = results.filter((r) => typeof r === 'object');
|
|
1595
|
+
if (attr2 === '$and') {
|
|
1596
|
+
// F=A∩B:任一子集已包含 compared,或任一子集已与 compared 相斥,都足以证明整体关系。
|
|
1597
|
+
if (results.some(r => r === true)) {
|
|
1598
|
+
return true;
|
|
1599
|
+
}
|
|
1600
|
+
if (deduced.length > 0) {
|
|
1601
|
+
orDeducedFilters.push(...deduced);
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
else {
|
|
1605
|
+
// F=A∪B:包含要求每个分支都包含;相斥要求每个分支都相斥。
|
|
1606
|
+
if (results.some(r => r === false)) {
|
|
1607
|
+
return false;
|
|
1608
|
+
}
|
|
1609
|
+
if (results.some(r => r === undefined)) {
|
|
1610
|
+
return;
|
|
1611
|
+
}
|
|
1612
|
+
if (deduced.length > 0) {
|
|
1613
|
+
orDeducedFilters.push({ $and: deduced });
|
|
1614
|
+
}
|
|
1615
|
+
else {
|
|
1616
|
+
return true;
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
break;
|
|
1620
|
+
}
|
|
1621
|
+
case '$xor': {
|
|
1622
|
+
const expanded = expandXorFilter(filter[attr2], 'judgeFilterSingleAttrRelation');
|
|
1623
|
+
if (!expanded) {
|
|
1624
|
+
return;
|
|
1625
|
+
}
|
|
1626
|
+
const result = judgeFilterSingleAttrRelation(entity, schema, attr, expanded, compared, contained);
|
|
1627
|
+
if (typeof result === 'boolean') {
|
|
1628
|
+
return result;
|
|
1629
|
+
}
|
|
1630
|
+
if (typeof result === 'object') {
|
|
1631
|
+
orDeducedFilters.push(result);
|
|
1632
|
+
}
|
|
1633
|
+
break;
|
|
1634
|
+
}
|
|
1635
|
+
case '$not': {
|
|
1636
|
+
if (!contained) {
|
|
1637
|
+
const logicQuery = filter[attr2];
|
|
1638
|
+
const r = judgeFilterRelation(entity, schema, { [attr]: comparedFilterAttrValue }, logicQuery, true);
|
|
1639
|
+
if (r === true) {
|
|
1640
|
+
return true;
|
|
1641
|
+
}
|
|
1642
|
+
else if (typeof r === 'object') {
|
|
1643
|
+
orDeducedFilters.push(r);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
break;
|
|
1647
|
+
}
|
|
1648
|
+
default: {
|
|
1649
|
+
console.warn(`judgeFilterSingleAttrRelation: unsupported logical operator ${attr2}; relation is unknown`);
|
|
1650
|
+
return;
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
else if (attr2.toLowerCase().startsWith(EXPRESSION_PREFIX)) {
|
|
1655
|
+
// 表达式会缩小 filter 结果集,但单个属性分析无法建模其取值范围。
|
|
1656
|
+
}
|
|
1657
|
+
else if (attr2.toLowerCase() === '$text') {
|
|
1658
|
+
// 全文条件会缩小 filter 结果集,单属性关系无法据此证明包含或相斥。
|
|
1659
|
+
}
|
|
1660
|
+
else {
|
|
1661
|
+
const rel = judgeRelation(schema, entity, attr2);
|
|
1662
|
+
if (attr === attr2) {
|
|
1663
|
+
if (rel === 1) {
|
|
1664
|
+
const r = judgeValueRelation(filter[attr2], comparedFilterAttrValue, contained);
|
|
1665
|
+
if (typeof r === 'boolean') {
|
|
1666
|
+
return r;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
else if (rel === 2) {
|
|
1670
|
+
const r = judgeFilterRelation(attr2, schema, filter[attr2], comparedFilterAttrValue, contained);
|
|
1671
|
+
if (typeof r === 'boolean') {
|
|
1672
|
+
return r;
|
|
1673
|
+
}
|
|
1674
|
+
else if (typeof r === 'object') {
|
|
1675
|
+
orDeducedFilters.push(r);
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
else if (typeof rel === 'string') {
|
|
1679
|
+
const r = judgeFilterRelation(rel, schema, filter[attr2], comparedFilterAttrValue, contained);
|
|
1680
|
+
if (typeof r === 'boolean') {
|
|
1681
|
+
return r;
|
|
1682
|
+
}
|
|
1683
|
+
else if (typeof r === 'object') {
|
|
1684
|
+
orDeducedFilters.push(r);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
else {
|
|
1688
|
+
// 一对多关系的量词关系无法在此单属性比较中静态证明。
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
else if (rel === 2 && attr === 'entity' && comparedFilterAttrValue === attr2 && Object.prototype.hasOwnProperty.call(compared, 'entityId')) {
|
|
1692
|
+
// compared指定了entity和entityId,而filter指定了该entity上的查询条件,此时转而比较此entity上的filter
|
|
1693
|
+
const r = judgeFilterRelation(attr2, schema, filter[attr2], {
|
|
1694
|
+
id: compared.entityId
|
|
1695
|
+
}, contained);
|
|
1696
|
+
if (typeof r === 'boolean') {
|
|
1697
|
+
return r;
|
|
1698
|
+
}
|
|
1699
|
+
else if (typeof r === 'object') {
|
|
1700
|
+
orDeducedFilters.push(r);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
else if (typeof rel === 'string' && attr === `${attr2}Id`) {
|
|
1704
|
+
// compared指定了外键,而filter指定了该外键对象上的查询条件,此时转而比较此entity上的filter
|
|
1705
|
+
const r = judgeFilterRelation(rel, schema, filter[attr2], {
|
|
1706
|
+
id: comparedFilterAttrValue
|
|
1707
|
+
}, contained);
|
|
1708
|
+
if (typeof r === 'boolean') {
|
|
1709
|
+
return r;
|
|
1710
|
+
}
|
|
1711
|
+
else if (typeof r === 'object') {
|
|
1712
|
+
orDeducedFilters.push(r);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
else {
|
|
1716
|
+
const rel2 = judgeRelation(schema, entity, attr);
|
|
1717
|
+
if (rel2 === 2 && attr2 === 'entity' && filter[attr2] === attr && Object.prototype.hasOwnProperty.call(filter, 'entityId')) {
|
|
1718
|
+
// filter限制了外键范围,而compared指定了该外键对象上的查询条件,此时转而比较目标实体filter。
|
|
1719
|
+
const r = judgeFilterRelation(attr, schema, {
|
|
1720
|
+
id: filter.entityId,
|
|
1721
|
+
}, comparedFilterAttrValue, contained);
|
|
1722
|
+
if (typeof r === 'boolean') {
|
|
1723
|
+
return r;
|
|
1724
|
+
}
|
|
1725
|
+
else if (typeof r === 'object') {
|
|
1726
|
+
orDeducedFilters.push(r);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
else if (typeof rel2 === 'string' && attr2 === `${attr}Id`) {
|
|
1730
|
+
// filter限制了外键范围,而compared指定了外键对象上的查询条件,此时转而比较目标实体filter。
|
|
1731
|
+
const r = judgeFilterRelation(rel2, schema, {
|
|
1732
|
+
id: filter[attr2],
|
|
1733
|
+
}, comparedFilterAttrValue, contained);
|
|
1734
|
+
if (typeof r === 'boolean') {
|
|
1735
|
+
return r;
|
|
1736
|
+
}
|
|
1737
|
+
else if (typeof r === 'object') {
|
|
1738
|
+
orDeducedFilters.push(r);
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
if (orDeducedFilters.length > 0) {
|
|
1745
|
+
return {
|
|
1746
|
+
$or: orDeducedFilters,
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1749
|
+
return;
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* 根据filter对compared查询的各个条件进行逐项分析
|
|
1753
|
+
* @param entity
|
|
1754
|
+
* @param schema
|
|
1755
|
+
* @param filter
|
|
1756
|
+
* @param compared
|
|
1757
|
+
* @param contained
|
|
1758
|
+
* @returns
|
|
1759
|
+
* sureAttributes中包含被判定肯定相容或肯定不相斥的属性(不用再继续判定了)
|
|
1760
|
+
* uncertainAttributes中包含的是无法判定结果的属性
|
|
1761
|
+
* totalAndDeducedFilters包含的是判定过程中推论的相容的充分条件(and关系)
|
|
1762
|
+
* totalOrDeducedFilters包含的是判定过程中推论的相斥的充分条件(or关系)
|
|
1763
|
+
*/
|
|
1764
|
+
/** 按 compared 的顶层键做合取分解,分别记录可证明、未知和需到数据层求证的条件。 */
|
|
1765
|
+
export function analyzeFilterRelation(entity, schema, filter, compared, contained) {
|
|
1766
|
+
const totalAndDeducedFilters = [];
|
|
1767
|
+
const totalOrDeducedFilters = [];
|
|
1768
|
+
const uncertainAttributes = [];
|
|
1769
|
+
const sureAttributes = []; // 对包容查询,肯定此属性可包容;对相斥查询,肯定此属性不相斥
|
|
1770
|
+
for (let attr in compared) {
|
|
1771
|
+
let result = undefined;
|
|
1772
|
+
const deducedCombinations = [];
|
|
1773
|
+
if (['$and', '$or', '$xor', '$not'].includes(attr)) {
|
|
1774
|
+
switch (attr) {
|
|
1775
|
+
case '$and': {
|
|
1776
|
+
if (!Array.isArray(compared[attr])) {
|
|
1777
|
+
console.warn('analyzeFilterRelation: malformed $and filter; relation is unknown');
|
|
1778
|
+
break;
|
|
1779
|
+
}
|
|
1780
|
+
const logicQueries = compared[attr];
|
|
1781
|
+
const results = logicQueries.map((logicQuery) => judgeFilterRelation(entity, schema, filter, logicQuery, contained));
|
|
1782
|
+
const andDeducedFilters = [];
|
|
1783
|
+
const orDeducedFilters = [];
|
|
1784
|
+
for (const r of results) {
|
|
1785
|
+
if (contained) {
|
|
1786
|
+
// 如果是包容关系,需要全部被包容,有一个被证伪就已经失败了
|
|
1787
|
+
if (r === false) {
|
|
1788
|
+
result = false;
|
|
1789
|
+
break;
|
|
1790
|
+
}
|
|
1791
|
+
else if (r === undefined) {
|
|
1792
|
+
// 有一个无法判断就放弃
|
|
1793
|
+
andDeducedFilters.splice(0, andDeducedFilters.length);
|
|
1794
|
+
result = undefined;
|
|
1795
|
+
break;
|
|
1796
|
+
}
|
|
1797
|
+
else if (typeof r === 'object') {
|
|
1798
|
+
andDeducedFilters.push(r);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
else {
|
|
1802
|
+
assert(!contained);
|
|
1803
|
+
// 如果是相斥关系,只要和一个相斥就可以,有一个被证实就成功了
|
|
1804
|
+
if (r === true) {
|
|
1805
|
+
orDeducedFilters.splice(0, orDeducedFilters.length);
|
|
1806
|
+
result = true;
|
|
1807
|
+
break;
|
|
1808
|
+
}
|
|
1809
|
+
else if (typeof r === 'object') {
|
|
1810
|
+
orDeducedFilters.push(r);
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
if (contained && result === undefined && andDeducedFilters.length === 0
|
|
1815
|
+
&& results.every(r => r === true)) {
|
|
1816
|
+
// F ⊆ (A ∩ B) 当且仅当 F ⊆ A 且 F ⊆ B;空 AND 为全集,亦满足此式。
|
|
1817
|
+
result = true;
|
|
1818
|
+
}
|
|
1819
|
+
if (andDeducedFilters.length > 0) {
|
|
1820
|
+
deducedCombinations.push({
|
|
1821
|
+
$and: andDeducedFilters,
|
|
1822
|
+
});
|
|
1823
|
+
}
|
|
1824
|
+
if (orDeducedFilters.length > 0) {
|
|
1825
|
+
deducedCombinations.push({
|
|
1826
|
+
$or: orDeducedFilters,
|
|
1827
|
+
});
|
|
1828
|
+
}
|
|
1829
|
+
break;
|
|
1830
|
+
}
|
|
1831
|
+
case '$or': {
|
|
1832
|
+
if (!Array.isArray(compared[attr])) {
|
|
1833
|
+
console.warn(`analyzeFilterRelation: malformed $or filter; relation is unknown`);
|
|
1834
|
+
break;
|
|
1835
|
+
}
|
|
1836
|
+
const logicQueries = compared[attr];
|
|
1837
|
+
const results = logicQueries.map((logicQuery) => judgeFilterRelation(entity, schema, filter, logicQuery, contained));
|
|
1838
|
+
const andDeducedFilters = [];
|
|
1839
|
+
const orDeducedFilters = [];
|
|
1840
|
+
for (const r of results) {
|
|
1841
|
+
if (contained) {
|
|
1842
|
+
// 如果是包容关系,只要包容一个(是其查询子集)就可以
|
|
1843
|
+
if (r === true) {
|
|
1844
|
+
orDeducedFilters.splice(0, orDeducedFilters.length);
|
|
1845
|
+
result = true;
|
|
1846
|
+
break;
|
|
1847
|
+
}
|
|
1848
|
+
else if (typeof r === 'object') {
|
|
1849
|
+
// 这里不能把or下降到所有的查询中去分别判定,有可能此条件需要多个compared中的情况来共同满足
|
|
1850
|
+
// orDeducedFilters.push(r);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
else {
|
|
1854
|
+
assert(!contained);
|
|
1855
|
+
// 如果是相斥关系,必须和每一个都相斥
|
|
1856
|
+
if (r === false) {
|
|
1857
|
+
result = false;
|
|
1858
|
+
break;
|
|
1859
|
+
}
|
|
1860
|
+
else if (r === undefined) {
|
|
1861
|
+
// 有一个无法判断就放弃
|
|
1862
|
+
andDeducedFilters.splice(0, andDeducedFilters.length);
|
|
1863
|
+
result = undefined;
|
|
1864
|
+
break;
|
|
1865
|
+
}
|
|
1866
|
+
else if (typeof r === 'object') {
|
|
1867
|
+
andDeducedFilters.push(r);
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
if (!contained && result === undefined && andDeducedFilters.length === 0
|
|
1872
|
+
&& results.every(r => r === true)) {
|
|
1873
|
+
// F ∩ (A ∪ B) = ∅ 当且仅当 F ∩ A = ∅ 且 F ∩ B = ∅;空 OR 为 ∅。
|
|
1874
|
+
result = true;
|
|
1875
|
+
}
|
|
1876
|
+
if (andDeducedFilters.length > 0) {
|
|
1877
|
+
deducedCombinations.push({
|
|
1878
|
+
$and: andDeducedFilters,
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
if (orDeducedFilters.length > 0) {
|
|
1882
|
+
deducedCombinations.push({
|
|
1883
|
+
$or: orDeducedFilters,
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1886
|
+
break;
|
|
1887
|
+
}
|
|
1888
|
+
case '$xor': {
|
|
1889
|
+
const expanded = expandXorFilter(compared[attr], 'analyzeFilterRelation');
|
|
1890
|
+
if (!expanded) {
|
|
1891
|
+
break;
|
|
1892
|
+
}
|
|
1893
|
+
const r = judgeFilterRelation(entity, schema, filter, expanded, contained);
|
|
1894
|
+
if (contained) {
|
|
1895
|
+
if (r === true || r === false) {
|
|
1896
|
+
result = r;
|
|
1897
|
+
}
|
|
1898
|
+
else if (typeof r === 'object') {
|
|
1899
|
+
deducedCombinations.push(r);
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
else if (r === true) {
|
|
1903
|
+
result = true;
|
|
1904
|
+
}
|
|
1905
|
+
else if (typeof r === 'object') {
|
|
1906
|
+
deducedCombinations.push(r);
|
|
1907
|
+
}
|
|
1908
|
+
break;
|
|
1909
|
+
}
|
|
1910
|
+
case '$not': {
|
|
1911
|
+
/**
|
|
1912
|
+
* 若filter与compared not所定义的部分相斥,则filter与conditionalFilter相容
|
|
1913
|
+
* 若filter将compared not所定义的部分包容,则filter与conditionalFilter相斥
|
|
1914
|
+
*/
|
|
1915
|
+
const logicQuery = compared[attr];
|
|
1916
|
+
if (contained) {
|
|
1917
|
+
const r = judgeFilterRelation(entity, schema, filter, logicQuery, false);
|
|
1918
|
+
if (r === true) {
|
|
1919
|
+
result = true;
|
|
1920
|
+
}
|
|
1921
|
+
else if (r === false) {
|
|
1922
|
+
// F ⊆ ¬A 要求 F ∩ A = ∅;已证明相交即可反证包含。
|
|
1923
|
+
result = false;
|
|
1924
|
+
}
|
|
1925
|
+
else if (typeof r === 'object') {
|
|
1926
|
+
deducedCombinations.push(r);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
else {
|
|
1930
|
+
const r = judgeFilterRelation(entity, schema, filter, logicQuery, true);
|
|
1931
|
+
if (r === true) {
|
|
1932
|
+
result = true;
|
|
1933
|
+
}
|
|
1934
|
+
else if (typeof r === 'object') {
|
|
1935
|
+
deducedCombinations.push(r);
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
break;
|
|
1939
|
+
}
|
|
1940
|
+
default: {
|
|
1941
|
+
console.warn(`analyzeFilterRelation: unsupported logical operator ${attr}; relation is unknown`);
|
|
1942
|
+
break;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
else if (attr.toLowerCase().startsWith(EXPRESSION_PREFIX)) {
|
|
1947
|
+
// 相当于缩小了compared查询结果,如果是判定相斥,对结果无影响,如果是判定相容,则认为无法判定,
|
|
1948
|
+
if (contained) {
|
|
1949
|
+
result = undefined;
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
else if (attr.toLowerCase() === '$text') {
|
|
1953
|
+
// 相当于缩小了compared查询结果,如果是判定相斥,对结果无影响,如果是判定相容,则认为无法判定,
|
|
1954
|
+
if (contained) {
|
|
1955
|
+
result = undefined;
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
else {
|
|
1959
|
+
const r = judgeFilterSingleAttrRelation(entity, schema, attr, filter, compared, contained);
|
|
1960
|
+
if (typeof r === 'object') {
|
|
1961
|
+
deducedCombinations.push(r);
|
|
1962
|
+
}
|
|
1963
|
+
else {
|
|
1964
|
+
result = r;
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
if (contained) {
|
|
1968
|
+
// 相容必须compared中的每个属性都被相容
|
|
1969
|
+
if (result === true) {
|
|
1970
|
+
sureAttributes.push(attr);
|
|
1971
|
+
}
|
|
1972
|
+
else if (result === false) {
|
|
1973
|
+
return false;
|
|
1974
|
+
}
|
|
1975
|
+
else if (deducedCombinations.length > 0) {
|
|
1976
|
+
totalAndDeducedFilters.push(...deducedCombinations);
|
|
1977
|
+
}
|
|
1978
|
+
else {
|
|
1979
|
+
// 判定不了,也推断不了
|
|
1980
|
+
uncertainAttributes.push(attr);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
else {
|
|
1984
|
+
// 相斥只要有一个被肻定就可以返回true了
|
|
1985
|
+
if (result === true) {
|
|
1986
|
+
return true;
|
|
1987
|
+
}
|
|
1988
|
+
else if (result === false) {
|
|
1989
|
+
sureAttributes.push(attr);
|
|
1990
|
+
}
|
|
1991
|
+
else if (deducedCombinations.length > 0) {
|
|
1992
|
+
totalOrDeducedFilters.push(...deducedCombinations);
|
|
1993
|
+
}
|
|
1994
|
+
else {
|
|
1995
|
+
// 判定不了,也推断不了
|
|
1996
|
+
uncertainAttributes.push(attr);
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
return {
|
|
2001
|
+
totalAndDeducedFilters,
|
|
2002
|
+
totalOrDeducedFilters,
|
|
2003
|
+
uncertainAttributes,
|
|
2004
|
+
sureAttributes,
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
/** 判断filter条件对compared条件是否相容或相斥
|
|
2008
|
+
* @param entity
|
|
2009
|
+
* @param schema
|
|
2010
|
+
* @param filter
|
|
2011
|
+
* @param compared
|
|
2012
|
+
* @param contained: true代表判定filter包容compared(filter的查询结果是compared查询结果的子集), false代表判定filter与compared相斥(filter的查询结果与compared没有交集)
|
|
2013
|
+
* @returns 返回true说明肯定相容(相斥),返回false说明无法判定相容(相斥),返回DeducedFilterCombination说明需要进一步判断此推断的条件
|
|
2014
|
+
*/
|
|
2015
|
+
/** 汇总各属性的静态结论;未知属性改写为待查 filter,并按包含用 AND、相斥用 OR 组合。 */
|
|
2016
|
+
/**
|
|
2017
|
+
* 计算只含逻辑/元数据节点的 filter 常量:`true` 表示全集 U,`false` 表示空集 ∅。
|
|
2018
|
+
* 逻辑恒等式:`AND[] = U`、`OR[] = ∅`、`NOT(U) = ∅`、`NOT(∅) = U`。
|
|
2019
|
+
* 含业务属性、表达式或未知算子时返回 `undefined`,交给常规关系分析保守处理。
|
|
2020
|
+
*/
|
|
2021
|
+
function getFilterConstant(filter, schema, entity) {
|
|
2022
|
+
const isSimplePredicate = (predicate) => {
|
|
2023
|
+
if (typeof predicate === 'string' || typeof predicate === 'boolean') {
|
|
2024
|
+
return true;
|
|
2025
|
+
}
|
|
2026
|
+
if (typeof predicate === 'number') {
|
|
2027
|
+
return Number.isFinite(predicate);
|
|
2028
|
+
}
|
|
2029
|
+
if (predicate instanceof Date) {
|
|
2030
|
+
return Number.isFinite(predicate.getTime());
|
|
2031
|
+
}
|
|
2032
|
+
if (!predicate || typeof predicate !== 'object' || Array.isArray(predicate)) {
|
|
2033
|
+
return false;
|
|
2034
|
+
}
|
|
2035
|
+
const keys = Object.keys(predicate);
|
|
2036
|
+
if (keys.length !== 1) {
|
|
2037
|
+
return false;
|
|
2038
|
+
}
|
|
2039
|
+
const record = predicate;
|
|
2040
|
+
return keys[0] === '$exists' && typeof record.$exists === 'boolean'
|
|
2041
|
+
|| !!toFiniteConstraint(record)
|
|
2042
|
+
|| !!toNumericConstraint(record)
|
|
2043
|
+
|| !!toStringConstraint(record);
|
|
2044
|
+
};
|
|
2045
|
+
const isFilterObject = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
2046
|
+
const isEmptySimplePredicate = (predicate) => {
|
|
2047
|
+
const finiteConstraint = toFiniteConstraint(predicate);
|
|
2048
|
+
if (finiteConstraint) {
|
|
2049
|
+
return finiteConstraintIsEmpty(finiteConstraint);
|
|
2050
|
+
}
|
|
2051
|
+
const numericConstraint = toNumericConstraint(predicate);
|
|
2052
|
+
if (numericConstraint) {
|
|
2053
|
+
return numericConstraintIsEmpty(numericConstraint);
|
|
2054
|
+
}
|
|
2055
|
+
const stringConstraint = toStringConstraint(predicate);
|
|
2056
|
+
return !!stringConstraint && stringConstraintIsEmpty(stringConstraint);
|
|
2057
|
+
};
|
|
2058
|
+
const maxConjunctiveAlternatives = 128;
|
|
2059
|
+
let warnedExpansionLimit = false;
|
|
2060
|
+
const warnExpansionLimit = () => {
|
|
2061
|
+
if (!warnedExpansionLimit) {
|
|
2062
|
+
console.warn('getFilterConstant: logical expansion limit reached; treating the remaining filter as unknown');
|
|
2063
|
+
warnedExpansionLimit = true;
|
|
2064
|
+
}
|
|
2065
|
+
};
|
|
2066
|
+
const emptyConstraintState = () => ({
|
|
2067
|
+
positives: new Map(),
|
|
2068
|
+
negatives: new Map(),
|
|
2069
|
+
});
|
|
2070
|
+
const cloneConstraintState = (state) => ({
|
|
2071
|
+
positives: new Map([...state.positives].map(([attr, predicates]) => [attr, [...predicates]])),
|
|
2072
|
+
negatives: new Map([...state.negatives].map(([attr, predicates]) => [attr, [...predicates]])),
|
|
2073
|
+
});
|
|
2074
|
+
const addConstraint = (state, negative, attr, predicate) => {
|
|
2075
|
+
const constraints = negative ? state.negatives : state.positives;
|
|
2076
|
+
const existing = constraints.get(attr);
|
|
2077
|
+
if (existing) {
|
|
2078
|
+
existing.push(predicate);
|
|
2079
|
+
}
|
|
2080
|
+
else {
|
|
2081
|
+
constraints.set(attr, [predicate]);
|
|
2082
|
+
}
|
|
2083
|
+
};
|
|
2084
|
+
const constraintsAreContradictory = (state) => {
|
|
2085
|
+
for (const [attr, positives] of state.positives) {
|
|
2086
|
+
for (let leftIndex = 0; leftIndex < positives.length; leftIndex++) {
|
|
2087
|
+
for (let rightIndex = leftIndex + 1; rightIndex < positives.length; rightIndex++) {
|
|
2088
|
+
if (judgeValueRelation(positives[leftIndex], positives[rightIndex], false) === true) {
|
|
2089
|
+
return true;
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
const negatives = state.negatives.get(attr) || [];
|
|
2094
|
+
if (positives.some(positive => negatives.some(negative => judgeValueRelation(positive, negative, true) === true))) {
|
|
2095
|
+
return true;
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
return false;
|
|
2099
|
+
};
|
|
2100
|
+
const mergeConstraintStates = (left, right) => {
|
|
2101
|
+
const merged = cloneConstraintState(left);
|
|
2102
|
+
for (const [attr, predicates] of right.positives) {
|
|
2103
|
+
predicates.forEach(predicate => addConstraint(merged, false, attr, predicate));
|
|
2104
|
+
}
|
|
2105
|
+
for (const [attr, predicates] of right.negatives) {
|
|
2106
|
+
predicates.forEach(predicate => addConstraint(merged, true, attr, predicate));
|
|
2107
|
+
}
|
|
2108
|
+
return constraintsAreContradictory(merged) ? undefined : merged;
|
|
2109
|
+
};
|
|
2110
|
+
const intersectAlternatives = (left, right) => {
|
|
2111
|
+
if (left.length === 0 || right.length === 0) {
|
|
2112
|
+
return [];
|
|
2113
|
+
}
|
|
2114
|
+
if (left.length * right.length > maxConjunctiveAlternatives) {
|
|
2115
|
+
warnExpansionLimit();
|
|
2116
|
+
return [emptyConstraintState()];
|
|
2117
|
+
}
|
|
2118
|
+
const result = [];
|
|
2119
|
+
for (const leftState of left) {
|
|
2120
|
+
for (const rightState of right) {
|
|
2121
|
+
const merged = mergeConstraintStates(leftState, rightState);
|
|
2122
|
+
if (merged) {
|
|
2123
|
+
result.push(merged);
|
|
2124
|
+
if (result.length > maxConjunctiveAlternatives) {
|
|
2125
|
+
warnExpansionLimit();
|
|
2126
|
+
return [emptyConstraintState()];
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
return result;
|
|
2132
|
+
};
|
|
2133
|
+
const unionAlternatives = (branches) => {
|
|
2134
|
+
const alternatives = [];
|
|
2135
|
+
for (const branch of branches) {
|
|
2136
|
+
alternatives.push(...getConjunctiveAlternatives(branch));
|
|
2137
|
+
if (alternatives.length > maxConjunctiveAlternatives) {
|
|
2138
|
+
warnExpansionLimit();
|
|
2139
|
+
return [emptyConstraintState()];
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
return alternatives;
|
|
2143
|
+
};
|
|
2144
|
+
const getNegatedAlternatives = (branch) => {
|
|
2145
|
+
if (!isFilterObject(branch)) {
|
|
2146
|
+
console.warn('getFilterConstant: malformed $not filter; treating it as unknown');
|
|
2147
|
+
return [emptyConstraintState()];
|
|
2148
|
+
}
|
|
2149
|
+
const entries = Object.entries(branch);
|
|
2150
|
+
if (entries.length !== 1 || entries[0][0].startsWith('$') || entries[0][0].startsWith('#')
|
|
2151
|
+
|| !isSimplePredicate(entries[0][1])) {
|
|
2152
|
+
// Unsupported negation is over-approximated by U so it cannot create a false empty-set proof.
|
|
2153
|
+
return [emptyConstraintState()];
|
|
2154
|
+
}
|
|
2155
|
+
const [attr, predicate] = entries[0];
|
|
2156
|
+
if (isEmptySimplePredicate(predicate)) {
|
|
2157
|
+
return [emptyConstraintState()];
|
|
2158
|
+
}
|
|
2159
|
+
const state = emptyConstraintState();
|
|
2160
|
+
addConstraint(state, true, attr, predicate);
|
|
2161
|
+
return [state];
|
|
2162
|
+
};
|
|
2163
|
+
const getConjunctiveAlternatives = (value) => {
|
|
2164
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
2165
|
+
return [emptyConstraintState()];
|
|
2166
|
+
}
|
|
2167
|
+
let alternatives = [emptyConstraintState()];
|
|
2168
|
+
for (const [attr, predicate] of Object.entries(value)) {
|
|
2169
|
+
if (attr.startsWith('#')) {
|
|
2170
|
+
continue;
|
|
2171
|
+
}
|
|
2172
|
+
let predicateAlternatives;
|
|
2173
|
+
if (attr === '$and') {
|
|
2174
|
+
if (!Array.isArray(predicate) || predicate.some(branch => !isFilterObject(branch))) {
|
|
2175
|
+
console.warn('getFilterConstant: malformed $and filter; treating it as unknown');
|
|
2176
|
+
continue;
|
|
2177
|
+
}
|
|
2178
|
+
predicateAlternatives = [emptyConstraintState()];
|
|
2179
|
+
for (const branch of predicate) {
|
|
2180
|
+
predicateAlternatives = intersectAlternatives(predicateAlternatives, getConjunctiveAlternatives(branch));
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
else if (attr === '$or') {
|
|
2184
|
+
if (!Array.isArray(predicate) || predicate.some(branch => !isFilterObject(branch))) {
|
|
2185
|
+
console.warn('getFilterConstant: malformed $or filter; treating it as unknown');
|
|
2186
|
+
continue;
|
|
2187
|
+
}
|
|
2188
|
+
predicateAlternatives = unionAlternatives(predicate);
|
|
2189
|
+
}
|
|
2190
|
+
else if (attr === '$xor') {
|
|
2191
|
+
if (!Array.isArray(predicate)) {
|
|
2192
|
+
console.warn('getFilterConstant: malformed $xor filter; treating it as unknown');
|
|
2193
|
+
continue;
|
|
2194
|
+
}
|
|
2195
|
+
if (predicate.some(branch => !isFilterObject(branch))) {
|
|
2196
|
+
console.warn('getFilterConstant: malformed $xor branch; treating it as unknown');
|
|
2197
|
+
continue;
|
|
2198
|
+
}
|
|
2199
|
+
if (predicate.length === 0) {
|
|
2200
|
+
console.warn('getFilterConstant: empty $xor filter; treating it as unknown');
|
|
2201
|
+
return [emptyConstraintState()];
|
|
2202
|
+
}
|
|
2203
|
+
if (predicate.length > maxXorExpansionBranches) {
|
|
2204
|
+
warnExpansionLimit();
|
|
2205
|
+
continue;
|
|
2206
|
+
}
|
|
2207
|
+
// XOR is odd parity: ⊕ᵢ Pᵢ = ⋃_{|S| odd}(⋂ᵢ∈S Pᵢ ∩ ⋂ᵢ∉S ¬Pᵢ).
|
|
2208
|
+
predicateAlternatives = [];
|
|
2209
|
+
const assignmentCount = 1 << predicate.length;
|
|
2210
|
+
for (let mask = 0; mask < assignmentCount; mask++) {
|
|
2211
|
+
let selectedCount = 0;
|
|
2212
|
+
for (let bits = mask; bits !== 0; bits &= bits - 1) {
|
|
2213
|
+
selectedCount++;
|
|
2214
|
+
}
|
|
2215
|
+
if (selectedCount % 2 === 0) {
|
|
2216
|
+
continue;
|
|
2217
|
+
}
|
|
2218
|
+
let termAlternatives = [emptyConstraintState()];
|
|
2219
|
+
for (let index = 0; index < predicate.length; index++) {
|
|
2220
|
+
const branchAlternatives = mask & (1 << index)
|
|
2221
|
+
? getConjunctiveAlternatives(predicate[index])
|
|
2222
|
+
: getNegatedAlternatives(predicate[index]);
|
|
2223
|
+
termAlternatives = intersectAlternatives(termAlternatives, branchAlternatives);
|
|
2224
|
+
}
|
|
2225
|
+
predicateAlternatives.push(...termAlternatives);
|
|
2226
|
+
if (predicateAlternatives.length > maxConjunctiveAlternatives) {
|
|
2227
|
+
warnExpansionLimit();
|
|
2228
|
+
predicateAlternatives = [emptyConstraintState()];
|
|
2229
|
+
break;
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
else if (attr === '$not') {
|
|
2234
|
+
predicateAlternatives = getNegatedAlternatives(predicate);
|
|
2235
|
+
}
|
|
2236
|
+
else if (attr.startsWith('$') && !attr.startsWith(EXPRESSION_PREFIX) && attr !== '$text') {
|
|
2237
|
+
console.warn(`getFilterConstant: unsupported logical operator ${attr}; treating it as unknown`);
|
|
2238
|
+
}
|
|
2239
|
+
else if (!attr.startsWith('$') && isSimplePredicate(predicate)) {
|
|
2240
|
+
if (isEmptySimplePredicate(predicate)) {
|
|
2241
|
+
return [];
|
|
2242
|
+
}
|
|
2243
|
+
const state = emptyConstraintState();
|
|
2244
|
+
addConstraint(state, false, attr, predicate);
|
|
2245
|
+
predicateAlternatives = [state];
|
|
2246
|
+
}
|
|
2247
|
+
if (predicateAlternatives) {
|
|
2248
|
+
alternatives = intersectAlternatives(alternatives, predicateAlternatives);
|
|
2249
|
+
if (alternatives.length === 0) {
|
|
2250
|
+
return [];
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
return alternatives;
|
|
2255
|
+
};
|
|
2256
|
+
const hasContradictoryConjuncts = (value) => getConjunctiveAlternatives(value).length === 0;
|
|
2257
|
+
const hasCompletePresencePartition = (branches) => {
|
|
2258
|
+
const coverageByAttribute = new Map();
|
|
2259
|
+
for (const branch of branches) {
|
|
2260
|
+
if (!branch || typeof branch !== 'object' || Array.isArray(branch)) {
|
|
2261
|
+
continue;
|
|
2262
|
+
}
|
|
2263
|
+
const entries = Object.entries(branch);
|
|
2264
|
+
if (entries.length !== 1) {
|
|
2265
|
+
continue;
|
|
2266
|
+
}
|
|
2267
|
+
const [attr, predicate] = entries[0];
|
|
2268
|
+
if (attr.startsWith('$') || attr.startsWith('#')) {
|
|
2269
|
+
continue;
|
|
2270
|
+
}
|
|
2271
|
+
const coverage = coverageByAttribute.get(attr) || {
|
|
2272
|
+
includesMissing: false,
|
|
2273
|
+
includesAllPresent: false,
|
|
2274
|
+
includedBooleanValues: new Set(),
|
|
2275
|
+
};
|
|
2276
|
+
if (predicate && typeof predicate === 'object' && !Array.isArray(predicate)) {
|
|
2277
|
+
const predicateEntries = Object.entries(predicate);
|
|
2278
|
+
if (predicateEntries.length !== 1) {
|
|
2279
|
+
continue;
|
|
2280
|
+
}
|
|
2281
|
+
const [operator, operand] = predicateEntries[0];
|
|
2282
|
+
if (operator === '$exists' && typeof operand === 'boolean') {
|
|
2283
|
+
if (operand) {
|
|
2284
|
+
coverage.includesAllPresent = true;
|
|
2285
|
+
}
|
|
2286
|
+
else {
|
|
2287
|
+
coverage.includesMissing = true;
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
else if (operator === '$nin' && Array.isArray(operand) && operand.length === 0) {
|
|
2291
|
+
coverage.includesAllPresent = true;
|
|
2292
|
+
}
|
|
2293
|
+
else if (schema[entity].attributes[attr]?.type === 'boolean') {
|
|
2294
|
+
const finiteConstraint = toFiniteConstraint(predicate);
|
|
2295
|
+
if (finiteConstraint) {
|
|
2296
|
+
for (const value of [false, true]) {
|
|
2297
|
+
if (finiteConstraintContainsValue(finiteConstraint, value)) {
|
|
2298
|
+
coverage.includedBooleanValues.add(value);
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
else if (schema[entity].attributes[attr]?.type === 'boolean') {
|
|
2305
|
+
const finiteConstraint = toFiniteConstraint(predicate);
|
|
2306
|
+
if (finiteConstraint) {
|
|
2307
|
+
for (const value of [false, true]) {
|
|
2308
|
+
if (finiteConstraintContainsValue(finiteConstraint, value)) {
|
|
2309
|
+
coverage.includedBooleanValues.add(value);
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
}
|
|
2314
|
+
coverageByAttribute.set(attr, coverage);
|
|
2315
|
+
}
|
|
2316
|
+
// `{x: {$exists:false}} ∪ {x: P(x)}` 覆盖全集,当 P 覆盖 x 的全部存在值域时成立。
|
|
2317
|
+
return [...coverageByAttribute.values()].some(coverage => coverage.includesMissing && (coverage.includesAllPresent
|
|
2318
|
+
|| coverage.includedBooleanValues.has(false) && coverage.includedBooleanValues.has(true)));
|
|
2319
|
+
};
|
|
2320
|
+
const evaluate = (value) => {
|
|
2321
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
2322
|
+
return;
|
|
2323
|
+
}
|
|
2324
|
+
let result = true;
|
|
2325
|
+
for (const [attr, operand] of Object.entries(value)) {
|
|
2326
|
+
let current;
|
|
2327
|
+
if (attr.startsWith('#')) {
|
|
2328
|
+
current = true;
|
|
2329
|
+
}
|
|
2330
|
+
else if (attr === '$and') {
|
|
2331
|
+
if (!Array.isArray(operand) || operand.some(branch => !isFilterObject(branch))) {
|
|
2332
|
+
console.warn('getFilterConstant: malformed $and filter; treating it as unknown');
|
|
2333
|
+
return;
|
|
2334
|
+
}
|
|
2335
|
+
current = true;
|
|
2336
|
+
for (const branch of operand) {
|
|
2337
|
+
const branchResult = evaluate(branch);
|
|
2338
|
+
if (branchResult === false) {
|
|
2339
|
+
current = false;
|
|
2340
|
+
break;
|
|
2341
|
+
}
|
|
2342
|
+
if (branchResult === undefined) {
|
|
2343
|
+
current = undefined;
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
else if (attr === '$or') {
|
|
2348
|
+
if (!Array.isArray(operand) || operand.some(branch => !isFilterObject(branch))) {
|
|
2349
|
+
console.warn('getFilterConstant: malformed $or filter; treating it as unknown');
|
|
2350
|
+
return;
|
|
2351
|
+
}
|
|
2352
|
+
if (hasCompletePresencePartition(operand)) {
|
|
2353
|
+
current = true;
|
|
2354
|
+
}
|
|
2355
|
+
else {
|
|
2356
|
+
current = false;
|
|
2357
|
+
for (const branch of operand) {
|
|
2358
|
+
const branchResult = evaluate(branch);
|
|
2359
|
+
if (branchResult === true) {
|
|
2360
|
+
current = true;
|
|
2361
|
+
break;
|
|
2362
|
+
}
|
|
2363
|
+
if (branchResult === undefined) {
|
|
2364
|
+
current = undefined;
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
else if (attr === '$not') {
|
|
2370
|
+
if (!isFilterObject(operand)) {
|
|
2371
|
+
console.warn('getFilterConstant: malformed $not filter; treating it as unknown');
|
|
2372
|
+
return;
|
|
2373
|
+
}
|
|
2374
|
+
const branchResult = evaluate(operand);
|
|
2375
|
+
current = branchResult === undefined ? undefined : !branchResult;
|
|
2376
|
+
}
|
|
2377
|
+
else if (attr === '$xor') {
|
|
2378
|
+
const expanded = expandXorFilter(operand, 'getFilterConstant');
|
|
2379
|
+
current = expanded ? evaluate(expanded) : undefined;
|
|
2380
|
+
}
|
|
2381
|
+
else if (attr.startsWith('$') && !attr.startsWith(EXPRESSION_PREFIX) && attr !== '$text') {
|
|
2382
|
+
console.warn(`getFilterConstant: unsupported logical operator ${attr}; treating it as unknown`);
|
|
2383
|
+
current = undefined;
|
|
2384
|
+
}
|
|
2385
|
+
else {
|
|
2386
|
+
current = undefined;
|
|
2387
|
+
}
|
|
2388
|
+
if (current === false) {
|
|
2389
|
+
return false;
|
|
2390
|
+
}
|
|
2391
|
+
if (current === undefined) {
|
|
2392
|
+
result = undefined;
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
// 已知的标量矛盾足以证明 AND 为空,即使同一 filter 还包含未知属性约束。
|
|
2396
|
+
if (hasContradictoryConjuncts(value)) {
|
|
2397
|
+
return false;
|
|
2398
|
+
}
|
|
2399
|
+
return result;
|
|
2400
|
+
};
|
|
2401
|
+
return evaluate(filter);
|
|
2402
|
+
}
|
|
2403
|
+
function judgeFilterRelation(entity, schema, filter, compared, contained) {
|
|
2404
|
+
const filterConstant = getFilterConstant(filter, schema, entity);
|
|
2405
|
+
const comparedConstant = getFilterConstant(compared, schema, entity);
|
|
2406
|
+
if (contained) {
|
|
2407
|
+
if (filterConstant === false || comparedConstant === true) {
|
|
2408
|
+
return true;
|
|
2409
|
+
}
|
|
2410
|
+
if (comparedConstant === false && filterConstant === true) {
|
|
2411
|
+
return false;
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
else {
|
|
2415
|
+
if (filterConstant === false || comparedConstant === false) {
|
|
2416
|
+
return true;
|
|
2417
|
+
}
|
|
2418
|
+
if (filterConstant === true && comparedConstant === true) {
|
|
2419
|
+
return false;
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
const result = analyzeFilterRelation(entity, schema, filter, compared, contained);
|
|
2423
|
+
if (typeof result === 'boolean') {
|
|
2424
|
+
return result;
|
|
2425
|
+
}
|
|
2426
|
+
const { sureAttributes, uncertainAttributes, totalAndDeducedFilters, totalOrDeducedFilters, } = result;
|
|
2427
|
+
if (contained) {
|
|
2428
|
+
if (sureAttributes.length === Object.keys(compared).length) {
|
|
2429
|
+
return true;
|
|
2430
|
+
}
|
|
2431
|
+
if (uncertainAttributes.length > 0) {
|
|
2432
|
+
// 有属性无法界定,此时只能拿本行去查询
|
|
2433
|
+
totalAndDeducedFilters.push({
|
|
2434
|
+
entity,
|
|
2435
|
+
filter: combineFilters(entity, schema, [filter, {
|
|
2436
|
+
$not: pick(compared, uncertainAttributes),
|
|
2437
|
+
}]),
|
|
2438
|
+
});
|
|
2439
|
+
}
|
|
2440
|
+
return {
|
|
2441
|
+
$and: totalAndDeducedFilters,
|
|
2442
|
+
};
|
|
2443
|
+
}
|
|
2444
|
+
else {
|
|
2445
|
+
if (sureAttributes.length === Object.keys(compared).length) {
|
|
2446
|
+
return false;
|
|
2447
|
+
}
|
|
2448
|
+
// uncertainAttributes中是无法判定的属性,和filter合并之后(同时满足)的查询结果如果不为空说明不互斥
|
|
2449
|
+
if (uncertainAttributes.length > 0) {
|
|
2450
|
+
totalOrDeducedFilters.push({
|
|
2451
|
+
entity,
|
|
2452
|
+
filter: combineFilters(entity, schema, [filter, pick(compared, uncertainAttributes)]),
|
|
2453
|
+
});
|
|
2454
|
+
}
|
|
2455
|
+
return {
|
|
2456
|
+
$or: totalOrDeducedFilters,
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
/**
|
|
2461
|
+
*
|
|
2462
|
+
* 判断filter是否包含contained中的查询条件,即filter查询的结果一定是contained查询结果的子集
|
|
2463
|
+
* filter = {
|
|
2464
|
+
* a: 1
|
|
2465
|
+
* b: 2,
|
|
2466
|
+
* c: 3,
|
|
2467
|
+
* },
|
|
2468
|
+
* conditionalFilter = {
|
|
2469
|
+
* a: 1
|
|
2470
|
+
* }
|
|
2471
|
+
* 则包含
|
|
2472
|
+
* @param entity
|
|
2473
|
+
* @param schema
|
|
2474
|
+
* @param filter
|
|
2475
|
+
* @param contained
|
|
2476
|
+
* @returns
|
|
2477
|
+
*/
|
|
2478
|
+
/** 判断 `S(filter) ⊆ S(contained)`;返回推导树表示需数据比较才能决定。 */
|
|
2479
|
+
export function contains(entity, schema, filter, contained) {
|
|
2480
|
+
assert(filter);
|
|
2481
|
+
assert(contained);
|
|
2482
|
+
if (!hasValidFilterShape(entity, schema, filter, 'contains')
|
|
2483
|
+
|| !hasValidFilterShape(entity, schema, contained, 'contains')) {
|
|
2484
|
+
// 非法结构没有可靠的集合语义;返回 false 表示“无法证明包含”,不能把它当作授权通过。
|
|
2485
|
+
return false;
|
|
2486
|
+
}
|
|
2487
|
+
return judgeFilterRelation(entity, schema, filter, contained, true);
|
|
2488
|
+
}
|
|
2489
|
+
/**
|
|
2490
|
+
* 判断filter1和filter2是否相斥,即filter1和filter2查询的结果一定没有交集
|
|
2491
|
+
* filter1 = {
|
|
2492
|
+
* a: 2
|
|
2493
|
+
* },
|
|
2494
|
+
* filter2 = {
|
|
2495
|
+
* a: 1
|
|
2496
|
+
* }
|
|
2497
|
+
* 则包含
|
|
2498
|
+
* @param entity
|
|
2499
|
+
* @param schema
|
|
2500
|
+
* @param filter
|
|
2501
|
+
* @param conditionalFilter
|
|
2502
|
+
*/
|
|
2503
|
+
/** 判断两个查询集合交集是否为空;反向 contains 用于补足单向分析无法识别的包含关系。 */
|
|
2504
|
+
function repel(entity, schema, filter1, filter2) {
|
|
2505
|
+
assert(filter1);
|
|
2506
|
+
assert(filter2);
|
|
2507
|
+
if (!hasValidFilterShape(entity, schema, filter1, 'repel')
|
|
2508
|
+
|| !hasValidFilterShape(entity, schema, filter2, 'repel')) {
|
|
2509
|
+
// 非法结构没有可靠的交集语义;返回 false,表示不能证明相斥。
|
|
2510
|
+
return false;
|
|
2511
|
+
}
|
|
2512
|
+
/**
|
|
2513
|
+
* 现在的算法检查相斥有一种情况,若filter1被filter2相容,可以判断出来是false,但如果filter2被filter1相容则检查不出来
|
|
2514
|
+
* filter1 = {
|
|
2515
|
+
* id: 'user',
|
|
2516
|
+
* }
|
|
2517
|
+
* filter2 = {
|
|
2518
|
+
id: 'user',
|
|
2519
|
+
$$seq$$: 'xc',
|
|
2520
|
+
* }
|
|
2521
|
+
* 这种情况就检查不出来,所以再反向判断一次是否相容
|
|
2522
|
+
*/
|
|
2523
|
+
const repelled = judgeFilterRelation(entity, schema, filter1, filter2, false);
|
|
2524
|
+
if (typeof repelled === 'boolean') {
|
|
2525
|
+
return repelled;
|
|
2526
|
+
}
|
|
2527
|
+
if (contains(entity, schema, filter2, filter1) === true) {
|
|
2528
|
+
return false;
|
|
2529
|
+
}
|
|
2530
|
+
return repelled;
|
|
2531
|
+
}
|
|
2532
|
+
/**
|
|
2533
|
+
* 从filter中判断是否有确定的id对象,如果有则返回这些id,没有返回空数组
|
|
2534
|
+
* @param filter
|
|
2535
|
+
* @returns
|
|
2536
|
+
*/
|
|
2537
|
+
/** 仅对可由 id/$and/$or 精确推出的过滤器返回候选 ID;undefined 表示无法安全缩窄。 */
|
|
2538
|
+
function getRelevantIdsInner(filter) {
|
|
2539
|
+
if (!filter) {
|
|
2540
|
+
return;
|
|
2541
|
+
}
|
|
2542
|
+
const attrs = Object.keys(filter);
|
|
2543
|
+
if (difference(attrs, ['id', '$and', '$or', '$xor']).length > 0) {
|
|
2544
|
+
return;
|
|
2545
|
+
}
|
|
2546
|
+
let result;
|
|
2547
|
+
if (Object.prototype.hasOwnProperty.call(filter, 'id')) {
|
|
2548
|
+
if (typeof filter.id === 'string') {
|
|
2549
|
+
result = [filter.id];
|
|
2550
|
+
}
|
|
2551
|
+
else if (typeof filter.id?.$eq === 'string') {
|
|
2552
|
+
result = [filter.id.$eq];
|
|
2553
|
+
}
|
|
2554
|
+
else if (filter.id?.$in instanceof Array && filter.id.$in.every((id) => typeof id === 'string')) {
|
|
2555
|
+
result = uniq(filter.id.$in);
|
|
2556
|
+
}
|
|
2557
|
+
else {
|
|
2558
|
+
return;
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
if (Object.prototype.hasOwnProperty.call(filter, '$and')) {
|
|
2562
|
+
if (!Array.isArray(filter.$and) || filter.$and.some((child) => !child || typeof child !== 'object' || Array.isArray(child))) {
|
|
2563
|
+
console.warn('getRelevantIds: malformed $and filter; treating candidate IDs as unknown');
|
|
2564
|
+
return;
|
|
2565
|
+
}
|
|
2566
|
+
if (filter.$and.length === 0) {
|
|
2567
|
+
return result;
|
|
2568
|
+
}
|
|
2569
|
+
const andResults = filter.$and.map((child) => getRelevantIdsInner(child));
|
|
2570
|
+
const andIds = andResults.filter((ids) => ids !== undefined);
|
|
2571
|
+
if (andIds.some(ids => ids.length === 0)) {
|
|
2572
|
+
return [];
|
|
2573
|
+
}
|
|
2574
|
+
if (andIds.length !== andResults.length) {
|
|
2575
|
+
return;
|
|
2576
|
+
}
|
|
2577
|
+
const intersectionIds = intersection(...andIds);
|
|
2578
|
+
result = result === undefined ? intersectionIds : intersection(result, intersectionIds);
|
|
2579
|
+
}
|
|
2580
|
+
if (Object.prototype.hasOwnProperty.call(filter, '$or')) {
|
|
2581
|
+
if (!Array.isArray(filter.$or) || filter.$or.some((child) => !child || typeof child !== 'object' || Array.isArray(child))) {
|
|
2582
|
+
console.warn('getRelevantIds: malformed $or filter; treating candidate IDs as unknown');
|
|
2583
|
+
return;
|
|
2584
|
+
}
|
|
2585
|
+
const orResults = filter.$or.map((child) => getRelevantIdsInner(child));
|
|
2586
|
+
if (orResults.some(ids => ids === undefined)) {
|
|
2587
|
+
return;
|
|
2588
|
+
}
|
|
2589
|
+
const orIds = union(...orResults);
|
|
2590
|
+
result = result === undefined ? orIds : intersection(result, orIds);
|
|
2591
|
+
}
|
|
2592
|
+
if (Object.prototype.hasOwnProperty.call(filter, '$xor')) {
|
|
2593
|
+
if (!Array.isArray(filter.$xor) || filter.$xor.length === 0) {
|
|
2594
|
+
console.warn('getRelevantIds: malformed or empty $xor filter; treating candidate IDs as unknown');
|
|
2595
|
+
return;
|
|
2596
|
+
}
|
|
2597
|
+
if (filter.$xor.some((child) => !child || typeof child !== 'object' || Array.isArray(child))) {
|
|
2598
|
+
console.warn('getRelevantIds: malformed $xor branch; treating candidate IDs as unknown');
|
|
2599
|
+
return;
|
|
2600
|
+
}
|
|
2601
|
+
const xorResults = filter.$xor.map((child) => getRelevantIdsInner(child));
|
|
2602
|
+
if (xorResults.some(ids => ids === undefined)) {
|
|
2603
|
+
return;
|
|
2604
|
+
}
|
|
2605
|
+
// XOR candidates are IDs present in an odd number of exact branch sets: I = {id | Σ 1[id ∈ Bᵢ] mod 2 = 1}.
|
|
2606
|
+
const parityById = new Map();
|
|
2607
|
+
for (const ids of xorResults) {
|
|
2608
|
+
for (const id of ids) {
|
|
2609
|
+
parityById.set(id, !parityById.get(id));
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
const xorIds = [...parityById].filter(([, odd]) => odd).map(([id]) => id);
|
|
2613
|
+
result = result === undefined ? xorIds : intersection(result, xorIds);
|
|
2614
|
+
}
|
|
2615
|
+
return result;
|
|
2616
|
+
}
|
|
2617
|
+
export function getRelevantIds(filter) {
|
|
2618
|
+
return getRelevantIdsInner(filter) || [];
|
|
2619
|
+
}
|
|
2620
|
+
/**
|
|
2621
|
+
* 判断两个过滤条件是否完全一致
|
|
2622
|
+
* @param entity
|
|
2623
|
+
* @param schema
|
|
2624
|
+
* @param filter1
|
|
2625
|
+
* @param filter2
|
|
2626
|
+
*/
|
|
2627
|
+
/** 判断两个纯 id filter 是否相同;带其他约束时保守返回 false,不冒充完整结构相等。 */
|
|
2628
|
+
export function same(entity, schema, filter1, filter2) {
|
|
2629
|
+
// 当前只需要判断是不是id相等就行了,在runningTree的operation合并的时间使用
|
|
2630
|
+
if (!filter1 || !Object.prototype.hasOwnProperty.call(filter1, 'id') || Object.keys(filter1).length > 1
|
|
2631
|
+
|| !filter2 || !Object.prototype.hasOwnProperty.call(filter2, 'id') || Object.keys(filter2).length > 1) {
|
|
2632
|
+
return false;
|
|
2633
|
+
}
|
|
2634
|
+
return filter1.id === filter2.id;
|
|
2635
|
+
}
|
|
2636
|
+
/**
|
|
2637
|
+
* 寻找在树形结构中满足条件的数据行的上层数据
|
|
2638
|
+
* 例如在area表中,如果“杭州市”满足这一条件,则希望查到更高层的“浙江省”和“中国”,即可构造出满足条件的filter
|
|
2639
|
+
* @param entity
|
|
2640
|
+
* @param parentKey parentId属性名
|
|
2641
|
+
* @param filter 查询当前行的条件
|
|
2642
|
+
* @param level
|
|
2643
|
+
*/
|
|
2644
|
+
/**
|
|
2645
|
+
* 通过重复反查 parentKey 生成祖先条件:`A_0=filter`,`A_{k+1}={id:{$in:{filter:A_k}}}`。
|
|
2646
|
+
* `includeAll` 把第 0..level 层以 OR 合并,`includeSelf` 决定是否将 A_0 放入该并集。
|
|
2647
|
+
*/
|
|
2648
|
+
export function makeTreeAncestorFilter(entity, parentKey, filter, level = 1, includeAll, includeSelf) {
|
|
2649
|
+
if (!Number.isSafeInteger(level) || level < 0) {
|
|
2650
|
+
console.warn(`makeTreeAncestorFilter: level must be a non-negative safe integer; received ${level}`);
|
|
2651
|
+
}
|
|
2652
|
+
assert(Number.isSafeInteger(level) && level >= 0);
|
|
2653
|
+
let idInFilters = [];
|
|
2654
|
+
if (includeSelf) {
|
|
2655
|
+
idInFilters.push(filter);
|
|
2656
|
+
}
|
|
2657
|
+
let currentLevelInFilter = filter;
|
|
2658
|
+
while (level > 0) {
|
|
2659
|
+
currentLevelInFilter = {
|
|
2660
|
+
id: {
|
|
2661
|
+
$in: {
|
|
2662
|
+
entity,
|
|
2663
|
+
data: {
|
|
2664
|
+
[parentKey]: 1,
|
|
2665
|
+
},
|
|
2666
|
+
filter: currentLevelInFilter,
|
|
2667
|
+
}
|
|
2668
|
+
},
|
|
2669
|
+
};
|
|
2670
|
+
if (includeAll) {
|
|
2671
|
+
idInFilters.push(currentLevelInFilter);
|
|
2672
|
+
}
|
|
2673
|
+
level--;
|
|
2674
|
+
}
|
|
2675
|
+
;
|
|
2676
|
+
if (includeAll) {
|
|
2677
|
+
return {
|
|
2678
|
+
$or: idInFilters,
|
|
2679
|
+
};
|
|
2680
|
+
}
|
|
2681
|
+
return currentLevelInFilter;
|
|
2682
|
+
}
|
|
2683
|
+
/**
|
|
2684
|
+
* 寻找在树形结构中满足条件的数据行的下层数据
|
|
2685
|
+
* 例如在area表中,如果“杭州市”满足这一条件,则希望查到更低层的“西湖区”,即可构造出满足条件的filter
|
|
2686
|
+
* @param entity
|
|
2687
|
+
* @param parentKey parentId属性名
|
|
2688
|
+
* @param filter 查询当前行的条件
|
|
2689
|
+
* @param level
|
|
2690
|
+
*/
|
|
2691
|
+
/**
|
|
2692
|
+
* 沿 parentKey 对应关系递归构造后代:`D_0=filter`,`D_{k+1}={parentRelation:D_k}`。
|
|
2693
|
+
* `includeAll` 以 OR 合并第 0..level 层,`includeSelf` 决定是否将 D_0 纳入并集。
|
|
2694
|
+
*/
|
|
2695
|
+
export function makeTreeDescendantFilter(entity, parentKey, filter, level = 1, includeAll, includeSelf) {
|
|
2696
|
+
if (!Number.isSafeInteger(level) || level < 0) {
|
|
2697
|
+
console.warn(`makeTreeDescendantFilter: level must be a non-negative safe integer; received ${level}`);
|
|
2698
|
+
}
|
|
2699
|
+
assert(Number.isSafeInteger(level) && level >= 0);
|
|
2700
|
+
if (!parentKey.endsWith('Id')) {
|
|
2701
|
+
console.warn(`makeTreeDescendantFilter: parentKey must end with "Id"; received ${parentKey}`);
|
|
2702
|
+
}
|
|
2703
|
+
assert(parentKey.endsWith('Id'));
|
|
2704
|
+
const parentKeyRef = parentKey.slice(0, parentKey.length - 2);
|
|
2705
|
+
let idInFilters = [];
|
|
2706
|
+
if (includeSelf) {
|
|
2707
|
+
idInFilters.push(filter);
|
|
2708
|
+
}
|
|
2709
|
+
let currentLevelInFilter = filter;
|
|
2710
|
+
while (level > 0) {
|
|
2711
|
+
currentLevelInFilter = {
|
|
2712
|
+
[parentKeyRef]: currentLevelInFilter,
|
|
2713
|
+
};
|
|
2714
|
+
if (includeAll) {
|
|
2715
|
+
idInFilters.push(currentLevelInFilter);
|
|
2716
|
+
}
|
|
2717
|
+
level--;
|
|
2718
|
+
}
|
|
2719
|
+
;
|
|
2720
|
+
if (includeAll) {
|
|
2721
|
+
return {
|
|
2722
|
+
$or: idInFilters,
|
|
2723
|
+
};
|
|
2724
|
+
}
|
|
2725
|
+
return currentLevelInFilter;
|
|
2726
|
+
}
|
|
2727
|
+
/** 执行静态分析生成的 AND/OR 推导树:叶子用 count 判空,支持同步与 Promise 结果。 */
|
|
2728
|
+
function checkDeduceFilters(dfc, context) {
|
|
2729
|
+
const { $and, $or } = dfc;
|
|
2730
|
+
assert(Boolean($and) !== Boolean($or));
|
|
2731
|
+
const conditions = $and ?? $or;
|
|
2732
|
+
const isAnd = Boolean($and);
|
|
2733
|
+
const isDecisive = (result) => isAnd
|
|
2734
|
+
? result === false || typeof result === 'number' && result > 0
|
|
2735
|
+
: result === true || result === 0;
|
|
2736
|
+
const evaluate = (index) => {
|
|
2737
|
+
for (let current = index; current < conditions.length; current++) {
|
|
2738
|
+
const condition = conditions[current];
|
|
2739
|
+
const result = Object.prototype.hasOwnProperty.call(condition, 'entity')
|
|
2740
|
+
? (() => {
|
|
2741
|
+
const leaf = condition;
|
|
2742
|
+
return context.count(leaf.entity, {
|
|
2743
|
+
filter: leaf.filter,
|
|
2744
|
+
count: 1,
|
|
2745
|
+
}, { ignoreAttrMiss: true });
|
|
2746
|
+
})()
|
|
2747
|
+
: checkDeduceFilters(condition, context);
|
|
2748
|
+
if (result instanceof Promise) {
|
|
2749
|
+
return result.then(value => isDecisive(value)
|
|
2750
|
+
? !isAnd
|
|
2751
|
+
: evaluate(current + 1));
|
|
2752
|
+
}
|
|
2753
|
+
if (isDecisive(result)) {
|
|
2754
|
+
return !isAnd;
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
return isAnd;
|
|
2758
|
+
};
|
|
2759
|
+
return evaluate(0);
|
|
2760
|
+
}
|
|
2761
|
+
/**
|
|
2762
|
+
* 检查filter是否包含contained(filter查询的数据是contained查询数据的子集)
|
|
2763
|
+
* @param entity
|
|
2764
|
+
* @param context
|
|
2765
|
+
* @param contained
|
|
2766
|
+
* @param filter
|
|
2767
|
+
* @param dataCompare
|
|
2768
|
+
* @returns
|
|
2769
|
+
*/
|
|
2770
|
+
export function checkFilterContains(entity, context, contained, filter, dataCompare, warningOnDataCompare) {
|
|
2771
|
+
assert(filter, `对${entity}的访问必须有filter`);
|
|
2772
|
+
const schema = context.getSchema();
|
|
2773
|
+
const result = contains(entity, schema, filter, contained);
|
|
2774
|
+
if (typeof result === 'boolean') {
|
|
2775
|
+
return result;
|
|
2776
|
+
}
|
|
2777
|
+
if (dataCompare) {
|
|
2778
|
+
if (warningOnDataCompare) {
|
|
2779
|
+
console.warn(`data compare on ${entity}, please optimize`);
|
|
2780
|
+
}
|
|
2781
|
+
return checkDeduceFilters(result, context);
|
|
2782
|
+
}
|
|
2783
|
+
return false;
|
|
2784
|
+
}
|
|
2785
|
+
export function checkFilterRepel(entity, context, filter1, filter2, dataCompare, warningOnDataCompare) {
|
|
2786
|
+
assert(filter1);
|
|
2787
|
+
assert(filter2);
|
|
2788
|
+
const schema = context.getSchema();
|
|
2789
|
+
const result = repel(entity, schema, filter2, filter1);
|
|
2790
|
+
if (typeof result === 'boolean') {
|
|
2791
|
+
return result;
|
|
2792
|
+
}
|
|
2793
|
+
if (dataCompare) {
|
|
2794
|
+
if (warningOnDataCompare) {
|
|
2795
|
+
console.warn(`data compare on ${entity}, please optimize`);
|
|
2796
|
+
}
|
|
2797
|
+
return checkDeduceFilters(result, context);
|
|
2798
|
+
}
|
|
2799
|
+
return false;
|
|
2800
|
+
}
|
|
2801
|
+
/**
|
|
2802
|
+
* 有的场景下将filter当成非结构化属性存储,又想支持对其查询,此时必须将查询的filter进行转换,处理其中$开头的escape
|
|
2803
|
+
* 只要filter是查询数据的标准子集,查询应当能返回true
|
|
2804
|
+
* @param filter
|
|
2805
|
+
*/
|
|
2806
|
+
/** 递归遍历对象与数组,仅将键名前缀 `$` 改写为 `.$`,值与数组结构保持不变。 */
|
|
2807
|
+
export function translateFilterToObjectPredicate(filter) {
|
|
2808
|
+
const translatedValues = new WeakMap();
|
|
2809
|
+
const ancestors = new WeakSet();
|
|
2810
|
+
const translateValue = (value) => {
|
|
2811
|
+
if (!value || typeof value !== 'object' || value instanceof Date) {
|
|
2812
|
+
return value;
|
|
2813
|
+
}
|
|
2814
|
+
const existing = translatedValues.get(value);
|
|
2815
|
+
if (existing !== undefined) {
|
|
2816
|
+
if (ancestors.has(value)) {
|
|
2817
|
+
console.warn('translateFilterToObjectPredicate: cyclic filter object; preserving the cycle');
|
|
2818
|
+
}
|
|
2819
|
+
return existing;
|
|
2820
|
+
}
|
|
2821
|
+
const translated = Array.isArray(value) ? [] : {};
|
|
2822
|
+
translatedValues.set(value, translated);
|
|
2823
|
+
ancestors.add(value);
|
|
2824
|
+
if (Array.isArray(value)) {
|
|
2825
|
+
for (const nestedValue of value) {
|
|
2826
|
+
translated.push(translateValue(nestedValue));
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
else {
|
|
2830
|
+
for (const [key, nestedValue] of Object.entries(value)) {
|
|
2831
|
+
const translatedKey = key.startsWith('$') ? `.${key}` : key;
|
|
2832
|
+
translated[translatedKey] = translateValue(nestedValue);
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
ancestors.delete(value);
|
|
2836
|
+
return translated;
|
|
2837
|
+
};
|
|
2838
|
+
return translateValue(filter);
|
|
2839
|
+
}
|
|
2840
|
+
/* export function getCascadeEntityFilter<ED extends EntityDict & BaseEntityDict, T extends keyof ED>(
|
|
2841
|
+
filter: NonNullable<ED[T]['Filter']>,
|
|
2842
|
+
attr: keyof NonNullable<ED[T]['Filter']>
|
|
2843
|
+
): ED[keyof ED]['Filter'] {
|
|
2844
|
+
const filters: ED[keyof ED]['Filter'][] = [];
|
|
2845
|
+
if (filter![attr]) {
|
|
2846
|
+
assert(typeof filter![attr] === 'object');
|
|
2847
|
+
filters.push(filter![attr]);
|
|
2848
|
+
}
|
|
2849
|
+
if (filter.$and) {
|
|
2850
|
+
filter.$and.forEach(
|
|
2851
|
+
(ele: NonNullable<ED[T]['Filter']>) => {
|
|
2852
|
+
const f2 = getCascadeEntityFilter(ele, attr);
|
|
2853
|
+
if (f2) {
|
|
2854
|
+
filters.push(f2)
|
|
2855
|
+
}
|
|
2856
|
+
}
|
|
2857
|
+
);
|
|
2858
|
+
}
|
|
2859
|
+
if (filters.length > 0) {
|
|
2860
|
+
return combineFilters(filters);
|
|
2861
|
+
}
|
|
2862
|
+
} */
|