Skip to content

Commit

Permalink
Merge pull request #1952 from kuzudb/remove-query-normalizer
Browse files Browse the repository at this point in the history
remove query normalizer
  • Loading branch information
andyfengHKU committed Aug 24, 2023
2 parents 2c45803 + c867eb9 commit fb7cd9f
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 229 deletions.
3 changes: 1 addition & 2 deletions src/binder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ add_library(kuzu_binder
bound_statement_result.cpp
bound_statement_visitor.cpp
expression_binder.cpp
expression_visitor.cpp
query_normalizer.cpp)
expression_visitor.cpp)

set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_binder>
Expand Down
2 changes: 2 additions & 0 deletions src/binder/bind/bind_projection_clause.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "binder/binder.h"
#include "binder/expression/literal_expression.h"
#include "binder/expression_visitor.h"
#include "binder/query/return_with_clause/bound_return_clause.h"
#include "binder/query/return_with_clause/bound_with_clause.h"
#include "parser/expression/parsed_property_expression.h"

using namespace kuzu::common;
Expand Down
61 changes: 36 additions & 25 deletions src/binder/bind/bind_query.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
#include "binder/binder.h"
#include "binder/query/return_with_clause/bound_return_clause.h"
#include "binder/query/return_with_clause/bound_with_clause.h"
#include "parser/query/return_with_clause/return_clause.h"
#include "parser/query/return_with_clause/with_clause.h"

using namespace kuzu::parser;

