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 13, 2023
1 parent bd69420 commit defa82b
Show file tree
Hide file tree
Showing 52 changed files with 4,841 additions and 4,185 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.11)

project(Kuzu VERSION 0.0.8.6 LANGUAGES CXX)
project(Kuzu VERSION 0.0.8.7 LANGUAGES CXX)

find_package(Threads REQUIRED)

Expand Down
10 changes: 10 additions & 0 deletions src/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ oC_Statement
| kU_CopyTO
| kU_StandaloneCall
| kU_CreateMacro
| kU_CommentOn
| kU_Transaction ;

kU_CopyFrom
Expand All @@ -40,6 +41,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 @@ -667,8 +673,12 @@ oC_SymbolicName
: UnescapedSymbolicName
| EscapedSymbolicName {if ($EscapedSymbolicName.text == "``") { notifyEmptyToken($EscapedSymbolicName); }}
| HexLetter
| kU_NonReservedKeywords
;

kU_NonReservedKeywords
: COMMENT ;

UnescapedSymbolicName
: IdentifierStart ( IdentifierPart )* ;

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(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 @@ -129,6 +129,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
41 changes: 41 additions & 0 deletions src/function/table_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,46 @@ 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;
for (auto i = 0u; i < numTablesToOutput; i++) {
auto tableSchema = tables[morsel.first + i];
outputVectors[0]->setValue(i, 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(i, typeString);
outputVectors[2]->setValue(i, tableSchema->comment);
}
outputVectors[0]->state->selVector->selectedSize = numTablesToOutput;
}

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 @@ -133,6 +133,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(std::move(tableName)), comment(std::move(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 @@ -74,6 +74,7 @@ class CatalogContent {
assert(tableSchemas.contains(tableID));
return tableSchemas.at(tableID)->getProperties();
}
inline uint64_t getTableCount() const { return tableSchemas.size(); }
inline std::vector<common::table_id_t> getNodeTableIDs() const {
return getTableIDs(common::TableType::NODE);
}
Expand All @@ -87,6 +88,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
9 changes: 5 additions & 4 deletions src/include/catalog/node_table_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class NodeTableSchema : public TableSchema {
std::move(properties)},
primaryKeyPropertyID{primaryPropertyId} {}
NodeTableSchema(std::string tableName, common::table_id_t tableID,
std::vector<std::unique_ptr<Property>> properties, common::property_id_t nextPropertyID,
common::property_id_t primaryKeyPropertyID,
std::vector<std::unique_ptr<Property>> properties, std::string comment,
common::property_id_t nextPropertyID, common::property_id_t primaryKeyPropertyID,
std::unordered_set<common::table_id_t> fwdRelTableIDSet,
std::unordered_set<common::table_id_t> bwdRelTableIDSet)
: TableSchema{common::TableType::NODE, std::move(tableName), tableID, std::move(properties),
nextPropertyID},
std::move(comment), nextPropertyID},
primaryKeyPropertyID{primaryKeyPropertyID}, fwdRelTableIDSet{std::move(fwdRelTableIDSet)},
bwdRelTableIDSet{std::move(bwdRelTableIDSet)} {}

Expand All @@ -42,13 +42,14 @@ class NodeTableSchema : public TableSchema {
inline const std::unordered_set<common::table_id_t>& getFwdRelTableIDSet() const {
return fwdRelTableIDSet;
}

inline const std::unordered_set<common::table_id_t>& getBwdRelTableIDSet() const {
return bwdRelTableIDSet;
}

inline std::unique_ptr<TableSchema> copy() const override {
return std::make_unique<NodeTableSchema>(tableName, tableID, Property::copy(properties),
nextPropertyID, primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet);
comment, nextPropertyID, primaryKeyPropertyID, fwdRelTableIDSet, bwdRelTableIDSet);
}

private:
Expand Down
11 changes: 6 additions & 5 deletions src/include/catalog/rel_table_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class RelTableSchema : public TableSchema {
relMultiplicity{relMultiplicity}, srcTableID{srcTableID}, dstTableID{dstTableID},
srcPKDataType{std::move(srcPKDataType)}, dstPKDataType{std::move(dstPKDataType)} {}
RelTableSchema(std::string tableName, common::table_id_t tableID,
std::vector<std::unique_ptr<Property>> properties, common::property_id_t nextPropertyID,
RelMultiplicity relMultiplicity, common::table_id_t srcTableID,
common::table_id_t dstTableID, std::unique_ptr<common::LogicalType> srcPKDataType,
std::vector<std::unique_ptr<Property>> properties, std::string comment,
common::property_id_t nextPropertyID, RelMultiplicity relMultiplicity,
common::table_id_t srcTableID, common::table_id_t dstTableID,
std::unique_ptr<common::LogicalType> srcPKDataType,
std::unique_ptr<common::LogicalType> dstPKDataType)
: TableSchema{common::TableType::REL, std::move(tableName), tableID, std::move(properties),
nextPropertyID},
std::move(comment), nextPropertyID},
relMultiplicity{relMultiplicity}, srcTableID{srcTableID}, dstTableID{dstTableID},
srcPKDataType{std::move(srcPKDataType)}, dstPKDataType{std::move(dstPKDataType)} {}

Expand Down Expand Up @@ -73,7 +74,7 @@ class RelTableSchema : public TableSchema {

inline std::unique_ptr<TableSchema> copy() const override {
return std::make_unique<RelTableSchema>(tableName, tableID, Property::copy(properties),
nextPropertyID, relMultiplicity, srcTableID, dstTableID, srcPKDataType->copy(),
comment, nextPropertyID, relMultiplicity, srcTableID, dstTableID, srcPKDataType->copy(),
dstPKDataType->copy());
}

Expand Down
Loading

0 comments on commit defa82b

Please sign in to comment.