summaryrefslogtreecommitdiff
path: root/src/mongo/db/query/optimizer/utils/abt_hash.cpp
blob: 90585939882f689f12165ab93ca0bfb8660a9516 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
 *    Copyright (C) 2022-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/db/query/optimizer/utils/abt_hash.h"

#include "mongo/db/query/optimizer/node.h"
#include "mongo/db/query/optimizer/utils/utils.h"

namespace mongo::optimizer {

static size_t computeCollationHash(const properties::CollationRequirement& prop) {
    size_t collationHash = 17;
    for (const auto& entry : prop.getCollationSpec()) {
        updateHash(collationHash, std::hash<ProjectionName>()(entry.first));
        updateHash(collationHash, std::hash<CollationOp>()(entry.second));
    }
    return collationHash;
}

static size_t computeLimitSkipHash(const properties::LimitSkipRequirement& prop) {
    size_t limitSkipHash = 17;
    updateHash(limitSkipHash, std::hash<int64_t>()(prop.getLimit()));
    updateHash(limitSkipHash, std::hash<int64_t>()(prop.getSkip()));
    return limitSkipHash;
}

static size_t computePropertyProjectionsHash(const ProjectionNameVector& projections) {
    size_t resultHash = 17;
    for (const ProjectionName& projection : projections) {
        updateHashUnordered(resultHash, std::hash<ProjectionName>()(projection));
    }
    return resultHash;
}

static size_t computeProjectionRequirementHash(const properties::ProjectionRequirement& prop) {
    return computePropertyProjectionsHash(prop.getProjections().getVector());
}

static size_t computeDistributionHash(const properties::DistributionRequirement& prop) {
    size_t resultHash = 17;
    const auto& distribAndProjections = prop.getDistributionAndProjections();
    updateHash(resultHash, std::hash<DistributionType>()(distribAndProjections._type));
    updateHash(resultHash, computePropertyProjectionsHash(distribAndProjections._projectionNames));
    return resultHash;
}

static void updateBoundHash(size_t& result, const BoundRequirement& bound) {
    updateHash(result, std::hash<bool>()(bound.isInclusive()));
    updateHash(result, ABTHashGenerator::generate(bound.getBound()));
};

template <class T>
class IntervalHasher {
public:
    size_t computeHash(const IntervalRequirement& req) {
        size_t result = 17;
        updateBoundHash(result, req.getLowBound());
        updateBoundHash(result, req.getHighBound());
        return 17;
    }

    size_t computeHash(const CompoundIntervalRequirement& req) {
        size_t result = 19;
        for (const auto& interval : req) {
            updateHash(result, computeHash(interval));
        }
        return result;
    }

    size_t transport(const typename T::Atom& node) {
        return computeHash(node.getExpr());
    }

    size_t transport(const typename T::Conjunction& node, const std::vector<size_t> childResults) {
        size_t result = 31;
        for (const size_t childResult : childResults) {
            updateHash(result, childResult);
        }
        return result;
    }

    size_t transport(const typename T::Disjunction& node, const std::vector<size_t> childResults) {
        size_t result = 29;
        for (const size_t childResult : childResults) {
            updateHash(result, childResult);
        }
        return result;
    }

    size_t compute(const typename T::Node& intervals) {
        return algebra::transport<false>(intervals, *this);
    }
};

static size_t computePartialSchemaReqHash(const PartialSchemaRequirements& reqMap) {
    size_t result = 17;

    IntervalHasher<IntervalReqExpr> intervalHasher;
    for (const auto& [key, req] : reqMap) {
        updateHash(result, std::hash<ProjectionName>()(key._projectionName));
        updateHash(result, ABTHashGenerator::generate(key._path));
        updateHash(result, std::hash<ProjectionName>()(req.getBoundProjectionName().value_or("")));
        updateHash(result, intervalHasher.compute(req.getIntervals()));
        updateHash(result, std::hash<bool>()(req.getIsPerfOnly()));
    }
    return result;
}

/**
 * Hasher for ABT nodes. Used in conjunction with memo.
 */
class ABTHashTransporter {
public:
    /**
     * Nodes
     */
    template <typename T, typename... Ts>
    size_t transport(const T& /*node*/, Ts&&...) {
        // Physical nodes do not currently need to implement hash.
        static_assert(!canBeLogicalNode<T>(), "Logical node must implement its hash.");
        uasserted(6624142, "must implement custom hash");
    }

    size_t transport(const References& references, std::vector<size_t> inResults) {
        return computeHashSeq<1>(computeVectorHash(inResults));
    }

    size_t transport(const ExpressionBinder& binders, std::vector<size_t> inResults) {
        return computeHashSeq<2>(computeVectorHash(binders.names()), computeVectorHash(inResults));
    }

    size_t transport(const ScanNode& node, size_t bindResult) {
        return computeHashSeq<3>(std::hash<std::string>()(node.getScanDefName()), bindResult);
    }

