-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1956 from kuzudb/rewrite-with-clause-projection-list
Add with clause projection list rewriter
- Loading branch information
Showing
10 changed files
with
191 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "binder/bound_statement_rewriter.h" | ||
|
||
#include "binder/rewriter/with_clause_projection_rewriter.h" | ||
|
||
namespace kuzu { | ||
namespace binder { | ||
|
||
void BoundStatementRewriter::rewrite(BoundStatement& boundStatement) { | ||
auto withClauseProjectionRewriter = WithClauseProjectionRewriter(); | ||
withClauseProjectionRewriter.visit(boundStatement); | ||
} | ||
|
||
} // namespace binder | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_library( | ||
kuzu_binder_rewriter | ||
OBJECT | ||
with_clause_projection_rewriter.cpp) | ||
|
||
set(ALL_OBJECT_FILES | ||
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_binder_rewriter> | ||
PARENT_SCOPE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "binder/rewriter/with_clause_projection_rewriter.h" | ||
|
||
#include "binder/visitor/property_collector.h" | ||
|
||
using namespace kuzu::common; | ||
|
||
namespace kuzu { | ||
namespace binder { | ||
|
||
static expression_vector getPropertiesOfSameVariable( | ||
const expression_vector& expressions, const std::string& variableName) { | ||
expression_vector result; | ||
for (auto& expression : expressions) { | ||
auto propertyExpression = (PropertyExpression*)expression.get(); | ||
if (propertyExpression->getVariableName() != variableName) { | ||
continue; | ||
} | ||
result.push_back(expression); | ||
} | ||
return result; | ||
} | ||
|
||
static expression_vector rewriteExpressions( | ||
const expression_vector& expressions, const expression_vector& properties) { | ||
expression_set distinctResult; | ||
for (auto& expression : expressions) { | ||
if (expression->expressionType != common::PROPERTY) { | ||
distinctResult.insert(expression); | ||
continue; | ||
} | ||
auto propertyExpression = (PropertyExpression*)expression.get(); | ||
if (!propertyExpression->isInternalID()) { | ||
distinctResult.insert(expression); | ||
continue; | ||
} | ||
// Expression is internal ID. Perform rewrite as all properties with the same variable. | ||
auto variableName = propertyExpression->getVariableName(); | ||
for (auto& property : getPropertiesOfSameVariable(properties, variableName)) { | ||
distinctResult.insert(property); | ||
} | ||
} | ||
return expression_vector{distinctResult.begin(), distinctResult.end()}; | ||
} | ||
|
||
void WithClauseProjectionRewriter::visitSingleQuery(const NormalizedSingleQuery& singleQuery) { | ||
auto propertyCollector = PropertyCollector(); | ||
propertyCollector.visitSingleQuery(singleQuery); | ||
auto properties = propertyCollector.getProperties(); | ||
for (auto i = 0; i < singleQuery.getNumQueryParts() - 1; ++i) { | ||
auto queryPart = singleQuery.getQueryPart(i); | ||
auto projectionBody = queryPart->getProjectionBody(); | ||
auto newProjectionExpressions = | ||
rewriteExpressions(projectionBody->getProjectionExpressions(), properties); | ||
projectionBody->setProjectionExpressions(std::move(newProjectionExpressions)); | ||
auto newGroupByExpressions = | ||
rewriteExpressions(projectionBody->getGroupByExpressions(), properties); | ||
projectionBody->setGroupByExpressions(std::move(newGroupByExpressions)); | ||
} | ||
} | ||
|
||
} // namespace binder | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "bound_statement.h" | ||
|
||
namespace kuzu { | ||
namespace binder { | ||
|
||
class BoundStatementRewriter { | ||
public: | ||
static void rewrite(BoundStatement& boundStatement); | ||
}; | ||
|
||
} // namespace binder | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/include/binder/rewriter/with_clause_projection_rewriter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "binder/bound_statement_visitor.h" | ||
|
||
namespace kuzu { | ||
namespace binder { | ||
|
||
// WithClauseProjectionRewriter first analyze the properties need to be scanned for each query. And | ||
// then rewrite node/rel expression in WITH clause as their properties. So We avoid eagerly evaluate | ||
// node/rel in WITH clause projection. E.g. | ||
// MATCH (a) WITH a MATCH (a)->(b); | ||
// will be rewritten as | ||
// MATCH (a) WITH a._id MATCH (a)->(b); | ||
// See bind_projection_clause.cpp for more details. | ||
class WithClauseProjectionRewriter : public BoundStatementVisitor { | ||
public: | ||
void visitSingleQuery(const NormalizedSingleQuery& singleQuery) override; | ||
}; | ||
|
||
} // namespace binder | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters