diff options
author | Maddie Zechar <mez2113@columbia.edu> | 2021-05-24 16:37:46 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-06-07 17:19:04 +0000 |
commit | 3d4e1c0aa3e446c7f6cbb90b703f33ac25283d53 (patch) | |
tree | 53b6e0c7d88271bab57d49300e4b0be8a927018a /src/mongo/db/pipeline/window_function | |
parent | c5ac2eb1ea145693e1c6b974e88a2cfc18780134 (diff) | |
download | mongo-3d4e1c0aa3e446c7f6cbb90b703f33ac25283d53.tar.gz |
SERVER-56849: Improve error reporting for $setWindowFields
Diffstat (limited to 'src/mongo/db/pipeline/window_function')
-rw-r--r-- | src/mongo/db/pipeline/window_function/window_function_expression.cpp | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/src/mongo/db/pipeline/window_function/window_function_expression.cpp b/src/mongo/db/pipeline/window_function/window_function_expression.cpp index d9b7ae217dd..9146b5d5b79 100644 --- a/src/mongo/db/pipeline/window_function/window_function_expression.cpp +++ b/src/mongo/db/pipeline/window_function/window_function_expression.cpp @@ -47,7 +47,7 @@ using boost::intrusive_ptr; using boost::optional; namespace mongo::window_function { - +using namespace std::string_literals; REGISTER_WINDOW_FUNCTION(derivative, ExpressionDerivative::parse); REGISTER_WINDOW_FUNCTION(first, ExpressionFirst::parse); REGISTER_WINDOW_FUNCTION(last, ExpressionLast::parse); @@ -57,16 +57,42 @@ StringMap<Expression::Parser> Expression::parserMap; intrusive_ptr<Expression> Expression::parse(BSONObj obj, const optional<SortPattern>& sortBy, ExpressionContext* expCtx) { - boost::optional<Expression::Parser> parser; + for (const auto& field : obj) { - // Found one valid window function. If there are multiple window functions they will be - // caught as invalid arguments to the Expression parser later. - auto parser = parserMap.find(field.fieldNameStringData()); - if (parser != parserMap.end()) { - return parser->second(obj, sortBy, expCtx); + // Check if window function is $-prefixed. + auto fieldName = field.fieldNameStringData(); + + if (fieldName.startsWith("$"_sd)) { + + if (auto parser = parserMap.find(field.fieldNameStringData()); + parser != parserMap.end()) { + // Found one valid window function. If there are multiple window functions they will + // be caught as invalid arguments to the Expression parser later. + return parser->second(obj, sortBy, expCtx); + } + // The window function provided in the window function expression is invalid. + + // For example, in this window function expression: + // {$setWindowFields: + // {output: + // {total: + // {$summ: "$x", windoww: {documents: ['unbounded', 'current']} + // } + // } + // } + // } + // + // the window function, $summ, is invalid as it is mispelled. + uasserted(ErrorCodes::FailedToParse, + str::stream() << "Unrecognized window function, " << fieldName); } } - uasserted(ErrorCodes::FailedToParse, "Unrecognized window function"); + // The command did not contain any $-prefixed window functions. + uasserted(ErrorCodes::FailedToParse, + "Expected a $-prefixed window function"s + + (obj.firstElementFieldNameStringData().empty() + ? ""s + : ", "s + obj.firstElementFieldNameStringData())); } void Expression::registerParser(std::string functionName, Parser parser) { |