Skip to content

Commit

Permalink
check max validate depth
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Dec 25, 2023
1 parent b84afe6 commit 3b299bd
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 35 deletions.
8 changes: 8 additions & 0 deletions src/graph/validator/AssignmentValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ namespace graph {

Status AssignmentValidator::validateImpl() {
auto *assignSentence = static_cast<AssignmentSentence *>(sentence_);
auto statusOr = makeValidator(assignSentence->sentence(), qctx_);
NG_RETURN_IF_ERROR(statusOr);
validator_ = std::move(statusOr).value();

if (validator_->noSpaceRequired()) {
setNoSpaceRequired();
}

NG_RETURN_IF_ERROR(validator_->validate());

auto outputs = validator_->outputCols();
Expand Down
9 changes: 1 addition & 8 deletions src/graph/validator/AssignmentValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ namespace nebula {
namespace graph {
class AssignmentValidator final : public Validator {
public:
AssignmentValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {
auto* assignSentence = static_cast<AssignmentSentence*>(sentence_);
validator_ = makeValidator(assignSentence->sentence(), qctx_);

if (validator_->noSpaceRequired()) {
setNoSpaceRequired();
}
}
AssignmentValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {}

private:
Status validateImpl() override;
Expand Down
12 changes: 12 additions & 0 deletions src/graph/validator/PipeValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ namespace graph {
// Validate pipe sentence which combine two sentence by input/output, e.g. GO ... | LIMIT 3
// Set input, and call validator of sub-sentence.
Status PipeValidator::validateImpl() {
auto pipeSentence = static_cast<PipedSentence*>(sentence_);
auto statusOr = makeValidator(pipeSentence->left(), qctx_);
NG_RETURN_IF_ERROR(statusOr);
lValidator_ = std::move(statusOr).value();
statusOr = makeValidator(pipeSentence->right(), qctx_);
NG_RETURN_IF_ERROR(statusOr);
rValidator_ = std::move(statusOr).value();

if (lValidator_->noSpaceRequired() && rValidator_->noSpaceRequired()) {
setNoSpaceRequired();
}

lValidator_->setInputCols(std::move(inputs_));
lValidator_->setInputVarName(inputVarName_);
NG_RETURN_IF_ERROR(lValidator_->validate());
Expand Down
13 changes: 1 addition & 12 deletions src/graph/validator/PipeValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ namespace graph {

class PipeValidator final : public Validator {
public:
PipeValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {
auto pipeSentence = static_cast<PipedSentence*>(sentence_);
auto left = pipeSentence->left();
lValidator_ = makeValidator(left, qctx_);

auto right = pipeSentence->right();
rValidator_ = makeValidator(right, qctx_);

if (lValidator_->noSpaceRequired() && rValidator_->noSpaceRequired()) {
setNoSpaceRequired();
}
}
PipeValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {}

private:
Status validateImpl() override;
Expand Down
5 changes: 3 additions & 2 deletions src/graph/validator/SequentialValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace graph {
// Validator of sequential sentences which combine multiple sentences, e.g. GO ...; GO ...;
// Call validator of sub-sentences.
Status SequentialValidator::validateImpl() {
Status status;
if (sentence_->kind() != Sentence::Kind::kSequential) {
return Status::SemanticError(
"Sequential validator validates a SequentialSentences, but %ld is "
Expand All @@ -36,7 +35,9 @@ Status SequentialValidator::validateImpl() {

seqAstCtx_->startNode = StartNode::make(seqAstCtx_->qctx);
for (auto* sentence : sentences) {
auto validator = makeValidator(sentence, qctx_);
auto statusOr = makeValidator(sentence, qctx_);
NG_RETURN_IF_ERROR(statusOr);
auto validator = std::move(statusOr).value();
NG_RETURN_IF_ERROR(validator->validate());
seqAstCtx_->validators.emplace_back(std::move(validator));
}
Expand Down
12 changes: 12 additions & 0 deletions src/graph/validator/SetValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ namespace graph {
// Validate sub-sentences of SET statement.
// Check columns of sub-sentences are the same.
Status SetValidator::validateImpl() {
auto setSentence = static_cast<SetSentence*>(sentence_);
auto statusOr = makeValidator(setSentence->left(), qctx_);
NG_RETURN_IF_ERROR(statusOr);
lValidator_ = std::move(statusOr).value();
statusOr = makeValidator(setSentence->right(), qctx_);
NG_RETURN_IF_ERROR(statusOr);
rValidator_ = std::move(statusOr).value();

if (lValidator_->noSpaceRequired() && rValidator_->noSpaceRequired()) {
setNoSpaceRequired();
}

NG_RETURN_IF_ERROR(lValidator_->validate());
NG_RETURN_IF_ERROR(rValidator_->validate());

Expand Down
10 changes: 1 addition & 9 deletions src/graph/validator/SetValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ namespace graph {

class SetValidator final : public Validator {
public:
SetValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {
auto setSentence = static_cast<SetSentence*>(sentence_);
lValidator_ = makeValidator(setSentence->left(), qctx_);
rValidator_ = makeValidator(setSentence->right(), qctx_);

if (lValidator_->noSpaceRequired() && rValidator_->noSpaceRequired()) {
setNoSpaceRequired();
}
}
SetValidator(Sentence* sentence, QueryContext* context) : Validator(sentence, context) {}

private:
Status validateImpl() override;
Expand Down
19 changes: 16 additions & 3 deletions src/graph/validator/Validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@
#include "graph/visitor/DeduceTypeVisitor.h"
#include "graph/visitor/EvaluableExprVisitor.h"
#include "parser/Sentence.h"

DEFINE_uint32(max_statements,
1024,
"threshold for maximun number of statements that can be validate");
namespace nebula {
namespace graph {

thread_local uint32_t Validator::maxStatements_ = 0;

Validator::Validator(Sentence* sentence, QueryContext* qctx)
: sentence_(DCHECK_NOTNULL(sentence)),
qctx_(DCHECK_NOTNULL(qctx)),
vctx_(DCHECK_NOTNULL(qctx->vctx())) {}

// Create validator according to sentence type.
std::unique_ptr<Validator> Validator::makeValidator(Sentence* sentence, QueryContext* context) {
StatusOr<std::unique_ptr<Validator>> Validator::makeValidator(Sentence* sentence,
QueryContext* context) {
if (++maxStatements_ > FLAGS_max_statements) {
return Status::SemanticError(
"statements is too large (%d > %d).", maxStatements_, FLAGS_max_statements);
}
auto kind = sentence->kind();
switch (kind) {
case Sentence::Kind::kExplain:
Expand Down Expand Up @@ -281,14 +290,18 @@ Status Validator::validate(Sentence* sentence, QueryContext* qctx) {
qctx->vctx()->switchToSpace(std::move(spaceInfo));
}

auto validator = makeValidator(sentence, qctx);
auto statusOr = makeValidator(sentence, qctx);
NG_RETURN_IF_ERROR(statusOr);
auto validator = std::move(statusOr).value();
NG_RETURN_IF_ERROR(validator->validate());

auto root = validator->root();
if (!root) {
return Status::SemanticError("Get null plan from sequential validator");
}
qctx->plan()->setRoot(root);
// reset maxStatements
maxStatements_ = 0;
return Status::OK();
}

Expand Down
4 changes: 3 additions & 1 deletion src/graph/validator/Validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class Validator {
public:
virtual ~Validator() = default;

static std::unique_ptr<Validator> makeValidator(Sentence* sentence, QueryContext* context);
static StatusOr<std::unique_ptr<Validator>> makeValidator(Sentence* sentence,
QueryContext* context);

// validate will call `spaceChosen` -> `validateImpl` -> `checkPermission` ->
// `toPlan` in order
Expand Down Expand Up @@ -203,6 +204,7 @@ class Validator {
std::set<std::string> userDefinedVarNameList_;
// vid's Type
Value::Type vidType_;
static thread_local uint32_t maxStatements_;
};

} // namespace graph
Expand Down

0 comments on commit 3b299bd

Please sign in to comment.