diff options
author | Lynne Wang <lynne.wang@mongodb.com> | 2022-08-10 12:46:20 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-08-10 13:20:08 +0000 |
commit | 85ff13118e4f882b7c254fd1d3c2f4abd4c5296b (patch) | |
tree | 6c67145c7f707cdf63ccd7e75a35aedf66151265 /src | |
parent | 86c0cc1046f22753ae9c5c82d62329f491d70e71 (diff) | |
download | mongo-85ff13118e4f882b7c254fd1d3c2f4abd4c5296b.tar.gz |
SERVER-67997 Validation of analyze parameters
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/commands/analyze_cmd.cpp | 54 | ||||
-rw-r--r-- | src/mongo/db/query/analyze_command.idl | 4 |
2 files changed, 56 insertions, 2 deletions
diff --git a/src/mongo/db/commands/analyze_cmd.cpp b/src/mongo/db/commands/analyze_cmd.cpp index 15868758600..89e80c8d775 100644 --- a/src/mongo/db/commands/analyze_cmd.cpp +++ b/src/mongo/db/commands/analyze_cmd.cpp @@ -31,6 +31,8 @@ #include "mongo/db/auth/authorization_session.h" #include "mongo/db/commands.h" +#include "mongo/db/db_raii.h" +#include "mongo/db/namespace_string.h" #include "mongo/db/query/analyze_command_gen.h" #include "mongo/db/query/query_feature_flags_gen.h" @@ -66,6 +68,58 @@ public: } void typedRun(OperationContext* opCtx) { + const auto& cmd = request(); + const NamespaceString& nss = ns(); + + // Validate collection + // Namespace exists + AutoGetCollectionForReadMaybeLockFree autoColl(opCtx, nss); + const auto& collection = autoColl.getCollection(); + uassert(6799700, str::stream() << "Couldn't find collection " << nss.ns(), collection); + + // Namespace cannot be capped collection + const bool isCapped = collection->isCapped(); + uassert(6799701, "Analyze command is not supported on capped collections", !isCapped); + + // Namespace is normal or clustered collection + const bool isNormalColl = nss.isNormalCollection(); + const bool isClusteredColl = collection->isClustered(); + uassert(6799702, + str::stream() << nss.toString() << " is not a normal or clustered collection", + isNormalColl || isClusteredColl); + + // Validate key + auto key = cmd.getKey(); + if (key) { + const FieldRef keyFieldRef(*key); + + // Empty path + uassert(6799703, "Key path is empty", !keyFieldRef.empty()); + + for (size_t i = 0; i < keyFieldRef.numParts(); ++i) { + FieldPath::uassertValidFieldName(keyFieldRef.getPart(i)); + } + + // Numerics + const auto numericPathComponents = keyFieldRef.getNumericPathComponents(0); + uassert(6799704, + str::stream() << "Key path contains numeric component " + << keyFieldRef.getPart(*(numericPathComponents.begin())), + numericPathComponents.empty()); + } + + // Sample rate and sample size can't both be present + auto sampleRate = cmd.getSampleRate(); + auto sampleSize = cmd.getSampleSize(); + uassert(6799705, + "Only one of sample rate and sample size may be present", + !sampleRate || !sampleSize); + + if (sampleSize || sampleRate) { + uassert( + 6799706, "It is illegal to pass sampleRate or sampleSize without a key", key); + } + uassert(6660400, "Analyze command requires common query framework feature flag to be enabled", serverGlobalParams.featureCompatibility.isVersionInitialized() && diff --git a/src/mongo/db/query/analyze_command.idl b/src/mongo/db/query/analyze_command.idl index b2feadc7126..124a6acbead 100644 --- a/src/mongo/db/query/analyze_command.idl +++ b/src/mongo/db/query/analyze_command.idl @@ -57,12 +57,12 @@ commands: sampleRate: description: "The proportion of the collection, in the range (0,1] to be sampled for creating stats." - type: safeInt + type: safeDouble validator: { gt: 0.0, lte: 1.0 } optional: true sampleSize: description: "The number of documents used to build the stats, based on a random sample of the collection." - type: safeInt64 + type: safeInt validator: { gt: 0 } optional: true |