Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

fix util functions #1277

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions src/executor/test/FilterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ class FilterTest : public QueryTestBase {
#define FILTER_RESUTL_CHECK(inputName, outputName, sentence, expected) \
do { \
qctx_->symTable()->newVariable(outputName); \
auto* pool = qctx_->objPool(); \
auto yieldSentence = getYieldSentence(sentence, qctx_.get()); \
auto columns = yieldSentence->columns(); \
for (auto& col : columns) { \
col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(pool, col->expr())); \
col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(col->expr())); \
} \
auto* whereSentence = yieldSentence->where(); \
whereSentence->setFilter( \
ExpressionUtils::rewriteLabelAttr2EdgeProp(pool, whereSentence->filter())); \
ExpressionUtils::rewriteLabelAttr2EdgeProp(whereSentence->filter())); \
auto* filterNode = Filter::make(qctx_.get(), nullptr, yieldSentence->where()->filter()); \
filterNode->setInputVar(inputName); \
filterNode->setOutputVar(outputName); \
Expand Down
7 changes: 3 additions & 4 deletions src/optimizer/rule/PushFilterDownLeftJoinRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ StatusOr<OptRule::TransformResult> PushFilterDownLeftJoinRule::transform(
const std::pair<std::string, int64_t>& leftVar = oldLeftJoinNode->leftVar();
auto symTable = octx->qctx()->symTable();
std::vector<std::string> leftVarColNames = symTable->getVar(leftVar.first)->colNames;
auto objPool = octx->qctx()->objPool();

// split the `condition` based on whether the varPropExpr comes from the left child
auto picker = [&leftVarColNames](const Expression* e) -> bool {
Expand All @@ -73,7 +72,7 @@ StatusOr<OptRule::TransformResult> PushFilterDownLeftJoinRule::transform(
};
Expression* filterPicked = nullptr;
Expression* filterUnpicked = nullptr;
graph::ExpressionUtils::splitFilter(objPool, condition, picker, &filterPicked, &filterUnpicked);
graph::ExpressionUtils::splitFilter(condition, picker, &filterPicked, &filterUnpicked);

if (!filterPicked) {
return TransformResult::noTransform();
Expand All @@ -83,7 +82,7 @@ StatusOr<OptRule::TransformResult> PushFilterDownLeftJoinRule::transform(
auto* newLeftFilterNode = graph::Filter::make(
octx->qctx(),
const_cast<graph::PlanNode*>(oldLeftJoinNode->dep()),
graph::ExpressionUtils::rewriteInnerVar(objPool, filterPicked, leftVar.first));
graph::ExpressionUtils::rewriteInnerVar(filterPicked, leftVar.first));
newLeftFilterNode->setInputVar(leftVar.first);
newLeftFilterNode->setColNames(leftVarColNames);
auto newFilterGroup = OptGroup::create(octx);
Expand All @@ -100,7 +99,7 @@ StatusOr<OptRule::TransformResult> PushFilterDownLeftJoinRule::transform(
std::vector<Expression*> newHashKeys;
for (auto* k : hashKeys) {
newHashKeys.emplace_back(
graph::ExpressionUtils::rewriteInnerVar(objPool, k, newLeftFilterOutputVar));
graph::ExpressionUtils::rewriteInnerVar(k, newLeftFilterOutputVar));
}
newLeftJoinNode->setHashKeys(newHashKeys);

Expand Down
3 changes: 1 addition & 2 deletions src/optimizer/rule/PushFilterDownProjectRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ StatusOr<OptRule::TransformResult> PushFilterDownProjectRule::transform(
DCHECK_EQ(projNode->kind(), PlanNode::Kind::kProject);
const auto* oldProjNode = static_cast<const graph::Project*>(projNode);
const auto* condition = oldFilterNode->condition();
auto objPool = octx->qctx()->objPool();

auto projColNames = oldProjNode->colNames();
auto projColumns = oldProjNode->columns()->columns();
Expand Down Expand Up @@ -86,7 +85,7 @@ StatusOr<OptRule::TransformResult> PushFilterDownProjectRule::transform(
};
Expression* filterPicked = nullptr;
Expression* filterUnpicked = nullptr;
graph::ExpressionUtils::splitFilter(objPool, condition, picker, &filterPicked, &filterUnpicked);
graph::ExpressionUtils::splitFilter(condition, picker, &filterPicked, &filterUnpicked);

if (!filterPicked) {
return TransformResult::noTransform();
Expand Down
3 changes: 1 addition & 2 deletions src/planner/match/LabelIndexSeek.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ StatusOr<SubPlan> LabelIndexSeek::transformNode(NodeContext* nodeCtx) {
}
}
if (canBeEmbeded2IndexScan) {
auto* srcFilter =
ExpressionUtils::rewriteLabelAttr2TagProp(pool, flattenFilter);
auto* srcFilter = ExpressionUtils::rewriteLabelAttr2TagProp(flattenFilter);
storage::cpp2::IndexQueryContext ctx;
ctx.set_filter(Expression::encode(*srcFilter));
scan->setIndexQueryContext({ctx});
Expand Down
64 changes: 34 additions & 30 deletions src/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ bool ExpressionUtils::isEvaluableExpr(const Expression *expr) {
}

// rewrite LabelAttr to EdgeProp
Expression *ExpressionUtils::rewriteLabelAttr2EdgeProp(ObjectPool *pool, const Expression *expr) {
Expression *ExpressionUtils::rewriteLabelAttr2EdgeProp(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 rewriter = [pool](const Expression *e) -> Expression * {
DCHECK_EQ(e->kind(), Expression::Kind::kLabelAttribute);
auto labelAttrExpr = static_cast<const LabelAttributeExpression *>(e);
auto leftName = labelAttrExpr->left()->name();
Expand All @@ -125,13 +126,12 @@ Expression *ExpressionUtils::rewriteLabelAttr2EdgeProp(ObjectPool *pool, const E
}

// rewrite var in VariablePropExpr to another var
Expression *ExpressionUtils::rewriteInnerVar(ObjectPool *pool,
const Expression *expr,
std::string newVar) {
Expression *ExpressionUtils::rewriteInnerVar(const Expression *expr, std::string newVar) {
ObjectPool* pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
return e->kind() == Expression::Kind::kVarProperty;
};
auto rewriter = [&](const Expression *e) -> Expression * {
auto rewriter = [pool, newVar](const Expression *e) -> Expression * {
DCHECK_EQ(e->kind(), Expression::Kind::kVarProperty);
auto varPropExpr = static_cast<const VariablePropertyExpression *>(e);
auto newProp = varPropExpr->prop();
Expand All @@ -142,11 +142,12 @@ Expression *ExpressionUtils::rewriteInnerVar(ObjectPool *pool,
}

// rewrite LabelAttr to tagProp
Expression *ExpressionUtils::rewriteLabelAttr2TagProp(ObjectPool* pool, const Expression *expr) {
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 rewriter = [pool](const Expression *e) -> Expression * {
DCHECK_EQ(e->kind(), Expression::Kind::kLabelAttribute);
auto labelAttrExpr = static_cast<const LabelAttributeExpression *>(e);
auto leftName = labelAttrExpr->left()->name();
Expand All @@ -158,19 +159,20 @@ Expression *ExpressionUtils::rewriteLabelAttr2TagProp(ObjectPool* pool, const Ex
}

// rewrite Agg to VarProp
Expression *ExpressionUtils::rewriteAgg2VarProp(ObjectPool *pool, const Expression *expr) {
Expression *ExpressionUtils::rewriteAgg2VarProp(const Expression *expr) {
ObjectPool* pool = expr->getObjPool();
auto matcher = [](const Expression *e) -> bool {
return e->kind() == Expression::Kind::kAggregate;
};
auto rewriter = [&pool](const Expression *e) -> Expression * {
auto rewriter = [pool](const Expression *e) -> Expression * {
return VariablePropertyExpression::make(pool, "", e->toString());
};

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

StatusOr<Expression *> ExpressionUtils::foldConstantExpr(ObjectPool *objPool,
const Expression *expr) {
StatusOr<Expression *> ExpressionUtils::foldConstantExpr(const Expression *expr) {
ObjectPool* objPool = expr->getObjPool();
auto newExpr = expr->clone();
FoldConstantExprVisitor visitor(objPool);
newExpr->accept(&visitor);
Expand All @@ -187,7 +189,7 @@ StatusOr<Expression *> ExpressionUtils::foldConstantExpr(ObjectPool *objPool,
return newExpr;
}

Expression *ExpressionUtils::reduceUnaryNotExpr(const Expression *expr, ObjectPool *pool) {
Expression *ExpressionUtils::reduceUnaryNotExpr(const Expression *expr) {
// Match the operand
auto operandMatcher = [](const Expression *operandExpr) -> bool {
return (operandExpr->kind() == Expression::Kind::kUnaryNot ||
Expand All @@ -214,10 +216,10 @@ Expression *ExpressionUtils::reduceUnaryNotExpr(const Expression *expr, ObjectPo
reducedExpr = castedExpr->operand();
} else if (reducedExpr->isRelExpr() && reducedExpr->kind() != Expression::Kind::kRelREG) {
auto castedExpr = static_cast<RelationalExpression *>(reducedExpr);
reducedExpr = reverseRelExpr(pool, castedExpr);
reducedExpr = reverseRelExpr(castedExpr);
} else if (reducedExpr->isLogicalExpr()) {
auto castedExpr = static_cast<LogicalExpression *>(reducedExpr);
reducedExpr = reverseLogicalExpr(pool, castedExpr);
reducedExpr = reverseLogicalExpr(castedExpr);
}
// Rewrite the output of rewrite if possible
return operandMatcher(reducedExpr)
Expand All @@ -228,7 +230,8 @@ Expression *ExpressionUtils::reduceUnaryNotExpr(const Expression *expr, ObjectPo
return RewriteVisitor::transform(expr, rootMatcher, rewriter);
}

Expression *ExpressionUtils::rewriteRelExpr(const Expression *expr, ObjectPool *pool) {
Expression *ExpressionUtils::rewriteRelExpr(const Expression *expr) {
ObjectPool* pool = expr->getObjPool();
// Match relational expressions containing at least one airthmetic expr
auto matcher = [](const Expression *e) -> bool {
if (e->isRelExpr()) {
Expand All @@ -247,7 +250,7 @@ Expression *ExpressionUtils::rewriteRelExpr(const Expression *expr, ObjectPool *
};

// Simplify relational expressions involving boolean literals
auto simplifyBoolOperand = [&pool](RelationalExpression *relExpr,
auto simplifyBoolOperand = [pool](RelationalExpression *relExpr,
Expression *lExpr,
Expression *rExpr) -> Expression * {
QueryExpressionContext ctx(nullptr);
Expand Down Expand Up @@ -288,17 +291,17 @@ Expression *ExpressionUtils::rewriteRelExpr(const Expression *expr, ObjectPool *
}
// Move all evaluable expression to the right side
auto relRightOperandExpr = relExpr->right()->clone();
auto relLeftOperandExpr = rewriteRelExprHelper(pool, relExpr->left(), relRightOperandExpr);
auto relLeftOperandExpr = rewriteRelExprHelper(relExpr->left(), relRightOperandExpr);
return RelationalExpression::makeKind(
pool, relExpr->kind(), relLeftOperandExpr->clone(), relRightOperandExpr->clone());
};

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

Expression *ExpressionUtils::rewriteRelExprHelper(ObjectPool *pool,
const Expression *expr,
Expression *ExpressionUtils::rewriteRelExprHelper(const Expression *expr,
Expression *&relRightOperandExpr) {
ObjectPool* pool = expr->getObjPool();
// TODO: Support rewrite mul/div expressoion after fixing overflow
auto matcher = [](const Expression *e) -> bool {
if (!e->isArithmeticExpr() || e->kind() == Expression::Kind::kMultiply ||
Expand Down Expand Up @@ -342,19 +345,19 @@ Expression *ExpressionUtils::rewriteRelExprHelper(ObjectPool *pool,
break;
}

return rewriteRelExprHelper(pool, root, relRightOperandExpr);
return rewriteRelExprHelper(root, relRightOperandExpr);
}

StatusOr<Expression*> ExpressionUtils::filterTransform(const Expression *filter, ObjectPool *pool) {
StatusOr<Expression*> ExpressionUtils::filterTransform(const Expression *filter) {
auto rewrittenExpr = const_cast<Expression *>(filter);
// Rewrite relational expression
rewrittenExpr = rewriteRelExpr(rewrittenExpr, pool);
rewrittenExpr = rewriteRelExpr(rewrittenExpr);
// Fold constant expression
auto constantFoldRes = foldConstantExpr(pool, rewrittenExpr);
auto constantFoldRes = foldConstantExpr(rewrittenExpr);
NG_RETURN_IF_ERROR(constantFoldRes);
rewrittenExpr = constantFoldRes.value();
// Reduce Unary expression
rewrittenExpr = reduceUnaryNotExpr(rewrittenExpr, pool);
rewrittenExpr = reduceUnaryNotExpr(rewrittenExpr);
return rewrittenExpr;
}

Expand Down Expand Up @@ -428,11 +431,11 @@ Expression* ExpressionUtils::flattenInnerLogicalExpr(const Expression *expr) {
}

// pick the subparts of expression that meet picker's criteria
void ExpressionUtils::splitFilter(ObjectPool *pool,
const Expression *expr,
void ExpressionUtils::splitFilter(const Expression *expr,
std::function<bool(const Expression *)> picker,
Expression **filterPicked,
Expression **filterUnpicked) {
ObjectPool* pool = expr->getObjPool();
// Pick the non-LogicalAndExpr directly
if (expr->kind() != Expression::Kind::kLogicalAnd) {
if (picker(expr)) {
Expand Down Expand Up @@ -641,8 +644,8 @@ bool ExpressionUtils::findInnerRandFunction(const Expression *expr) {
}

// Negate the given relational expr
RelationalExpression *ExpressionUtils::reverseRelExpr(ObjectPool *pool,
RelationalExpression *expr) {
RelationalExpression *ExpressionUtils::reverseRelExpr(RelationalExpression *expr) {
ObjectPool *pool = expr->getObjPool();
auto left = static_cast<RelationalExpression *>(expr)->left();
auto right = static_cast<RelationalExpression *>(expr)->right();
auto negatedKind = getNegatedRelExprKind(expr->kind());
Expand Down Expand Up @@ -706,7 +709,8 @@ Expression::Kind ExpressionUtils::getNegatedArithmeticType(const Expression::Kin
}
}

LogicalExpression*ExpressionUtils::reverseLogicalExpr(ObjectPool* pool, LogicalExpression *expr) {
LogicalExpression*ExpressionUtils::reverseLogicalExpr(LogicalExpression *expr) {
ObjectPool* pool = expr->getObjPool();
std::vector<Expression *> operands;
if (expr->kind() == Expression::Kind::kLogicalAnd) {
pullAnds(expr);
Expand Down
28 changes: 12 additions & 16 deletions src/util/ExpressionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,33 @@ class ExpressionUtils {

static bool isEvaluableExpr(const Expression* expr);

static Expression* rewriteLabelAttr2TagProp(ObjectPool* pool, const Expression* expr);
static Expression* rewriteLabelAttr2TagProp(const Expression* expr);

static Expression* rewriteLabelAttr2EdgeProp(ObjectPool* pool, const Expression* expr);
static Expression* rewriteLabelAttr2EdgeProp(const Expression* expr);

static Expression* rewriteAgg2VarProp(ObjectPool* pool, const Expression* expr);
static Expression* rewriteAgg2VarProp(const Expression* expr);

static Expression* rewriteInnerVar(ObjectPool* pool,
const Expression* expr,
std::string newVar);
static Expression* rewriteInnerVar(const Expression* expr, std::string newVar);

// Rewrite relational expression, gather evaluable expressions to one side
static Expression* rewriteRelExpr(const Expression* expr, ObjectPool* pool);
static Expression* rewriteRelExprHelper(ObjectPool* pool,
const Expression* expr,
static Expression* rewriteRelExpr(const Expression* expr);
static Expression* rewriteRelExprHelper(const Expression* expr,
Expression*& relRightOperandExpr);

// Clone and fold constant expression
static StatusOr<Expression*> foldConstantExpr(ObjectPool* objPool, const Expression* expr);
static StatusOr<Expression*> foldConstantExpr(const Expression* expr);

// Clone and reduce unaryNot expression
static Expression* reduceUnaryNotExpr(const Expression* expr, ObjectPool* pool);
static Expression* reduceUnaryNotExpr(const Expression* expr);

// Transform filter using multiple expression rewrite strategies
static StatusOr<Expression*> filterTransform(const Expression* expr, ObjectPool* objPool);
static StatusOr<Expression*> filterTransform(const Expression* expr);

// Negate the given logical expr: (A && B) -> (!A || !B)
static LogicalExpression* reverseLogicalExpr(ObjectPool* pool, LogicalExpression* expr);
static LogicalExpression* reverseLogicalExpr(LogicalExpression* expr);

// Negate the given relational expr: (A > B) -> (A <= B)
static RelationalExpression* reverseRelExpr(ObjectPool* pool, RelationalExpression* expr);
static RelationalExpression* reverseRelExpr(RelationalExpression* expr);

// Return the negation of the given relational kind
static Expression::Kind getNegatedRelExprKind(const Expression::Kind kind);
Expand Down Expand Up @@ -119,8 +116,7 @@ class ExpressionUtils {

static Expression* flattenInnerLogicalExpr(const Expression* expr);

static void splitFilter(ObjectPool* pool,
const Expression* expr,
static void splitFilter(const Expression* expr,
std::function<bool(const Expression*)> picker,
Expression** filterPicked,
Expression** filterUnpicked);
Expand Down
4 changes: 2 additions & 2 deletions src/util/test/ExpressionUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ TEST_F(ExpressionUtilsTest, splitFilter) {
};
Expression *newExpr1 = nullptr;
Expression *newExpr2 = nullptr;
ExpressionUtils::splitFilter(pool, expr, picker, &newExpr1, &newExpr2);
ExpressionUtils::splitFilter(expr, picker, &newExpr1, &newExpr2);
ASSERT_EQ(*expected1, *newExpr1);
ASSERT_EQ(*second, *newExpr2);
}
Expand All @@ -534,7 +534,7 @@ TEST_F(ExpressionUtilsTest, splitFilter) {
};
Expression *newExpr1 = nullptr;
Expression *newExpr2 = nullptr;
ExpressionUtils::splitFilter(pool, expr, picker, &newExpr1, &newExpr2);
ExpressionUtils::splitFilter(expr, picker, &newExpr1, &newExpr2);
ASSERT_EQ(*expr, *newExpr1);
ASSERT_EQ(nullptr, newExpr2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/validator/FetchEdgesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Status FetchEdgesValidator::preparePropertiesWithYield(const YieldClause *yield)
propsName.reserve(newYield_->columns().size());
dedup_ = newYield_->isDistinct();
for (auto col : newYield_->columns()) {
col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(pool, col->expr()));
col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(col->expr()));
NG_RETURN_IF_ERROR(invalidLabelIdentifiers(col->expr()));
const auto *invalidExpr = findInvalidYieldExpression(col->expr());
if (invalidExpr != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/validator/FetchVerticesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Status FetchVerticesValidator::preparePropertiesWithYield(const YieldClause *yie

auto* pool = qctx_->objPool();
for (auto col : yield->columns()) {
col->setExpr(ExpressionUtils::rewriteLabelAttr2TagProp(pool, col->expr()));
col->setExpr(ExpressionUtils::rewriteLabelAttr2TagProp(col->expr()));
NG_RETURN_IF_ERROR(invalidLabelIdentifiers(col->expr()));
col->expr()->accept(&deducePropsVisitor);
if (!deducePropsVisitor.ok()) {
Expand Down
Loading