    size_t transport(const ValueScanNode& node, size_t bindResult) {
        // Specifically not hashing props here. Those are compared in the equality operator.
        return computeHashSeq<46>(std::hash<bool>()(node.getHasRID()),
                                  std::hash<size_t>()(node.getArraySize()),
                                  ABTHashGenerator::generate(node.getValueArray()),
                                  bindResult);
    }

    size_t transport(const MemoLogicalDelegatorNode& node) {
        return computeHashSeq<4>(std::hash<GroupIdType>()(node.getGroupId()));
    }

    size_t transport(const FilterNode& node, size_t childResult, size_t filterResult) {
        return computeHashSeq<5>(filterResult, childResult);
    }

    size_t transport(const EvaluationNode& node, size_t childResult, size_t projectionResult) {
        return computeHashSeq<6>(projectionResult, childResult);
    }

    size_t transport(const SargableNode& node,
                     size_t childResult,
                     size_t /*bindResult*/,
                     size_t /*refResult*/) {
        // Specifically not hashing the candidate indexes and ScanParams. Those are derivative of
        // the requirements, and can have temp projection names.
        return computeHashSeq<44>(computePartialSchemaReqHash(node.getReqMap()),
                                  std::hash<IndexReqTarget>()(node.getTarget()),
                                  childResult);
    }

    size_t transport(const RIDIntersectNode& node,
                     size_t leftChildResult,
                     size_t rightChildResult) {
        // Specifically always including children.
        return computeHashSeq<45>(std::hash<ProjectionName>()(node.getScanProjectionName()),
                                  std::hash<bool>()(node.hasLeftIntervals()),
                                  std::hash<bool>()(node.hasRightIntervals()),
                                  leftChildResult,
                                  rightChildResult);
    }

    size_t transport(const BinaryJoinNode& node,
                     size_t leftChildResult,
                     size_t rightChildResult,
                     size_t filterResult) {
        // Specifically always including children.
        return computeHashSeq<7>(filterResult, leftChildResult, rightChildResult);
    }

    size_t transport(const UnionNode& node,
                     std::vector<size_t> childResults,
                     size_t bindResult,
                     size_t refsResult) {
        // Specifically always including children.
        return computeHashSeq<9>(bindResult, refsResult, computeVectorHash(childResults));
    }

    size_t transport(const GroupByNode& node,
                     size_t childResult,
                     size_t bindAggResult,
                     size_t refsAggResult,
                     size_t bindGbResult,
                     size_t refsGbResult) {
        return computeHashSeq<10>(bindAggResult,
                                  refsAggResult,
                                  bindGbResult,
                                  refsGbResult,
                                  std::hash<GroupNodeType>()(node.getType()),
                                  childResult);
    }

    size_t transport(const UnwindNode& node,
                     size_t childResult,
                     size_t bindResult,
                     size_t refsResult) {
        return computeHashSeq<11>(
            std::hash<bool>()(node.getRetainNonArrays()), bindResult, refsResult, childResult);
    }

    size_t transport(const CollationNode& node, size_t childResult, size_t /*refsResult*/) {
        return computeHashSeq<13>(computeCollationHash(node.getProperty()), childResult);
    }

    size_t transport(const LimitSkipNode& node, size_t childResult) {
        return computeHashSeq<14>(computeLimitSkipHash(node.getProperty()), childResult);
    }

    size_t transport(const ExchangeNode& node, size_t childResult, size_t /*refsResult*/) {
        return computeHashSeq<43>(computeDistributionHash(node.getProperty()), childResult);
    }

    size_t transport(const RootNode& node, size_t childResult, size_t /*refsResult*/) {
        return computeHashSeq<15>(computeProjectionRequirementHash(node.getProperty()),
                                  childResult);
    }

    /**
     * Expressions
     */
    size_t transport(const Blackhole& expr) {
        return computeHashSeq<16>();
    }

    size_t transport(const Constant& expr) {
        auto [tag, val] = expr.get();
        return computeHashSeq<17>(sbe::value::hashValue(tag, val));
    }

    size_t transport(const Variable& expr) {
        return computeHashSeq<18>(std::hash<std::string>()(expr.name()));
    }

    size_t transport(const UnaryOp& expr, size_t inResult) {
        return computeHashSeq<19>(std::hash<Operations>()(expr.op()), inResult);
    }

    size_t transport(const BinaryOp& expr, size_t leftResult, size_t rightResult) {
        return computeHashSeq<20>(std::hash<Operations>()(expr.op()), leftResult, rightResult);
    }

    size_t transport(const If& expr, size_t condResult, size_t thenResult, size_t elseResult) {
        return computeHashSeq<21>(condResult, thenResult, elseResult);
    }

    size_t transport(const Let& expr, size_t bindResult, size_t exprResult) {
        return computeHashSeq<22>(std::hash<std::string>()(expr.varName()), bindResult, exprResult);
    }

    size_t transport(const LambdaAbstraction& expr, size_t inResult) {
        return computeHashSeq<23>(std::hash<std::string>()(expr.varName()), inResult);
    }

    size_t transport(const LambdaApplication& expr, size_t lambdaResult, size_t argumentResult) {
        return computeHashSeq<24>(lambdaResult, argumentResult);
    }

