summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavis Haupt <davis.haupt@mongodb.com>2022-12-20 23:29:59 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-12-21 00:59:00 +0000
commit877a8295ec3ccd9df1e4b3b843b33bbdbb761e71 (patch)
tree5b4e76510b9d6dae849da64e859a3a963190d6f8
parentb420a58ed7c1494570c4bdbd90e0650572e14578 (diff)
downloadmongo-877a8295ec3ccd9df1e4b3b843b33bbdbb761e71.tar.gz
SERVER-72056 golden test for lowering ABT constant expressions to SBE
-rw-r--r--src/mongo/db/exec/sbe/SConscript11
-rw-r--r--src/mongo/db/exec/sbe/abt/abt_lower_test.cpp78
-rw-r--r--src/mongo/db/query/optimizer/syntax/expr.cpp5
-rw-r--r--src/mongo/db/query/optimizer/syntax/expr.h2
-rw-r--r--src/mongo/db/test_output/exec/sbe/a_b_t_plan_generation/lower_constant_expression.txt64
5 files changed, 160 insertions, 0 deletions
diff --git a/src/mongo/db/exec/sbe/SConscript b/src/mongo/db/exec/sbe/SConscript
index 8e2d1762d1e..d037d0b2267 100644
--- a/src/mongo/db/exec/sbe/SConscript
+++ b/src/mongo/db/exec/sbe/SConscript
@@ -268,3 +268,14 @@ env.CppUnitTest(
'sbe_abt_test_util',
],
)
+
+env.CppUnitTest(
+ target='abt_lowering_test',
+ source=[
+ 'abt/abt_lower_test.cpp',
+ ],
+ LIBDEPS=[
+ '$BUILD_DIR/mongo/unittest/unittest',
+ 'query_sbe_abt',
+ ],
+)
diff --git a/src/mongo/db/exec/sbe/abt/abt_lower_test.cpp b/src/mongo/db/exec/sbe/abt/abt_lower_test.cpp
new file mode 100644
index 00000000000..7a63e0d61db
--- /dev/null
+++ b/src/mongo/db/exec/sbe/abt/abt_lower_test.cpp
@@ -0,0 +1,78 @@
+/**
+ * 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/bson/timestamp.h"
+#include "mongo/db/exec/sbe/abt/abt_lower.h"
+#include "mongo/db/query/optimizer/explain.h"
+#include "mongo/unittest/golden_test.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo::optimizer {
+namespace {
+
+unittest::GoldenTestConfig goldenTestConfig{"src/mongo/db/test_output/exec/sbe"};
+using GoldenTestContext = unittest::GoldenTestContext;
+using GoldenTestConfig = unittest::GoldenTestConfig;
+class ABTPlanGeneration : public unittest::Test {
+protected:
+ void runExpressionVariation(GoldenTestContext& gctx, const std::string& name, const ABT& n) {
+ auto& stream = gctx.outStream();
+ if (stream.tellp()) {
+ stream << std::endl;
+ }
+ stream << "==== VARIATION: " << name << " ====" << std::endl;
+ stream << "-- INPUT:" << std::endl;
+ stream << ExplainGenerator::explainV2(n) << std::endl;
+ stream << "-- OUTPUT:" << std::endl;
+ auto env = VariableEnvironment::build(n);
+ SlotVarMap map;
+ auto expr = SBEExpressionLowering{env, map}.optimize(n);
+ stream << expr->toString() << std::endl;
+ }
+};
+
+
+TEST_F(ABTPlanGeneration, LowerConstantExpression) {
+ GoldenTestContext ctx(&goldenTestConfig);
+ ctx.printTestHeader(GoldenTestContext::HeaderFormat::Text);
+ runExpressionVariation(ctx, "string", Constant::str("hello world"_sd));
+
+ runExpressionVariation(ctx, "int64", Constant::int64(100));
+ runExpressionVariation(ctx, "int32", Constant::int32(32));
+ runExpressionVariation(ctx, "double", Constant::fromDouble(3.14));
+ runExpressionVariation(ctx, "decimal", Constant::fromDecimal(Decimal128("3.14")));
+
+ runExpressionVariation(ctx, "timestamp", Constant::timestamp(Timestamp::max()));
+ runExpressionVariation(ctx, "date", Constant::date(Date_t::fromMillisSinceEpoch(100)));
+
+ runExpressionVariation(ctx, "boolean true", Constant::boolean(true));
+ runExpressionVariation(ctx, "boolean false", Constant::boolean(false));
+}
+} // namespace
+} // namespace mongo::optimizer
diff --git a/src/mongo/db/query/optimizer/syntax/expr.cpp b/src/mongo/db/query/optimizer/syntax/expr.cpp
index bc50cb4a4df..5652adcb97d 100644
--- a/src/mongo/db/query/optimizer/syntax/expr.cpp
+++ b/src/mongo/db/query/optimizer/syntax/expr.cpp
@@ -75,6 +75,11 @@ ABT Constant::fromDouble(double value) {
return make<Constant>(TypeTags::NumberDouble, bitcastFrom<double>(value));
}
+ABT Constant::fromDecimal(const Decimal128& value) {
+ auto [tag, val] = makeCopyDecimal(value);
+ return make<Constant>(tag, val);
+}
+
ABT Constant::timestamp(const Timestamp& t) {
return make<Constant>(TypeTags::Timestamp, bitcastFrom<uint64_t>(t.asULL()));
}
diff --git a/src/mongo/db/query/optimizer/syntax/expr.h b/src/mongo/db/query/optimizer/syntax/expr.h
index 7fed5b15734..ba1c4666c10 100644
--- a/src/mongo/db/query/optimizer/syntax/expr.h
+++ b/src/mongo/db/query/optimizer/syntax/expr.h
@@ -58,6 +58,8 @@ public:
static ABT fromDouble(double value);
+ static ABT fromDecimal(const Decimal128& value);
+
static ABT emptyObject();
static ABT emptyArray();
diff --git a/src/mongo/db/test_output/exec/sbe/a_b_t_plan_generation/lower_constant_expression.txt b/src/mongo/db/test_output/exec/sbe/a_b_t_plan_generation/lower_constant_expression.txt
new file mode 100644
index 00000000000..886e00729dc
--- /dev/null
+++ b/src/mongo/db/test_output/exec/sbe/a_b_t_plan_generation/lower_constant_expression.txt
@@ -0,0 +1,64 @@
+# Golden test output of ABTPlanGeneration/LowerConstantExpression
+
+==== VARIATION: string ====
+-- INPUT:
+Const ["hello world"]
+
+-- OUTPUT:
+"hello world"
+
+==== VARIATION: int64 ====
+-- INPUT:
+Const [100]
+
+-- OUTPUT:
+100ll
+
+==== VARIATION: int32 ====
+-- INPUT:
+Const [32]
+
+-- OUTPUT:
+32
+
+==== VARIATION: double ====
+-- INPUT:
+Const [3.14]
+
+-- OUTPUT:
+3.140000L
+
+==== VARIATION: decimal ====
+-- INPUT:
+Const [3.14]
+
+-- OUTPUT:
+NumberDecimal(3.14)
+
+==== VARIATION: timestamp ====
+-- INPUT:
+Const [Timestamp(4294967295, 4294967295)]
+
+-- OUTPUT:
+Timestamp(4294967295, 4294967295)
+
+==== VARIATION: date ====
+-- INPUT:
+Const [100]
+
+-- OUTPUT:
+Date(100)
+
+==== VARIATION: boolean true ====
+-- INPUT:
+Const [true]
+
+-- OUTPUT:
+true
+
+==== VARIATION: boolean false ====
+-- INPUT:
+Const [false]
+
+-- OUTPUT:
+false