This repository was archived by the owner on Oct 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathquery_graph_label_analyzer.cpp
More file actions
407 lines (375 loc) · 16.8 KB
/
query_graph_label_analyzer.cpp
File metadata and controls
407 lines (375 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "binder/query/query_graph_label_analyzer.h"
#include "catalog/catalog.h"
#include "catalog/catalog_entry/node_table_catalog_entry.h"
#include "catalog/catalog_entry/rel_table_catalog_entry.h"
#include "common/exception/binder.h"
#include "common/string_format.h"
using namespace kuzu::common;
using namespace kuzu::catalog;
using namespace kuzu::transaction;
namespace kuzu {
namespace binder {
// NOLINTNEXTLINE(readability-non-const-parameter): graph is supposed to be modified.
void QueryGraphLabelAnalyzer::pruneLabel(QueryGraph& graph) const {
for (auto i = 0u; i < graph.getNumQueryNodes(); ++i) {
pruneNode(graph, *graph.getQueryNode(i));
}
for (auto i = 0u; i < graph.getNumQueryRels(); ++i) {
pruneRel(*graph.getQueryRel(i));
}
}
void QueryGraphLabelAnalyzer::pruneNode(const QueryGraph& graph, NodeExpression& node) const {
auto catalog = clientContext.getCatalog();
for (auto i = 0u; i < graph.getNumQueryRels(); ++i) {
auto queryRel = graph.getQueryRel(i);
if (queryRel->isRecursive()) {
continue;
}
table_id_set_t candidates;
std::unordered_set<std::string> candidateNamesSet;
auto isSrcConnect = *queryRel->getSrcNode() == node;
auto isDstConnect = *queryRel->getDstNode() == node;
auto tx = clientContext.getTransaction();
if (queryRel->getDirectionType() == RelDirectionType::BOTH) {
if (isSrcConnect || isDstConnect) {
for (auto entry : queryRel->getEntries()) {
auto& relEntry = entry->constCast<RelTableCatalogEntry>();
auto srcTableID = relEntry.getSrcTableID();
auto dstTableID = relEntry.getDstTableID();
candidates.insert(srcTableID);
candidates.insert(dstTableID);
auto srcEntry = catalog->getTableCatalogEntry(tx, srcTableID);
auto dstEntry = catalog->getTableCatalogEntry(tx, dstTableID);
candidateNamesSet.insert(srcEntry->getName());
candidateNamesSet.insert(dstEntry->getName());
}
}
} else {
if (isSrcConnect) {
for (auto entry : queryRel->getEntries()) {
auto& relEntry = entry->constCast<RelTableCatalogEntry>();
auto srcTableID = relEntry.getSrcTableID();
candidates.insert(srcTableID);
auto srcEntry = catalog->getTableCatalogEntry(tx, srcTableID);
candidateNamesSet.insert(srcEntry->getName());
}
} else if (isDstConnect) {
for (auto entry : queryRel->getEntries()) {
auto& relEntry = entry->constCast<RelTableCatalogEntry>();
auto dstTableID = relEntry.getDstTableID();
candidates.insert(dstTableID);
auto dstEntry = catalog->getTableCatalogEntry(tx, dstTableID);
candidateNamesSet.insert(dstEntry->getName());
}
}
}
if (candidates.empty()) { // No need to prune.
continue;
}
std::vector<TableCatalogEntry*> prunedEntries;
for (auto entry : node.getEntries()) {
if (!candidates.contains(entry->getTableID())) {
continue;
}
prunedEntries.push_back(entry);
}
node.setEntries(prunedEntries);
if (prunedEntries.empty()) {
if (throwOnViolate) {
auto candidateNames =
std::vector<std::string>{candidateNamesSet.begin(), candidateNamesSet.end()};
auto candidateStr = candidateNames[0];
for (auto j = 1u; j < candidateNames.size(); ++j) {
candidateStr += ", " + candidateNames[j];
}
throw BinderException(
stringFormat("Query node {} violates schema. Expected labels are {}.",
node.toString(), candidateStr));
}
}
}
}
std::vector<TableCatalogEntry*> QueryGraphLabelAnalyzer::pruneNonRecursiveRel(
const std::vector<TableCatalogEntry*>& relEntries, const table_id_set_t& srcTableIDSet,
const table_id_set_t& dstTableIDSet, const RelDirectionType directionType) const {
auto forwardPruningFunc = [&](table_id_t srcTableID, table_id_t dstTableID) {
return srcTableIDSet.contains(srcTableID) && dstTableIDSet.contains(dstTableID);
};
auto backwardPruningFunc = [&](table_id_t srcTableID, table_id_t dstTableID) {
return dstTableIDSet.contains(srcTableID) && srcTableIDSet.contains(dstTableID);
};
std::vector<TableCatalogEntry*> prunedEntries;
for (auto& entry : relEntries) {
auto& relEntry = entry->constCast<RelTableCatalogEntry>();
auto srcTableID = relEntry.getSrcTableID();
auto dstTableID = relEntry.getDstTableID();
auto satisfyForwardPruning = forwardPruningFunc(srcTableID, dstTableID);
if (directionType == RelDirectionType::BOTH) {
if (satisfyForwardPruning || backwardPruningFunc(srcTableID, dstTableID)) {
prunedEntries.push_back(entry);
}
} else {
if (satisfyForwardPruning) {
prunedEntries.push_back(entry);
}
}
}
return prunedEntries;
}
table_id_set_t QueryGraphLabelAnalyzer::collectRelNodes(const RelDataDirection direction,
std::vector<TableCatalogEntry*> relEntries) const {
table_id_set_t nodeIDs;
for (const auto& entry : relEntries) {
const auto& relEntry = entry->constCast<RelTableCatalogEntry>();
if (direction == RelDataDirection::FWD) {
nodeIDs.insert(relEntry.getDstTableID());
} else if (direction == RelDataDirection::BWD) {
nodeIDs.insert(relEntry.getSrcTableID());
} else {
KU_UNREACHABLE;
}
}
return nodeIDs;
}
std::pair<std::vector<table_id_set_t>, std::vector<table_id_set_t>>
QueryGraphLabelAnalyzer::pruneRecursiveRel(const std::vector<TableCatalogEntry*>& relEntries,
const table_id_set_t srcTableIDSet, const table_id_set_t dstTableIDSet, size_t lowerBound,
size_t upperBound, RelDirectionType relDirectionType) const {
// src-->[dst,[rels]]
std::unordered_map<table_id_t, std::unordered_map<table_id_t, table_id_vector_t>>
stepFromLeftGraph;
std::unordered_map<table_id_t, std::unordered_map<table_id_t, table_id_vector_t>>
stepFromRightGraph;
table_id_t maxTableID = 0;
for (auto entry : relEntries) {
auto& relEntry = entry->constCast<RelTableCatalogEntry>();
auto srcTableID = relEntry.getSrcTableID();
auto dstTableID = relEntry.getDstTableID();
auto tableID = relEntry.getTableID();
stepFromLeftGraph[srcTableID][dstTableID].push_back(tableID);
stepFromRightGraph[dstTableID][srcTableID].push_back(tableID);
if (relDirectionType == RelDirectionType::BOTH) {
stepFromLeftGraph[dstTableID][srcTableID].push_back(tableID);
stepFromRightGraph[srcTableID][dstTableID].push_back(tableID);
}
maxTableID = std::max(maxTableID, tableID);
}
auto stepFromLeft = pruneRecursiveRel(stepFromLeftGraph, stepFromRightGraph, srcTableIDSet,
dstTableIDSet, lowerBound, upperBound, maxTableID);
auto stepFromRight = pruneRecursiveRel(stepFromRightGraph, stepFromLeftGraph, dstTableIDSet,
srcTableIDSet, lowerBound, upperBound, maxTableID);
return {stepFromLeft, stepFromRight};
}
std::vector<table_id_set_t> QueryGraphLabelAnalyzer::pruneRecursiveRel(
const std::unordered_map<table_id_t, std::unordered_map<table_id_t, table_id_vector_t>>& graph,
const std::unordered_map<table_id_t, std::unordered_map<table_id_t, table_id_vector_t>>&
reseveGraph,
const table_id_set_t& startTableIDSet, const table_id_set_t& endTableIDSet, size_t lowerBound,
size_t upperBound, table_id_t maxTableID) const {
// f[i][j] represent whether the edge numbered i can be reached by jumping j times through the
// set A.
std::unordered_map<table_id_t, std::vector<bool>> f, g;
auto initFunc = [upperBound](const std::unordered_map<table_id_t,
std::unordered_map<table_id_t, table_id_vector_t>>& _graph,
std::unordered_map<table_id_t, std::vector<bool>>& ans,
const table_id_set_t& beginTableIDSet) {
for (auto [_, map] : _graph) {
for (auto [_, rels] : map) {
for (auto rel : rels) {
ans.emplace(rel, std::vector<bool>(upperBound + 1, false));
}
}
}
for (auto tableID : beginTableIDSet) {
if (!_graph.contains(tableID)) {
continue;
}
for (auto [dst, rels] : _graph.at(tableID)) {
for (auto rel : rels) {
ans[rel][1] = true;
ans[rel][0] = true;
}
}
}
};
initFunc(graph, f, startTableIDSet);
initFunc(reseveGraph, g, endTableIDSet);
auto isOk = [&](const table_id_vector_t& rels, int j,
std::unordered_map<table_id_t, std::vector<bool>>& map) -> bool {
for (auto rel : rels) {
if (map[rel][j - 1]) {
return true;
}
}
return false;
};
auto bfsFunc =
[upperBound, maxTableID, isOk](
const std::unordered_map<table_id_t, std::unordered_map<table_id_t, table_id_vector_t>>&
_graph,
const std::unordered_map<table_id_t, std::unordered_map<table_id_t, table_id_vector_t>>&
_reseveGraph,
std::unordered_map<table_id_t, std::vector<bool>>& map) {
for (int j = 2; j <= upperBound; ++j) {
for (auto v = 0u; v < maxTableID; ++v) {
bool flag = false;
if (_reseveGraph.contains(v)) {
for (auto [_, rels] : _reseveGraph.at(v)) {
if (isOk(rels, j, map)) {
flag = true;
break;
}
}
}
if (flag && _graph.contains(v)) {
for (auto [dst, rels] : _graph.at(v)) {
for (auto rel : rels) {
map[rel][j] = true;
}
}
}
}
}
};
bfsFunc(graph, reseveGraph, f);
bfsFunc(reseveGraph, graph, g);
std::vector<table_id_set_t> stepActiveTableIDs(upperBound);
for (auto [rel, vector] : f) {
for (int j = 0; j <= upperBound; ++j) {
if (!vector[j]) {
continue;
}
for (int k = 0; k <= upperBound; ++k) {
if (!g[rel][k]) {
continue;
}
auto step = j + k;
if (step != upperBound) {
// rel repeat count
step--;
}
if (step < lowerBound) {
continue;
} else if (step > upperBound) {
break;
} else {
int index = j == 0 ? 0 : j - 1;
stepActiveTableIDs[index].emplace(rel);
break;
}
}
}
}
return stepActiveTableIDs;
}
std::vector<TableCatalogEntry*> QueryGraphLabelAnalyzer::getTableCatalogEntries(
table_id_set_t tableIDs) const {
std::vector<TableCatalogEntry*> relEntries;
for (const auto& tableID : tableIDs) {
relEntries.push_back(catalog->getTableCatalogEntry(tx, tableID));
}
return relEntries;
}
std::vector<table_id_t> QueryGraphLabelAnalyzer::getNodeTableIDs() const {
std::vector<table_id_t> nodeTableIDs;
for (auto node_table_entry : catalog->getNodeTableEntries(tx)) {
nodeTableIDs.push_back(node_table_entry->getTableID());
}
return nodeTableIDs;
}
std::unordered_set<TableCatalogEntry*> QueryGraphLabelAnalyzer::mergeTableIDs(
const std::vector<table_id_set_t>& v1, const std::vector<table_id_set_t>& v2) const {
std::unordered_set<table_id_t> temp;
for (auto tableIDs : v1) {
temp.insert(tableIDs.begin(), tableIDs.end());
}
for (auto tableIDs : v2) {
temp.insert(tableIDs.begin(), tableIDs.end());
}
std::unordered_set<TableCatalogEntry*> ans;
for (table_id_t tableID : temp) {
ans.emplace(catalog->getTableCatalogEntry(tx, tableID));
}
return ans;
}
static std::vector<catalog::TableCatalogEntry*> intersectEntries(
std::vector<catalog::TableCatalogEntry*> v1, std::vector<catalog::TableCatalogEntry*> v2) {
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<catalog::TableCatalogEntry*> intersection;
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),
std::back_inserter(intersection));
return intersection;
}
static bool isSameTableCatalogEntryVector(std::vector<TableCatalogEntry*> v1,
std::vector<TableCatalogEntry*> v2) {
auto compareFunc = [](TableCatalogEntry* a, TableCatalogEntry* b) {
return a->getTableID() < b->getTableID();
};
std::sort(v1.begin(), v1.end(), compareFunc);
std::sort(v2.begin(), v2.end(), compareFunc);
return std::equal(v1.begin(), v1.end(), v2.begin(), v2.end());
}
void QueryGraphLabelAnalyzer::pruneRel(RelExpression& rel) const {
auto srcTableIDSet = rel.getSrcNode()->getTableIDsSet();
auto dstTableIDSet = rel.getDstNode()->getTableIDsSet();
if (rel.isRecursive()) {
auto nodeTableIDs = getNodeTableIDs();
// there is no label on both sides
if (rel.getUpperBound() == 0 || rel.getEntries().empty()) {
return;
}
auto [stepFromLeftTableIDs, stepFromRightTableIDs] =
pruneRecursiveRel(rel.getEntries(), srcTableIDSet, dstTableIDSet, rel.getLowerBound(),
rel.getUpperBound(), rel.getDirectionType());
auto recursiveInfo = rel.getRecursiveInfoUnsafe();
recursiveInfo->stepFromLeftActivationRelInfos = stepFromLeftTableIDs;
recursiveInfo->stepFromRightActivationRelInfos = stepFromRightTableIDs;
// todo we need reset rel entries?
auto temp = mergeTableIDs(stepFromLeftTableIDs, stepFromRightTableIDs);
std::vector<TableCatalogEntry*> newRelEntries{temp.begin(), temp.end()};
if (!isSameTableCatalogEntryVector(newRelEntries, rel.getEntries())) {
rel.setEntries(newRelEntries);
recursiveInfo->rel->setEntries(newRelEntries);
// update src&dst entries
auto forwardRelNodes = collectRelNodes(RelDataDirection::BWD,
getTableCatalogEntries(stepFromLeftTableIDs.front()));
std::unordered_set<table_id_t> backwardRelNodes;
for (auto i = rel.getLowerBound(); i <= rel.getUpperBound(); ++i) {
if (i == 0) {
continue;
}
const auto relSrcNodes = collectRelNodes(RelDataDirection::FWD,
getTableCatalogEntries(stepFromLeftTableIDs.at(i - 1)));
backwardRelNodes.insert(relSrcNodes.begin(), relSrcNodes.end());
}
if (rel.getDirectionType() == RelDirectionType::BOTH) {
forwardRelNodes.insert(backwardRelNodes.begin(), backwardRelNodes.end());
backwardRelNodes = forwardRelNodes;
}
auto newSrcNodeEntries = intersectEntries(rel.getSrcNode()->getEntries(),
getTableCatalogEntries({forwardRelNodes.begin(), forwardRelNodes.end()}));
rel.getSrcNode()->setEntries(newSrcNodeEntries);
auto newDstNodeEntries = intersectEntries(rel.getDstNode()->getEntries(),
getTableCatalogEntries({backwardRelNodes.begin(), backwardRelNodes.end()}));
rel.getDstNode()->setEntries(newDstNodeEntries);
}
} else {
auto prunedEntries = pruneNonRecursiveRel(rel.getEntries(), srcTableIDSet, dstTableIDSet,
rel.getDirectionType());
rel.setEntries(prunedEntries);
}
// Note the pruning for node should guarantee the following exception won't be triggered.
// For safety (and consistency) reason, we still write the check but skip coverage check.
// LCOV_EXCL_START
if (rel.getEntries().empty()) {
if (throwOnViolate) {
throw BinderException(stringFormat("Cannot find a label for relationship {} that "
"connects to all of its neighbour nodes.",
rel.toString()));
}
}
// LCOV_EXCL_STOP
}
} // namespace binder
} // namespace kuzu