    size_t transport(const FunctionCall& expr, std::vector<size_t> argResults) {
        return computeHashSeq<25>(std::hash<std::string>()(expr.name()),
                                  computeVectorHash(argResults));
    }

    size_t transport(const EvalPath& expr, size_t pathResult, size_t inputResult) {
        return computeHashSeq<26>(pathResult, inputResult);
    }

    size_t transport(const EvalFilter& expr, size_t pathResult, size_t inputResult) {
        return computeHashSeq<27>(pathResult, inputResult);
    }

    size_t transport(const Source& expr) {
        return computeHashSeq<28>();
    }

    /**
     * Paths
     */
    size_t transport(const PathConstant& path, size_t inResult) {
        return computeHashSeq<29>(inResult);
    }

    size_t transport(const PathLambda& path, size_t inResult) {
        return computeHashSeq<30>(inResult);
    }

    size_t transport(const PathIdentity& path) {
        return computeHashSeq<31>();
    }

    size_t transport(const PathDefault& path, size_t inResult) {
        return computeHashSeq<32>(inResult);
    }

    size_t transport(const PathCompare& path, size_t valueResult) {
        return computeHashSeq<33>(std::hash<Operations>()(path.op()), valueResult);
    }

    size_t transport(const PathDrop& path) {
        size_t namesHash = 17;
        for (const std::string& name : path.getNames()) {
            updateHash(namesHash, std::hash<std::string>()(name));
        }
        return computeHashSeq<34>(namesHash);
    }

    size_t transport(const PathKeep& path) {
        size_t namesHash = 17;
        for (const std::string& name : path.getNames()) {
            updateHash(namesHash, std::hash<std::string>()(name));
        }
        return computeHashSeq<35>(namesHash);
    }

    size_t transport(const PathObj& path) {
        return computeHashSeq<36>();
    }

    size_t transport(const PathArr& path) {
        return computeHashSeq<37>();
    }

    size_t transport(const PathTraverse& path, size_t inResult) {
        return computeHashSeq<38>(inResult, std::hash<size_t>()(path.getMaxDepth()));
    }

    size_t transport(const PathField& path, size_t inResult) {
        return computeHashSeq<39>(std::hash<std::string>()(path.name()), inResult);
    }

    size_t transport(const PathGet& path, size_t inResult) {
        return computeHashSeq<40>(std::hash<std::string>()(path.name()), inResult);
    }

    size_t transport(const PathComposeM& path, size_t leftResult, size_t rightResult) {
        return computeHashSeq<41>(leftResult, rightResult);
    }

    size_t transport(const PathComposeA& path, size_t leftResult, size_t rightResult) {
        return computeHashSeq<42>(leftResult, rightResult);
    }

    size_t generate(const ABT& node) {
        return algebra::transport<false>(node, *this);
    }

    size_t generate(const ABT::reference_type& nodeRef) {
        return algebra::transport<false>(nodeRef, *this);
    }
};

size_t ABTHashGenerator::generate(const ABT& node) {
    ABTHashTransporter gen;
    return gen.generate(node);
}

size_t ABTHashGenerator::generate(const ABT::reference_type& nodeRef) {
    ABTHashTransporter gen;
    return gen.generate(nodeRef);
}

class PhysPropsHasher {
public:
    size_t operator()(const properties::PhysProperty&,
                      const properties::CollationRequirement& prop) {
        return computeHashSeq<1>(computeCollationHash(prop));
    }

    size_t operator()(const properties::PhysProperty&,
                      const properties::LimitSkipRequirement& prop) {
        return computeHashSeq<2>(computeLimitSkipHash(prop));
    }

    size_t operator()(const properties::PhysProperty&,
                      const properties::ProjectionRequirement& prop) {
        return computeHashSeq<3>(computeProjectionRequirementHash(prop));
    }

    size_t operator()(const properties::PhysProperty&,
                      const properties::DistributionRequirement& prop) {
        return computeHashSeq<4>(computeDistributionHash(prop));
    }

    size_t operator()(const properties::PhysProperty&,
                      const properties::IndexingRequirement& prop) {
        return computeHashSeq<5>(std::hash<IndexReqTarget>()(prop.getIndexReqTarget()),
                                 std::hash<bool>()(prop.getDedupRID()));
    }

    size_t operator()(const properties::PhysProperty&, const properties::RepetitionEstimate& prop) {
        return computeHashSeq<6>(std::hash<CEType>()(prop.getEstimate()));
    }

    size_t operator()(const properties::PhysProperty&, const properties::LimitEstimate& prop) {
        return computeHashSeq<7>(std::hash<CEType>()(prop.getEstimate()));
    }

    static size_t computeHash(const properties::PhysProps& props) {
        PhysPropsHasher visitor;
        size_t result = 17;
        for (const auto& prop : props) {
            updateHashUnordered(result, prop.second.visit(visitor));
        }
        return result;
    }
};

size_t ABTHashGenerator::generateForPhysProps(const properties::PhysProps& props) {
    return PhysPropsHasher::computeHash(props);
}

}  // namespace mongo::optimizer