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

[Enhancement] Add opt rule to push down STARTS WITH expr #4433

Merged
merged 4 commits into from
Jul 19, 2022
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
4 changes: 4 additions & 0 deletions src/graph/optimizer/rule/OptimizeTagIndexScanByFilterRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ StatusOr<TransformResult> OptimizeTagIndexScanByFilterRule::transform(
if (conditionType == ExprKind::kRelIn) {
transformedExpr = graph::ExpressionUtils::rewriteInExpr(condition);
DCHECK(transformedExpr->kind() == ExprKind::kRelEQ);
} else if (conditionType == ExprKind::kStartsWith) {
// StartsWith expr is converted to a RelAnd expr with a constant value
transformedExpr = graph::ExpressionUtils::rewriteStartsWithExpr(condition);
DCHECK(transformedExpr->kind() == ExprKind::kLogicalAnd);
}

// case2: logical AND expr
Expand Down
27 changes: 27 additions & 0 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ Expression *ExpressionUtils::rewriteInExpr(const Expression *expr) {
return orExpr;
}

// Rewrite starts with expr to an AND expr to trigger a range scan
// I.g. v.name starts with "abc" => (v.name >= "abc") and (v.name < "abd")
Expression *ExpressionUtils::rewriteStartsWithExpr(const Expression *expr) {
DCHECK(expr->kind() == Expression::Kind::kStartsWith);
auto pool = expr->getObjPool();
auto startsWithExpr = static_cast<RelationalExpression *>(expr->clone());

// rhs is a string value
QueryExpressionContext ctx(nullptr);
auto rightBoundary = startsWithExpr->right()->eval(ctx).getStr();

// Increment the last char of the right boundary to get the range scan boundary
// Do not increment the last char of the string if it could cause overflow
if (*rightBoundary.end() < 127) {
rightBoundary[rightBoundary.size() - 1] = ++rightBoundary[rightBoundary.size() - 1];
} else { // If the last char is already 127, append a char of minimum value to the string
rightBoundary += static_cast<char>(0);
}

auto resultLeft =
RelationalExpression::makeGE(pool, startsWithExpr->left(), startsWithExpr->right());
auto resultRight = RelationalExpression::makeLT(
pool, startsWithExpr->left(), ConstantExpression::make(pool, rightBoundary));

return LogicalExpression::makeAnd(pool, resultLeft, resultRight);
}

Expression *ExpressionUtils::rewriteLogicalAndToLogicalOr(const Expression *expr) {
DCHECK(expr->kind() == Expression::Kind::kLogicalAnd);

Expand Down
3 changes: 3 additions & 0 deletions src/graph/util/ExpressionUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class ExpressionUtils {
// Rewrites IN expression into OR expression or relEQ expression
static Expression* rewriteInExpr(const Expression* expr);

// Rewrites STARTS WITH to logical AND expression to support range scan
static Expression* rewriteStartsWithExpr(const Expression* expr);

// Rewrite Logical AND expr that contains Logical OR expr to Logical OR expr using distributive
// law
// Examples:
Expand Down
9 changes: 4 additions & 5 deletions tests/tck/features/lookup/TagIndexFullScan.feature
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,10 @@ Feature: Lookup tag index full scan
| "Timberwolves" |
| "Thunders" |
And the execution plan should be:
| id | name | dependencies | operator info |
| 3 | Project | 2 | |
| 2 | Filter | 4 | {"condition": "(team.name STARTS WITH \"T\")"} |
| 4 | TagIndexFullScan | 0 | |
| 0 | Start | | |
| id | name | dependencies | operator info |
| 3 | Project | 4 | |
| 4 | TagIndexRangeScan | 0 | |
| 0 | Start | | |
When executing query:
"""
LOOKUP ON team WHERE team.name STARTS WITH "ABC" YIELD id(vertex) as id
Expand Down