Skip to content

Commit

Permalink
Merge pull request #1169 from kuzudb/drop-col
Browse files Browse the repository at this point in the history
add support for deleting property
  • Loading branch information
acquamarin committed Jan 11, 2023
2 parents 7022f5b + 884e930 commit d206fa8
Show file tree
Hide file tree
Showing 66 changed files with 3,462 additions and 3,060 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
5 changes: 2 additions & 3 deletions src/binder/bind/bind_copy_csv.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "binder/bind/bound_copy_csv.h"
#include "binder/binder.h"
#include "binder/copy_csv/bound_copy_csv.h"
#include "binder/expression/literal_expression.h"
#include "parser/copy_csv/copy_csv.h"

Expand All @@ -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
35 changes: 29 additions & 6 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_table.h"
#include "binder/binder.h"
#include "binder/ddl/bound_create_node_clause.h"
#include "binder/ddl/bound_create_rel_clause.h"
#include "binder/ddl/bound_drop_property.h"
#include "binder/ddl/bound_drop_table.h"
#include "parser/ddl/create_node_clause.h"
#include "parser/ddl/create_rel_clause.h"
#include "parser/ddl/drop_property.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::bindDropProperty(const Statement& statement) {
auto& dropProperty = (DropProperty&)statement;
auto tableName = dropProperty.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<BoundDropProperty>(tableID,
bindPropertyName(catalogContent->getTableSchema(tableID), dropProperty.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
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_PROPERTY: {
return bindDropProperty(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->getTableName(i));
} 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
22 changes: 0 additions & 22 deletions src/include/binder/bind/bound_drop_table.h

This file was deleted.

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> bindDropProperty(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
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
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
#pragma once

#include "binder/bound_statement.h"
#include "bound_ddl.h"
#include "catalog/catalog_structs.h"

using namespace kuzu::catalog;

namespace kuzu {
namespace binder {

class BoundCreateTable : public BoundStatement {
class BoundCreateTable : public BoundDDL {
public:
explicit BoundCreateTable(StatementType statementType, string tableName,
vector<PropertyNameDataType> propertyNameDataTypes)
: BoundStatement{statementType, BoundStatementResult::createSingleStringColumnResult()},
tableName{std::move(tableName)}, propertyNameDataTypes{std::move(propertyNameDataTypes)} {
}
: BoundDDL{statementType, std::move(tableName)}, propertyNameDataTypes{
std::move(propertyNameDataTypes)} {}

inline string getTableName() const { return tableName; }
inline vector<PropertyNameDataType> getPropertyNameDataTypes() const {
return propertyNameDataTypes;
}

private:
string tableName;
vector<PropertyNameDataType> propertyNameDataTypes;
};

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

#include "binder/bound_statement.h"

namespace kuzu {
namespace binder {

class BoundDDL : public BoundStatement {
public:
explicit BoundDDL(StatementType statementType, string tableName)
: BoundStatement{statementType, BoundStatementResult::createSingleStringColumnResult()},
tableName{std::move(tableName)} {}

inline string getTableName() const { return tableName; }

private:
string tableName;
};

} // namespace binder
} // namespace kuzu
24 changes: 24 additions & 0 deletions src/include/binder/ddl/bound_drop_property.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "bound_ddl.h"

namespace kuzu {
namespace binder {

class BoundDropProperty : public BoundDDL {
public:
explicit BoundDropProperty(table_id_t tableID, property_id_t propertyID, string tableName)
: BoundDDL{StatementType::DROP_PROPERTY, std::move(tableName)}, tableID{tableID},
propertyID{propertyID} {}

inline table_id_t getTableID() const { return tableID; }

inline property_id_t getPropertyID() const { return propertyID; }

private:
table_id_t tableID;
property_id_t propertyID;
};

} // namespace binder
} // namespace kuzu
20 changes: 20 additions & 0 deletions src/include/binder/ddl/bound_drop_table.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "bound_ddl.h"

namespace kuzu {
namespace binder {

class BoundDropTable : public BoundDDL {
public:
explicit BoundDropTable(table_id_t tableID, string tableName)
: BoundDDL{StatementType::DROP_TABLE, std::move(tableName)}, tableID{tableID} {}

inline table_id_t getTableID() const { return tableID; }

private:
table_id_t tableID;
};

} // namespace binder
} // namespace kuzu
15 changes: 8 additions & 7 deletions src/include/catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,8 @@ 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 {
Expand All @@ -68,6 +63,12 @@ class CatalogContent {
assert(containRelTable(tableID));
return relTableSchemas.at(tableID).get();
}
inline TableSchema* getTableSchema(table_id_t tableID) const {
assert(containRelTable(tableID) || containNodeTable(tableID));
return nodeTableSchemas.contains(tableID) ?
(TableSchema*)nodeTableSchemas.at(tableID).get() :
(TableSchema*)relTableSchemas.at(tableID).get();
}

inline bool containNodeTable(const string& tableName) const {
return nodeTableNameToIDMap.contains(tableName);
Expand Down
Loading

0 comments on commit d206fa8

Please sign in to comment.