Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments to util module #3884

Merged
merged 7 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/graph/util/AnonColGenerator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
// Copyright (c) 2020 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

#ifndef GRAPH_UTIL_ANONCOLGENERATOR_H_
#define GRAPH_UTIL_ANONCOLGENERATOR_H_
Expand All @@ -19,9 +18,7 @@ constexpr char kEdgesStr[] = "_edges";
constexpr char kPathStr[] = "_path";
constexpr char kCostStr[] = "_cost";

/**
* An utility to generate an anonymous column name.
*/
// An utility to generate an anonymous column name.
class AnonColGenerator final {
public:
AnonColGenerator() {
Expand Down
7 changes: 3 additions & 4 deletions src/graph/util/AnonVarGenerator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
// Copyright (c) 2020 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

#ifndef GRAPH_UTIL_ANONVARGENERATOR_H_
#define GRAPH_UTIL_ANONVARGENERATOR_H_
Expand Down
8 changes: 4 additions & 4 deletions src/graph/util/AstUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
// Copyright (c) 2020 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

#ifndef GRAPH_UTIL_ASTUTIL_H_
#define GRAPH_UTIL_ASTUTIL_H_
Expand All @@ -11,6 +10,7 @@
namespace nebula {
namespace graph {

// Used to test GQL parser
class AstUtils final {
public:
explicit AstUtils(...) = delete;
Expand Down
33 changes: 0 additions & 33 deletions src/graph/util/ContainerConv.h

This file was deleted.

124 changes: 35 additions & 89 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
// Copyright (c) 2021 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

#include "graph/util/ExpressionUtils.h"

#include <memory>
#include <queue>
#include <unordered_set>

#include "common/base/ObjectPool.h"
#include "common/expression/ArithmeticExpression.h"
#include "common/expression/Expression.h"
Expand All @@ -25,16 +20,14 @@ namespace nebula {
namespace graph {

bool ExpressionUtils::isPropertyExpr(const Expression *expr) {
auto kind = expr->kind();

return std::unordered_set<Expression::Kind>{Expression::Kind::kTagProperty,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kInputProperty,
Expression::Kind::kVarProperty,
Expression::Kind::kDstProperty,
Expression::Kind::kSrcProperty}
.count(kind);
return isKindOf(expr,
{Expression::Kind::kTagProperty,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kInputProperty,
Expression::Kind::kVarProperty,
Expression::Kind::kDstProperty,
Expression::Kind::kSrcProperty});
}

const Expression *ExpressionUtils::findAny(const Expression *self,
Expand All @@ -57,8 +50,8 @@ const Expression *ExpressionUtils::findAny(const Expression *self,
return nullptr;
}

// Find all expression fit any kind
// Empty for not found any one
// Finds all expressions fit the exprected list
// Returns an empty vector if no expression found
std::vector<const Expression *> ExpressionUtils::collectAll(
const Expression *self, const std::unordered_set<Expression::Kind> &expected) {
auto finder = [&expected](const Expression *expr) -> bool {
Expand All @@ -83,52 +76,12 @@ bool ExpressionUtils::checkVarExprIfExist(const Expression *expr, const QueryCon
return false;
}

std::vector<const Expression *> ExpressionUtils::findAllStorage(const Expression *expr) {
return collectAll(expr,
{Expression::Kind::kTagProperty,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kDstProperty,
Expression::Kind::kSrcProperty,
Expression::Kind::kEdgeSrc,
Expression::Kind::kEdgeType,
Expression::Kind::kEdgeRank,
Expression::Kind::kEdgeDst,
Expression::Kind::kVertex,
Expression::Kind::kEdge});
}

std::vector<const Expression *> ExpressionUtils::findAllInputVariableProp(const Expression *expr) {
return collectAll(expr, {Expression::Kind::kInputProperty, Expression::Kind::kVarProperty});
}

bool ExpressionUtils::isConstExpr(const Expression *expr) {
return !hasAny(expr,
{Expression::Kind::kInputProperty,
Expression::Kind::kVarProperty,
Expression::Kind::kVar,
Expression::Kind::kVersionedVar,
Expression::Kind::kLabelAttribute,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kDstProperty,
Expression::Kind::kSrcProperty,
Expression::Kind::kEdgeSrc,
Expression::Kind::kEdgeType,
Expression::Kind::kEdgeRank,
Expression::Kind::kEdgeDst,
Expression::Kind::kVertex,
Expression::Kind::kEdge});
}

bool ExpressionUtils::isEvaluableExpr(const Expression *expr, const QueryContext *qctx) {
EvaluableExprVisitor visitor(qctx);
const_cast<Expression *>(expr)->accept(&visitor);
return visitor.ok();
}

// rewrite Attribute to LabelTagProp
Expression *ExpressionUtils::rewriteAttr2LabelTagProp(
const Expression *expr, const std::unordered_map<std::string, AliasType> &aliasTypeMap) {
ObjectPool *pool = expr->getObjPool();
Expand Down Expand Up @@ -166,7 +119,21 @@ Expression *ExpressionUtils::rewriteAttr2LabelTagProp(
return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

// rewrite LabelAttr to EdgeProp
Expression *ExpressionUtils::rewriteLabelAttr2TagProp(const Expression *expr) {
ObjectPool *pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
return e->kind() == Expression::Kind::kLabelAttribute;
};
auto rewriter = [pool](const Expression *e) -> Expression * {
auto labelAttrExpr = static_cast<const LabelAttributeExpression *>(e);
auto leftName = labelAttrExpr->left()->name();
auto rightName = labelAttrExpr->right()->value().getStr();
return TagPropertyExpression::make(pool, leftName, rightName);
};

return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

Expression *ExpressionUtils::rewriteLabelAttr2EdgeProp(const Expression *expr) {
ObjectPool *pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
Expand All @@ -182,7 +149,6 @@ Expression *ExpressionUtils::rewriteLabelAttr2EdgeProp(const Expression *expr) {
return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

// rewrite var in VariablePropExpr to another var
Expression *ExpressionUtils::rewriteInnerVar(const Expression *expr, std::string newVar) {
ObjectPool *pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
Expand All @@ -197,7 +163,6 @@ Expression *ExpressionUtils::rewriteInnerVar(const Expression *expr, std::string
return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

// rewrite parameter to Constant
Expression *ExpressionUtils::rewriteParameter(const Expression *expr, QueryContext *qctx) {
auto matcher = [qctx](const Expression *e) -> bool {
return e->kind() == Expression::Kind::kVar &&
Expand All @@ -212,23 +177,6 @@ Expression *ExpressionUtils::rewriteParameter(const Expression *expr, QueryConte
return graph::RewriteVisitor::transform(expr, matcher, rewriter);
}

// rewrite LabelAttr to tagProp
Expression *ExpressionUtils::rewriteLabelAttr2TagProp(const Expression *expr) {
ObjectPool *pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
return e->kind() == Expression::Kind::kLabelAttribute;
};
auto rewriter = [pool](const Expression *e) -> Expression * {
auto labelAttrExpr = static_cast<const LabelAttributeExpression *>(e);
auto leftName = labelAttrExpr->left()->name();
auto rightName = labelAttrExpr->right()->value().getStr();
return TagPropertyExpression::make(pool, leftName, rightName);
};

return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

// rewrite Agg to VarProp
Expression *ExpressionUtils::rewriteAgg2VarProp(const Expression *expr) {
ObjectPool *pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
Expand Down Expand Up @@ -449,10 +397,10 @@ Expression *ExpressionUtils::rewriteRelExpr(const Expression *expr) {
// Match relational expressions following these rules:
// 1. the right operand of rel expr should be evaluable
// 2. the left operand of rel expr should be:
// 2.a an arithmetic expr that does not contains string and has at least one operand that is
// - 2.a an arithmetic expr that does not contains string and has at least one operand that is
// evaluable
// OR
// 2.b an relational expr so that it might could be simplified:
// - 2.b an relational expr so that it might could be simplified:
// ((v.age > 40 == true) => (v.age > 40))
auto matcher = [&checkArithmExpr](const Expression *e) -> bool {
if (!e->isRelExpr()) {
Expand Down Expand Up @@ -703,11 +651,11 @@ void ExpressionUtils::splitFilter(const Expression *expr,
auto filterUnpickedPtr = LogicalExpression::makeAnd(pool);

std::vector<Expression *> &operands = logicExpr->operands();
for (auto iter = operands.begin(); iter != operands.end(); ++iter) {
if (picker((*iter))) {
filterPickedPtr->addOperand((*iter)->clone());
for (auto &operand : operands) {
if (picker(operand)) {
filterPickedPtr->addOperand(operand->clone());
} else {
filterUnpickedPtr->addOperand((*iter)->clone());
filterUnpickedPtr->addOperand(operand->clone());
}
}
auto foldLogicalExpr = [](const LogicalExpression *e) -> Expression * {
Expand Down Expand Up @@ -850,9 +798,7 @@ std::vector<Expression *> ExpressionUtils::expandImplOr(const Expression *expr)
}

Status ExpressionUtils::checkAggExpr(const AggregateExpression *aggExpr) {
auto func = aggExpr->name();
std::transform(func.begin(), func.end(), func.begin(), ::toupper);

const auto &func = aggExpr->name();
NG_RETURN_IF_ERROR(AggFunctionManager::find(func));

auto *aggArg = aggExpr->arg();
Expand Down
Loading