From 278aaefc50fee4350d804ed30fecbe561d1c3841 Mon Sep 17 00:00:00 2001 From: xiyang Date: Sun, 31 Dec 2023 17:35:59 +0800 Subject: [PATCH] Clear parser unique_ptr --- src/binder/bind/bind_copy.cpp | 4 +- src/binder/bind/bind_ddl.cpp | 8 +- src/binder/bind/bind_graph_pattern.cpp | 4 +- src/binder/bind/bind_projection_clause.cpp | 2 +- src/binder/bind/bind_reading_clause.cpp | 11 +- src/include/binder/binder.h | 4 +- src/include/common/copy_constructors.h | 4 + src/include/parser/copy.h | 11 +- src/include/parser/ddl/alter.h | 6 +- src/include/parser/ddl/alter_info.h | 2 + src/include/parser/ddl/create_table.h | 12 +- src/include/parser/ddl/create_table_info.h | 8 +- src/include/parser/ddl/ddl.h | 22 --- .../expression/parsed_case_expression.h | 32 ++-- .../parser/expression/parsed_expression.h | 16 +- .../expression/parsed_function_expression.h | 4 +- .../expression/parsed_literal_expression.h | 20 +-- .../expression/parsed_parameter_expression.h | 5 +- .../expression/parsed_property_expression.h | 4 +- .../expression/parsed_subquery_expression.h | 17 +- .../expression/parsed_variable_expression.h | 4 +- .../parser/query/graph_pattern/node_pattern.h | 13 +- .../query/graph_pattern/pattern_element.h | 24 +-- .../graph_pattern/pattern_element_chain.h | 18 +- .../parser/query/graph_pattern/rel_pattern.h | 31 ++-- src/include/parser/query/query_part.h | 8 +- .../reading_clause/in_query_call_clause.h | 7 +- .../parser/query/reading_clause/load_from.h | 2 +- .../query/reading_clause/match_clause.h | 11 +- .../query/reading_clause/unwind_clause.h | 5 +- src/include/parser/query/regular_query.h | 12 +- .../return_with_clause/projection_body.h | 3 +- .../query/return_with_clause/return_clause.h | 8 +- .../query/return_with_clause/with_clause.h | 8 +- src/include/parser/query/single_query.h | 26 +-- .../query/updating_clause/delete_clause.h | 2 +- .../query/updating_clause/insert_clause.h | 8 +- .../query/updating_clause/merge_clause.h | 20 +-- .../parser/query/updating_clause/set_clause.h | 10 +- src/include/parser/transformer.h | 165 +++++------------- .../expression/parsed_case_expression.cpp | 16 +- src/parser/expression/parsed_expression.cpp | 6 +- src/parser/parsed_expression_visitor.cpp | 2 +- src/parser/parsed_statement_visitor.cpp | 4 +- src/parser/transform/transform_copy.cpp | 3 +- src/parser/transform/transform_ddl.cpp | 59 +++---- src/parser/transform/transform_expression.cpp | 37 ++-- .../transform/transform_graph_pattern.cpp | 64 +++---- src/parser/transform/transform_projection.cpp | 23 ++- src/parser/transform/transform_query.cpp | 28 ++- .../transform/transform_updating_clause.cpp | 2 +- 51 files changed, 335 insertions(+), 490 deletions(-) delete mode 100644 src/include/parser/ddl/ddl.h diff --git a/src/binder/bind/bind_copy.cpp b/src/binder/bind/bind_copy.cpp index fb686ad99e..36718da4c6 100644 --- a/src/binder/bind/bind_copy.cpp +++ b/src/binder/bind/bind_copy.cpp @@ -25,7 +25,9 @@ std::unique_ptr Binder::bindCopyToClause(const Statement& statem auto fileType = bindFileType(boundFilePath); std::vector columnNames; std::vector> columnTypes; - auto query = bindQuery(*copyToStatement.getRegularQuery()); + auto parsedQuery = + ku_dynamic_cast(copyToStatement.getStatement()); + auto query = bindQuery(*parsedQuery); auto columns = query->getStatementResult()->getColumns(); for (auto& column : columns) { auto columnName = column->hasAlias() ? column->getAlias() : column->toString(); diff --git a/src/binder/bind/bind_ddl.cpp b/src/binder/bind/bind_ddl.cpp index 50ee83ed8b..533370f4be 100644 --- a/src/binder/bind/bind_ddl.cpp +++ b/src/binder/bind/bind_ddl.cpp @@ -142,10 +142,10 @@ std::unique_ptr Binder::bindCreateRelTableGroupInfo( .append(srcTableName) .append("_") .append(dstTableName); - auto relExtraInfo = + auto relCreateInfo = std::make_unique(TableType::REL, relTableName); + relCreateInfo->propertyNameDataTypes = info->propertyNameDataTypes; + relCreateInfo->extraInfo = std::make_unique(relMultiplicity, srcTableName, dstTableName); - auto relCreateInfo = std::make_unique( - TableType::REL, relTableName, info->propertyNameDataTypes, std::move(relExtraInfo)); boundCreateRelTableInfos.push_back(bindCreateRelTableInfo(relCreateInfo.get())); } auto boundExtraInfo = @@ -156,7 +156,7 @@ std::unique_ptr Binder::bindCreateRelTableGroupInfo( std::unique_ptr Binder::bindCreateTable(const Statement& statement) { auto& createTable = ku_dynamic_cast(statement); - auto tableName = createTable.getTableName(); + auto tableName = createTable.getInfo()->tableName; if (catalog.containsTable(clientContext->getTx(), tableName)) { throw BinderException(tableName + " already exists in catalog."); } diff --git a/src/binder/bind/bind_graph_pattern.cpp b/src/binder/bind/bind_graph_pattern.cpp index 70e59d29cf..7bd38bcc67 100644 --- a/src/binder/bind/bind_graph_pattern.cpp +++ b/src/binder/bind/bind_graph_pattern.cpp @@ -28,12 +28,12 @@ namespace binder { // We do not store key-value pairs in query graph primarily because we will merge key-value // std::pairs with other predicates specified in WHERE clause. std::unique_ptr Binder::bindGraphPattern( - const std::vector>& graphPattern) { + const std::vector& graphPattern) { auto propertyCollection = std::make_unique(); auto queryGraphCollection = std::make_unique(); for (auto& patternElement : graphPattern) { queryGraphCollection->addAndMergeQueryGraphIfConnected( - bindPatternElement(*patternElement, *propertyCollection)); + bindPatternElement(patternElement, *propertyCollection)); } auto boundPattern = std::make_unique(); boundPattern->queryGraphCollection = std::move(queryGraphCollection); diff --git a/src/binder/bind/bind_projection_clause.cpp b/src/binder/bind/bind_projection_clause.cpp index 58d61ab1a6..f5000f5d07 100644 --- a/src/binder/bind/bind_projection_clause.cpp +++ b/src/binder/bind/bind_projection_clause.cpp @@ -182,7 +182,7 @@ std::unique_ptr Binder::bindProjectionBody( } expression_vector Binder::bindProjectionExpressions( - const parsed_expression_vector& projectionExpressions) { + const parsed_expr_vector& projectionExpressions) { expression_vector result; for (auto& expression : projectionExpressions) { if (expression->getExpressionType() == ExpressionType::STAR) { diff --git a/src/binder/bind/bind_reading_clause.cpp b/src/binder/bind/bind_reading_clause.cpp index 57ab630474..e5f4af3856 100644 --- a/src/binder/bind/bind_reading_clause.cpp +++ b/src/binder/bind/bind_reading_clause.cpp @@ -44,7 +44,7 @@ std::unique_ptr Binder::bindReadingClause(const ReadingClaus std::unique_ptr Binder::bindMatchClause(const ReadingClause& readingClause) { auto& matchClause = ku_dynamic_cast(readingClause); - auto boundGraphPattern = bindGraphPattern(matchClause.getPatternElements()); + auto boundGraphPattern = bindGraphPattern(matchClause.getPatternElementsRef()); if (matchClause.hasWherePredicate()) { boundGraphPattern->where = bindWhereExpression(*matchClause.getWherePredicate()); } @@ -105,11 +105,10 @@ std::unique_ptr Binder::bindInQueryCall(const ReadingClause& auto funcName = funcExpr->getFunctionName(); StringUtils::toUpper(funcName); if (funcName == common::READ_PANDAS_FUNC_NAME && clientContext->replaceFunc) { - auto replacedValue = clientContext->replaceFunc( - ku_dynamic_cast(funcExpr->getChild(0)) - ->getValue()); - auto parameterExpression = - std::make_unique(std::move(replacedValue), "pd"); + auto literalExpr = + ku_dynamic_cast(funcExpr->getChild(0)); + auto replacedValue = clientContext->replaceFunc(literalExpr->getValueUnsafe()); + auto parameterExpression = std::make_unique(*replacedValue, "pd"); auto inQueryCallParameterReplacer = std::make_unique( std::make_pair(funcName, parameterExpression.get())); funcExpr = inQueryCallParameterReplacer->visit(funcExpr); diff --git a/src/include/binder/binder.h b/src/include/binder/binder.h index 554fec777d..5052b7b828 100644 --- a/src/include/binder/binder.h +++ b/src/include/binder/binder.h @@ -210,7 +210,7 @@ class Binder { const expression_vector& projectionExpressions); expression_vector bindProjectionExpressions( - const parser::parsed_expression_vector& parsedExpressions); + const parser::parsed_expr_vector& parsedExpressions); expression_vector bindOrderByExpressions( const std::vector>& orderByExpressions); @@ -221,7 +221,7 @@ class Binder { /*** bind graph pattern ***/ std::unique_ptr bindGraphPattern( - const std::vector>& graphPattern); + const std::vector& graphPattern); std::unique_ptr bindPatternElement( const parser::PatternElement& patternElement, PropertyKeyValCollection& collection); diff --git a/src/include/common/copy_constructors.h b/src/include/common/copy_constructors.h index 6a7f9e8a7a..08d5ebfc24 100644 --- a/src/include/common/copy_constructors.h +++ b/src/include/common/copy_constructors.h @@ -4,8 +4,10 @@ #define DELETE_COPY_CONSTRUCT(Object) Object(const Object& other) = delete #define DELETE_COPY_ASSN(Object) Object& operator=(const Object& other) = delete +// NOLINTBEGIN #define DELETE_MOVE_CONSTRUCT(Object) Object(Object&& other) = delete #define DELETE_MOVE_ASSN(Object) Object& operator=(Object&& other) = delete +// NOLINTEND #define DELETE_BOTH_COPY(Object) \ DELETE_COPY_CONSTRUCT(Object); \ @@ -15,8 +17,10 @@ DELETE_MOVE_CONSTRUCT(Object); \ DELETE_MOVE_ASSN(Object) +// NOLINTBEGIN #define DEFAULT_MOVE_CONSTRUCT(Object) Object(Object&& other) = default #define DEFAULT_MOVE_ASSN(Object) Object& operator=(Object&& other) = default +// NOLINTEND #define DEFAULT_BOTH_MOVE(Object) \ DEFAULT_MOVE_CONSTRUCT(Object); \ diff --git a/src/include/parser/copy.h b/src/include/parser/copy.h index 667a9190be..c92f5c67f7 100644 --- a/src/include/parser/copy.h +++ b/src/include/parser/copy.h @@ -3,7 +3,6 @@ #include #include "parser/expression/parsed_expression.h" -#include "parser/query/regular_query.h" #include "parser/statement.h" namespace kuzu { @@ -44,16 +43,16 @@ class CopyFrom : public Copy { class CopyTo : public Copy { public: - CopyTo(std::string filePath, std::unique_ptr regularQuery) - : Copy{common::StatementType::COPY_TO}, filePath{std::move(filePath)}, - regularQuery{std::move(regularQuery)} {} + CopyTo(std::string filePath, std::unique_ptr statement) + : Copy{common::StatementType::COPY_TO}, filePath{std::move(filePath)}, statement{std::move( + statement)} {} inline std::string getFilePath() const { return filePath; } - inline RegularQuery* getRegularQuery() const { return regularQuery.get(); } + inline const Statement* getStatement() const { return statement.get(); } private: std::string filePath; - std::unique_ptr regularQuery; + std::unique_ptr statement; }; } // namespace parser diff --git a/src/include/parser/ddl/alter.h b/src/include/parser/ddl/alter.h index 6e4b4fe9c5..cb220baabe 100644 --- a/src/include/parser/ddl/alter.h +++ b/src/include/parser/ddl/alter.h @@ -8,13 +8,13 @@ namespace parser { class Alter : public Statement { public: - explicit Alter(std::unique_ptr info) + explicit Alter(AlterInfo info) : Statement{common::StatementType::ALTER}, info{std::move(info)} {} - inline AlterInfo* getInfo() const { return info.get(); } + inline const AlterInfo* getInfo() const { return &info; } private: - std::unique_ptr info; + AlterInfo info; }; } // namespace parser diff --git a/src/include/parser/ddl/alter_info.h b/src/include/parser/ddl/alter_info.h index 31d76b4c1d..26d04c81a0 100644 --- a/src/include/parser/ddl/alter_info.h +++ b/src/include/parser/ddl/alter_info.h @@ -2,6 +2,7 @@ #include +#include "common/copy_constructors.h" #include "common/enums/alter_type.h" #include "parser/expression/parsed_expression.h" @@ -20,6 +21,7 @@ struct AlterInfo { AlterInfo( common::AlterType type, std::string tableName, std::unique_ptr extraInfo) : type{type}, tableName{std::move(tableName)}, extraInfo{std::move(extraInfo)} {} + DELETE_COPY_DEFAULT_MOVE(AlterInfo); }; struct ExtraRenameTableInfo : public ExtraAlterInfo { diff --git a/src/include/parser/ddl/create_table.h b/src/include/parser/ddl/create_table.h index f027b94798..722a37584d 100644 --- a/src/include/parser/ddl/create_table.h +++ b/src/include/parser/ddl/create_table.h @@ -1,20 +1,20 @@ #pragma once #include "create_table_info.h" -#include "ddl.h" +#include "parser/statement.h" namespace kuzu { namespace parser { -class CreateTable : public DDL { +class CreateTable final : public Statement { public: - CreateTable(std::string tableName, std::unique_ptr info) - : DDL{common::StatementType::CREATE_TABLE, std::move(tableName)}, info{std::move(info)} {} + explicit CreateTable(CreateTableInfo info) + : Statement{common::StatementType::CREATE_TABLE}, info{std::move(info)} {} - inline CreateTableInfo* getInfo() const { return info.get(); } + inline const CreateTableInfo* getInfo() const { return &info; } private: - std::unique_ptr info; + CreateTableInfo info; }; } // namespace parser diff --git a/src/include/parser/ddl/create_table_info.h b/src/include/parser/ddl/create_table_info.h index cc5665f695..7cac23c113 100644 --- a/src/include/parser/ddl/create_table_info.h +++ b/src/include/parser/ddl/create_table_info.h @@ -5,6 +5,7 @@ #include #include +#include "common/copy_constructors.h" #include "common/enums/table_type.h" namespace kuzu { @@ -22,12 +23,7 @@ struct CreateTableInfo { CreateTableInfo(common::TableType tableType, std::string tableName) : tableType{tableType}, tableName{std::move(tableName)}, extraInfo{nullptr} {} - CreateTableInfo(common::TableType tableType, std::string tableName, - std::vector> propertyNameDataTypes, - std::unique_ptr extraInfo) - : tableType{tableType}, tableName{std::move(tableName)}, - propertyNameDataTypes{std::move(propertyNameDataTypes)}, extraInfo{ - std::move(extraInfo)} {}; + DELETE_COPY_DEFAULT_MOVE(CreateTableInfo); }; struct ExtraCreateNodeTableInfo : public ExtraCreateTableInfo { diff --git a/src/include/parser/ddl/ddl.h b/src/include/parser/ddl/ddl.h deleted file mode 100644 index ea9e9948eb..0000000000 --- a/src/include/parser/ddl/ddl.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include - -#include "parser/statement.h" - -namespace kuzu { -namespace parser { - -class DDL : public Statement { -public: - explicit DDL(common::StatementType statementType, std::string tableName) - : Statement{statementType}, tableName{std::move(tableName)} {} - - inline std::string getTableName() const { return tableName; } - -protected: - std::string tableName; -}; - -} // namespace parser -} // namespace kuzu diff --git a/src/include/parser/expression/parsed_case_expression.h b/src/include/parser/expression/parsed_case_expression.h index 69fcd1d527..0cd4b74a17 100644 --- a/src/include/parser/expression/parsed_case_expression.h +++ b/src/include/parser/expression/parsed_case_expression.h @@ -1,5 +1,6 @@ #pragma once +#include "common/copy_constructors.h" #include "parsed_expression.h" namespace kuzu { @@ -9,17 +10,17 @@ struct ParsedCaseAlternative { std::unique_ptr whenExpression; std::unique_ptr thenExpression; + ParsedCaseAlternative() = default; ParsedCaseAlternative(std::unique_ptr whenExpression, std::unique_ptr thenExpression) : whenExpression{std::move(whenExpression)}, thenExpression{std::move(thenExpression)} {} + ParsedCaseAlternative(const ParsedCaseAlternative& other) + : whenExpression{other.whenExpression->copy()}, thenExpression{ + other.thenExpression->copy()} {} + DEFAULT_BOTH_MOVE(ParsedCaseAlternative); void serialize(common::Serializer& serializer) const; - static std::unique_ptr deserialize(common::Deserializer& deserializer); - - inline std::unique_ptr copy() const { - return std::make_unique( - whenExpression->copy(), thenExpression->copy()); - } + static ParsedCaseAlternative deserialize(common::Deserializer& deserializer); }; // Cypher supports 2 types of CaseExpression @@ -27,16 +28,16 @@ struct ParsedCaseAlternative { // WHEN 20 THEN ... // 2. CASE // WHEN a.age = 20 THEN ... -class ParsedCaseExpression : public ParsedExpression { +class ParsedCaseExpression final : public ParsedExpression { friend class ParsedExpressionChildrenVisitor; public: explicit ParsedCaseExpression(std::string raw) : ParsedExpression{common::ExpressionType::CASE_ELSE, std::move(raw)} {}; - ParsedCaseExpression(std::string alias, std::string rawName, parsed_expression_vector children, + ParsedCaseExpression(std::string alias, std::string rawName, parsed_expr_vector children, std::unique_ptr caseExpression, - std::vector> caseAlternatives, + std::vector caseAlternatives, std::unique_ptr elseExpression) : ParsedExpression{common::ExpressionType::CASE_ELSE, std::move(alias), std::move(rawName), std::move(children)}, @@ -44,7 +45,7 @@ class ParsedCaseExpression : public ParsedExpression { elseExpression{std::move(elseExpression)} {} ParsedCaseExpression(std::unique_ptr caseExpression, - std::vector> caseAlternatives, + std::vector caseAlternatives, std::unique_ptr elseExpression) : ParsedExpression{common::ExpressionType::CASE_ELSE}, caseExpression{std::move( caseExpression)}, @@ -57,12 +58,15 @@ class ParsedCaseExpression : public ParsedExpression { inline bool hasCaseExpression() const { return caseExpression != nullptr; } inline ParsedExpression* getCaseExpression() const { return caseExpression.get(); } - inline void addCaseAlternative(std::unique_ptr caseAlternative) { + inline void addCaseAlternative(ParsedCaseAlternative caseAlternative) { caseAlternatives.push_back(std::move(caseAlternative)); } inline uint32_t getNumCaseAlternative() const { return caseAlternatives.size(); } - inline ParsedCaseAlternative* getCaseAlternative(uint32_t idx) const { - return caseAlternatives[idx].get(); + inline ParsedCaseAlternative* getCaseAlternativeUnsafe(uint32_t idx) { + return &caseAlternatives[idx]; + } + inline const ParsedCaseAlternative* getCaseAlternative(uint32_t idx) const { + return &caseAlternatives[idx]; } inline void setElseExpression(std::unique_ptr expression) { @@ -81,7 +85,7 @@ class ParsedCaseExpression : public ParsedExpression { private: // Optional. If not specified, directly check next whenExpression std::unique_ptr caseExpression; - std::vector> caseAlternatives; + std::vector caseAlternatives; // Optional. If not specified, evaluate as null std::unique_ptr elseExpression; }; diff --git a/src/include/parser/expression/parsed_expression.h b/src/include/parser/expression/parsed_expression.h index 5d86b7b8f6..a8a3257adb 100644 --- a/src/include/parser/expression/parsed_expression.h +++ b/src/include/parser/expression/parsed_expression.h @@ -5,6 +5,7 @@ #include #include +#include "common/copy_constructors.h" #include "common/enums/expression_type.h" namespace kuzu { @@ -19,9 +20,10 @@ namespace parser { class ParsedExpression; class ParsedExpressionChildrenVisitor; -using parsed_expression_vector = std::vector>; -using parsed_expression_pair = +using parsed_expr_vector = std::vector>; +using parsed_expr_pair = std::pair, std::unique_ptr>; +using s_parsed_expr_pair = std::pair>; class ParsedExpression { friend class ParsedExpressionChildrenVisitor; @@ -39,10 +41,10 @@ class ParsedExpression { explicit ParsedExpression(common::ExpressionType type) : type{type} {} ParsedExpression(common::ExpressionType type, std::string alias, std::string rawName, - parsed_expression_vector children) + parsed_expr_vector children) : type{type}, alias{std::move(alias)}, rawName{std::move(rawName)}, children{std::move( children)} {} - + DELETE_COPY_DEFAULT_MOVE(ParsedExpression); virtual ~ParsedExpression() = default; inline common::ExpressionType getExpressionType() const { return type; } @@ -69,16 +71,16 @@ class ParsedExpression { static std::unique_ptr deserialize(common::Deserializer& deserializer); protected: - parsed_expression_vector copyChildren() const; + parsed_expr_vector copyChildren() const; private: - virtual inline void serializeInternal(common::Serializer& /*serializer*/) const {} + virtual inline void serializeInternal(common::Serializer&) const {} protected: common::ExpressionType type; std::string alias; std::string rawName; - parsed_expression_vector children; + parsed_expr_vector children; }; using parsing_option_t = std::unordered_map>; diff --git a/src/include/parser/expression/parsed_function_expression.h b/src/include/parser/expression/parsed_function_expression.h index 63fe777df3..1c9e7951c4 100644 --- a/src/include/parser/expression/parsed_function_expression.h +++ b/src/include/parser/expression/parsed_function_expression.h @@ -22,8 +22,8 @@ class ParsedFunctionExpression : public ParsedExpression { std::move(rawName)}, isDistinct{isDistinct}, functionName{std::move(functionName)} {} - ParsedFunctionExpression(std::string alias, std::string rawName, - parsed_expression_vector children, std::string functionName, bool isDistinct) + ParsedFunctionExpression(std::string alias, std::string rawName, parsed_expr_vector children, + std::string functionName, bool isDistinct) : ParsedExpression{common::ExpressionType::FUNCTION, std::move(alias), std::move(rawName), std::move(children)}, isDistinct{isDistinct}, functionName{std::move(functionName)} {} diff --git a/src/include/parser/expression/parsed_literal_expression.h b/src/include/parser/expression/parsed_literal_expression.h index 748d6e7784..bfe14d8208 100644 --- a/src/include/parser/expression/parsed_literal_expression.h +++ b/src/include/parser/expression/parsed_literal_expression.h @@ -8,38 +8,38 @@ namespace parser { class ParsedLiteralExpression : public ParsedExpression { public: - ParsedLiteralExpression(std::unique_ptr value, std::string raw) + ParsedLiteralExpression(common::Value value, std::string raw) : ParsedExpression{common::ExpressionType::LITERAL, std::move(raw)}, value{std::move( value)} {} - ParsedLiteralExpression(std::string alias, std::string rawName, - parsed_expression_vector children, std::unique_ptr value) + ParsedLiteralExpression( + std::string alias, std::string rawName, parsed_expr_vector children, common::Value value) : ParsedExpression{common::ExpressionType::LITERAL, std::move(alias), std::move(rawName), std::move(children)}, value{std::move(value)} {} - explicit ParsedLiteralExpression(std::unique_ptr value) + explicit ParsedLiteralExpression(common::Value value) : ParsedExpression{common::ExpressionType::LITERAL}, value{std::move(value)} {} - inline common::Value* getValue() const { return value.get(); } + inline const common::Value* getValue() const { return &value; } + inline common::Value* getValueUnsafe() { return &value; } static inline std::unique_ptr deserialize( common::Deserializer& deserializer) { - return std::make_unique(common::Value::deserialize(deserializer)); + return std::make_unique(*common::Value::deserialize(deserializer)); } inline std::unique_ptr copy() const override { - return std::make_unique( - alias, rawName, copyChildren(), value->copy()); + return std::make_unique(alias, rawName, copyChildren(), value); } private: void serializeInternal(common::Serializer& serializer) const override { - value->serialize(serializer); + value.serialize(serializer); } private: - std::unique_ptr value; + common::Value value; }; } // namespace parser diff --git a/src/include/parser/expression/parsed_parameter_expression.h b/src/include/parser/expression/parsed_parameter_expression.h index dee7b73eef..b9cf61cbb6 100644 --- a/src/include/parser/expression/parsed_parameter_expression.h +++ b/src/include/parser/expression/parsed_parameter_expression.h @@ -14,15 +14,14 @@ class ParsedParameterExpression : public ParsedExpression { inline std::string getParameterName() const { return parameterName; } - static std::unique_ptr deserialize( - common::Deserializer& /*deserializer*/) { + static std::unique_ptr deserialize(common::Deserializer&) { KU_UNREACHABLE; } inline std::unique_ptr copy() const override { KU_UNREACHABLE; } private: - void serializeInternal(common::Serializer& /*serializer*/) const override { KU_UNREACHABLE; } + void serializeInternal(common::Serializer&) const override { KU_UNREACHABLE; } private: std::string parameterName; diff --git a/src/include/parser/expression/parsed_property_expression.h b/src/include/parser/expression/parsed_property_expression.h index 60e2698f3a..c89e21c520 100644 --- a/src/include/parser/expression/parsed_property_expression.h +++ b/src/include/parser/expression/parsed_property_expression.h @@ -14,8 +14,8 @@ class ParsedPropertyExpression : public ParsedExpression { : ParsedExpression{common::ExpressionType::PROPERTY, std::move(child), std::move(raw)}, propertyName{std::move(propertyName)} {} - ParsedPropertyExpression(std::string alias, std::string rawName, - parsed_expression_vector children, std::string propertyName) + ParsedPropertyExpression(std::string alias, std::string rawName, parsed_expr_vector children, + std::string propertyName) : ParsedExpression{common::ExpressionType::PROPERTY, std::move(alias), std::move(rawName), std::move(children)}, propertyName{std::move(propertyName)} {} diff --git a/src/include/parser/expression/parsed_subquery_expression.h b/src/include/parser/expression/parsed_subquery_expression.h index 5dfdb53f7d..bbd1d57a76 100644 --- a/src/include/parser/expression/parsed_subquery_expression.h +++ b/src/include/parser/expression/parsed_subquery_expression.h @@ -16,35 +16,32 @@ class ParsedSubqueryExpression : public ParsedExpression { inline common::SubqueryType getSubqueryType() const { return subqueryType; } - inline void addPatternElement(std::unique_ptr element) { + inline void addPatternElement(PatternElement element) { patternElements.push_back(std::move(element)); } - inline void setPatternElements(std::vector> elements) { + inline void setPatternElements(std::vector elements) { patternElements = std::move(elements); } - inline const std::vector>& getPatternElements() const { - return patternElements; - } + inline const std::vector& getPatternElements() const { return patternElements; } inline void setWhereClause(std::unique_ptr expression) { whereClause = std::move(expression); } inline bool hasWhereClause() const { return whereClause != nullptr; } - inline ParsedExpression* getWhereClause() const { return whereClause.get(); } + inline const ParsedExpression* getWhereClause() const { return whereClause.get(); } - static std::unique_ptr deserialize( - common::Deserializer& /*deserializer*/) { + static std::unique_ptr deserialize(common::Deserializer&) { KU_UNREACHABLE; } std::unique_ptr copy() const override { KU_UNREACHABLE; } private: - void serializeInternal(common::Serializer& /*serializer*/) const override { KU_UNREACHABLE; } + void serializeInternal(common::Serializer&) const override { KU_UNREACHABLE; } private: common::SubqueryType subqueryType; - std::vector> patternElements; + std::vector patternElements; std::unique_ptr whereClause; }; diff --git a/src/include/parser/expression/parsed_variable_expression.h b/src/include/parser/expression/parsed_variable_expression.h index e8e8199da9..40618c0adc 100644 --- a/src/include/parser/expression/parsed_variable_expression.h +++ b/src/include/parser/expression/parsed_variable_expression.h @@ -13,8 +13,8 @@ class ParsedVariableExpression : public ParsedExpression { : ParsedExpression{common::ExpressionType::VARIABLE, std::move(raw)}, variableName{std::move(variableName)} {} - ParsedVariableExpression(std::string alias, std::string rawName, - parsed_expression_vector children, std::string variableName) + ParsedVariableExpression(std::string alias, std::string rawName, parsed_expr_vector children, + std::string variableName) : ParsedExpression{common::ExpressionType::VARIABLE, std::move(alias), std::move(rawName), std::move(children)}, variableName{std::move(variableName)} {} diff --git a/src/include/parser/query/graph_pattern/node_pattern.h b/src/include/parser/query/graph_pattern/node_pattern.h index 794b2896e7..80a4474cae 100644 --- a/src/include/parser/query/graph_pattern/node_pattern.h +++ b/src/include/parser/query/graph_pattern/node_pattern.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include "parser/expression/parsed_expression.h" @@ -8,17 +7,13 @@ namespace kuzu { namespace parser { -/** - * NodePattern represents "(nodeName:NodeTable+ {p1:v1, p2:v2, ...})" - */ class NodePattern { public: - using property_key_val_t = std::pair>; - NodePattern(std::string name, std::vector tableNames, - std::vector propertyKeyVals) + std::vector propertyKeyVals) : variableName{std::move(name)}, tableNames{std::move(tableNames)}, propertyKeyVals{std::move(propertyKeyVals)} {} + DELETE_COPY_DEFAULT_MOVE(NodePattern); virtual ~NodePattern() = default; @@ -26,14 +21,14 @@ class NodePattern { inline std::vector getTableNames() const { return tableNames; } - inline const std::vector& getPropertyKeyVals() const { + inline const std::vector& getPropertyKeyVals() const { return propertyKeyVals; } protected: std::string variableName; std::vector tableNames; - std::vector propertyKeyVals; + std::vector propertyKeyVals; }; } // namespace parser diff --git a/src/include/parser/query/graph_pattern/pattern_element.h b/src/include/parser/query/graph_pattern/pattern_element.h index 7b93c9d113..83c66d56c7 100644 --- a/src/include/parser/query/graph_pattern/pattern_element.h +++ b/src/include/parser/query/graph_pattern/pattern_element.h @@ -7,35 +7,29 @@ namespace kuzu { namespace parser { -/** - * PatternElement represents "NodePattern - PatternElementChain - ..." - */ class PatternElement { - public: - explicit PatternElement(std::unique_ptr nodePattern) - : nodePattern{std::move(nodePattern)} {} - - ~PatternElement() = default; + explicit PatternElement(NodePattern nodePattern) : nodePattern{std::move(nodePattern)} {} + DELETE_COPY_DEFAULT_MOVE(PatternElement); inline void setPathName(std::string name) { pathName = std::move(name); } inline bool hasPathName() const { return !pathName.empty(); } inline std::string getPathName() const { return pathName; } - inline NodePattern* getFirstNodePattern() const { return nodePattern.get(); } + inline const NodePattern* getFirstNodePattern() const { return &nodePattern; } - inline void addPatternElementChain(std::unique_ptr patternElementChain) { - patternElementChains.push_back(std::move(patternElementChain)); + inline void addPatternElementChain(PatternElementChain chain) { + patternElementChains.push_back(std::move(chain)); } inline uint32_t getNumPatternElementChains() const { return patternElementChains.size(); } - inline PatternElementChain* getPatternElementChain(uint32_t idx) const { - return patternElementChains[idx].get(); + inline const PatternElementChain* getPatternElementChain(uint32_t idx) const { + return &patternElementChains[idx]; } private: std::string pathName; - std::unique_ptr nodePattern; - std::vector> patternElementChains; + NodePattern nodePattern; + std::vector patternElementChains; }; } // namespace parser diff --git a/src/include/parser/query/graph_pattern/pattern_element_chain.h b/src/include/parser/query/graph_pattern/pattern_element_chain.h index ea006fcaec..e6830d175c 100644 --- a/src/include/parser/query/graph_pattern/pattern_element_chain.h +++ b/src/include/parser/query/graph_pattern/pattern_element_chain.h @@ -5,25 +5,19 @@ namespace kuzu { namespace parser { -/** - * PatternElementChain represents " - RelPattern - NodePattern" - */ class PatternElementChain { - public: - PatternElementChain( - std::unique_ptr relPattern, std::unique_ptr nodePattern) + PatternElementChain(RelPattern relPattern, NodePattern nodePattern) : relPattern{std::move(relPattern)}, nodePattern{std::move(nodePattern)} {} + DELETE_COPY_DEFAULT_MOVE(PatternElementChain); - ~PatternElementChain() = default; - - inline RelPattern* getRelPattern() const { return relPattern.get(); } + inline const RelPattern* getRelPattern() const { return &relPattern; } - inline NodePattern* getNodePattern() const { return nodePattern.get(); } + inline const NodePattern* getNodePattern() const { return &nodePattern; } private: - std::unique_ptr relPattern; - std::unique_ptr nodePattern; + RelPattern relPattern; + NodePattern nodePattern; }; } // namespace parser diff --git a/src/include/parser/query/graph_pattern/rel_pattern.h b/src/include/parser/query/graph_pattern/rel_pattern.h index e5aa47f3d1..424580f7bb 100644 --- a/src/include/parser/query/graph_pattern/rel_pattern.h +++ b/src/include/parser/query/graph_pattern/rel_pattern.h @@ -1,5 +1,6 @@ #pragma once +#include "common/copy_constructors.h" #include "common/enums/query_rel_type.h" #include "node_pattern.h" @@ -13,41 +14,35 @@ struct RecursiveRelPatternInfo { std::string upperBound; std::string relName; std::string nodeName; - std::unique_ptr whereExpression; - bool hasProjection; - std::vector> relProjectionList; - std::vector> nodeProjectionList; - - RecursiveRelPatternInfo(std::string lowerBound, std::string upperBound) - : lowerBound{std::move(lowerBound)}, upperBound{std::move(upperBound)}, hasProjection{ - false} {} + std::unique_ptr whereExpression = nullptr; + bool hasProjection = false; + parsed_expr_vector relProjectionList; + parsed_expr_vector nodeProjectionList; + + RecursiveRelPatternInfo() = default; + DELETE_COPY_DEFAULT_MOVE(RecursiveRelPatternInfo); }; -/** - * RelationshipPattern represents "-[relName:RelTableName+]-" - */ class RelPattern : public NodePattern { public: RelPattern(std::string name, std::vector tableNames, common::QueryRelType relType, - ArrowDirection arrowDirection, - std::vector>> propertyKeyValPairs, - std::unique_ptr recursiveInfo) + ArrowDirection arrowDirection, std::vector propertyKeyValPairs, + RecursiveRelPatternInfo recursiveInfo) : NodePattern{std::move(name), std::move(tableNames), std::move(propertyKeyValPairs)}, relType{relType}, arrowDirection{arrowDirection}, recursiveInfo{ std::move(recursiveInfo)} {} - - ~RelPattern() override = default; + DELETE_COPY_DEFAULT_MOVE(RelPattern); inline common::QueryRelType getRelType() const { return relType; } inline ArrowDirection getDirection() const { return arrowDirection; } - inline RecursiveRelPatternInfo* getRecursiveInfo() const { return recursiveInfo.get(); } + inline const RecursiveRelPatternInfo* getRecursiveInfo() const { return &recursiveInfo; } private: common::QueryRelType relType; ArrowDirection arrowDirection; - std::unique_ptr recursiveInfo; + RecursiveRelPatternInfo recursiveInfo; }; } // namespace parser diff --git a/src/include/parser/query/query_part.h b/src/include/parser/query/query_part.h index f50f985c75..c4751d5e05 100644 --- a/src/include/parser/query/query_part.h +++ b/src/include/parser/query/query_part.h @@ -8,10 +8,8 @@ namespace kuzu { namespace parser { class QueryPart { - public: - explicit QueryPart(std::unique_ptr withClause) - : withClause{std::move(withClause)} {} + explicit QueryPart(WithClause withClause) : withClause{std::move(withClause)} {} inline uint32_t getNumUpdatingClauses() const { return updatingClauses.size(); } inline UpdatingClause* getUpdatingClause(uint32_t idx) const { @@ -27,12 +25,12 @@ class QueryPart { readingClauses.push_back(std::move(readingClause)); } - inline WithClause* getWithClause() const { return withClause.get(); } + inline const WithClause* getWithClause() const { return &withClause; } private: std::vector> readingClauses; std::vector> updatingClauses; - std::unique_ptr withClause; + WithClause withClause; }; } // namespace parser diff --git a/src/include/parser/query/reading_clause/in_query_call_clause.h b/src/include/parser/query/reading_clause/in_query_call_clause.h index 7af5c85e3b..d79e8d954f 100644 --- a/src/include/parser/query/reading_clause/in_query_call_clause.h +++ b/src/include/parser/query/reading_clause/in_query_call_clause.h @@ -6,18 +6,19 @@ namespace kuzu { namespace parser { -class InQueryCallClause : public ReadingClause { +class InQueryCallClause final : public ReadingClause { public: explicit InQueryCallClause(std::unique_ptr functionExpression) : ReadingClause{common::ClauseType::IN_QUERY_CALL}, functionExpression{ std::move(functionExpression)} {} - ParsedExpression* getFunctionExpression() const { return functionExpression.get(); } + + inline ParsedExpression* getFunctionExpression() const { return functionExpression.get(); } inline void setWherePredicate(std::unique_ptr expression) { wherePredicate = std::move(expression); } inline bool hasWherePredicate() const { return wherePredicate != nullptr; } - inline ParsedExpression* getWherePredicate() const { return wherePredicate.get(); } + inline const ParsedExpression* getWherePredicate() const { return wherePredicate.get(); } private: std::unique_ptr functionExpression; diff --git a/src/include/parser/query/reading_clause/load_from.h b/src/include/parser/query/reading_clause/load_from.h index aa605d7d3d..b455b9053f 100644 --- a/src/include/parser/query/reading_clause/load_from.h +++ b/src/include/parser/query/reading_clause/load_from.h @@ -29,7 +29,7 @@ class LoadFrom : public ReadingClause { wherePredicate = std::move(expression); } inline bool hasWherePredicate() const { return wherePredicate != nullptr; } - inline ParsedExpression* getWherePredicate() const { return wherePredicate.get(); } + inline const ParsedExpression* getWherePredicate() const { return wherePredicate.get(); } private: std::vector filePaths; diff --git a/src/include/parser/query/reading_clause/match_clause.h b/src/include/parser/query/reading_clause/match_clause.h index 3034c60d39..05f3975524 100644 --- a/src/include/parser/query/reading_clause/match_clause.h +++ b/src/include/parser/query/reading_clause/match_clause.h @@ -9,13 +9,12 @@ namespace parser { class MatchClause : public ReadingClause { public: - MatchClause(std::vector> patternElements, - common::MatchClauseType matchClauseType) + MatchClause( + std::vector patternElements, common::MatchClauseType matchClauseType) : ReadingClause{common::ClauseType::MATCH}, patternElements{std::move(patternElements)}, matchClauseType{matchClauseType} {} - ~MatchClause() override = default; - inline const std::vector>& getPatternElements() const { + inline const std::vector& getPatternElementsRef() const { return patternElements; } @@ -23,12 +22,12 @@ class MatchClause : public ReadingClause { wherePredicate = std::move(expression); } inline bool hasWherePredicate() const { return wherePredicate != nullptr; } - inline ParsedExpression* getWherePredicate() const { return wherePredicate.get(); } + inline const ParsedExpression* getWherePredicate() const { return wherePredicate.get(); } inline common::MatchClauseType getMatchClauseType() const { return matchClauseType; } private: - std::vector> patternElements; + std::vector patternElements; std::unique_ptr wherePredicate; common::MatchClauseType matchClauseType; }; diff --git a/src/include/parser/query/reading_clause/unwind_clause.h b/src/include/parser/query/reading_clause/unwind_clause.h index 8d54a3d497..c3554cc94c 100644 --- a/src/include/parser/query/reading_clause/unwind_clause.h +++ b/src/include/parser/query/reading_clause/unwind_clause.h @@ -7,15 +7,12 @@ namespace kuzu { namespace parser { class UnwindClause : public ReadingClause { - public: explicit UnwindClause(std::unique_ptr expression, std::string listAlias) : ReadingClause{common::ClauseType::UNWIND}, expression{std::move(expression)}, alias{std::move(listAlias)} {} - ~UnwindClause() override = default; - - inline ParsedExpression* getExpression() const { return expression.get(); } + inline const ParsedExpression* getExpression() const { return expression.get(); } inline std::string getAlias() const { return alias; } diff --git a/src/include/parser/query/regular_query.h b/src/include/parser/query/regular_query.h index ab3d6f03de..07288a7af8 100644 --- a/src/include/parser/query/regular_query.h +++ b/src/include/parser/query/regular_query.h @@ -7,28 +7,26 @@ namespace kuzu { namespace parser { class RegularQuery : public Statement { - public: - explicit RegularQuery(std::unique_ptr singleQuery) - : Statement{common::StatementType::QUERY} { + explicit RegularQuery(SingleQuery singleQuery) : Statement{common::StatementType::QUERY} { singleQueries.push_back(std::move(singleQuery)); } - inline void addSingleQuery(std::unique_ptr singleQuery, bool isUnionAllQuery) { + inline void addSingleQuery(SingleQuery singleQuery, bool isUnionAllQuery) { singleQueries.push_back(std::move(singleQuery)); isUnionAll.push_back(isUnionAllQuery); } inline uint64_t getNumSingleQueries() const { return singleQueries.size(); } - inline SingleQuery* getSingleQuery(uint32_t singleQueryIdx) const { - return singleQueries[singleQueryIdx].get(); + inline const SingleQuery* getSingleQuery(uint32_t singleQueryIdx) const { + return &singleQueries[singleQueryIdx]; } inline std::vector getIsUnionAll() const { return isUnionAll; } private: - std::vector> singleQueries; + std::vector singleQueries; std::vector isUnionAll; }; diff --git a/src/include/parser/query/return_with_clause/projection_body.h b/src/include/parser/query/return_with_clause/projection_body.h index c6a8a6a0b0..697557f89b 100644 --- a/src/include/parser/query/return_with_clause/projection_body.h +++ b/src/include/parser/query/return_with_clause/projection_body.h @@ -1,5 +1,6 @@ #pragma once +#include "common/copy_constructors.h" #include "parser/expression/parsed_expression.h" namespace kuzu { @@ -10,6 +11,7 @@ class ProjectionBody { ProjectionBody( bool isDistinct, std::vector> projectionExpressions) : isDistinct{isDistinct}, projectionExpressions{std::move(projectionExpressions)} {} + DELETE_COPY_DEFAULT_MOVE(ProjectionBody); inline bool getIsDistinct() const { return isDistinct; } @@ -23,7 +25,6 @@ class ProjectionBody { isAscOrders = std::move(sortOrders); } inline bool hasOrderByExpressions() const { return !orderByExpressions.empty(); } - inline uint32_t numOrderByExpressions() const { return orderByExpressions.size(); } inline const std::vector>& getOrderByExpressions() const { return orderByExpressions; } diff --git a/src/include/parser/query/return_with_clause/return_clause.h b/src/include/parser/query/return_with_clause/return_clause.h index 0450256f62..0583497b37 100644 --- a/src/include/parser/query/return_with_clause/return_clause.h +++ b/src/include/parser/query/return_with_clause/return_clause.h @@ -6,17 +6,17 @@ namespace kuzu { namespace parser { class ReturnClause { - public: - explicit ReturnClause(std::unique_ptr projectionBody) + explicit ReturnClause(ProjectionBody projectionBody) : projectionBody{std::move(projectionBody)} {} + DELETE_COPY_DEFAULT_MOVE(ReturnClause); virtual ~ReturnClause() = default; - inline ProjectionBody* getProjectionBody() const { return projectionBody.get(); } + inline const ProjectionBody* getProjectionBody() const { return &projectionBody; } private: - std::unique_ptr projectionBody; + ProjectionBody projectionBody; }; } // namespace parser diff --git a/src/include/parser/query/return_with_clause/with_clause.h b/src/include/parser/query/return_with_clause/with_clause.h index 4b2a4dae77..4a75e8aee5 100644 --- a/src/include/parser/query/return_with_clause/with_clause.h +++ b/src/include/parser/query/return_with_clause/with_clause.h @@ -5,14 +5,10 @@ namespace kuzu { namespace parser { -/** - * WIth clause may have where expression - */ class WithClause : public ReturnClause { - public: - explicit WithClause(std::unique_ptr projectionBody) - : ReturnClause{std::move(projectionBody)} {} + explicit WithClause(ProjectionBody projectionBody) : ReturnClause{std::move(projectionBody)} {} + DELETE_COPY_DEFAULT_MOVE(WithClause); inline void setWhereExpression(std::unique_ptr expression) { whereExpression = std::move(expression); diff --git a/src/include/parser/query/single_query.h b/src/include/parser/query/single_query.h index a69f6d8dbd..9e95aed621 100644 --- a/src/include/parser/query/single_query.h +++ b/src/include/parser/query/single_query.h @@ -1,21 +1,22 @@ #pragma once +#include + +#include "common/assert.h" +#include "common/copy_constructors.h" #include "query_part.h" namespace kuzu { namespace parser { class SingleQuery { - public: SingleQuery() = default; - ~SingleQuery() = default; + DELETE_COPY_DEFAULT_MOVE(SingleQuery); - inline void addQueryPart(std::unique_ptr queryPart) { - queryParts.push_back(std::move(queryPart)); - } + inline void addQueryPart(QueryPart queryPart) { queryParts.push_back(std::move(queryPart)); } inline uint32_t getNumQueryParts() const { return queryParts.size(); } - inline QueryPart* getQueryPart(uint32_t idx) const { return queryParts[idx].get(); } + inline const QueryPart* getQueryPart(uint32_t idx) const { return &queryParts[idx]; } inline uint32_t getNumUpdatingClauses() const { return updatingClauses.size(); } inline UpdatingClause* getUpdatingClause(uint32_t idx) const { @@ -31,17 +32,18 @@ class SingleQuery { readingClauses.push_back(std::move(readingClause)); } - inline void setReturnClause(std::unique_ptr returnClause_) { - this->returnClause = std::move(returnClause_); + inline void setReturnClause(ReturnClause clause) { returnClause = std::move(clause); } + inline bool hasReturnClause() const { return returnClause.has_value(); } + inline const ReturnClause* getReturnClause() const { + KU_ASSERT(returnClause.has_value()); + return &returnClause.value(); } - inline bool hasReturnClause() const { return returnClause != nullptr; } - inline ReturnClause* getReturnClause() const { return returnClause.get(); } private: - std::vector> queryParts; + std::vector queryParts; std::vector> readingClauses; std::vector> updatingClauses; - std::unique_ptr returnClause; + std::optional returnClause; }; } // namespace parser diff --git a/src/include/parser/query/updating_clause/delete_clause.h b/src/include/parser/query/updating_clause/delete_clause.h index eec687cf89..30afcfe1d3 100644 --- a/src/include/parser/query/updating_clause/delete_clause.h +++ b/src/include/parser/query/updating_clause/delete_clause.h @@ -20,7 +20,7 @@ class DeleteClause final : public UpdatingClause { private: common::DeleteClauseType deleteClauseType; - std::vector> expressions; + parsed_expr_vector expressions; }; } // namespace parser diff --git a/src/include/parser/query/updating_clause/insert_clause.h b/src/include/parser/query/updating_clause/insert_clause.h index 6d67e194dd..ea836013a5 100644 --- a/src/include/parser/query/updating_clause/insert_clause.h +++ b/src/include/parser/query/updating_clause/insert_clause.h @@ -6,18 +6,18 @@ namespace kuzu { namespace parser { -class InsertClause : public UpdatingClause { +class InsertClause final : public UpdatingClause { public: - explicit InsertClause(std::vector> patternElements) + explicit InsertClause(std::vector patternElements) : UpdatingClause{common::ClauseType::INSERT}, patternElements{ std::move(patternElements)} {}; - inline const std::vector>& getPatternElementsRef() const { + inline const std::vector& getPatternElementsRef() const { return patternElements; } private: - std::vector> patternElements; + std::vector patternElements; }; } // namespace parser diff --git a/src/include/parser/query/updating_clause/merge_clause.h b/src/include/parser/query/updating_clause/merge_clause.h index 0f9e89bf28..c1e45b1682 100644 --- a/src/include/parser/query/updating_clause/merge_clause.h +++ b/src/include/parser/query/updating_clause/merge_clause.h @@ -6,34 +6,34 @@ namespace kuzu { namespace parser { -class MergeClause : public UpdatingClause { +class MergeClause final : public UpdatingClause { public: - explicit MergeClause(std::vector> patternElements) + explicit MergeClause(std::vector patternElements) : UpdatingClause{common::ClauseType::MERGE}, patternElements{std::move(patternElements)} {} - inline const std::vector>& getPatternElementsRef() const { + inline const std::vector& getPatternElementsRef() const { return patternElements; } - inline void addOnMatchSetItems(parsed_expression_pair setItem) { + inline void addOnMatchSetItems(parsed_expr_pair setItem) { onMatchSetItems.push_back(std::move(setItem)); } inline bool hasOnMatchSetItems() const { return !onMatchSetItems.empty(); } - inline const std::vector& getOnMatchSetItemsRef() const { + inline const std::vector& getOnMatchSetItemsRef() const { return onMatchSetItems; } - inline void addOnCreateSetItems(parsed_expression_pair setItem) { + inline void addOnCreateSetItems(parsed_expr_pair setItem) { onCreateSetItems.push_back(std::move(setItem)); } inline bool hasOnCreateSetItems() const { return !onCreateSetItems.empty(); } - inline const std::vector& getOnCreateSetItemsRef() const { + inline const std::vector& getOnCreateSetItemsRef() const { return onCreateSetItems; } private: - std::vector> patternElements; - std::vector onMatchSetItems; - std::vector onCreateSetItems; + std::vector patternElements; + std::vector onMatchSetItems; + std::vector onCreateSetItems; }; } // namespace parser diff --git a/src/include/parser/query/updating_clause/set_clause.h b/src/include/parser/query/updating_clause/set_clause.h index 07e4e5f048..d8b74dc4c9 100644 --- a/src/include/parser/query/updating_clause/set_clause.h +++ b/src/include/parser/query/updating_clause/set_clause.h @@ -6,17 +6,15 @@ namespace kuzu { namespace parser { -class SetClause : public UpdatingClause { +class SetClause final : public UpdatingClause { public: SetClause() : UpdatingClause{common::ClauseType::SET} {}; - inline void addSetItem(parsed_expression_pair setItem) { - setItems.push_back(std::move(setItem)); - } - inline const std::vector& getSetItemsRef() const { return setItems; } + inline void addSetItem(parsed_expr_pair setItem) { setItems.push_back(std::move(setItem)); } + inline const std::vector& getSetItemsRef() const { return setItems; } private: - std::vector setItems; + std::vector setItems; }; } // namespace parser diff --git a/src/include/parser/transformer.h b/src/include/parser/transformer.h index a5f5a448c9..5cb6da3122 100644 --- a/src/include/parser/transformer.h +++ b/src/include/parser/transformer.h @@ -35,7 +35,14 @@ class Transformer { private: std::unique_ptr transformStatement(CypherParser::OC_StatementContext& ctx); - /* Copy From */ + std::unique_ptr transformWhere(CypherParser::OC_WhereContext& ctx); + + std::string transformVariable(CypherParser::OC_VariableContext& ctx); + std::string transformSchemaName(CypherParser::OC_SchemaNameContext& ctx); + std::string transformSymbolicName(CypherParser::OC_SymbolicNameContext& ctx); + std::string transformStringLiteral(antlr4::tree::TerminalNode& stringLiteral); + + // Transform copy statement. std::unique_ptr transformCopyTo(CypherParser::KU_CopyTOContext& ctx); std::unique_ptr transformCopyFrom(CypherParser::KU_CopyFromContext& ctx); std::unique_ptr transformCopyFromByColumn( @@ -43,259 +50,173 @@ class Transformer { std::vector transformColumnNames(CypherParser::KU_ColumnNamesContext& ctx); std::vector transformFilePaths( const std::vector& stringLiteral); - std::unordered_map> transformParsingOptions( - CypherParser::KU_ParsingOptionsContext& ctx); + parsing_option_t transformParsingOptions(CypherParser::KU_ParsingOptionsContext& ctx); - std::unique_ptr transformQuery(CypherParser::OC_QueryContext& ctx); - - std::unique_ptr transformRegularQuery(CypherParser::OC_RegularQueryContext& ctx); - - std::unique_ptr transformSingleQuery(CypherParser::OC_SingleQueryContext& ctx); - - std::unique_ptr transformSinglePartQuery( - CypherParser::OC_SinglePartQueryContext& ctx); - - std::unique_ptr transformQueryPart(CypherParser::KU_QueryPartContext& ctx); + // Transform query statement. + std::unique_ptr transformQuery(CypherParser::OC_QueryContext& ctx); + std::unique_ptr transformRegularQuery(CypherParser::OC_RegularQueryContext& ctx); + SingleQuery transformSingleQuery(CypherParser::OC_SingleQueryContext& ctx); + SingleQuery transformSinglePartQuery(CypherParser::OC_SinglePartQueryContext& ctx); + QueryPart transformQueryPart(CypherParser::KU_QueryPartContext& ctx); + // Transform updating. std::unique_ptr transformUpdatingClause( CypherParser::OC_UpdatingClauseContext& ctx); + std::unique_ptr transformCreate(CypherParser::OC_CreateContext& ctx); + std::unique_ptr transformMerge(CypherParser::OC_MergeContext& ctx); + std::unique_ptr transformSet(CypherParser::OC_SetContext& ctx); + parsed_expr_pair transformSetItem(CypherParser::OC_SetItemContext& ctx); + std::unique_ptr transformDelete(CypherParser::OC_DeleteContext& ctx); + // Transform reading. std::unique_ptr transformReadingClause( CypherParser::OC_ReadingClauseContext& ctx); - std::unique_ptr transformMatch(CypherParser::OC_MatchContext& ctx); std::unique_ptr transformUnwind(CypherParser::OC_UnwindContext& ctx); std::unique_ptr transformInQueryCall(CypherParser::KU_InQueryCallContext& ctx); std::unique_ptr transformLoadFrom(CypherParser::KU_LoadFromContext& ctx); - std::unique_ptr transformCreate(CypherParser::OC_CreateContext& ctx); - - std::unique_ptr transformMerge(CypherParser::OC_MergeContext& ctx); - - std::unique_ptr transformSet(CypherParser::OC_SetContext& ctx); - - parsed_expression_pair transformSetItem(CypherParser::OC_SetItemContext& ctx); - - std::unique_ptr transformDelete(CypherParser::OC_DeleteContext& ctx); - - std::unique_ptr transformWith(CypherParser::OC_WithContext& ctx); - - std::unique_ptr transformReturn(CypherParser::OC_ReturnContext& ctx); - - std::unique_ptr transformProjectionBody( - CypherParser::OC_ProjectionBodyContext& ctx); - + // Transform projection. + WithClause transformWith(CypherParser::OC_WithContext& ctx); + ReturnClause transformReturn(CypherParser::OC_ReturnContext& ctx); + ProjectionBody transformProjectionBody(CypherParser::OC_ProjectionBodyContext& ctx); std::vector> transformProjectionItems( CypherParser::OC_ProjectionItemsContext& ctx); - std::unique_ptr transformProjectionItem( CypherParser::OC_ProjectionItemContext& ctx); - std::unique_ptr transformWhere(CypherParser::OC_WhereContext& ctx); - - std::vector> transformPattern( - CypherParser::OC_PatternContext& ctx); - - std::unique_ptr transformPatternPart(CypherParser::OC_PatternPartContext& ctx); - - std::unique_ptr transformAnonymousPatternPart( - CypherParser::OC_AnonymousPatternPartContext& ctx); - - std::unique_ptr transformPatternElement( - CypherParser::OC_PatternElementContext& ctx); - - std::unique_ptr transformNodePattern(CypherParser::OC_NodePatternContext& ctx); - - std::unique_ptr transformPatternElementChain( + // Transform graph pattern. + std::vector transformPattern(CypherParser::OC_PatternContext& ctx); + PatternElement transformPatternPart(CypherParser::OC_PatternPartContext& ctx); + PatternElement transformAnonymousPatternPart(CypherParser::OC_AnonymousPatternPartContext& ctx); + PatternElement transformPatternElement(CypherParser::OC_PatternElementContext& ctx); + NodePattern transformNodePattern(CypherParser::OC_NodePatternContext& ctx); + PatternElementChain transformPatternElementChain( CypherParser::OC_PatternElementChainContext& ctx); - - std::unique_ptr transformRelationshipPattern( - CypherParser::OC_RelationshipPatternContext& ctx); - - std::vector>> transformProperties( - CypherParser::KU_PropertiesContext& ctx); - + RelPattern transformRelationshipPattern(CypherParser::OC_RelationshipPatternContext& ctx); + std::vector transformProperties(CypherParser::KU_PropertiesContext& ctx); std::vector transformRelTypes(CypherParser::OC_RelationshipTypesContext& ctx); - std::vector transformNodeLabels(CypherParser::OC_NodeLabelsContext& ctx); - std::string transformNodeLabel(CypherParser::OC_NodeLabelContext& ctx); - std::string transformLabelName(CypherParser::OC_LabelNameContext& ctx); - std::string transformRelTypeName(CypherParser::OC_RelTypeNameContext& ctx); + // Transform expression. std::unique_ptr transformExpression(CypherParser::OC_ExpressionContext& ctx); - std::unique_ptr transformOrExpression( CypherParser::OC_OrExpressionContext& ctx); - std::unique_ptr transformXorExpression( CypherParser::OC_XorExpressionContext& ctx); - std::unique_ptr transformAndExpression( CypherParser::OC_AndExpressionContext& ctx); - std::unique_ptr transformNotExpression( CypherParser::OC_NotExpressionContext& ctx); - std::unique_ptr transformComparisonExpression( CypherParser::OC_ComparisonExpressionContext& ctx); - std::unique_ptr transformBitwiseOrOperatorExpression( CypherParser::KU_BitwiseOrOperatorExpressionContext& ctx); - std::unique_ptr transformBitwiseAndOperatorExpression( CypherParser::KU_BitwiseAndOperatorExpressionContext& ctx); - std::unique_ptr transformBitShiftOperatorExpression( CypherParser::KU_BitShiftOperatorExpressionContext& ctx); - std::unique_ptr transformAddOrSubtractExpression( CypherParser::OC_AddOrSubtractExpressionContext& ctx); - std::unique_ptr transformMultiplyDivideModuloExpression( CypherParser::OC_MultiplyDivideModuloExpressionContext& ctx); - std::unique_ptr transformPowerOfExpression( CypherParser::OC_PowerOfExpressionContext& ctx); - std::unique_ptr transformUnaryAddSubtractOrFactorialExpression( CypherParser::OC_UnaryAddSubtractOrFactorialExpressionContext& ctx); - std::unique_ptr transformStringListNullOperatorExpression( CypherParser::OC_StringListNullOperatorExpressionContext& ctx); - std::unique_ptr transformStringOperatorExpression( CypherParser::OC_StringOperatorExpressionContext& ctx, std::unique_ptr propertyExpression); - std::unique_ptr transformListOperatorExpression( CypherParser::OC_ListOperatorExpressionContext& ctx, std::unique_ptr childExpression); - std::unique_ptr transformListSliceOperatorExpression( CypherParser::KU_ListSliceOperatorExpressionContext& ctx, std::unique_ptr propertyExpression); - std::unique_ptr transformListExtractOperatorExpression( CypherParser::KU_ListExtractOperatorExpressionContext& ctx, std::unique_ptr propertyExpression); - std::unique_ptr transformNullOperatorExpression( CypherParser::OC_NullOperatorExpressionContext& ctx, std::unique_ptr propertyExpression); - std::unique_ptr transformPropertyOrLabelsExpression( CypherParser::OC_PropertyOrLabelsExpressionContext& ctx); - std::unique_ptr transformAtom(CypherParser::OC_AtomContext& ctx); - std::unique_ptr transformLiteral(CypherParser::OC_LiteralContext& ctx); - std::unique_ptr transformBooleanLiteral( CypherParser::OC_BooleanLiteralContext& ctx); - std::unique_ptr transformListLiteral( CypherParser::OC_ListLiteralContext& ctx); - std::unique_ptr transformStructLiteral( CypherParser::KU_StructLiteralContext& ctx); - std::unique_ptr transformParameterExpression( CypherParser::OC_ParameterContext& ctx); - std::unique_ptr transformParenthesizedExpression( CypherParser::OC_ParenthesizedExpressionContext& ctx); - std::unique_ptr transformFunctionInvocation( CypherParser::OC_FunctionInvocationContext& ctx); - std::string transformFunctionName(CypherParser::OC_FunctionNameContext& ctx); - std::unique_ptr transformFunctionParameterExpression( CypherParser::KU_FunctionParameterContext& ctx); - std::unique_ptr transformPathPattern( CypherParser::OC_PathPatternsContext& ctx); std::unique_ptr transformExistSubquery( CypherParser::OC_ExistSubqueryContext& ctx); std::unique_ptr transformCountSubquery( CypherParser::KU_CountSubqueryContext& ctx); - std::unique_ptr createPropertyExpression( CypherParser::OC_PropertyLookupContext& ctx, std::unique_ptr child); - std::unique_ptr transformCaseExpression( CypherParser::OC_CaseExpressionContext& ctx); - - std::unique_ptr transformCaseAlternative( - CypherParser::OC_CaseAlternativeContext& ctx); - - std::string transformVariable(CypherParser::OC_VariableContext& ctx); - + ParsedCaseAlternative transformCaseAlternative(CypherParser::OC_CaseAlternativeContext& ctx); std::unique_ptr transformNumberLiteral( CypherParser::OC_NumberLiteralContext& ctx); - std::unique_ptr transformProperty( CypherParser::OC_PropertyExpressionContext& ctx); - std::string transformPropertyKeyName(CypherParser::OC_PropertyKeyNameContext& ctx); - std::unique_ptr transformIntegerLiteral( CypherParser::OC_IntegerLiteralContext& ctx); - std::unique_ptr transformDoubleLiteral( CypherParser::OC_DoubleLiteralContext& ctx); - std::string transformSchemaName(CypherParser::OC_SchemaNameContext& ctx); - - std::string transformSymbolicName(CypherParser::OC_SymbolicNameContext& ctx); - + // Transform ddl. std::unique_ptr transformDDL(CypherParser::KU_DDLContext& ctx); - std::unique_ptr transformAlterTable(CypherParser::KU_AlterTableContext& ctx); - std::unique_ptr transformCreateNodeTable( CypherParser::KU_CreateNodeTableContext& ctx); - std::unique_ptr transformCreateRelTable(CypherParser::KU_CreateRelTableContext& ctx); - std::unique_ptr transformCreateRelTableGroup( CypherParser::KU_CreateRelTableGroupContext& ctx); - std::unique_ptr transformCreateRdfGraphClause( CypherParser::KU_CreateRdfGraphContext& ctx); - std::unique_ptr transformDropTable(CypherParser::KU_DropTableContext& ctx); - std::unique_ptr transformRenameTable(CypherParser::KU_AlterTableContext& ctx); - std::unique_ptr transformAddProperty(CypherParser::KU_AlterTableContext& ctx); - std::unique_ptr transformDropProperty(CypherParser::KU_AlterTableContext& ctx); - std::unique_ptr transformRenameProperty(CypherParser::KU_AlterTableContext& ctx); - std::string transformDataType(CypherParser::KU_DataTypeContext& ctx); - std::string transformPrimaryKey(CypherParser::KU_CreateNodeConstraintContext& ctx); - std::vector> transformPropertyDefinitions( CypherParser::KU_PropertyDefinitionsContext& ctx); + // Transform standalone call. std::unique_ptr transformStandaloneCall(CypherParser::KU_StandaloneCallContext& ctx); - std::vector transformPositionalArgs(CypherParser::KU_PositionalArgsContext& ctx); - + // Transform create macro. std::unique_ptr transformCreateMacro(CypherParser::KU_CreateMacroContext& ctx); + std::vector transformPositionalArgs(CypherParser::KU_PositionalArgsContext& ctx); + // Transform transaction. std::unique_ptr transformTransaction(CypherParser::KU_TransactionContext& ctx); + // Transform comment on. std::unique_ptr transformCommentOn(CypherParser::KU_CommentOnContext& ctx); - std::string transformStringLiteral(antlr4::tree::TerminalNode& stringLiteral); - private: CypherParser::OC_CypherContext& root; }; diff --git a/src/parser/expression/parsed_case_expression.cpp b/src/parser/expression/parsed_case_expression.cpp index 32be138525..9276b6bca5 100644 --- a/src/parser/expression/parsed_case_expression.cpp +++ b/src/parser/expression/parsed_case_expression.cpp @@ -13,20 +13,18 @@ void ParsedCaseAlternative::serialize(Serializer& serializer) const { thenExpression->serialize(serializer); } -std::unique_ptr ParsedCaseAlternative::deserialize( - Deserializer& deserializer) { +ParsedCaseAlternative ParsedCaseAlternative::deserialize(Deserializer& deserializer) { auto whenExpression = ParsedExpression::deserialize(deserializer); auto thenExpression = ParsedExpression::deserialize(deserializer); - return std::make_unique( - std::move(whenExpression), std::move(thenExpression)); + return ParsedCaseAlternative(std::move(whenExpression), std::move(thenExpression)); } std::unique_ptr ParsedCaseExpression::deserialize( Deserializer& deserializer) { std::unique_ptr caseExpression; deserializer.deserializeOptionalValue(caseExpression); - std::vector> caseAlternatives; - deserializer.deserializeVectorOfPtrs(caseAlternatives); + std::vector caseAlternatives; + deserializer.deserializeVector(caseAlternatives); std::unique_ptr elseExpression; deserializer.deserializeOptionalValue(elseExpression); return std::make_unique( @@ -34,10 +32,10 @@ std::unique_ptr ParsedCaseExpression::deserialize( } std::unique_ptr ParsedCaseExpression::copy() const { - std::vector> caseAlternativesCopy; + std::vector caseAlternativesCopy; caseAlternativesCopy.reserve(caseAlternatives.size()); for (auto& caseAlternative : caseAlternatives) { - caseAlternativesCopy.push_back(caseAlternative->copy()); + caseAlternativesCopy.push_back(caseAlternative); } return std::make_unique(alias, rawName, copyChildren(), caseExpression ? caseExpression->copy() : nullptr, std::move(caseAlternativesCopy), @@ -46,7 +44,7 @@ std::unique_ptr ParsedCaseExpression::copy() const { void ParsedCaseExpression::serializeInternal(Serializer& serializer) const { serializer.serializeOptionalValue(caseExpression); - serializer.serializeVectorOfPtrs(caseAlternatives); + serializer.serializeVector(caseAlternatives); serializer.serializeOptionalValue(elseExpression); } diff --git a/src/parser/expression/parsed_expression.cpp b/src/parser/expression/parsed_expression.cpp index 2324a385ce..e3d3c93495 100644 --- a/src/parser/expression/parsed_expression.cpp +++ b/src/parser/expression/parsed_expression.cpp @@ -28,8 +28,8 @@ ParsedExpression::ParsedExpression(ExpressionType type, std::unique_ptrcopy()); @@ -49,7 +49,7 @@ std::unique_ptr ParsedExpression::deserialize(Deserializer& de ExpressionType type; std::string alias; std::string rawName; - parsed_expression_vector children; + parsed_expr_vector children; deserializer.deserializeValue(type); deserializer.deserializeValue(alias); deserializer.deserializeValue(rawName); diff --git a/src/parser/parsed_expression_visitor.cpp b/src/parser/parsed_expression_visitor.cpp index a94a587d80..d3efd05a22 100644 --- a/src/parser/parsed_expression_visitor.cpp +++ b/src/parser/parsed_expression_visitor.cpp @@ -64,7 +64,7 @@ void ParsedExpressionChildrenVisitor::setCaseChild(kuzu::parser::ParsedExpressio parsedCaseExpr.caseExpression = std::move(expressionToSet); } else if (idx < 1 + parsedCaseExpr.getNumCaseAlternative() * 2) { auto caseAlternativeIdx = (idx - 1) / 2; - auto caseAlternative = parsedCaseExpr.getCaseAlternative(caseAlternativeIdx); + auto caseAlternative = parsedCaseExpr.getCaseAlternativeUnsafe(caseAlternativeIdx); if (idx % 2 == 0) { caseAlternative->thenExpression = std::move(expressionToSet); } else { diff --git a/src/parser/parsed_statement_visitor.cpp b/src/parser/parsed_statement_visitor.cpp index 597275a4b5..ad8a94e194 100644 --- a/src/parser/parsed_statement_visitor.cpp +++ b/src/parser/parsed_statement_visitor.cpp @@ -71,7 +71,9 @@ void StatementVisitor::visitSingleQuery(const SingleQuery* singleQuery) { for (auto i = 0u; i < singleQuery->getNumUpdatingClauses(); ++i) { visitUpdatingClause(singleQuery->getUpdatingClause(i)); } - visitReturnClause(singleQuery->getReturnClause()); + if (singleQuery->hasReturnClause()) { + visitReturnClause(singleQuery->getReturnClause()); + } } void StatementVisitor::visitQueryPart(const QueryPart* queryPart) { diff --git a/src/parser/transform/transform_copy.cpp b/src/parser/transform/transform_copy.cpp index b65a31a7a9..9bf52f9413 100644 --- a/src/parser/transform/transform_copy.cpp +++ b/src/parser/transform/transform_copy.cpp @@ -57,8 +57,7 @@ std::vector Transformer::transformFilePaths( return csvFiles; } -std::unordered_map> -Transformer::transformParsingOptions(CypherParser::KU_ParsingOptionsContext& ctx) { +parsing_option_t Transformer::transformParsingOptions(CypherParser::KU_ParsingOptionsContext& ctx) { std::unordered_map> copyOptions; for (auto loadOption : ctx.kU_ParsingOption()) { auto optionName = transformSymbolicName(*loadOption->oC_SymbolicName()); diff --git a/src/parser/transform/transform_ddl.cpp b/src/parser/transform/transform_ddl.cpp index 2104e1cffc..84fc56ec5d 100644 --- a/src/parser/transform/transform_ddl.cpp +++ b/src/parser/transform/transform_ddl.cpp @@ -41,44 +41,41 @@ std::unique_ptr Transformer::transformAlterTable( std::unique_ptr Transformer::transformCreateNodeTable( CypherParser::KU_CreateNodeTableContext& ctx) { auto tableName = transformSchemaName(*ctx.oC_SchemaName()); - auto propertyDefinitions = transformPropertyDefinitions(*ctx.kU_PropertyDefinitions()); std::string pkName; if (ctx.kU_CreateNodeConstraint()) { pkName = transformPrimaryKey(*ctx.kU_CreateNodeConstraint()); } - auto extraInfo = std::make_unique(pkName); - auto createTableInfo = std::make_unique( - TableType::NODE, tableName, propertyDefinitions, std::move(extraInfo)); - return std::make_unique(tableName, std::move(createTableInfo)); + auto createTableInfo = CreateTableInfo(TableType::NODE, tableName); + createTableInfo.propertyNameDataTypes = + transformPropertyDefinitions(*ctx.kU_PropertyDefinitions()); + createTableInfo.extraInfo = std::make_unique(pkName); + return std::make_unique(std::move(createTableInfo)); } std::unique_ptr Transformer::transformCreateRelTable( CypherParser::KU_CreateRelTableContext& ctx) { auto tableName = transformSchemaName(*ctx.oC_SchemaName()); - std::vector> propertyDefinitions; - if (ctx.kU_PropertyDefinitions()) { - propertyDefinitions = transformPropertyDefinitions(*ctx.kU_PropertyDefinitions()); - } std::string relMultiplicity = "MANY_MANY"; if (ctx.oC_SymbolicName()) { relMultiplicity = transformSymbolicName(*ctx.oC_SymbolicName()); } auto srcTableName = transformSchemaName(*ctx.kU_RelTableConnection()->oC_SchemaName(0)); auto dstTableName = transformSchemaName(*ctx.kU_RelTableConnection()->oC_SchemaName(1)); - auto extraInfo = std::make_unique( + auto createTableInfo = CreateTableInfo(TableType::REL, tableName); + if (ctx.kU_PropertyDefinitions()) { + createTableInfo.propertyNameDataTypes = + transformPropertyDefinitions(*ctx.kU_PropertyDefinitions()); + } + createTableInfo.extraInfo = std::make_unique( relMultiplicity, std::move(srcTableName), std::move(dstTableName)); - auto createTableInfo = std::make_unique( - TableType::REL, tableName, propertyDefinitions, std::move(extraInfo)); - return std::make_unique(tableName, std::move(createTableInfo)); + return std::make_unique(std::move(createTableInfo)); } std::unique_ptr Transformer::transformCreateRelTableGroup( CypherParser::KU_CreateRelTableGroupContext& ctx) { auto tableName = transformSchemaName(*ctx.oC_SchemaName()); std::vector> propertyDefinitions; - if (ctx.kU_PropertyDefinitions()) { - propertyDefinitions = transformPropertyDefinitions(*ctx.kU_PropertyDefinitions()); - } + std::string relMultiplicity = "MANY_MANY"; if (ctx.oC_SymbolicName()) { relMultiplicity = transformSymbolicName(*ctx.oC_SymbolicName()); @@ -89,18 +86,21 @@ std::unique_ptr Transformer::transformCreateRelTableGroup( auto dstTableName = transformSchemaName(*connection->oC_SchemaName(1)); srcDstTablePairs.emplace_back(srcTableName, dstTableName); } - auto extraInfo = std::make_unique( + auto createTableInfo = CreateTableInfo(TableType::REL_GROUP, tableName); + if (ctx.kU_PropertyDefinitions()) { + createTableInfo.propertyNameDataTypes = + transformPropertyDefinitions(*ctx.kU_PropertyDefinitions()); + } + createTableInfo.extraInfo = std::make_unique( relMultiplicity, std::move(srcDstTablePairs)); - auto createTableInfo = std::make_unique( - TableType::REL_GROUP, tableName, propertyDefinitions, std::move(extraInfo)); - return std::make_unique(tableName, std::move(createTableInfo)); + return std::make_unique(std::move(createTableInfo)); } std::unique_ptr Transformer::transformCreateRdfGraphClause( CypherParser::KU_CreateRdfGraphContext& ctx) { auto rdfGraphName = transformSchemaName(*ctx.oC_SchemaName()); - auto createTableInfo = std::make_unique(TableType::RDF, rdfGraphName); - return std::make_unique(rdfGraphName, std::move(createTableInfo)); + auto createTableInfo = CreateTableInfo(TableType::RDF, rdfGraphName); + return std::make_unique(std::move(createTableInfo)); } std::unique_ptr Transformer::transformDropTable(CypherParser::KU_DropTableContext& ctx) { @@ -113,8 +113,7 @@ std::unique_ptr Transformer::transformRenameTable( auto tableName = transformSchemaName(*ctx.oC_SchemaName()); auto newName = transformSchemaName(*ctx.kU_AlterOptions()->kU_RenameTable()->oC_SchemaName()); auto extraInfo = std::make_unique(std::move(newName)); - auto info = - std::make_unique(AlterType::RENAME_TABLE, tableName, std::move(extraInfo)); + auto info = AlterInfo(AlterType::RENAME_TABLE, tableName, std::move(extraInfo)); return std::make_unique(std::move(info)); } @@ -128,13 +127,11 @@ std::unique_ptr Transformer::transformAddProperty( if (addPropertyCtx->oC_Expression()) { defaultValue = transformExpression(*addPropertyCtx->oC_Expression()); } else { - defaultValue = - std::make_unique(Value::createNullValue().copy(), "NULL"); + defaultValue = std::make_unique(Value::createNullValue(), "NULL"); } auto extraInfo = std::make_unique( std::move(propertyName), std::move(dataType), std::move(defaultValue)); - auto info = - std::make_unique(AlterType::ADD_PROPERTY, tableName, std::move(extraInfo)); + auto info = AlterInfo(AlterType::ADD_PROPERTY, tableName, std::move(extraInfo)); return std::make_unique(std::move(info)); } @@ -144,8 +141,7 @@ std::unique_ptr Transformer::transformDropProperty( auto propertyName = transformPropertyKeyName(*ctx.kU_AlterOptions()->kU_DropProperty()->oC_PropertyKeyName()); auto extraInfo = std::make_unique(std::move(propertyName)); - auto info = - std::make_unique(AlterType::DROP_PROPERTY, tableName, std::move(extraInfo)); + auto info = AlterInfo(AlterType::DROP_PROPERTY, tableName, std::move(extraInfo)); return std::make_unique(std::move(info)); } @@ -157,8 +153,7 @@ std::unique_ptr Transformer::transformRenameProperty( auto newName = transformPropertyKeyName( *ctx.kU_AlterOptions()->kU_RenameProperty()->oC_PropertyKeyName()[1]); auto extraInfo = std::make_unique(propertyName, newName); - auto info = - std::make_unique(AlterType::RENAME_PROPERTY, tableName, std::move(extraInfo)); + auto info = AlterInfo(AlterType::RENAME_PROPERTY, tableName, std::move(extraInfo)); return std::make_unique(std::move(info)); } diff --git a/src/parser/transform/transform_expression.cpp b/src/parser/transform/transform_expression.cpp index 9283b67308..5ab474b61d 100644 --- a/src/parser/transform/transform_expression.cpp +++ b/src/parser/transform/transform_expression.cpp @@ -283,8 +283,7 @@ std::unique_ptr Transformer::transformStringOperatorExpression } static std::unique_ptr getZeroLiteral() { - auto literal = std::make_unique(0); - return std::make_unique(std::move(literal), "0"); + return std::make_unique(Value(0), "0"); } std::unique_ptr Transformer::transformListOperatorExpression( @@ -402,12 +401,11 @@ std::unique_ptr Transformer::transformLiteral( return transformBooleanLiteral(*ctx.oC_BooleanLiteral()); } else if (ctx.StringLiteral()) { return std::make_unique( - std::make_unique( - LogicalType::STRING(), transformStringLiteral(*ctx.StringLiteral())), + Value(LogicalType::STRING(), transformStringLiteral(*ctx.StringLiteral())), ctx.getText()); } else if (ctx.NULL_()) { return std::make_unique( - std::make_unique(Value::createNullValue()), ctx.getText()); + Value(Value::createNullValue()), ctx.getText()); } else if (ctx.kU_StructLiteral()) { return transformStructLiteral(*ctx.kU_StructLiteral()); } else { @@ -418,14 +416,12 @@ std::unique_ptr Transformer::transformLiteral( std::unique_ptr Transformer::transformBooleanLiteral( CypherParser::OC_BooleanLiteralContext& ctx) { - std::unique_ptr literal; if (ctx.TRUE()) { - literal = std::make_unique(true); + return std::make_unique(Value(true), ctx.getText()); } else if (ctx.FALSE()) { - literal = std::make_unique(false); + return std::make_unique(Value(false), ctx.getText()); } - KU_ASSERT(literal); - return std::make_unique(std::move(literal), ctx.getText()); + KU_UNREACHABLE; } std::unique_ptr Transformer::transformListLiteral( @@ -440,7 +436,7 @@ std::unique_ptr Transformer::transformListLiteral( if (listEntry->oC_Expression() == nullptr) { auto nullValue = Value::createNullValue(); listCreation->addChild( - std::make_unique(nullValue.copy(), nullValue.toString())); + std::make_unique(nullValue, nullValue.toString())); } else { listCreation->addChild(transformExpression(*listEntry->oC_Expression())); } @@ -513,10 +509,9 @@ std::unique_ptr Transformer::transformFunctionParameterExpress std::unique_ptr Transformer::transformPathPattern( CypherParser::OC_PathPatternsContext& ctx) { auto subquery = std::make_unique(SubqueryType::EXISTS, ctx.getText()); - auto patternElement = - std::make_unique(transformNodePattern(*ctx.oC_NodePattern())); + auto patternElement = PatternElement(transformNodePattern(*ctx.oC_NodePattern())); for (auto& chain : ctx.oC_PatternElementChain()) { - patternElement->addPatternElementChain(transformPatternElementChain(*chain)); + patternElement.addPatternElementChain(transformPatternElementChain(*chain)); } subquery->addPatternElement(std::move(patternElement)); return subquery; @@ -576,12 +571,11 @@ std::unique_ptr Transformer::transformCaseExpression( return parsedCaseExpression; } -std::unique_ptr Transformer::transformCaseAlternative( +ParsedCaseAlternative Transformer::transformCaseAlternative( CypherParser::OC_CaseAlternativeContext& ctx) { auto whenExpression = transformExpression(*ctx.oC_Expression(0)); auto thenExpression = transformExpression(*ctx.oC_Expression(1)); - return std::make_unique( - std::move(whenExpression), std::move(thenExpression)); + return ParsedCaseAlternative(std::move(whenExpression), std::move(thenExpression)); } std::unique_ptr Transformer::transformNumberLiteral( @@ -610,13 +604,11 @@ std::unique_ptr Transformer::transformIntegerLiteral( ku_string_t literal{text.c_str(), text.length()}; int64_t result; if (function::CastString::tryCast(literal, result)) { - return std::make_unique( - std::make_unique(result), ctx.getText()); + return std::make_unique(Value(result), ctx.getText()); } int128_t result128; function::CastString::operation(literal, result128); - return std::make_unique( - std::make_unique(result128), ctx.getText()); + return std::make_unique(Value(result128), ctx.getText()); } std::unique_ptr Transformer::transformDoubleLiteral( @@ -625,8 +617,7 @@ std::unique_ptr Transformer::transformDoubleLiteral( ku_string_t literal{text.c_str(), text.length()}; double_t result; function::CastString::operation(literal, result); - return std::make_unique( - std::make_unique(result), ctx.getText()); + return std::make_unique(Value(result), ctx.getText()); } } // namespace parser diff --git a/src/parser/transform/transform_graph_pattern.cpp b/src/parser/transform/transform_graph_pattern.cpp index a143719107..9ccf59d620 100644 --- a/src/parser/transform/transform_graph_pattern.cpp +++ b/src/parser/transform/transform_graph_pattern.cpp @@ -7,48 +7,43 @@ using namespace kuzu::common; namespace kuzu { namespace parser { -std::vector> Transformer::transformPattern( - CypherParser::OC_PatternContext& ctx) { - std::vector> pattern; +std::vector Transformer::transformPattern(CypherParser::OC_PatternContext& ctx) { + std::vector patterns; for (auto& patternPart : ctx.oC_PatternPart()) { - pattern.push_back(transformPatternPart(*patternPart)); + patterns.push_back(transformPatternPart(*patternPart)); } - return pattern; + return patterns; } -std::unique_ptr Transformer::transformPatternPart( - CypherParser::OC_PatternPartContext& ctx) { +PatternElement Transformer::transformPatternPart(CypherParser::OC_PatternPartContext& ctx) { auto patternElement = transformAnonymousPatternPart(*ctx.oC_AnonymousPatternPart()); if (ctx.oC_Variable()) { auto variable = transformVariable(*ctx.oC_Variable()); - patternElement->setPathName(variable); + patternElement.setPathName(variable); } return patternElement; } -std::unique_ptr Transformer::transformAnonymousPatternPart( +PatternElement Transformer::transformAnonymousPatternPart( CypherParser::OC_AnonymousPatternPartContext& ctx) { return transformPatternElement(*ctx.oC_PatternElement()); } -std::unique_ptr Transformer::transformPatternElement( - CypherParser::OC_PatternElementContext& ctx) { +PatternElement Transformer::transformPatternElement(CypherParser::OC_PatternElementContext& ctx) { if (ctx.oC_PatternElement()) { // parenthesized pattern element return transformPatternElement(*ctx.oC_PatternElement()); } - auto patternElement = - std::make_unique(transformNodePattern(*ctx.oC_NodePattern())); + auto patternElement = PatternElement(transformNodePattern(*ctx.oC_NodePattern())); if (!ctx.oC_PatternElementChain().empty()) { for (auto& patternElementChain : ctx.oC_PatternElementChain()) { - patternElement->addPatternElementChain( + patternElement.addPatternElementChain( transformPatternElementChain(*patternElementChain)); } } return patternElement; } -std::unique_ptr Transformer::transformNodePattern( - CypherParser::OC_NodePatternContext& ctx) { +NodePattern Transformer::transformNodePattern(CypherParser::OC_NodePatternContext& ctx) { auto variable = std::string(); if (ctx.oC_Variable()) { variable = transformVariable(*ctx.oC_Variable()); @@ -61,18 +56,16 @@ std::unique_ptr Transformer::transformNodePattern( if (ctx.kU_Properties()) { properties = transformProperties(*ctx.kU_Properties()); } - return std::make_unique( - std::move(variable), std::move(nodeLabels), std::move(properties)); + return NodePattern(std::move(variable), std::move(nodeLabels), std::move(properties)); } -std::unique_ptr Transformer::transformPatternElementChain( +PatternElementChain Transformer::transformPatternElementChain( CypherParser::OC_PatternElementChainContext& ctx) { - return std::make_unique( - transformRelationshipPattern(*ctx.oC_RelationshipPattern()), + return PatternElementChain(transformRelationshipPattern(*ctx.oC_RelationshipPattern()), transformNodePattern(*ctx.oC_NodePattern())); } -std::unique_ptr Transformer::transformRelationshipPattern( +RelPattern Transformer::transformRelationshipPattern( CypherParser::OC_RelationshipPatternContext& ctx) { auto relDetail = ctx.oC_RelationshipDetail(); auto variable = std::string(); @@ -101,7 +94,7 @@ std::unique_ptr Transformer::transformRelationshipPattern( } auto relType = QueryRelType::NON_RECURSIVE; - std::unique_ptr recursiveInfo; + auto recursiveInfo = RecursiveRelPatternInfo(); if (relDetail && relDetail->oC_RangeLiteral()) { if (relDetail->oC_RangeLiteral()->ALL()) { relType = QueryRelType::ALL_SHORTEST; @@ -124,38 +117,39 @@ std::unique_ptr Transformer::transformRelationshipPattern( if (range->oC_UpperBound()) { upperBound = range->oC_UpperBound()->getText(); } - recursiveInfo = std::make_unique(lowerBound, upperBound); + recursiveInfo.lowerBound = lowerBound; + recursiveInfo.upperBound = upperBound; if (range->kU_RecursiveRelationshipComprehension()) { auto comprehension = range->kU_RecursiveRelationshipComprehension(); - recursiveInfo->relName = transformVariable(*comprehension->oC_Variable(0)); - recursiveInfo->nodeName = transformVariable(*comprehension->oC_Variable(1)); + recursiveInfo.relName = transformVariable(*comprehension->oC_Variable(0)); + recursiveInfo.nodeName = transformVariable(*comprehension->oC_Variable(1)); if (comprehension->oC_Where()) { - recursiveInfo->whereExpression = transformWhere(*comprehension->oC_Where()); + recursiveInfo.whereExpression = transformWhere(*comprehension->oC_Where()); } if (comprehension->kU_IntermediateRelProjectionItems()) { - recursiveInfo->hasProjection = true; + recursiveInfo.hasProjection = true; auto relProjectionItem = comprehension->kU_IntermediateRelProjectionItems(); if (relProjectionItem->oC_ProjectionItems()) { - recursiveInfo->relProjectionList = + recursiveInfo.relProjectionList = transformProjectionItems(*relProjectionItem->oC_ProjectionItems()); } } if (comprehension->kU_IntermediateNodeProjectionItems()) { - recursiveInfo->hasProjection = true; + recursiveInfo.hasProjection = true; auto nodeProjectionItem = comprehension->kU_IntermediateNodeProjectionItems(); if (nodeProjectionItem->oC_ProjectionItems()) { - recursiveInfo->nodeProjectionList = + recursiveInfo.nodeProjectionList = transformProjectionItems(*nodeProjectionItem->oC_ProjectionItems()); } } } } - return std::make_unique(variable, relTypes, relType, arrowDirection, - std::move(properties), std::move(recursiveInfo)); + return RelPattern(variable, relTypes, relType, arrowDirection, std::move(properties), + std::move(recursiveInfo)); } -std::vector>> -Transformer::transformProperties(CypherParser::KU_PropertiesContext& ctx) { +std::vector Transformer::transformProperties( + CypherParser::KU_PropertiesContext& ctx) { std::vector>> result; KU_ASSERT(ctx.oC_PropertyKeyName().size() == ctx.oC_Expression().size()); for (auto i = 0u; i < ctx.oC_PropertyKeyName().size(); ++i) { diff --git a/src/parser/transform/transform_projection.cpp b/src/parser/transform/transform_projection.cpp index 5db6cf5f6f..3cbe690dec 100644 --- a/src/parser/transform/transform_projection.cpp +++ b/src/parser/transform/transform_projection.cpp @@ -7,22 +7,20 @@ using namespace kuzu::common; namespace kuzu { namespace parser { -std::unique_ptr Transformer::transformWith(CypherParser::OC_WithContext& ctx) { - auto withClause = - std::make_unique(transformProjectionBody(*ctx.oC_ProjectionBody())); +WithClause Transformer::transformWith(CypherParser::OC_WithContext& ctx) { + auto withClause = WithClause(transformProjectionBody(*ctx.oC_ProjectionBody())); if (ctx.oC_Where()) { - withClause->setWhereExpression(transformWhere(*ctx.oC_Where())); + withClause.setWhereExpression(transformWhere(*ctx.oC_Where())); } return withClause; } -std::unique_ptr Transformer::transformReturn(CypherParser::OC_ReturnContext& ctx) { - return std::make_unique(transformProjectionBody(*ctx.oC_ProjectionBody())); +ReturnClause Transformer::transformReturn(CypherParser::OC_ReturnContext& ctx) { + return ReturnClause(transformProjectionBody(*ctx.oC_ProjectionBody())); } -std::unique_ptr Transformer::transformProjectionBody( - CypherParser::OC_ProjectionBodyContext& ctx) { - auto projectionBody = std::make_unique( +ProjectionBody Transformer::transformProjectionBody(CypherParser::OC_ProjectionBodyContext& ctx) { + auto projectionBody = ProjectionBody( nullptr != ctx.DISTINCT(), transformProjectionItems(*ctx.oC_ProjectionItems())); if (ctx.oC_Order()) { std::vector> orderByExpressions; @@ -31,14 +29,13 @@ std::unique_ptr Transformer::transformProjectionBody( orderByExpressions.push_back(transformExpression(*sortItem->oC_Expression())); isAscOrders.push_back(!(sortItem->DESC() || sortItem->DESCENDING())); } - projectionBody->setOrderByExpressions( - std::move(orderByExpressions), std::move(isAscOrders)); + projectionBody.setOrderByExpressions(std::move(orderByExpressions), std::move(isAscOrders)); } if (ctx.oC_Skip()) { - projectionBody->setSkipExpression(transformExpression(*ctx.oC_Skip()->oC_Expression())); + projectionBody.setSkipExpression(transformExpression(*ctx.oC_Skip()->oC_Expression())); } if (ctx.oC_Limit()) { - projectionBody->setLimitExpression(transformExpression(*ctx.oC_Limit()->oC_Expression())); + projectionBody.setLimitExpression(transformExpression(*ctx.oC_Limit()->oC_Expression())); } return projectionBody; } diff --git a/src/parser/transform/transform_query.cpp b/src/parser/transform/transform_query.cpp index ec1d5f9f0e..6a14af76c2 100644 --- a/src/parser/transform/transform_query.cpp +++ b/src/parser/transform/transform_query.cpp @@ -4,11 +4,11 @@ namespace kuzu { namespace parser { -std::unique_ptr Transformer::transformQuery(CypherParser::OC_QueryContext& ctx) { +std::unique_ptr Transformer::transformQuery(CypherParser::OC_QueryContext& ctx) { return transformRegularQuery(*ctx.oC_RegularQuery()); } -std::unique_ptr Transformer::transformRegularQuery( +std::unique_ptr Transformer::transformRegularQuery( CypherParser::OC_RegularQueryContext& ctx) { auto regularQuery = std::make_unique(transformSingleQuery(*ctx.oC_SingleQuery())); for (auto unionClause : ctx.oC_Union()) { @@ -18,42 +18,40 @@ std::unique_ptr Transformer::transformRegularQuery( return regularQuery; } -std::unique_ptr Transformer::transformSingleQuery( - CypherParser::OC_SingleQueryContext& ctx) { +SingleQuery Transformer::transformSingleQuery(CypherParser::OC_SingleQueryContext& ctx) { auto singleQuery = ctx.oC_MultiPartQuery() ? transformSinglePartQuery(*ctx.oC_MultiPartQuery()->oC_SinglePartQuery()) : transformSinglePartQuery(*ctx.oC_SinglePartQuery()); if (ctx.oC_MultiPartQuery()) { for (auto queryPart : ctx.oC_MultiPartQuery()->kU_QueryPart()) { - singleQuery->addQueryPart(transformQueryPart(*queryPart)); + singleQuery.addQueryPart(transformQueryPart(*queryPart)); } } return singleQuery; } -std::unique_ptr Transformer::transformSinglePartQuery( - CypherParser::OC_SinglePartQueryContext& ctx) { - auto singleQuery = std::make_unique(); +SingleQuery Transformer::transformSinglePartQuery(CypherParser::OC_SinglePartQueryContext& ctx) { + auto singleQuery = SingleQuery(); for (auto& readingClause : ctx.oC_ReadingClause()) { - singleQuery->addReadingClause(transformReadingClause(*readingClause)); + singleQuery.addReadingClause(transformReadingClause(*readingClause)); } for (auto& updatingClause : ctx.oC_UpdatingClause()) { - singleQuery->addUpdatingClause(transformUpdatingClause(*updatingClause)); + singleQuery.addUpdatingClause(transformUpdatingClause(*updatingClause)); } if (ctx.oC_Return()) { - singleQuery->setReturnClause(transformReturn(*ctx.oC_Return())); + singleQuery.setReturnClause(transformReturn(*ctx.oC_Return())); } return singleQuery; } -std::unique_ptr Transformer::transformQueryPart(CypherParser::KU_QueryPartContext& ctx) { - auto queryPart = std::make_unique(transformWith(*ctx.oC_With())); +QueryPart Transformer::transformQueryPart(CypherParser::KU_QueryPartContext& ctx) { + auto queryPart = QueryPart(transformWith(*ctx.oC_With())); for (auto& readingClause : ctx.oC_ReadingClause()) { - queryPart->addReadingClause(transformReadingClause(*readingClause)); + queryPart.addReadingClause(transformReadingClause(*readingClause)); } for (auto& updatingClause : ctx.oC_UpdatingClause()) { - queryPart->addUpdatingClause(transformUpdatingClause(*updatingClause)); + queryPart.addUpdatingClause(transformUpdatingClause(*updatingClause)); } return queryPart; } diff --git a/src/parser/transform/transform_updating_clause.cpp b/src/parser/transform/transform_updating_clause.cpp index 82bb6e06d2..de36ad9ccb 100644 --- a/src/parser/transform/transform_updating_clause.cpp +++ b/src/parser/transform/transform_updating_clause.cpp @@ -51,7 +51,7 @@ std::unique_ptr Transformer::transformSet(CypherParser::OC_SetCo return setClause; } -parsed_expression_pair Transformer::transformSetItem(CypherParser::OC_SetItemContext& ctx) { +parsed_expr_pair Transformer::transformSetItem(CypherParser::OC_SetItemContext& ctx) { return make_pair( transformProperty(*ctx.oC_PropertyExpression()), transformExpression(*ctx.oC_Expression())); }