Skip to content

Commit

Permalink
Implement Commenting on Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Riolku committed Sep 11, 2023
1 parent 134ced4 commit 284f315
Show file tree
Hide file tree
Showing 56 changed files with 4,757 additions and 4,175 deletions.
2 changes: 1 addition & 1 deletion dataset/ldbc-sf01/copy.cypher
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
COPY Comment FROM "dataset/ldbc-sf01/Comment.csv" (HEADER=true, DELIM='|');
COPY `Comment` FROM "dataset/ldbc-sf01/Comment.csv" (HEADER=true, DELIM='|');
COPY Forum FROM "dataset/ldbc-sf01/Forum.csv" (HEADER=true, DELIM='|');
COPY Organisation FROM "dataset/ldbc-sf01/Organisation.csv" (HEADER=true, DELIM='|');
COPY Person FROM "dataset/ldbc-sf01/Person.csv" (HEADER=true, DELIM='|');
Expand Down
14 changes: 7 additions & 7 deletions dataset/ldbc-sf01/schema.cypher
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
create node table Comment (id int64, creationDate INT64, locationIP STRING, browserUsed STRING, content STRING, length INT32, PRIMARY KEY (id));
create node table `Comment` (id int64, creationDate INT64, locationIP STRING, browserUsed STRING, content STRING, length INT32, PRIMARY KEY (id));
create node table Forum (id INT64, title STRING, creationDate INT64, PRIMARY KEY (id));
create node table Organisation (id INT64, label STRING, name STRING, url STRING, PRIMARY KEY (id));
create node table Person (id INT64, firstName STRING, lastName STRING, gender STRING, birthday INT64, creationDate INT64, locationIP STRING, browserUsed STRING, PRIMARY KEY (id));
create node table Place (id INT64, name STRING, url STRING, label STRING, PRIMARY KEY (id));
create node table Post (id INT64, imageFile STRING, creationDate INT64, locationIP STRING, browserUsed STRING, language STRING, content STRING, length INT32, PRIMARY KEY (id));
create node table Tag (id INT64, name STRING, url STRING, PRIMARY KEY (id));
create node table TagClass (id INT64, name STRING, url STRING, PRIMARY KEY (id));
create rel table Comment_hasCreator_Person (FROM Comment TO Person, MANY_MANY);
create rel table Comment_hasTag_Tag (FROM Comment TO Tag, MANY_MANY);
create rel table Comment_isLocatedIn_Place (FROM Comment TO Place, MANY_ONE);
create rel table Comment_replyOf_Comment (FROM Comment TO Comment, MANY_ONE);
create rel table Comment_replyOf_Post (FROM Comment TO Post, MANY_ONE);
create rel table Comment_hasCreator_Person (FROM `Comment` TO Person, MANY_MANY);
create rel table Comment_hasTag_Tag (FROM `Comment` TO Tag, MANY_MANY);
create rel table Comment_isLocatedIn_Place (FROM `Comment` TO Place, MANY_ONE);
create rel table Comment_replyOf_Comment (FROM `Comment` TO `Comment`, MANY_ONE);
create rel table Comment_replyOf_Post (FROM `Comment` TO Post, MANY_ONE);
create rel table Forum_containerOf_Post (FROM Forum TO Post, ONE_MANY);
create rel table Forum_hasMember_Person (FROM Forum TO Person, joinDate INT64, MANY_MANY);
create rel table Forum_hasModerator_Person (FROM Forum TO Person, MANY_MANY);
Expand All @@ -19,7 +19,7 @@ create rel table Organisation_isLocatedIn_Place (FROM Organisation TO Place, MAN
create rel table Person_hasInterest_Tag (FROM Person TO Tag, MANY_MANY);
create rel table Person_isLocatedIn_Place (FROM Person TO Place, MANY_ONE);
create rel table Person_knows_Person (FROM Person TO Person, creationDate INT64, MANY_MANY);
create rel table Person_likes_Comment (FROM Person TO Comment, creationDate INT64, MANY_MANY);
create rel table Person_likes_Comment (FROM Person TO `Comment`, creationDate INT64, MANY_MANY);
create rel table Person_likes_Post (FROM Person TO Post, creationDate INT64, MANY_MANY);
create rel table Person_studyAt_Organisation (FROM Person TO Organisation, classYear INT32, MANY_ONE);
create rel table Person_workAt_Organisation (FROM Person TO Organisation, workFrom INT32, MANY_MANY);
Expand Down
4 changes: 2 additions & 2 deletions dataset/sf-0.1/copy.cypher
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
COPY City from "dataset/sf-0.1/City.csv" (HEADER=true, DELIM='|');
COPY Comment from "dataset/sf-0.1/Comment.csv" (HEADER=true, DELIM='|');
COPY `Comment` from "dataset/sf-0.1/Comment.csv" (HEADER=true, DELIM='|');
COPY Company from "dataset/sf-0.1/Company.csv" (HEADER=true, DELIM='|');
COPY Continent from "dataset/sf-0.1/Continent.csv" (HEADER=true, DELIM='|');
COPY Country from "dataset/sf-0.1/Country.csv" (HEADER=true, DELIM='|');
Expand Down Expand Up @@ -33,4 +33,4 @@ COPY Post_hasTag_Tag from "dataset/sf-0.1/Post_hasTag_Tag.csv" (HEADER=true, DEL
COPY Post_isLocatedIn_Country from "dataset/sf-0.1/Post_isLocatedIn_Country.csv" (HEADER=true, DELIM='|');
COPY Tag_hasType_TagClass from "dataset/sf-0.1/Tag_hasType_TagClass.csv" (HEADER=true, DELIM='|');
COPY TagClass_isSubclassOf_TagClass from "dataset/sf-0.1/TagClass_isSubclassOf_TagClass.csv" (HEADER=true, DELIM='|');
COPY University_isLocatedIn_City from "dataset/sf-0.1/University_isLocatedIn_City.csv" (HEADER=true, DELIM='|');
COPY University_isLocatedIn_City from "dataset/sf-0.1/University_isLocatedIn_City.csv" (HEADER=true, DELIM='|');
14 changes: 7 additions & 7 deletions dataset/sf-0.1/schema.cypher
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create node table City (id int64, PRIMARY KEY (id));
create node table Comment (id int64, PRIMARY KEY (id));
create node table `Comment` (id int64, PRIMARY KEY (id));
create node table Company (id int64, PRIMARY KEY (id));
create node table Continent (id int64, PRIMARY KEY (id));
create node table Country (id int64, PRIMARY KEY (id));
Expand All @@ -10,11 +10,11 @@ create node table Tag (id int64, PRIMARY KEY (id));
create node table TagClass (id int64, PRIMARY KEY (id));
create node table University (id int64, PRIMARY KEY (id));
create rel table City_isPartOf_Country (FROM City TO Country, MANY_ONE);
create rel table Comment_hasCreator_Person (FROM Comment TO Person, MANY_ONE);
create rel table Comment_hasTag_Tag (FROM Comment TO Tag, MANY_MANY);
create rel table Comment_isLocatedIn_Country (FROM Comment TO Country, MANY_ONE);
create rel table Comment_replyOf_Comment (FROM Comment TO Comment, MANY_ONE);
create rel table Comment_replyOf_Post (FROM Comment TO Post, MANY_ONE);
create rel table Comment_hasCreator_Person (FROM `Comment` TO Person, MANY_ONE);
create rel table Comment_hasTag_Tag (FROM `Comment` TO Tag, MANY_MANY);
create rel table Comment_isLocatedIn_Country (FROM `Comment` TO Country, MANY_ONE);
create rel table Comment_replyOf_Comment (FROM `Comment` TO `Comment`, MANY_ONE);
create rel table Comment_replyOf_Post (FROM `Comment` TO Post, MANY_ONE);
create rel table Company_isLocatedIn_Country (FROM Company TO Country, MANY_ONE);
create rel table Country_isPartOf_Continent (FROM Country TO Continent, MANY_ONE);
create rel table Forum_containerOf_Post (FROM Forum TO Post, MANY_MANY);
Expand All @@ -24,7 +24,7 @@ create rel table Forum_hasTag_Tag (FROM Forum TO Tag, MANY_MANY);
create rel table Person_hasInterest_Tag (FROM Person TO Tag, MANY_MANY);
create rel table Person_isLocatedIn_City (FROM Person TO City, MANY_ONE);
create rel table Person_knows_Person (FROM Person TO Person, MANY_MANY);
create rel table Person_likes_Comment (FROM Person TO Comment, MANY_MANY);
create rel table Person_likes_Comment (FROM Person TO `Comment`, MANY_MANY);
create rel table Person_likes_Post (FROM Person TO Post, MANY_MANY);
create rel table Person_studyAt_University (FROM Person TO University, MANY_ONE);
create rel table Person_workAt_Company (FROM Person TO Company, MANY_MANY);
Expand Down
6 changes: 6 additions & 0 deletions src/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ kU_StandaloneCall

CALL : ( 'C' | 'c' ) ( 'A' | 'a' ) ( 'L' | 'l' ) ( 'L' | 'l' ) ;

kU_CommentOn
: COMMENT SP ON SP TABLE SP oC_SchemaName SP IS SP StringLiteral ;

COMMENT : ( 'C' | 'c' ) ( 'O' | 'o' ) ( 'M' | 'm' ) ( 'M' | 'm' ) ( 'E' | 'e' ) ( 'N' | 'n' ) ( 'T' | 't' ) ;

kU_CreateMacro
: CREATE SP MACRO SP oC_FunctionName SP? '(' SP? kU_PositionalArgs? SP? kU_DefaultArg? ( SP? ',' SP? kU_DefaultArg )* SP? ')' SP AS SP oC_Expression ;

Expand Down Expand Up @@ -175,6 +180,7 @@ oC_Statement
| kU_CopyTO
| kU_StandaloneCall
| kU_CreateMacro
| kU_CommentOn
| kU_Transaction ;

kU_Transaction
Expand Down
7 changes: 4 additions & 3 deletions src/binder/bind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
add_library(
kuzu_binder_bind
OBJECT
bind_standalone_call.cpp
bind_transaction.cpp
bind_create_macro.cpp
bind_comment_on.cpp
bind_copy.cpp
bind_create_macro.cpp
bind_ddl.cpp
bind_explain.cpp
bind_graph_pattern.cpp
bind_projection_clause.cpp
bind_query.cpp
bind_reading_clause.cpp
bind_standalone_call.cpp
bind_transaction.cpp
bind_updating_clause.cpp)

set(ALL_OBJECT_FILES
Expand Down
21 changes: 21 additions & 0 deletions src/binder/bind/bind_comment_on.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "binder/binder.h"
#include "binder/bound_comment_on.h"
#include "parser/comment_on.h"

namespace kuzu {
namespace binder {

std::unique_ptr<BoundStatement> Binder::bindCommentOn(const parser::Statement& statement) {
auto& commentOnStatement = reinterpret_cast<const parser::CommentOn&>(statement);
auto tableName = commentOnStatement.getTable();
auto comment = commentOnStatement.getComment();

validateTableExist(catalog, tableName);
auto catalogContent = catalog.getReadOnlyVersion();
auto tableID = catalogContent->getTableID(tableName);

return std::make_unique<BoundCommentOn>(tableID, tableName, comment);
}

} // namespace binder
} // namespace kuzu
3 changes: 3 additions & 0 deletions src/binder/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ std::unique_ptr<BoundStatement> Binder::bind(const Statement& statement) {
case StatementType::STANDALONE_CALL: {
boundStatement = bindStandaloneCall(statement);
} break;
case StatementType::COMMENT_ON: {
boundStatement = bindCommentOn(statement);
} break;
case StatementType::EXPLAIN: {
boundStatement = bindExplain(statement);
} break;
Expand Down
3 changes: 3 additions & 0 deletions src/binder/bound_statement_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ void BoundStatementVisitor::visit(const kuzu::binder::BoundStatement& statement)
case StatementType::STANDALONE_CALL: {
visitStandaloneCall(statement);
} break;
case StatementType::COMMENT_ON: {
visitCommentOn(statement);
} break;
case StatementType::EXPLAIN: {
visitExplain(statement);
} break;
Expand Down
5 changes: 5 additions & 0 deletions src/catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,10 @@ void Catalog::addScalarMacroFunction(
catalogContentForWriteTrx->addScalarMacroFunction(std::move(name), std::move(macro));
}

void Catalog::setTableComment(table_id_t tableID, const std::string& comment) {
initCatalogContentForWriteTrxIfNecessary();
catalogContentForWriteTrx->getTableSchema(tableID)->setComment(comment);
}

} // namespace catalog
} // namespace kuzu
8 changes: 8 additions & 0 deletions src/catalog/catalog_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ Property* CatalogContent::getRelProperty(
throw CatalogException("Cannot find rel property " + propertyName + ".");
}

std::vector<TableSchema*> CatalogContent::getTableSchemas() const {
std::vector<TableSchema*> allTableSchemas;
for (auto&& [_, schema] : tableSchemas) {
allTableSchemas.push_back(schema.get());
}
return allTableSchemas;
}

void CatalogContent::dropTableSchema(table_id_t tableID) {
auto tableName = getTableName(tableID);
tableNameToIDMap.erase(tableName);
Expand Down
4 changes: 4 additions & 0 deletions src/catalog/table_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void TableSchema::serialize(FileInfo* fileInfo, uint64_t& offset) {
SerDeser::serializeValue(tableID, fileInfo, offset);
SerDeser::serializeValue(tableType, fileInfo, offset);
SerDeser::serializeVectorOfPtrs(properties, fileInfo, offset);
SerDeser::serializeValue(comment, fileInfo, offset);
SerDeser::serializeValue(nextPropertyID, fileInfo, offset);
serializeInternal(fileInfo, offset);
}
Expand All @@ -82,11 +83,13 @@ std::unique_ptr<TableSchema> TableSchema::deserialize(FileInfo* fileInfo, uint64
table_id_t tableID;
TableType tableType;
std::vector<std::unique_ptr<Property>> properties;
std::string comment;
property_id_t nextPropertyID;
SerDeser::deserializeValue(tableName, fileInfo, offset);
SerDeser::deserializeValue(tableID, fileInfo, offset);
SerDeser::deserializeValue(tableType, fileInfo, offset);
SerDeser::deserializeVectorOfPtrs(properties, fileInfo, offset);
SerDeser::deserializeValue(comment, fileInfo, offset);
SerDeser::deserializeValue(nextPropertyID, fileInfo, offset);
std::unique_ptr<TableSchema> result;
switch (tableType) {
Expand All @@ -110,6 +113,7 @@ std::unique_ptr<TableSchema> TableSchema::deserialize(FileInfo* fileInfo, uint64
result->tableID = tableID;
result->tableType = tableType;
result->properties = std::move(properties);
result->comment = std::move(comment);
result->nextPropertyID = nextPropertyID;
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/function/built_in_table_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void BuiltInTableFunctions::registerTableFunctions() {
tableFunctions.insert({TABLE_INFO_FUNC_NAME, TableInfoFunction::getDefinitions()});
tableFunctions.insert({DB_VERSION_FUNC_NAME, DBVersionFunction::getDefinitions()});
tableFunctions.insert({CURRENT_SETTING_FUNC_NAME, CurrentSettingFunction::getDefinitions()});
tableFunctions.insert({SHOW_TABLES_FUNC_NAME, ShowTablesFunction::getDefinitions()});
}

TableFunctionDefinition* BuiltInTableFunctions::mathTableFunction(const std::string& name) {
Expand Down
46 changes: 46 additions & 0 deletions src/function/table_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,51 @@ std::unique_ptr<TableFuncBindData> CurrentSettingFunction::bindFunc(main::Client
std::move(returnTypes), std::move(returnColumnNames), 1 /* one row result */);
}

void ShowTablesFunction::tableFunc(std::pair<offset_t, offset_t> morsel,
function::TableFuncBindData* bindData, std::vector<ValueVector*> outputVectors) {
auto tables = reinterpret_cast<function::ShowTablesBindData*>(bindData)->tables;
auto numTablesToOutput = morsel.second - morsel.first;
auto outVectorPos = 0;
for (auto i = 0u; i < numTablesToOutput; i++) {
auto tableSchema = tables[morsel.first + i];
outputVectors[0]->setValue(outVectorPos, tableSchema->tableName);

std::string typeString;
switch (tableSchema->tableType) {
case TableType::NODE:
typeString = "NODE";
break;
case TableType::REL:
typeString = "REL";
break;
case TableType::RDF:
typeString = "RDF";
break;
};
outputVectors[1]->setValue(outVectorPos, typeString);
outputVectors[2]->setValue(outVectorPos, tableSchema->comment);

outVectorPos++;
}
for (auto& outputVector : outputVectors) {
outputVector->state->selVector->selectedSize = outVectorPos;
}
}

std::unique_ptr<TableFuncBindData> ShowTablesFunction::bindFunc(main::ClientContext* context,
kuzu::function::TableFuncBindInput input, catalog::CatalogContent* catalog) {
std::vector<std::string> returnColumnNames;
std::vector<LogicalType> returnTypes;
returnColumnNames.emplace_back("TableName");
returnTypes.emplace_back(LogicalTypeID::STRING);
returnColumnNames.emplace_back("TableType");
returnTypes.emplace_back(LogicalTypeID::STRING);
returnColumnNames.emplace_back("TableComment");
returnTypes.emplace_back(LogicalTypeID::STRING);

return std::make_unique<ShowTablesBindData>(catalog->getTableSchemas(), std::move(returnTypes),
std::move(returnColumnNames), catalog->getTableCount());
}

} // namespace function
} // namespace kuzu
3 changes: 3 additions & 0 deletions src/include/binder/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class Binder {
/*** bind transaction ***/
std::unique_ptr<BoundStatement> bindTransaction(const parser::Statement& statement);

/*** bind comment on ***/
std::unique_ptr<BoundStatement> bindCommentOn(const parser::Statement& statement);

/*** bind explain ***/
std::unique_ptr<BoundStatement> bindExplain(const parser::Statement& statement);

Expand Down
26 changes: 26 additions & 0 deletions src/include/binder/bound_comment_on.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "bound_statement_result.h"
#include "common/statement_type.h"

namespace kuzu {
namespace binder {

class BoundCommentOn : public BoundStatement {
public:
BoundCommentOn(common::table_id_t tableID, std::string tableName, std::string comment)
: BoundStatement{common::StatementType::COMMENT_ON,
BoundStatementResult::createSingleStringColumnResult()},
tableID(tableID), tableName(tableName), comment(comment) {}

inline common::table_id_t getTableID() const { return tableID; }
inline std::string getTableName() const { return tableName; }
inline std::string getComment() const { return comment; }

private:
common::table_id_t tableID;
std::string tableName, comment;
};

} // namespace binder
} // namespace kuzu
1 change: 1 addition & 0 deletions src/include/binder/bound_statement_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BoundStatementVisitor {
virtual void visitRenameProperty(const BoundStatement& statement) {}
virtual void visitCopy(const BoundStatement& statement) {}
virtual void visitStandaloneCall(const BoundStatement& statement) {}
virtual void visitCommentOn(const BoundStatement& statement) {}
virtual void visitExplain(const BoundStatement& statement);
virtual void visitCreateMacro(const BoundStatement& statement) {}
virtual void visitTransaction(const BoundStatement& statement) {}
Expand Down
11 changes: 6 additions & 5 deletions src/include/binder/visitor/statement_read_write_analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class StatementReadWriteAnalyzer : public BoundStatementVisitor {
bool isReadOnly(const BoundStatement& statement);

private:
void visitCreateTable(const BoundStatement& statement) override { readOnly = false; }
void visitDropTable(const BoundStatement& statement) override { readOnly = false; }
void visitRenameTable(const BoundStatement& statement) override { readOnly = false; }
void visitAddProperty(const BoundStatement& statement) override { readOnly = false; }
void visitDropProperty(const BoundStatement& statement) override { readOnly = false; }
void visitRenameProperty(const BoundStatement& statement) override { readOnly = false; }
void visitCommentOn(const BoundStatement& statement) override { readOnly = false; }
void visitCopy(const BoundStatement& statement) override { readOnly = false; }
void visitCreateMacro(const BoundStatement& statement) override { readOnly = false; }
void visitCreateTable(const BoundStatement& statement) override { readOnly = false; }
void visitDropProperty(const BoundStatement& statement) override { readOnly = false; }
void visitDropTable(const BoundStatement& statement) override { readOnly = false; }
void visitRenameProperty(const BoundStatement& statement) override { readOnly = false; }
void visitRenameTable(const BoundStatement& statement) override { readOnly = false; }
void visitQueryPart(const NormalizedQueryPart& queryPart) final;

private:
Expand Down
2 changes: 2 additions & 0 deletions src/include/catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Catalog {
void addScalarMacroFunction(
std::string name, std::unique_ptr<function::ScalarMacroFunction> macro);

void setTableComment(common::table_id_t tableID, const std::string& comment);

// TODO(Ziyi): pass transaction pointer here.
inline function::ScalarMacroFunction* getScalarMacroFunction(const std::string& name) const {
return catalogContentForReadOnlyTrx->macros.at(name).get();
Expand Down
3 changes: 3 additions & 0 deletions src/include/catalog/catalog_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class CatalogContent {
assert(tableSchemas.contains(tableID));
return tableSchemas.at(tableID)->getProperties();
}
inline size_t getTableCount() const { return tableSchemas.size(); }
inline std::vector<common::table_id_t> getNodeTableIDs() const {
return getTableIDs(common::TableType::NODE);
}
Expand All @@ -82,6 +83,8 @@ class CatalogContent {
return getTableSchemas(common::TableType::REL);
}

std::vector<TableSchema*> getTableSchemas() const;

inline bool containMacro(const std::string& macroName) const {
return macros.contains(macroName);
}
Expand Down
Loading

0 comments on commit 284f315

Please sign in to comment.