summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Storch <david.storch@mongodb.com>2022-09-23 12:18:02 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-09-23 22:16:49 +0000
commitcdafd3e4b159ed66d61da9fc6d15c8707a3e5b3e (patch)
tree02a1749530eedbc762c68d599710d521fa1a80a1
parentfbe055e5297871bd4c97904561dfcfc4b1ed85bf (diff)
downloadmongo-cdafd3e4b159ed66d61da9fc6d15c8707a3e5b3e.tar.gz
SERVER-69149 Make debug print of SBE expressions more readable
Reapplies the original change with a corrected out-of-bounds check.
-rw-r--r--jstests/core/column_scan_skip_row_store_projection.js2
-rw-r--r--src/mongo/db/exec/sbe/expressions/expression.cpp47
-rw-r--r--src/mongo/db/exec/sbe/stages/union.cpp15
-rw-r--r--src/mongo/db/exec/sbe/util/debug_print.cpp11
4 files changed, 50 insertions, 25 deletions
diff --git a/jstests/core/column_scan_skip_row_store_projection.js b/jstests/core/column_scan_skip_row_store_projection.js
index 23dcb6b2ae9..97c10f8b3df 100644
--- a/jstests/core/column_scan_skip_row_store_projection.js
+++ b/jstests/core/column_scan_skip_row_store_projection.js
@@ -63,7 +63,7 @@ function test({agg, requiresRowStoreExpr, rowstoreFetches}) {
const nullRegex =
/COLUMN_SCAN s.* ((s.*)|(none)) paths\[.*\] pathFilters\[.*\] rowStoreExpr\[\] @.* @.*/;
const notNullRegex =
- /COLUMN_SCAN s.* ((s.*)|(none)) paths\[.*\] pathFilters\[.*\] rowStoreExpr\[.*, .*\] @.* @.*/;
+ /COLUMN_SCAN s.* ((s.*)|(none)) paths\[.*\] pathFilters\[.*\] rowStoreExpr\[.*, \n/;
if (requiresRowStoreExpr) {
assert(!nullRegex.test(sbeStages), `Don't expect null rowstoreExpr in ${sbeStages}`);
assert(notNullRegex.test(sbeStages), `Expected non-null rowstoreExpr in ${sbeStages}`);
diff --git a/src/mongo/db/exec/sbe/expressions/expression.cpp b/src/mongo/db/exec/sbe/expressions/expression.cpp
index d337cd8fa14..7176ce3e56a 100644
--- a/src/mongo/db/exec/sbe/expressions/expression.cpp
+++ b/src/mongo/db/exec/sbe/expressions/expression.cpp
@@ -239,6 +239,7 @@ std::vector<DebugPrinter::Block> EPrimBinary::debugPrint() const {
invariant(!hasCollatorArg || isComparisonOp(_op));
+ ret.emplace_back("(`");
DebugPrinter::addBlocks(ret, _nodes[0]->debugPrint());
switch (_op) {
@@ -292,6 +293,7 @@ std::vector<DebugPrinter::Block> EPrimBinary::debugPrint() const {
}
DebugPrinter::addBlocks(ret, _nodes[1]->debugPrint());
+ ret.emplace_back("`)");
return ret;
}
@@ -335,7 +337,9 @@ std::vector<DebugPrinter::Block> EPrimUnary::debugPrint() const {
MONGO_UNREACHABLE;
}
+ ret.emplace_back("`(`");
DebugPrinter::addBlocks(ret, _nodes[0]->debugPrint());
+ ret.emplace_back("`)");
return ret;
}
@@ -733,7 +737,7 @@ std::vector<DebugPrinter::Block> EFunction::debugPrint() const {
std::vector<DebugPrinter::Block> ret;
DebugPrinter::addKeyword(ret, _name);
- ret.emplace_back("(`");
+ ret.emplace_back("`(`");
for (size_t idx = 0; idx < _nodes.size(); ++idx) {
if (idx) {
ret.emplace_back("`,");
@@ -780,20 +784,24 @@ vm::CodeFragment EIf::compileDirect(CompileCtx& ctx) const {
std::vector<DebugPrinter::Block> EIf::debugPrint() const {
std::vector<DebugPrinter::Block> ret;
- DebugPrinter::addKeyword(ret, "if");
- ret.emplace_back("(`");
+ ret.emplace_back(DebugPrinter::Block::cmdIncIndent);
// Print the condition.
+ DebugPrinter::addKeyword(ret, "if");
DebugPrinter::addBlocks(ret, _nodes[0]->debugPrint());
- ret.emplace_back("`,");
+ DebugPrinter::addNewLine(ret);
+
// Print thenBranch.
+ DebugPrinter::addKeyword(ret, "then");
DebugPrinter::addBlocks(ret, _nodes[1]->debugPrint());
- ret.emplace_back("`,");
+ DebugPrinter::addNewLine(ret);
+
// Print elseBranch.
+ DebugPrinter::addKeyword(ret, "else");
DebugPrinter::addBlocks(ret, _nodes[2]->debugPrint());
- ret.emplace_back("`)");
+ ret.emplace_back(DebugPrinter::Block::cmdDecIndent);
return ret;
}
@@ -836,22 +844,30 @@ vm::CodeFragment ELocalBind::compileDirect(CompileCtx& ctx) const {
std::vector<DebugPrinter::Block> ELocalBind::debugPrint() const {
std::vector<DebugPrinter::Block> ret;
- DebugPrinter::addKeyword(ret, "let");
+ ret.emplace_back(DebugPrinter::Block::cmdIncIndent);
+ DebugPrinter::addKeyword(ret, "let");
ret.emplace_back("[`");
+ ret.emplace_back(DebugPrinter::Block::cmdIncIndent);
+
for (size_t idx = 0; idx < _nodes.size() - 1; ++idx) {
if (idx != 0) {
- ret.emplace_back("`,");
+ DebugPrinter::addNewLine(ret);
}
DebugPrinter::addIdentifier(ret, _frameId, idx);
ret.emplace_back("=");
DebugPrinter::addBlocks(ret, _nodes[idx]->debugPrint());
}
- ret.emplace_back("`]");
+ ret.emplace_back(DebugPrinter::Block::cmdDecIndent);
+ ret.emplace_back("]");
+ DebugPrinter::addNewLine(ret);
+ DebugPrinter::addKeyword(ret, "in");
DebugPrinter::addBlocks(ret, _nodes.back()->debugPrint());
+ ret.emplace_back(DebugPrinter::Block::cmdDecIndent);
+
return ret;
}
@@ -896,10 +912,13 @@ vm::CodeFragment ELocalLambda::compileDirect(CompileCtx& ctx) const {
std::vector<DebugPrinter::Block> ELocalLambda::debugPrint() const {
std::vector<DebugPrinter::Block> ret;
- DebugPrinter::addKeyword(ret, "\\");
+ DebugPrinter::addKeyword(ret, "lambda");
+ ret.emplace_back("`(`");
DebugPrinter::addIdentifier(ret, _frameId, 0);
- ret.emplace_back(".");
+ ret.emplace_back("`)");
+ ret.emplace_back("{");
DebugPrinter::addBlocks(ret, _nodes.back()->debugPrint());
+ ret.emplace_back("}");
return ret;
}
@@ -930,11 +949,13 @@ std::vector<DebugPrinter::Block> EFail::debugPrint() const {
std::vector<DebugPrinter::Block> ret;
DebugPrinter::addKeyword(ret, "fail");
- ret.emplace_back("(");
+ ret.emplace_back("`(`");
ret.emplace_back(std::to_string(_code));
- ret.emplace_back(",`");
+ ret.emplace_back("`,");
+ ret.emplace_back("\"`");
ret.emplace_back(getStringView(_messageTag, _messageVal));
+ ret.emplace_back("`\"`");
ret.emplace_back("`)");
diff --git a/src/mongo/db/exec/sbe/stages/union.cpp b/src/mongo/db/exec/sbe/stages/union.cpp
index 2fd6d0b4fc5..9d619f2f505 100644
--- a/src/mongo/db/exec/sbe/stages/union.cpp
+++ b/src/mongo/db/exec/sbe/stages/union.cpp
@@ -27,10 +27,10 @@
* it in the license file.
*/
-#include "mongo/platform/basic.h"
-
#include "mongo/db/exec/sbe/stages/union.h"
+#include <fmt/format.h>
+
#include "mongo/db/exec/sbe/expressions/expression.h"
#include "mongo/db/exec/sbe/size_estimator.h"
@@ -197,9 +197,10 @@ std::vector<DebugPrinter::Block> UnionStage::debugPrint() const {
}
ret.emplace_back(DebugPrinter::Block("`]"));
- ret.emplace_back(DebugPrinter::Block("[`"));
ret.emplace_back(DebugPrinter::Block::cmdIncIndent);
for (size_t childNum = 0; childNum < _children.size(); childNum++) {
+ DebugPrinter::addKeyword(ret, "branch{}"_format(childNum));
+
ret.emplace_back(DebugPrinter::Block("[`"));
for (size_t idx = 0; idx < _inputVals[childNum].size(); idx++) {
if (idx) {
@@ -209,15 +210,11 @@ std::vector<DebugPrinter::Block> UnionStage::debugPrint() const {
}
ret.emplace_back(DebugPrinter::Block("`]"));
+ ret.emplace_back(DebugPrinter::Block::cmdIncIndent);
DebugPrinter::addBlocks(ret, _children[childNum]->debugPrint());
-
- if (childNum + 1 < _children.size()) {
- ret.emplace_back(DebugPrinter::Block(","));
- DebugPrinter::addNewLine(ret);
- }
+ ret.emplace_back(DebugPrinter::Block::cmdDecIndent);
}
ret.emplace_back(DebugPrinter::Block::cmdDecIndent);
- ret.emplace_back(DebugPrinter::Block("`]"));
return ret;
}
diff --git a/src/mongo/db/exec/sbe/util/debug_print.cpp b/src/mongo/db/exec/sbe/util/debug_print.cpp
index e3d831b5ab5..e33397530fc 100644
--- a/src/mongo/db/exec/sbe/util/debug_print.cpp
+++ b/src/mongo/db/exec/sbe/util/debug_print.cpp
@@ -38,6 +38,7 @@ namespace sbe {
std::string DebugPrinter::print(const std::vector<Block>& blocks) {
std::string ret;
int ident = 0;
+ size_t blockIndex = 0;
for (auto& b : blocks) {
bool addSpace = true;
switch (b.cmd) {
@@ -48,8 +49,13 @@ std::string DebugPrinter::print(const std::vector<Block>& blocks) {
break;
case Block::cmdDecIndent:
--ident;
- ret.append("\n");
- addIndent(ident, ret);
+ // Avoid unnecessary whitespace if there are multiple adjacent "decrement indent"
+ // tokens.
+ if (blockIndex >= (blocks.size() - 1) ||
+ blocks[blockIndex + 1].cmd != Block::cmdDecIndent) {
+ ret.append("\n");
+ addIndent(ident, ret);
+ }
break;
case Block::cmdNewLine:
ret.append("\n");
@@ -111,6 +117,7 @@ std::string DebugPrinter::print(const std::vector<Block>& blocks) {
}
}
}
+ ++blockIndex;
}
return ret;