Skip to content

Commit

Permalink
split new visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
heroicNeZha committed Dec 13, 2021
1 parent 80073ab commit f429029
Show file tree
Hide file tree
Showing 16 changed files with 373 additions and 88 deletions.
1 change: 1 addition & 0 deletions src/graph/validator/FetchEdgesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Status FetchEdgesValidator::validateYield(const YieldClause *yield) {
}
col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(col->expr()));
NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr()));
NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));

auto colExpr = col->expr();
auto typeStatus = deduceExprType(colExpr);
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/FetchVerticesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Status FetchVerticesValidator::validateYield(YieldClause *yield) {
}
col->setExpr(ExpressionUtils::rewriteLabelAttr2TagProp(col->expr()));
NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr()));
NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));

auto colExpr = col->expr();
auto typeStatus = deduceExprType(colExpr);
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/FindPathValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Status FindPathValidator::validateWhere(WhereClause* where) {
}
where->setFilter(ExpressionUtils::rewriteLabelAttr2EdgeProp(expr));
auto filter = where->filter();
NG_RETURN_IF_ERROR(checkExprDepth(filter));

auto typeStatus = deduceExprType(filter);
NG_RETURN_IF_ERROR(typeStatus);
Expand Down
2 changes: 2 additions & 0 deletions src/graph/validator/GoValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Status GoValidator::validateWhere(WhereClause* where) {
NG_RETURN_IF_ERROR(foldRes);

auto filter = foldRes.value();
NG_RETURN_IF_ERROR(checkExprDepth(filter));
auto typeStatus = deduceExprType(filter);
NG_RETURN_IF_ERROR(typeStatus);
auto type = typeStatus.value();
Expand Down Expand Up @@ -141,6 +142,7 @@ Status GoValidator::validateYield(YieldClause* yield) {

col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(col->expr()));
NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr()));
NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));

auto* colExpr = col->expr();
if (ExpressionUtils::hasAny(colExpr, {Expression::Kind::kEdge})) {
Expand Down
2 changes: 2 additions & 0 deletions src/graph/validator/GroupByValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Status GroupByValidator::validateGroup(const GroupClause* groupClause) {
return Status::SemanticError("Group `%s' invalid", col->expr()->toString().c_str());
}

NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));
NG_RETURN_IF_ERROR(deduceExprType(col->expr()));
NG_RETURN_IF_ERROR(deduceProps(col->expr(), exprProps_));

Expand Down Expand Up @@ -145,6 +146,7 @@ Status GroupByValidator::groupClauseSemanticCheck() {
// deduce group items and build outputs_
DCHECK_EQ(aggOutputColNames_.size(), groupItems_.size());
for (auto i = 0u; i < groupItems_.size(); ++i) {
NG_RETURN_IF_ERROR(checkExprDepth(groupItems_[i]));
auto type = deduceExprType(groupItems_[i]);
NG_RETURN_IF_ERROR(type);
outputs_.emplace_back(aggOutputColNames_[i], std::move(type).value());
Expand Down
3 changes: 3 additions & 0 deletions src/graph/validator/LookupValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Status LookupValidator::validateYieldEdge() {
}
col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(col->expr()));
NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr()));
NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));

auto colExpr = col->expr();
auto typeStatus = deduceExprType(colExpr);
Expand Down Expand Up @@ -148,6 +149,7 @@ Status LookupValidator::validateYieldTag() {
}
col->setExpr(ExpressionUtils::rewriteLabelAttr2TagProp(col->expr()));
NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr()));
NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));

auto colExpr = col->expr();
auto typeStatus = deduceExprType(colExpr);
Expand Down Expand Up @@ -209,6 +211,7 @@ Status LookupValidator::validateFilter() {
NG_RETURN_IF_ERROR(ret);
lookupCtx_->filter = std::move(ret).value();
// Make sure the type of the rewritted filter expr is right
NG_RETURN_IF_ERROR(checkExprDepth(lookupCtx_->filter));
NG_RETURN_IF_ERROR(deduceExprType(lookupCtx_->filter));
}
NG_RETURN_IF_ERROR(deduceProps(lookupCtx_->filter, exprProps_));
Expand Down
2 changes: 2 additions & 0 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ Status MatchValidator::validateFilter(const Expression *filter,
auto transformRes = ExpressionUtils::filterTransform(filter);
NG_RETURN_IF_ERROR(transformRes);
whereClauseCtx.filter = transformRes.value();
NG_RETURN_IF_ERROR(checkExprDepth(whereClauseCtx.filter));

auto typeStatus = deduceExprType(whereClauseCtx.filter);
NG_RETURN_IF_ERROR(typeStatus);
Expand Down Expand Up @@ -899,6 +900,7 @@ Status MatchValidator::checkAlias(
Status MatchValidator::buildOutputs(const YieldColumns *yields) {
for (auto *col : yields->columns()) {
auto colName = col->name();
NG_RETURN_IF_ERROR(checkExprDepth(col->expr()));
auto typeStatus = deduceExprType(col->expr());
NG_RETURN_IF_ERROR(typeStatus);
auto type = typeStatus.value();
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/MutateValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ Status UpdateValidator::getCondition() {
if (symExpr != nullptr) {
filter = symExpr;
}
NG_RETURN_IF_ERROR(checkExprDepth(filter));
auto typeStatus = deduceExprType(filter);
NG_RETURN_IF_ERROR(typeStatus);
auto type = typeStatus.value();
Expand Down
7 changes: 7 additions & 0 deletions src/graph/validator/Validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "graph/validator/SetValidator.h"
#include "graph/validator/UseValidator.h"
#include "graph/validator/YieldValidator.h"
#include "graph/visitor/CheckDepthVisitor.h"
#include "graph/visitor/DeduceTypeVisitor.h"
#include "graph/visitor/EvaluableExprVisitor.h"
#include "parser/Sentence.h"
Expand Down Expand Up @@ -352,6 +353,12 @@ StatusOr<Value::Type> Validator::deduceExprType(const Expression* expr) const {
return visitor.type();
}

Status Validator::checkExprDepth(const Expression* expr) const {
CheckDepthVisitor visitor;
const_cast<Expression*>(expr)->accept(&visitor);
return std::move(visitor).status();
}

Status Validator::deduceProps(const Expression* expr, ExpressionProps& exprProps) {
DeducePropsVisitor visitor(qctx_, space_.id, &exprProps, &userDefinedVarNameList_);
const_cast<Expression*>(expr)->accept(&visitor);
Expand Down
2 changes: 2 additions & 0 deletions src/graph/validator/Validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class Validator {

StatusOr<Value::Type> deduceExprType(const Expression* expr) const;

Status checkExprDepth(const Expression* expr) const;

Status deduceProps(const Expression* expr, ExpressionProps& exprProps);

static StatusOr<size_t> checkPropNonexistOrDuplicate(const ColsDef& cols,
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/YieldValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Status YieldValidator::makeOutputColumn(YieldColumn *column) {
DCHECK(colExpr != nullptr);

auto expr = colExpr->clone();
NG_RETURN_IF_ERROR(checkExprDepth(expr));
NG_RETURN_IF_ERROR(deduceProps(expr, exprProps_));

auto status = deduceExprType(expr);
Expand Down
1 change: 1 addition & 0 deletions src/graph/visitor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nebula_add_library(
RewriteVisitor.cpp
FindVisitor.cpp
VidExtractVisitor.cpp
CheckDepthVisitor.cpp
)

nebula_add_subdirectory(test)
249 changes: 249 additions & 0 deletions src/graph/visitor/CheckDepthVisitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "graph/visitor/CheckDepthVisitor.h"

#include <sstream>
#include <unordered_map>

#include "common/datatypes/DataSet.h"
#include "common/datatypes/Edge.h"
#include "common/datatypes/List.h"
#include "common/datatypes/Map.h"
#include "common/datatypes/Path.h"
#include "common/datatypes/Set.h"
#include "common/function/FunctionManager.h"
#include "graph/context/QueryContext.h"
#include "graph/context/QueryExpressionContext.h"
#include "graph/context/ValidateContext.h"
#include "graph/util/SchemaUtil.h"
#include "graph/visitor/EvaluableExprVisitor.h"

namespace nebula {
namespace graph {

CheckDepthVisitor::CheckDepthVisitor() {}

void CheckDepthVisitor::visit(ConstantExpression *expr) {}

void CheckDepthVisitor::visit(UnaryExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->operand()->accept(this);
}

void CheckDepthVisitor::visit(TypeCastingExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->operand()->accept(this);
if (!ok()) return;
}

void CheckDepthVisitor::visit(LabelExpression *) {}

void CheckDepthVisitor::visit(ArithmeticExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->left()->accept(this);
if (!ok()) return;
expr->right()->accept(this);
}

void CheckDepthVisitor::visit(RelationalExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->left()->accept(this);
if (!ok()) return;
expr->right()->accept(this);
}

void CheckDepthVisitor::visit(SubscriptExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->left()->accept(this);
if (!ok()) return;
expr->right()->accept(this);
}

void CheckDepthVisitor::visit(AttributeExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->left()->accept(this);
if (!ok()) return;
expr->right()->accept(this);
}

void CheckDepthVisitor::visit(LogicalExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
auto &operands = expr->operands();
for (auto i = 0u; i < operands.size(); i++) {
operands[i]->accept(this);
if (!ok()) return;
}
}

void CheckDepthVisitor::visit(LabelAttributeExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
const_cast<LabelExpression *>(expr->left())->accept(this);
if (!ok()) return;
const_cast<ConstantExpression *>(expr->right())->accept(this);
}

void CheckDepthVisitor::visit(FunctionCallExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
for (auto &arg : expr->args()->args()) {
arg->accept(this);
if (!ok()) return;
}
}

void CheckDepthVisitor::visit(AggregateExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->arg()->accept(this);
}

void CheckDepthVisitor::visit(CaseExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
if (expr->hasCondition()) {
expr->condition()->accept(this);
if (!ok()) return;
}
if (expr->hasDefault()) {
expr->defaultResult()->accept(this);
if (!ok()) return;
}
for (const auto &whenThen : expr->cases()) {
whenThen.when->accept(this);
if (!ok()) return;
whenThen.then->accept(this);
if (!ok()) return;
}
}

void CheckDepthVisitor::visit(PredicateExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
if (expr->hasFilter()) {
expr->filter()->accept(this);
if (!ok()) {
return;
}
}
expr->collection()->accept(this);
}

void CheckDepthVisitor::visit(ListComprehensionExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
if (expr->hasFilter()) {
expr->filter()->accept(this);
if (!ok()) {
return;
}
}
if (expr->hasMapping()) {
expr->mapping()->accept(this);
if (!ok()) {
return;
}
}
expr->collection()->accept(this);
if (!ok()) {
return;
}
}

void CheckDepthVisitor::visit(ReduceExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->initial()->accept(this);
if (!ok()) return;
expr->mapping()->accept(this);
if (!ok()) return;
expr->collection()->accept(this);
if (!ok()) return;
}

void CheckDepthVisitor::visit(SubscriptRangeExpression *expr) {
checkDepth();
SCOPE_EXIT { recoverDepth(); };
if (!ok()) return;
expr->list()->accept(this);
if (!ok()) {
return;
}
if (expr->lo() != nullptr) {
expr->lo()->accept(this);
if (!ok()) {
return;
}
}

if (expr->hi() != nullptr) {
expr->hi()->accept(this);
if (!ok()) {
return;
}
}
}

void CheckDepthVisitor::visit(UUIDExpression *) {}

void CheckDepthVisitor::visit(VariableExpression *) {}

void CheckDepthVisitor::visit(VersionedVariableExpression *) {}

void CheckDepthVisitor::visit(ListExpression *) {}

void CheckDepthVisitor::visit(SetExpression *) {}

void CheckDepthVisitor::visit(MapExpression *) {}

void CheckDepthVisitor::visit(TagPropertyExpression *expr) {}

void CheckDepthVisitor::visit(EdgePropertyExpression *expr) {}

void CheckDepthVisitor::visit(VariablePropertyExpression *expr) {}

void CheckDepthVisitor::visit(DestPropertyExpression *expr) {}

void CheckDepthVisitor::visit(SourcePropertyExpression *expr) {}

void CheckDepthVisitor::visit(EdgeSrcIdExpression *) {}

void CheckDepthVisitor::visit(EdgeTypeExpression *) {}

void CheckDepthVisitor::visit(EdgeRankExpression *) {}

void CheckDepthVisitor::visit(EdgeDstIdExpression *) {}

void CheckDepthVisitor::visit(VertexExpression *) {}

void CheckDepthVisitor::visit(EdgeExpression *) {}

void CheckDepthVisitor::visit(ColumnExpression *) {}

void CheckDepthVisitor::visit(PathBuildExpression *) {}
} // namespace graph
} // namespace nebula
Loading

0 comments on commit f429029

Please sign in to comment.