Skip to content

Commit

Permalink
add support for deleting property
Browse files Browse the repository at this point in the history
  • Loading branch information
acquamarin committed Jan 11, 2023
1 parent 7022f5b commit 123995c
Show file tree
Hide file tree
Showing 67 changed files with 3,451 additions and 3,002 deletions.
8 changes: 7 additions & 1 deletion src/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ FROM : ( 'F' | 'f' ) ( 'R' | 'r' ) ( 'O' | 'o' ) ( 'M' | 'm' );
kU_DDL
: kU_CreateNode
| kU_CreateRel
| kU_DropTable;
| kU_DropTable
| kU_AlterTable;

kU_CreateNode
: CREATE SP NODE SP TABLE SP oC_SchemaName SP? '(' SP? kU_PropertyDefinitions SP? ( ',' SP? kU_CreateNodeConstraint ) SP? ')' ;
Expand All @@ -51,6 +52,11 @@ kU_DropTable

DROP : ( 'D' | 'd' ) ( 'R' | 'r' ) ( 'O' | 'o' ) ( 'P' | 'p' ) ;

kU_AlterTable
: ALTER SP TABLE SP oC_SchemaName SP DROP SP oC_PropertyKeyName ;

ALTER: ( 'A' | 'a' ) ( 'L' | 'l' ) ( 'T' | 't' ) ( 'E' | 'e' ) ( 'R' | 'r' ) ;

kU_RelConnections : kU_RelConnection ( SP? ',' SP? kU_RelConnection )* ;

kU_RelConnection: FROM SP kU_NodeLabels SP TO SP kU_NodeLabels ;
Expand Down
3 changes: 1 addition & 2 deletions src/binder/bind/bind_copy_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ unique_ptr<BoundStatement> Binder::bindCopyCSV(const Statement& statement) {
catalogContent->getRelTableIDFromName(tableName);
auto filePath = copyCSV.getCSVFileName();
auto csvReaderConfig = bindParsingOptions(copyCSV.getParsingOptions());
return make_unique<BoundCopyCSV>(
CSVDescription(filePath, csvReaderConfig), TableSchema(tableName, tableID, isNodeTable));
return make_unique<BoundCopyCSV>(CSVDescription(filePath, csvReaderConfig), tableID, tableName);
}