namespace kuzu {
namespace binder {

std::unique_ptr<BoundRegularQuery> Binder::bindQuery(const RegularQuery& regularQuery) {
std::vector<std::unique_ptr<BoundSingleQuery>> boundSingleQueries;
std::vector<std::unique_ptr<NormalizedSingleQuery>> normalizedSingleQueries;
for (auto i = 0u; i < regularQuery.getNumSingleQueries(); i++) {
// Don't clear scope within bindSingleQuery() yet because it is also used for subquery
// binding.
scope->clear();
boundSingleQueries.push_back(bindSingleQuery(*regularQuery.getSingleQuery(i)));
}
validateUnionColumnsOfTheSameType(boundSingleQueries);
assert(!boundSingleQueries.empty());
auto statementResult =
boundSingleQueries[0]->hasReturnClause() ?
boundSingleQueries[0]->getReturnClause()->getStatementResult()->copy() :
BoundStatementResult::createEmptyResult();
normalizedSingleQueries.push_back(bindSingleQuery(*regularQuery.getSingleQuery(i)));
}
validateUnionColumnsOfTheSameType(normalizedSingleQueries);
assert(!normalizedSingleQueries.empty());
auto boundRegularQuery = std::make_unique<BoundRegularQuery>(
regularQuery.getIsUnionAll(), std::move(statementResult));
for (auto& boundSingleQuery : boundSingleQueries) {
auto normalizedSingleQuery = QueryNormalizer::normalizeQuery(*boundSingleQuery);
regularQuery.getIsUnionAll(), normalizedSingleQueries[0]->getStatementResult()->copy());
for (auto& normalizedSingleQuery : normalizedSingleQueries) {
validateReadNotFollowUpdate(*normalizedSingleQuery);
boundRegularQuery->addSingleQuery(std::move(normalizedSingleQuery));
}
validateIsAllUnionOrUnionAll(*boundRegularQuery);
return boundRegularQuery;
}

std::unique_ptr<BoundSingleQuery> Binder::bindSingleQuery(const SingleQuery& singleQuery) {
auto boundSingleQuery = std::make_unique<BoundSingleQuery>();
std::unique_ptr<NormalizedSingleQuery> Binder::bindSingleQuery(const SingleQuery& singleQuery) {
auto normalizedSingleQuery = std::make_unique<NormalizedSingleQuery>();
for (auto i = 0u; i < singleQuery.getNumQueryParts(); ++i) {
boundSingleQuery->addQueryPart(bindQueryPart(*singleQuery.getQueryPart(i)));
normalizedSingleQuery->appendQueryPart(bindQueryPart(*singleQuery.getQueryPart(i)));
}
auto lastQueryPart = std::make_unique<NormalizedQueryPart>();
for (auto i = 0u; i < singleQuery.getNumReadingClauses(); i++) {
boundSingleQuery->addReadingClause(bindReadingClause(*singleQuery.getReadingClause(i)));
lastQueryPart->addReadingClause(bindReadingClause(*singleQuery.getReadingClause(i)));
}
for (auto i = 0u; i < singleQuery.getNumUpdatingClauses(); ++i) {
boundSingleQuery->addUpdatingClause(bindUpdatingClause(*singleQuery.getUpdatingClause(i)));
lastQueryPart->addUpdatingClause(bindUpdatingClause(*singleQuery.getUpdatingClause(i)));
}
std::unique_ptr<BoundStatementResult> statementResult;
if (singleQuery.hasReturnClause()) {
boundSingleQuery->setReturnClause(bindReturnClause(*singleQuery.getReturnClause()));
auto boundReturnClause = bindReturnClause(*singleQuery.getReturnClause());
lastQueryPart->setProjectionBody(boundReturnClause->getProjectionBody()->copy());
statementResult = boundReturnClause->getStatementResult()->copy();
} else {
statementResult = BoundStatementResult::createEmptyResult();
}
return boundSingleQuery;
normalizedSingleQuery->appendQueryPart(std::move(lastQueryPart));
normalizedSingleQuery->setStatementResult(std::move(statementResult));
return normalizedSingleQuery;
}

std::unique_ptr<BoundQueryPart> Binder::bindQueryPart(const QueryPart& queryPart) {
auto boundQueryPart = std::make_unique<BoundQueryPart>();
std::unique_ptr<NormalizedQueryPart> Binder::bindQueryPart(const QueryPart& queryPart) {
auto normalizedQueryPart = std::make_unique<NormalizedQueryPart>();
for (auto i = 0u; i < queryPart.getNumReadingClauses(); i++) {
boundQueryPart->addReadingClause(bindReadingClause(*queryPart.getReadingClause(i)));
normalizedQueryPart->addReadingClause(bindReadingClause(*queryPart.getReadingClause(i)));
}
for (auto i = 0u; i < queryPart.getNumUpdatingClauses(); ++i) {
boundQueryPart->addUpdatingClause(bindUpdatingClause(*queryPart.getUpdatingClause(i)));
normalizedQueryPart->addUpdatingClause(bindUpdatingClause(*queryPart.getUpdatingClause(i)));
}
auto boundWithClause = bindWithClause(*queryPart.getWithClause());
normalizedQueryPart->setProjectionBody(boundWithClause->getProjectionBody()->copy());
if (boundWithClause->hasWhereExpression()) {
normalizedQueryPart->setProjectionBodyPredicate(boundWithClause->getWhereExpression());
}
boundQueryPart->setWithClause(bindWithClause(*queryPart.getWithClause()));
return boundQueryPart;
return normalizedQueryPart;
}

} // namespace binder
Expand Down
1 change: 1 addition & 0 deletions src/binder/bind/bind_reading_clause.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "binder/binder.h"
#include "binder/expression/literal_expression.h"
#include "binder/query/reading_clause/bound_in_query_call.h"
#include "binder/query/reading_clause/bound_match_clause.h"
#include "binder/query/reading_clause/bound_unwind_clause.h"
#include "parser/query/reading_clause/in_query_call_clause.h"
#include "parser/query/reading_clause/unwind_clause.h"
Expand Down
18 changes: 9 additions & 9 deletions src/binder/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ void Binder::validateOrderByFollowedBySkipOrLimitInWithClause(
}

void Binder::validateUnionColumnsOfTheSameType(
const std::vector<std::unique_ptr<BoundSingleQuery>>& boundSingleQueries) {
if (boundSingleQueries.size() <= 1) {
const std::vector<std::unique_ptr<NormalizedSingleQuery>>& normalizedSingleQueries) {
if (normalizedSingleQueries.size() <= 1) {
return;
}
auto expressionsToProject = boundSingleQueries[0]->getExpressionsToCollect();
for (auto i = 1u; i < boundSingleQueries.size(); i++) {
auto expressionsToProjectToCheck = boundSingleQueries[i]->getExpressionsToCollect();
if (expressionsToProject.size() != expressionsToProjectToCheck.size()) {
auto columns = normalizedSingleQueries[0]->getStatementResult()->getColumns();
for (auto i = 1u; i < normalizedSingleQueries.size(); i++) {
auto otherColumns = normalizedSingleQueries[i]->getStatementResult()->getColumns();
if (columns.size() != otherColumns.size()) {
throw BinderException("The number of columns to union/union all must be the same.");
}
// Check whether the dataTypes in union expressions are exactly the same in each single
// query.
for (auto j = 0u; j < expressionsToProject.size(); j++) {
ExpressionBinder::validateExpectedDataType(*expressionsToProjectToCheck[j],
expressionsToProject[j]->dataType.getLogicalTypeID());
for (auto j = 0u; j < columns.size(); j++) {
ExpressionBinder::validateExpectedDataType(
*otherColumns[j], columns[j]->dataType.getLogicalTypeID());
}
}
}
Expand Down
56 changes: 0 additions & 56 deletions src/binder/query_normalizer.cpp

This file was deleted.

9 changes: 5 additions & 4 deletions src/include/binder/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "expression_binder.h"
#include "parser/copy.h"
#include "parser/query/regular_query.h"
#include "query_normalizer.h"

namespace kuzu {
namespace main {
Expand All @@ -17,6 +16,8 @@ namespace binder {
class BoundCreateInfo;
class BoundSetPropertyInfo;
class BoundDeleteInfo;
class BoundWithClause;
class BoundReturnClause;

// BinderScope keeps track of expressions in scope and their aliases. We maintain the order of
// expressions in
Expand Down Expand Up @@ -110,8 +111,8 @@ class Binder {

/*** bind query ***/
std::unique_ptr<BoundRegularQuery> bindQuery(const parser::RegularQuery& regularQuery);
std::unique_ptr<BoundSingleQuery> bindSingleQuery(const parser::SingleQuery& singleQuery);
std::unique_ptr<BoundQueryPart> bindQueryPart(const parser::QueryPart& queryPart);
std::unique_ptr<NormalizedSingleQuery> bindSingleQuery(const parser::SingleQuery& singleQuery);
std::unique_ptr<NormalizedQueryPart> bindQueryPart(const parser::QueryPart& queryPart);

/*** bind call ***/
std::unique_ptr<BoundStatement> bindStandaloneCall(const parser::Statement& statement);
Expand Down Expand Up @@ -216,7 +217,7 @@ class Binder {
const BoundProjectionBody& boundProjectionBody);

static void validateUnionColumnsOfTheSameType(
const std::vector<std::unique_ptr<BoundSingleQuery>>& boundSingleQueries);
const std::vector<std::unique_ptr<NormalizedSingleQuery>>& normalizedSingleQueries);

static void validateIsAllUnionOrUnionAll(const BoundRegularQuery& regularQuery);

Expand Down
49 changes: 0 additions & 49 deletions src/include/binder/query/bound_query_part.h

This file was deleted.

58 changes: 0 additions & 58 deletions src/include/binder/query/bound_single_query.h

This file was deleted.

9 changes: 6 additions & 3 deletions src/include/binder/query/normalized_single_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ namespace binder {

class NormalizedSingleQuery {
public:
NormalizedSingleQuery() = default;
~NormalizedSingleQuery() = default;

inline void appendQueryPart(std::unique_ptr<NormalizedQueryPart> queryPart) {
queryParts.push_back(std::move(queryPart));
}
inline uint32_t getNumQueryParts() const { return queryParts.size(); }
inline NormalizedQueryPart* getQueryPart(uint32_t idx) const { return queryParts[idx].get(); }

inline void setStatementResult(std::unique_ptr<BoundStatementResult> result) {
statementResult = std::move(result);
}
inline BoundStatementResult* getStatementResult() const { return statementResult.get(); }

private:
std::vector<std::unique_ptr<NormalizedQueryPart>> queryParts;
std::unique_ptr<BoundStatementResult> statementResult;
};

} // namespace binder
Expand Down
Loading

0 comments on commit fb7cd9f

Please sign in to comment.