From f42902987a19f72b00b4039abaad38778d690475 Mon Sep 17 00:00:00 2001 From: heroicNeZha <25311962+heroicNeZha@users.noreply.github.com> Date: Mon, 13 Dec 2021 14:37:50 +0800 Subject: [PATCH] split new visitor --- src/graph/validator/FetchEdgesValidator.cpp | 1 + .../validator/FetchVerticesValidator.cpp | 1 + src/graph/validator/FindPathValidator.cpp | 1 + src/graph/validator/GoValidator.cpp | 2 + src/graph/validator/GroupByValidator.cpp | 2 + src/graph/validator/LookupValidator.cpp | 3 + src/graph/validator/MatchValidator.cpp | 2 + src/graph/validator/MutateValidator.cpp | 1 + src/graph/validator/Validator.cpp | 7 + src/graph/validator/Validator.h | 2 + src/graph/validator/YieldValidator.cpp | 1 + src/graph/visitor/CMakeLists.txt | 1 + src/graph/visitor/CheckDepthVisitor.cpp | 249 ++++++++++++++++++ src/graph/visitor/CheckDepthVisitor.h | 99 +++++++ src/graph/visitor/DeduceTypeVisitor.cpp | 77 +----- src/graph/visitor/DeduceTypeVisitor.h | 12 - 16 files changed, 373 insertions(+), 88 deletions(-) create mode 100644 src/graph/visitor/CheckDepthVisitor.cpp create mode 100644 src/graph/visitor/CheckDepthVisitor.h diff --git a/src/graph/validator/FetchEdgesValidator.cpp b/src/graph/validator/FetchEdgesValidator.cpp index d0f5c0a2b4e..817f2e14e48 100644 --- a/src/graph/validator/FetchEdgesValidator.cpp +++ b/src/graph/validator/FetchEdgesValidator.cpp @@ -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); diff --git a/src/graph/validator/FetchVerticesValidator.cpp b/src/graph/validator/FetchVerticesValidator.cpp index dd902d79002..43748935087 100644 --- a/src/graph/validator/FetchVerticesValidator.cpp +++ b/src/graph/validator/FetchVerticesValidator.cpp @@ -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); diff --git a/src/graph/validator/FindPathValidator.cpp b/src/graph/validator/FindPathValidator.cpp index bfd39c137d6..585ee1959f0 100644 --- a/src/graph/validator/FindPathValidator.cpp +++ b/src/graph/validator/FindPathValidator.cpp @@ -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); diff --git a/src/graph/validator/GoValidator.cpp b/src/graph/validator/GoValidator.cpp index 1866f66e9a8..4bc801984c0 100644 --- a/src/graph/validator/GoValidator.cpp +++ b/src/graph/validator/GoValidator.cpp @@ -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(); @@ -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})) { diff --git a/src/graph/validator/GroupByValidator.cpp b/src/graph/validator/GroupByValidator.cpp index 1c12f217450..6c19d9c2509 100644 --- a/src/graph/validator/GroupByValidator.cpp +++ b/src/graph/validator/GroupByValidator.cpp @@ -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_)); @@ -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()); diff --git a/src/graph/validator/LookupValidator.cpp b/src/graph/validator/LookupValidator.cpp index 6426b8975fc..2bb732c57b6 100644 --- a/src/graph/validator/LookupValidator.cpp +++ b/src/graph/validator/LookupValidator.cpp @@ -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); @@ -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); @@ -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_)); diff --git a/src/graph/validator/MatchValidator.cpp b/src/graph/validator/MatchValidator.cpp index 0327cd3411e..829ea56991c 100644 --- a/src/graph/validator/MatchValidator.cpp +++ b/src/graph/validator/MatchValidator.cpp @@ -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); @@ -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(); diff --git a/src/graph/validator/MutateValidator.cpp b/src/graph/validator/MutateValidator.cpp index 9fbb5500a85..80717144bcb 100644 --- a/src/graph/validator/MutateValidator.cpp +++ b/src/graph/validator/MutateValidator.cpp @@ -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(); diff --git a/src/graph/validator/Validator.cpp b/src/graph/validator/Validator.cpp index 687d4731492..f7375fdc6bd 100644 --- a/src/graph/validator/Validator.cpp +++ b/src/graph/validator/Validator.cpp @@ -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" @@ -352,6 +353,12 @@ StatusOr Validator::deduceExprType(const Expression* expr) const { return visitor.type(); } +Status Validator::checkExprDepth(const Expression* expr) const { + CheckDepthVisitor visitor; + const_cast(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(expr)->accept(&visitor); diff --git a/src/graph/validator/Validator.h b/src/graph/validator/Validator.h index 0568d0eb054..7d1000c4228 100644 --- a/src/graph/validator/Validator.h +++ b/src/graph/validator/Validator.h @@ -107,6 +107,8 @@ class Validator { StatusOr deduceExprType(const Expression* expr) const; + Status checkExprDepth(const Expression* expr) const; + Status deduceProps(const Expression* expr, ExpressionProps& exprProps); static StatusOr checkPropNonexistOrDuplicate(const ColsDef& cols, diff --git a/src/graph/validator/YieldValidator.cpp b/src/graph/validator/YieldValidator.cpp index 901774e8697..4a780bb0b70 100644 --- a/src/graph/validator/YieldValidator.cpp +++ b/src/graph/validator/YieldValidator.cpp @@ -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); diff --git a/src/graph/visitor/CMakeLists.txt b/src/graph/visitor/CMakeLists.txt index bce16e2f074..09d7695f0a6 100644 --- a/src/graph/visitor/CMakeLists.txt +++ b/src/graph/visitor/CMakeLists.txt @@ -14,6 +14,7 @@ nebula_add_library( RewriteVisitor.cpp FindVisitor.cpp VidExtractVisitor.cpp + CheckDepthVisitor.cpp ) nebula_add_subdirectory(test) diff --git a/src/graph/visitor/CheckDepthVisitor.cpp b/src/graph/visitor/CheckDepthVisitor.cpp new file mode 100644 index 00000000000..23263560e55 --- /dev/null +++ b/src/graph/visitor/CheckDepthVisitor.cpp @@ -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 +#include + +#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(expr->left())->accept(this); + if (!ok()) return; + const_cast(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 diff --git a/src/graph/visitor/CheckDepthVisitor.h b/src/graph/visitor/CheckDepthVisitor.h new file mode 100644 index 00000000000..baf137fb049 --- /dev/null +++ b/src/graph/visitor/CheckDepthVisitor.h @@ -0,0 +1,99 @@ +/* Copyright (c) 2020 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License. + */ + +#ifndef GRAPH_VISITOR_CHECKDEPTHVISITOR_H_ +#define GRAPH_VISITOR_CHECKDEPTHVISITOR_H_ + +#include "common/base/Status.h" +#include "common/datatypes/Value.h" +#include "common/expression/ExprVisitor.h" +#include "graph/context/ValidateContext.h" + +namespace nebula { +namespace graph { + +class QueryContext; + +class CheckDepthVisitor final : public ExprVisitor { + public: + CheckDepthVisitor(); + ~CheckDepthVisitor() = default; + + bool ok() const { return status_.ok(); } + + Status status() && { return std::move(status_); } + + private: + void visit(ConstantExpression *expr) override; + void visit(UnaryExpression *expr) override; + void visit(TypeCastingExpression *expr) override; + void visit(LabelExpression *expr) override; + void visit(LabelAttributeExpression *expr) override; + // binary expression + void visit(ArithmeticExpression *expr) override; + void visit(RelationalExpression *expr) override; + void visit(SubscriptExpression *expr) override; + void visit(AttributeExpression *expr) override; + void visit(LogicalExpression *expr) override; + // function call + void visit(FunctionCallExpression *expr) override; + void visit(AggregateExpression *expr) override; + void visit(UUIDExpression *expr) override; + // variable expression + void visit(VariableExpression *expr) override; + void visit(VersionedVariableExpression *expr) override; + // container expression + void visit(ListExpression *expr) override; + void visit(SetExpression *expr) override; + void visit(MapExpression *expr) override; + // property Expression + void visit(TagPropertyExpression *expr) override; + void visit(EdgePropertyExpression *expr) override; + void visit(InputPropertyExpression *expr) override; + void visit(VariablePropertyExpression *expr) override; + void visit(DestPropertyExpression *expr) override; + void visit(SourcePropertyExpression *expr) override; + void visit(EdgeSrcIdExpression *expr) override; + void visit(EdgeTypeExpression *expr) override; + void visit(EdgeRankExpression *expr) override; + void visit(EdgeDstIdExpression *expr) override; + // vertex/edge expression + void visit(VertexExpression *expr) override; + void visit(EdgeExpression *expr) override; + // case expression + void visit(CaseExpression *expr) override; + // path build expression + void visit(PathBuildExpression *expr) override; + // column expression + void visit(ColumnExpression *expr) override; + // predicate expression + void visit(PredicateExpression *expr) override; + // list comprehension expression + void visit(ListComprehensionExpression *) override; + // reduce expression + void visit(ReduceExpression *expr) override; + // subscript range + void visit(SubscriptRangeExpression *expr) override; + + inline void checkDepth() { + if (++depth > MAX_DEPTH) { + status_ = Status::SemanticError( + "The above expression is not a valid expression, " + "because its depth exceeds the maximum depth"); + } + } + + inline void recoverDepth() { --depth; } + + Status status_; + + int32_t depth = 0; + const int32_t MAX_DEPTH = 512; +}; + +} // namespace graph +} // namespace nebula + +#endif // GRAPH_VISITOR_CHECKDEPTHVISITOR_H_ diff --git a/src/graph/visitor/DeduceTypeVisitor.cpp b/src/graph/visitor/DeduceTypeVisitor.cpp index a711b36db1c..7421bc0e0bc 100644 --- a/src/graph/visitor/DeduceTypeVisitor.cpp +++ b/src/graph/visitor/DeduceTypeVisitor.cpp @@ -116,10 +116,6 @@ void DeduceTypeVisitor::visit(ConstantExpression *expr) { } void DeduceTypeVisitor::visit(UnaryExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->operand()->accept(this); if (!ok()) return; switch (expr->kind()) { @@ -178,14 +174,9 @@ void DeduceTypeVisitor::visit(UnaryExpression *expr) { break; } } - recoverDepth(); } void DeduceTypeVisitor::visit(TypeCastingExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->operand()->accept(this); if (!ok()) return; @@ -217,16 +208,11 @@ void DeduceTypeVisitor::visit(TypeCastingExpression *expr) { } type_ = val.type(); status_ = Status::OK(); - recoverDepth(); } void DeduceTypeVisitor::visit(LabelExpression *) { type_ = Value::Type::__EMPTY__; } void DeduceTypeVisitor::visit(ArithmeticExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } switch (expr->kind()) { case Expression::Kind::kAdd: { DETECT_BIEXPR_TYPE(+); @@ -253,14 +239,9 @@ void DeduceTypeVisitor::visit(ArithmeticExpression *expr) { break; } } - recoverDepth(); } void DeduceTypeVisitor::visit(RelationalExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->left()->accept(this); if (!ok()) return; expr->right()->accept(this); @@ -278,14 +259,9 @@ void DeduceTypeVisitor::visit(RelationalExpression *expr) { } type_ = Value::Type::BOOL; - recoverDepth(); } void DeduceTypeVisitor::visit(SubscriptExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->left()->accept(this); if (!ok()) return; auto leftType = type_; @@ -341,14 +317,9 @@ void DeduceTypeVisitor::visit(SubscriptExpression *expr) { // Will not deduce the actual type of the value in list. type_ = Value::Type::__EMPTY__; - recoverDepth(); } void DeduceTypeVisitor::visit(AttributeExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->left()->accept(this); if (!ok()) return; switch (type_) { @@ -385,14 +356,9 @@ void DeduceTypeVisitor::visit(AttributeExpression *expr) { // Will not deduce the actual type of the attribute. type_ = Value::Type::__EMPTY__; - recoverDepth(); } void DeduceTypeVisitor::visit(LogicalExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } switch (expr->kind()) { case Expression::Kind::kLogicalAnd: { DETECT_NARYEXPR_TYPE(&&); @@ -408,14 +374,9 @@ void DeduceTypeVisitor::visit(LogicalExpression *expr) { break; } } - recoverDepth(); } void DeduceTypeVisitor::visit(LabelAttributeExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } const_cast(expr->left())->accept(this); if (!ok()) return; if (type_ != Value::Type::STRING && !isSuperiorType(type_)) { @@ -438,14 +399,9 @@ void DeduceTypeVisitor::visit(LabelAttributeExpression *expr) { // Will not deduce the actual type of the attribute. type_ = Value::Type::__EMPTY__; - recoverDepth(); } void DeduceTypeVisitor::visit(FunctionCallExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } std::vector argsTypeList; argsTypeList.reserve(expr->args()->numArgs()); for (auto &arg : expr->args()->args()) { @@ -466,18 +422,12 @@ void DeduceTypeVisitor::visit(FunctionCallExpression *expr) { return; } type_ = result.value(); - recoverDepth(); } void DeduceTypeVisitor::visit(AggregateExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->arg()->accept(this); if (!ok()) return; type_ = Value::Type::__EMPTY__; - recoverDepth(); } void DeduceTypeVisitor::visit(UUIDExpression *) { type_ = Value::Type::STRING; } @@ -574,10 +524,6 @@ void DeduceTypeVisitor::visit(EdgeExpression *) { type_ = Value::Type::EDGE; } void DeduceTypeVisitor::visit(ColumnExpression *) { type_ = Value::Type::__EMPTY__; } void DeduceTypeVisitor::visit(CaseExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } if (expr->hasCondition()) { expr->condition()->accept(this); if (!ok()) return; @@ -607,14 +553,9 @@ void DeduceTypeVisitor::visit(CaseExpression *expr) { } else { type_ = Value::Type::__EMPTY__; } - recoverDepth(); } void DeduceTypeVisitor::visit(PredicateExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } if (expr->hasFilter()) { expr->filter()->accept(this); if (!ok()) { @@ -635,14 +576,9 @@ void DeduceTypeVisitor::visit(PredicateExpression *expr) { } type_ = Value::Type::BOOL; - recoverDepth(); } void DeduceTypeVisitor::visit(ListComprehensionExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } if (expr->hasFilter()) { expr->filter()->accept(this); if (!ok()) { @@ -673,14 +609,9 @@ void DeduceTypeVisitor::visit(ListComprehensionExpression *expr) { } type_ = Value::Type::LIST; - recoverDepth(); } void DeduceTypeVisitor::visit(ReduceExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->initial()->accept(this); if (!ok()) return; expr->mapping()->accept(this); @@ -702,14 +633,9 @@ void DeduceTypeVisitor::visit(ReduceExpression *expr) { // Will not deduce the actual value type returned by reduce expression. type_ = Value::Type::__EMPTY__; - recoverDepth(); } void DeduceTypeVisitor::visit(SubscriptRangeExpression *expr) { - checkDepth(); - if (!status_.ok()) { - return; - } expr->list()->accept(this); if (!ok()) { return; @@ -745,7 +671,6 @@ void DeduceTypeVisitor::visit(SubscriptRangeExpression *expr) { } } type_ = Value::Type::LIST; - recoverDepth(); } void DeduceTypeVisitor::visitVertexPropertyExpr(PropertyExpression *expr) { @@ -775,6 +700,6 @@ void DeduceTypeVisitor::visit(PathBuildExpression *) { type_ = Value::Type::PATH #undef DETECT_NARYEXPR_TYPE #undef DETECT_UNARYEXPR_TYPE #undef DETECT_BIEXPR_TYPE -#undef CHECK_DEPTH + } // namespace graph } // namespace nebula diff --git a/src/graph/visitor/DeduceTypeVisitor.h b/src/graph/visitor/DeduceTypeVisitor.h index df319b471f6..8ffa078bcd7 100644 --- a/src/graph/visitor/DeduceTypeVisitor.h +++ b/src/graph/visitor/DeduceTypeVisitor.h @@ -92,16 +92,6 @@ class DeduceTypeVisitor final : public ExprVisitor { return type == Value::Type::NULLVALUE || type == Value::Type::__EMPTY__; } - inline void checkDepth() { - if (++depth > MAX_DEPTH) { - status_ = Status::SemanticError( - "The above expression is not a valid expression, " - "because its depth exceeds the maximum depth"); - } - } - - inline void recoverDepth() { --depth; } - const QueryContext *qctx_{nullptr}; const ValidateContext *vctx_{nullptr}; const ColsDef &inputs_; @@ -109,8 +99,6 @@ class DeduceTypeVisitor final : public ExprVisitor { Status status_; Value::Type type_; Value::Type vidType_; - int32_t depth = 0; - const int32_t MAX_DEPTH = 816; }; } // namespace graph