diff options
author | Svilen Mihaylov <svilen.mihaylov@mongodb.com> | 2023-01-19 20:57:36 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-01-19 21:46:22 +0000 |
commit | a195bd41f07598b610ea5f56f26f0b08e0f0999b (patch) | |
tree | 57e35ff6ff1b459a4b5086899a980a0f8096e055 /src/mongo/db/query/optimizer/comparison_op.h | |
parent | 49b6f9b3716e15d00da3c31c856a0b12eb28aabd (diff) | |
download | mongo-a195bd41f07598b610ea5f56f26f0b08e0f0999b.tar.gz |
SERVER-72645 Split sbe_stage_builder_helpers.h into separate ABT/non ABT headers
Diffstat (limited to 'src/mongo/db/query/optimizer/comparison_op.h')
-rw-r--r-- | src/mongo/db/query/optimizer/comparison_op.h | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/mongo/db/query/optimizer/comparison_op.h b/src/mongo/db/query/optimizer/comparison_op.h new file mode 100644 index 00000000000..1023a281a94 --- /dev/null +++ b/src/mongo/db/query/optimizer/comparison_op.h @@ -0,0 +1,144 @@ +/** + * Copyright (C) 2023-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. + */ + +#pragma once + +#include "mongo/db/query/optimizer/utils/printable_enum.h" + +namespace mongo::optimizer { + +#define PATHSYNTAX_OPNAMES(F) \ + /* comparison operations */ \ + F(Eq) \ + F(EqMember) \ + F(Neq) \ + F(Gt) \ + F(Gte) \ + F(Lt) \ + F(Lte) \ + F(Cmp3w) \ + \ + /* binary operations */ \ + F(Add) \ + F(Sub) \ + F(Mult) \ + F(Div) \ + \ + /* unary operations */ \ + F(Neg) \ + \ + /* Nothing-handling */ \ + F(FillEmpty) \ + \ + /* logical operations */ \ + F(And) \ + F(Or) \ + F(Not) + +MAKE_PRINTABLE_ENUM(Operations, PATHSYNTAX_OPNAMES); +MAKE_PRINTABLE_ENUM_STRING_ARRAY(OperationsEnum, Operations, PATHSYNTAX_OPNAMES); +#undef PATHSYNTAX_OPNAMES + +inline constexpr bool isUnaryOp(Operations op) { + return op == Operations::Neg || op == Operations::Not; +} + +inline constexpr bool isBinaryOp(Operations op) { + return !isUnaryOp(op); +} + +inline constexpr bool isComparisonOp(Operations op) { + switch (op) { + case Operations::Eq: + case Operations::EqMember: + case Operations::Neq: + case Operations::Gt: + case Operations::Gte: + case Operations::Lt: + case Operations::Lte: + case Operations::Cmp3w: + return true; + default: + return false; + } +} + +/** + * Flip the argument order of a comparison op. + * + * Not to be confused with boolean negation: see 'negateComparisonOp'. + */ +inline constexpr Operations flipComparisonOp(Operations op) { + switch (op) { + case Operations::Eq: + case Operations::Neq: + return op; + + case Operations::Lt: + return Operations::Gt; + case Operations::Lte: + return Operations::Gte; + case Operations::Gt: + return Operations::Lt; + case Operations::Gte: + return Operations::Lte; + + default: + MONGO_UNREACHABLE; + } +} + +/** + * Negate a comparison op, such that negate(op)(x, y) == not(op(x, y)). + * + * If the op is not a comparison, return none. + * If the op can't be negated (for example EqMember), return none. + * + * Not to be confused with flipping the argument order: see 'flipComparisonOp'. + */ +inline boost::optional<Operations> negateComparisonOp(Operations op) { + switch (op) { + case Operations::Lt: + return Operations::Gte; + case Operations::Lte: + return Operations::Gt; + case Operations::Eq: + return Operations::Neq; + case Operations::Gte: + return Operations::Lt; + case Operations::Gt: + return Operations::Lte; + case Operations::Neq: + return Operations::Eq; + default: + return {}; + } +} + +} // namespace mongo::optimizer |