CSVReaderConfig Binder::bindParsingOptions(
Expand Down
29 changes: 26 additions & 3 deletions src/binder/bind/bind_ddl.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "binder/bind/bound_create_node_clause.h"
#include "binder/bind/bound_create_rel_clause.h"
#include "binder/bind/bound_drop_column.h"
#include "binder/bind/bound_drop_table.h"
#include "binder/binder.h"
#include "parser/ddl/create_node_clause.h"
#include "parser/ddl/create_rel_clause.h"
#include "parser/ddl/drop_column.h"
#include "parser/ddl/drop_table.h"

namespace kuzu {
Expand Down Expand Up @@ -52,9 +54,20 @@ unique_ptr<BoundStatement> Binder::bindDropTable(const Statement& statement) {
if (isNodeTable) {
validateNodeTableHasNoEdge(catalog, tableID);
}
return make_unique<BoundDropTable>(
isNodeTable ? (TableSchema*)catalogContent->getNodeTableSchema(tableID) :
(TableSchema*)catalogContent->getRelTableSchema(tableID));
return make_unique<BoundDropTable>(tableID, tableName);
}

unique_ptr<BoundStatement> Binder::bindDropColumn(const Statement& statement) {
auto& dropColumn = (DropColumn&)statement;
auto tableName = dropColumn.getTableName();
validateTableExist(catalog, tableName);
auto catalogContent = catalog.getReadOnlyVersion();
auto isNodeTable = catalogContent->containNodeTable(tableName);
auto tableID = isNodeTable ? catalogContent->getNodeTableIDFromName(tableName) :
catalogContent->getRelTableIDFromName(tableName);
return make_unique<BoundDropColumn>(tableID,
bindPropertyName(catalogContent->getTableSchema(tableID), dropColumn.getPropertyName()),
tableName);
}

vector<PropertyNameDataType> Binder::bindPropertyNameDataTypes(
Expand Down Expand Up @@ -100,5 +113,15 @@ uint32_t Binder::bindPrimaryKey(
return primaryKeyIdx;
}

property_id_t Binder::bindPropertyName(TableSchema* tableSchema, string propertyName) {
for (auto& property : tableSchema->properties) {
if (property.name == propertyName) {
return property.propertyID;
}
}
throw BinderException(
tableSchema->tableName + " table doesn't have property: " + propertyName + ".");
}

} // namespace binder
} // namespace kuzu
9 changes: 6 additions & 3 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ static void validateNodeRelConnectivity(const Catalog& catalog_, const RelExpres
set<pair<table_id_t, table_id_t>> srcDstTableIDs;
for (auto relTableID : rel.getTableIDs()) {
for (auto [srcTableID, dstTableID] :
catalog_.getReadOnlyVersion()->getRelTableSchema(relTableID)->srcDstTableIDs) {
((RelTableSchema*)catalog_.getReadOnlyVersion()->getTableSchema(relTableID))
->srcDstTableIDs) {
srcDstTableIDs.insert({srcTableID, dstTableID});
}
}
Expand Down Expand Up @@ -131,7 +132,8 @@ void Binder::bindQueryRel(const RelPattern& relPattern, const shared_ptr<NodeExp
// resolve properties associate with rel table
vector<RelTableSchema*> relTableSchemas;
for (auto tableID : tableIDs) {
relTableSchemas.push_back(catalog.getReadOnlyVersion()->getRelTableSchema(tableID));
relTableSchemas.push_back(
(RelTableSchema*)catalog.getReadOnlyVersion()->getTableSchema(tableID));
}
// we don't support reading property for variable length rel yet.
if (!queryRel->isVariableLength()) {
Expand Down Expand Up @@ -209,7 +211,8 @@ shared_ptr<NodeExpression> Binder::createQueryNode(const NodePattern& nodePatter
// resolve properties associate with node table
vector<NodeTableSchema*> nodeTableSchemas;
for (auto tableID : tableIDs) {
nodeTableSchemas.push_back(catalog.getReadOnlyVersion()->getNodeTableSchema(tableID));
nodeTableSchemas.push_back(
(NodeTableSchema*)catalog.getReadOnlyVersion()->getTableSchema(tableID));
}
for (auto& [propertyName, propertySchemas] :
getNodePropertyNameAndPropertiesPairs(nodeTableSchemas)) {
Expand Down
6 changes: 4 additions & 2 deletions src/binder/bind/bind_updating_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ unique_ptr<BoundCreateNode> Binder::bindCreateNode(
"Create node " + node->getRawName() + " with multiple node labels is not supported.");
}
auto nodeTableID = node->getSingleTableID();
auto nodeTableSchema = catalog.getReadOnlyVersion()->getNodeTableSchema(nodeTableID);
auto nodeTableSchema =
(NodeTableSchema*)catalog.getReadOnlyVersion()->getTableSchema(nodeTableID);
auto primaryKey = nodeTableSchema->getPrimaryKey();
shared_ptr<Expression> primaryKeyExpression;
vector<expression_pair> setItems;
Expand Down Expand Up @@ -180,7 +181,8 @@ unique_ptr<BoundDeleteNode> Binder::bindDeleteNode(shared_ptr<NodeExpression> no
"Delete node " + node->getRawName() + " with multiple node labels is not supported.");
}
auto nodeTableID = node->getSingleTableID();
auto nodeTableSchema = catalog.getReadOnlyVersion()->getNodeTableSchema(nodeTableID);
auto nodeTableSchema =
(NodeTableSchema*)catalog.getReadOnlyVersion()->getTableSchema(nodeTableID);
auto primaryKeyExpression =
expressionBinder.bindNodePropertyExpression(*node, nodeTableSchema->getPrimaryKey().name);
return make_unique<BoundDeleteNode>(node, primaryKeyExpression);
Expand Down
5 changes: 4 additions & 1 deletion src/binder/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ unique_ptr<BoundStatement> Binder::bind(const Statement& statement) {
case StatementType::COPY_CSV: {
return bindCopyCSV(statement);
}
case StatementType::DROP_COLUMN: {
return bindDropColumn(statement);
}
case StatementType::QUERY: {
return bindQuery((const RegularQuery&)statement);
}
Expand Down Expand Up @@ -189,7 +192,7 @@ unordered_map<string, shared_ptr<Expression>> Binder::enterSubquery() {
}

void Binder::exitSubquery(unordered_map<string, shared_ptr<Expression>> prevVariablesInScope) {
variablesInScope = move(prevVariablesInScope);
variablesInScope = std::move(prevVariablesInScope);
}

} // namespace binder
Expand Down
5 changes: 2 additions & 3 deletions src/binder/expression_binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ shared_ptr<Expression> ExpressionBinder::bindNodeLabelFunction(const Expression&
auto catalogContent = binder->catalog.getReadOnlyVersion();
auto& node = (NodeExpression&)expression;
if (!node.isMultiLabeled()) {
auto labelName = catalogContent->getNodeTableSchema(node.getSingleTableID())->tableName;
auto labelName = catalogContent->getTableName(node.getSingleTableID());
return make_shared<LiteralExpression>(STRING, make_unique<Literal>(labelName));
}
// bind string node labels as list literal
Expand All @@ -351,8 +351,7 @@ shared_ptr<Expression> ExpressionBinder::bindNodeLabelFunction(const Expression&
nodeLabels.resize(maxNodeTableID + 1);
for (auto i = 0; i < nodeLabels.size(); ++i) {
if (catalogContent->containNodeTable(i)) {
auto tableSchema = catalogContent->getNodeTableSchema(i);
nodeLabels[i] = Literal(tableSchema->tableName);
nodeLabels[i] = Literal(catalogContent->getTableSchema(i)->tableName);
} else {
// TODO(Xiyang/Guodong): change to null literal once we support null in LIST type.
nodeLabels[i] = Literal(string(""));
Expand Down
11 changes: 11 additions & 0 deletions src/catalog/catalog_structs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "catalog/catalog_structs.h"

#include "common/exception.h"
#include "common/utils.h"

namespace kuzu {
namespace catalog {
Expand Down Expand Up @@ -37,6 +38,16 @@ string getRelMultiplicityAsString(RelMultiplicity relMultiplicity) {
}
}

string TableSchema::getPropertyName(property_id_t propertyID) const {
for (auto& property : properties) {
if (property.propertyID == propertyID) {
return property.name;
}
}
throw RuntimeException(StringUtils::string_format(
"Table: %s doesn't have a property with propertyID=%d.", tableName.c_str(), propertyID));
}

unordered_set<table_id_t> RelTableSchema::getAllNodeTableIDs() const {
unordered_set<table_id_t> allNodeTableIDs;
for (auto& [srcTableID, dstTableID] : srcDstTableIDs) {
Expand Down
12 changes: 8 additions & 4 deletions src/include/binder/bind/bound_copy_csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ namespace binder {

class BoundCopyCSV : public BoundStatement {
public:
BoundCopyCSV(CSVDescription csvDescription, TableSchema tableSchema)
BoundCopyCSV(CSVDescription csvDescription, table_id_t tableID, string tableName)
: BoundStatement{StatementType::COPY_CSV,
BoundStatementResult::createSingleStringColumnResult()},
csvDescription{std::move(csvDescription)}, tableSchema{std::move(tableSchema)} {}
csvDescription{std::move(csvDescription)}, tableID{tableID}, tableName{
std::move(tableName)} {}

inline CSVDescription getCSVDescription() const { return csvDescription; }

inline TableSchema getTableSchema() const { return tableSchema; }
inline table_id_t getTableID() const { return tableID; }

inline string getTableName() const { return tableName; }

private:
CSVDescription csvDescription;
TableSchema tableSchema;
table_id_t tableID;
string tableName;
};

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

#include "binder/bound_statement.h"

namespace kuzu {
namespace binder {

class BoundDropColumn : public BoundStatement {
public:
explicit BoundDropColumn(table_id_t tableID, property_id_t propertyID, string tableName)
: BoundStatement{StatementType::DROP_COLUMN,
BoundStatementResult::createSingleStringColumnResult()},
tableID{tableID}, propertyID{propertyID}, tableName{std::move(tableName)} {}

inline table_id_t getTableID() const { return tableID; }

inline property_id_t getPropertyID() const { return propertyID; }

inline string getTableName() const { return tableName; }

private:
table_id_t tableID;
property_id_t propertyID;
string tableName;
};

} // namespace binder
} // namespace kuzu
11 changes: 7 additions & 4 deletions src/include/binder/bind/bound_drop_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ namespace binder {

class BoundDropTable : public BoundStatement {
public:
explicit BoundDropTable(TableSchema* tableSchema)
explicit BoundDropTable(table_id_t tableID, string tableName)
: BoundStatement{StatementType::DROP_TABLE,
BoundStatementResult::createSingleStringColumnResult()},
tableSchema{tableSchema} {}
tableID{tableID}, tableName{std::move(tableName)} {}

inline TableSchema* getTableSchema() const { return tableSchema; }
inline table_id_t getTableID() const { return tableID; }

inline string getTableName() const { return tableName; }

private:
TableSchema* tableSchema;
table_id_t tableID;
string tableName;
};

} // namespace binder
Expand Down
2 changes: 2 additions & 0 deletions src/include/binder/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class Binder {
unique_ptr<BoundStatement> bindCreateNodeClause(const Statement& statement);
unique_ptr<BoundStatement> bindCreateRelClause(const Statement& statement);
unique_ptr<BoundStatement> bindDropTable(const Statement& statement);
unique_ptr<BoundStatement> bindDropColumn(const Statement& statement);

vector<PropertyNameDataType> bindPropertyNameDataTypes(
vector<pair<string, string>> propertyNameDataTypes);
uint32_t bindPrimaryKey(string pkColName, vector<pair<string, string>> propertyNameDataTypes);
property_id_t bindPropertyName(TableSchema* tableSchema, string propertyName);

/*** bind copy csv ***/
unique_ptr<BoundStatement> bindCopyCSV(const Statement& statement);
Expand Down
2 changes: 0 additions & 2 deletions src/include/binder/bound_statement.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <cstdint>

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

Expand Down
22 changes: 7 additions & 15 deletions src/include/catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,14 @@ class CatalogContent {
return relTableSchemas.contains(tableID);
}

inline string getNodeTableName(table_id_t tableID) const {
assert(containNodeTable(tableID));
return nodeTableSchemas.at(tableID)->tableName;
}
inline string getRelTableName(table_id_t tableID) const {
assert(containRelTable(tableID));
return relTableSchemas.at(tableID)->tableName;
inline string getTableName(table_id_t tableID) const {
return getTableSchema(tableID)->tableName;
}

inline NodeTableSchema* getNodeTableSchema(table_id_t tableID) const {
assert(containNodeTable(tableID));
return nodeTableSchemas.at(tableID).get();
}
inline RelTableSchema* getRelTableSchema(table_id_t tableID) const {
assert(containRelTable(tableID));
return relTableSchemas.at(tableID).get();
TableSchema* getTableSchema(table_id_t tableID) const {
return nodeTableSchemas.contains(tableID) ?
(TableSchema*)nodeTableSchemas.at(tableID).get() :
(TableSchema*)relTableSchemas.at(tableID).get();
}

inline bool containNodeTable(const string& tableName) const {
Expand Down Expand Up @@ -121,7 +113,7 @@ class CatalogContent {
}
inline unordered_set<table_id_t> getNodeTableIDsForRelTableDirection(
table_id_t relTableID, RelDirection direction) const {
auto relTableSchema = getRelTableSchema(relTableID);
auto relTableSchema = (RelTableSchema*)getTableSchema(relTableID);
return direction == FWD ? relTableSchema->getUniqueSrcTableIDs() :
relTableSchema->getUniqueDstTableIDs();
}
Expand Down
Loading

0 comments on commit 123995c

Please sign in to comment.