Skip to content

Commit

Permalink
Merge pull request #1528 from kuzudb/undirected-query
Browse files Browse the repository at this point in the history
Undirected query logical changes
  • Loading branch information
aziz-mu committed May 12, 2023
2 parents be74276 + 7f1f767 commit 4f75391
Show file tree
Hide file tree
Showing 34 changed files with 1,885 additions and 1,819 deletions.
1 change: 1 addition & 0 deletions src/antlr4/Cypher.g4
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ oC_PatternElementChain
oC_RelationshipPattern
: ( oC_LeftArrowHead SP? oC_Dash SP? oC_RelationshipDetail? SP? oC_Dash )
| ( oC_Dash SP? oC_RelationshipDetail? SP? oC_Dash SP? oC_RightArrowHead )
| ( oC_Dash SP? oC_RelationshipDetail? SP? oC_Dash )
;

oC_RelationshipDetail
Expand Down
3 changes: 2 additions & 1 deletion src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ void Binder::bindQueryRel(const RelPattern& relPattern,
// bind variable length
auto [lowerBound, upperBound] = bindVariableLengthRelBound(relPattern);
auto queryRel = make_shared<RelExpression>(getUniqueExpressionName(parsedName), parsedName,
tableIDs, srcNode, dstNode, relPattern.getRelType(), lowerBound, upperBound);
tableIDs, srcNode, dstNode, relPattern.getDirection() != BOTH, relPattern.getRelType(),
lowerBound, upperBound);
queryRel->setAlias(parsedName);
// resolve properties associate with rel table
std::vector<RelTableSchema*> relTableSchemas;
Expand Down
4 changes: 2 additions & 2 deletions src/common/types/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,11 @@ bool Types::isNumerical(const kuzu::common::DataType& dataType) {
}
}

RelDirection operator!(RelDirection& direction) {
RelDataDirection operator!(RelDataDirection& direction) {
return (FWD == direction) ? BWD : FWD;
}

std::string getRelDirectionAsString(RelDirection direction) {
std::string getRelDataDirectionAsString(RelDataDirection direction) {
return (FWD == direction) ? "forward" : "backward";
}

Expand Down
11 changes: 7 additions & 4 deletions src/include/binder/expression/rel_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class RelExpression : public NodeOrRelExpression {
public:
RelExpression(std::string uniqueName, std::string variableName,
std::vector<common::table_id_t> tableIDs, std::shared_ptr<NodeExpression> srcNode,
std::shared_ptr<NodeExpression> dstNode, common::QueryRelType relType, uint64_t lowerBound,
uint64_t upperBound)
std::shared_ptr<NodeExpression> dstNode, bool directed, common::QueryRelType relType,
uint64_t lowerBound, uint64_t upperBound)
: NodeOrRelExpression{common::REL, std::move(uniqueName), std::move(variableName),
std::move(tableIDs)},
srcNode{std::move(srcNode)}, dstNode{std::move(dstNode)}, relType{relType},
lowerBound{lowerBound}, upperBound{upperBound} {}
srcNode{std::move(srcNode)}, dstNode{std::move(dstNode)}, directed{directed},
relType{relType}, lowerBound{lowerBound}, upperBound{upperBound} {}

inline bool isBoundByMultiLabeledNode() const {
return srcNode->isMultiLabeled() || dstNode->isMultiLabeled();
Expand All @@ -31,6 +31,8 @@ class RelExpression : public NodeOrRelExpression {
inline uint64_t getLowerBound() const { return lowerBound; }
inline uint64_t getUpperBound() const { return upperBound; }

inline bool isDirected() const { return directed; }

inline bool hasInternalIDProperty() const {
return hasPropertyExpression(common::INTERNAL_ID_SUFFIX);
}
Expand All @@ -48,6 +50,7 @@ class RelExpression : public NodeOrRelExpression {
private:
std::shared_ptr<NodeExpression> srcNode;
std::shared_ptr<NodeExpression> dstNode;
bool directed;
common::QueryRelType relType;
uint64_t lowerBound;
uint64_t upperBound;
Expand Down
2 changes: 1 addition & 1 deletion src/include/catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CatalogContent {
relTableNameToIDMap.at(tableName);
}
inline bool isSingleMultiplicityInDirection(
common::table_id_t tableID, common::RelDirection direction) const {
common::table_id_t tableID, common::RelDataDirection direction) const {
return relTableSchemas.at(tableID)->isSingleMultiplicityInDirection(direction);
}

Expand Down
13 changes: 7 additions & 6 deletions src/include/catalog/catalog_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ struct RelTableSchema : TableSchema {
throw common::InternalException("Cannot find internal rel ID definition.");
}

inline bool isSingleMultiplicityInDirection(common::RelDirection direction) const {
inline bool isSingleMultiplicityInDirection(common::RelDataDirection direction) const {
return relMultiplicity == ONE_ONE ||
relMultiplicity == (direction == common::RelDirection::FWD ? MANY_ONE : ONE_MANY);
relMultiplicity ==
(direction == common::RelDataDirection::FWD ? MANY_ONE : ONE_MANY);
}

inline uint32_t getNumUserDefinedProperties() const {
Expand All @@ -152,12 +153,12 @@ struct RelTableSchema : TableSchema {
return srcTableID == tableID || dstTableID == tableID;
}

inline common::table_id_t getBoundTableID(common::RelDirection relDirection) const {
return relDirection == common::RelDirection::FWD ? srcTableID : dstTableID;
inline common::table_id_t getBoundTableID(common::RelDataDirection relDirection) const {
return relDirection == common::RelDataDirection::FWD ? srcTableID : dstTableID;
}

inline common::table_id_t getNbrTableID(common::RelDirection relDirection) const {
return relDirection == common::RelDirection::FWD ? dstTableID : srcTableID;
inline common::table_id_t getNbrTableID(common::RelDataDirection relDirection) const {
return relDirection == common::RelDataDirection::FWD ? dstTableID : srcTableID;
}

RelMultiplicity relMultiplicity;
Expand Down
12 changes: 7 additions & 5 deletions src/include/common/types/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ class Types {
static DataTypeID dataTypeIDFromString(const std::string& dataTypeIDString);
};

// RelDirection
enum RelDirection : uint8_t { FWD = 0, BWD = 1 };
const std::vector<RelDirection> REL_DIRECTIONS = {FWD, BWD};
RelDirection operator!(RelDirection& direction);
std::string getRelDirectionAsString(RelDirection relDirection);
// RelDataDirection
enum RelDataDirection : uint8_t { FWD = 0, BWD = 1 };
const std::vector<RelDataDirection> REL_DIRECTIONS = {FWD, BWD};
RelDataDirection operator!(RelDataDirection& direction);
std::string getRelDataDirectionAsString(RelDataDirection relDirection);

enum class ExtendDirection : uint8_t { FWD = 0, BWD = 1, BOTH = 2 };

enum class DBFileType : uint8_t { ORIGINAL = 0, WAL_VERSION = 1 };

Expand Down
2 changes: 1 addition & 1 deletion src/include/parser/query/graph_pattern/rel_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace kuzu {
namespace parser {

enum ArrowDirection : uint8_t { LEFT = 0, RIGHT = 1 };
enum ArrowDirection : uint8_t { LEFT = 0, RIGHT = 1, BOTH = 2 };

/**
* RelationshipPattern represents "-[relName:RelTableName+]-"
Expand Down
8 changes: 4 additions & 4 deletions src/include/planner/join_order_enumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class JoinOrderEnumerator {
void planBaseTableScan();
void planNodeScan(uint32_t nodePos);
void planRelScan(uint32_t relPos);
void appendExtendAndFilter(std::shared_ptr<RelExpression> rel, common::RelDirection direction,
const expression_vector& predicates, LogicalPlan& plan);
void appendExtendAndFilter(std::shared_ptr<RelExpression> rel,
common::ExtendDirection direction, const expression_vector& predicates, LogicalPlan& plan);

void planLevel(uint32_t level);
void planLevelExactly(uint32_t level);
Expand All @@ -84,11 +84,11 @@ class JoinOrderEnumerator {

void appendNonRecursiveExtend(std::shared_ptr<NodeExpression> boundNode,
std::shared_ptr<NodeExpression> nbrNode, std::shared_ptr<RelExpression> rel,
common::RelDirection direction, const binder::expression_vector& properties,
common::ExtendDirection direction, const binder::expression_vector& properties,
LogicalPlan& plan);
void appendRecursiveExtend(std::shared_ptr<NodeExpression> boundNode,
std::shared_ptr<NodeExpression> nbrNode, std::shared_ptr<RelExpression> rel,
common::RelDirection direction, LogicalPlan& plan);
common::ExtendDirection direction, LogicalPlan& plan);

void planJoin(const binder::expression_vector& joinNodeIDs, common::JoinType joinType,
std::shared_ptr<Expression> mark, LogicalPlan& probePlan, LogicalPlan& buildPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ class BaseLogicalExtend : public LogicalOperator {
BaseLogicalExtend(LogicalOperatorType operatorType,
std::shared_ptr<binder::NodeExpression> boundNode,
std::shared_ptr<binder::NodeExpression> nbrNode, std::shared_ptr<binder::RelExpression> rel,
common::RelDirection direction, std::shared_ptr<LogicalOperator> child)
common::ExtendDirection direction, std::shared_ptr<LogicalOperator> child)
: LogicalOperator{operatorType, std::move(child)}, boundNode{std::move(boundNode)},
nbrNode{std::move(nbrNode)}, rel{std::move(rel)}, direction{direction} {}

inline std::shared_ptr<binder::NodeExpression> getBoundNode() const { return boundNode; }
inline std::shared_ptr<binder::NodeExpression> getNbrNode() const { return nbrNode; }
inline std::shared_ptr<binder::RelExpression> getRel() const { return rel; }
inline common::RelDirection getDirection() const { return direction; }

inline common::ExtendDirection getDirection() const { return direction; }
virtual f_group_pos_set getGroupsPosToFlatten() = 0;

std::string getExpressionsForPrinting() const override;
Expand All @@ -28,7 +27,7 @@ class BaseLogicalExtend : public LogicalOperator {
std::shared_ptr<binder::NodeExpression> boundNode;
std::shared_ptr<binder::NodeExpression> nbrNode;
std::shared_ptr<binder::RelExpression> rel;
common::RelDirection direction;
common::ExtendDirection direction;
};

} // namespace planner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class LogicalExtend : public BaseLogicalExtend {
public:
LogicalExtend(std::shared_ptr<binder::NodeExpression> boundNode,
std::shared_ptr<binder::NodeExpression> nbrNode, std::shared_ptr<binder::RelExpression> rel,
common::RelDirection direction, binder::expression_vector properties, bool hasAtMostOneNbr,
std::shared_ptr<LogicalOperator> child)
common::ExtendDirection direction, binder::expression_vector properties,
bool hasAtMostOneNbr, std::shared_ptr<LogicalOperator> child)
: BaseLogicalExtend{LogicalOperatorType::EXTEND, std::move(boundNode), std::move(nbrNode),
std::move(rel), direction, std::move(child)},
properties{std::move(properties)}, hasAtMostOneNbr{hasAtMostOneNbr} {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LogicalRecursiveExtend : public BaseLogicalExtend {
public:
LogicalRecursiveExtend(std::shared_ptr<binder::NodeExpression> boundNode,
std::shared_ptr<binder::NodeExpression> nbrNode, std::shared_ptr<binder::RelExpression> rel,
common::RelDirection direction, std::shared_ptr<LogicalOperator> child)
common::ExtendDirection direction, std::shared_ptr<LogicalOperator> child)
: BaseLogicalExtend{LogicalOperatorType::RECURSIVE_EXTEND, std::move(boundNode),
std::move(nbrNode), std::move(rel), direction, std::move(child)} {}

Expand Down
4 changes: 2 additions & 2 deletions src/include/storage/copier/rel_copy_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class RelCopyExecutor : public TableCopyExecutor {

void saveToFile() override;

void initializeColumns(common::RelDirection relDirection);
void initializeColumns(common::RelDataDirection relDirection);

void initializeLists(common::RelDirection relDirection);
void initializeLists(common::RelDataDirection relDirection);

void initAdjListsHeaders();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ListsUpdatesStore {
}
initListsUpdatesPerTablePerDirection();
}
inline ListsUpdatesPerChunk& getListsUpdatesPerChunk(common::RelDirection relDirection) {
inline ListsUpdatesPerChunk& getListsUpdatesPerChunk(common::RelDataDirection relDirection) {
return listsUpdatesPerDirection[relDirection];
}

Expand Down Expand Up @@ -148,7 +148,7 @@ class ListsUpdatesStore {
void initListsUpdatesPerTablePerDirection();

ListsUpdatesForNodeOffset* getOrCreateListsUpdatesForNodeOffset(
common::RelDirection relDirection, common::nodeID_t nodeID);
common::RelDataDirection relDirection, common::nodeID_t nodeID);

ListsUpdatesForNodeOffset* getListsUpdatesForNodeOffsetIfExists(
ListFileID& listFileID, common::offset_t nodeOffset) const;
Expand Down
23 changes: 12 additions & 11 deletions src/include/storage/storage_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class StorageUtils {
// Returns the StorageStructureIDAndFName for the "base" lists structure/file. Callers need to
// modify it to obtain versions for METADATA and HEADERS structures/files.
static inline StorageStructureIDAndFName getAdjListsStructureIDAndFName(
const std::string& directory, common::table_id_t relTableID, common::RelDirection dir) {
const std::string& directory, common::table_id_t relTableID, common::RelDataDirection dir) {
auto fName = getAdjListsFName(directory, relTableID, dir, common::DBFileType::ORIGINAL);
return StorageStructureIDAndFName(
StorageStructureID::newAdjListsID(relTableID, dir, ListFileType::BASE_LISTS), fName);
Expand All @@ -110,7 +110,7 @@ class StorageUtils {
// Returns the StorageStructureIDAndFName for the "base" lists structure/file. Callers need to
// modify it to obtain versions for METADATA and HEADERS structures/files.
static inline StorageStructureIDAndFName getRelPropertyListsStructureIDAndFName(
const std::string& directory, common::table_id_t relTableID, common::RelDirection dir,
const std::string& directory, common::table_id_t relTableID, common::RelDataDirection dir,
const catalog::Property& property) {
auto fName = getRelPropertyListsFName(
directory, relTableID, dir, property.propertyID, common::DBFileType::ORIGINAL);
Expand All @@ -120,29 +120,29 @@ class StorageUtils {
}

static std::string getAdjColumnFName(const std::string& directory,
const common::table_id_t& relTableID, const common::RelDirection& relDirection,
const common::table_id_t& relTableID, const common::RelDataDirection& relDirection,
common::DBFileType dbFileType);

static inline StorageStructureIDAndFName getAdjColumnStructureIDAndFName(
const std::string& directory, const common::table_id_t& relTableID,
const common::RelDirection& relDirection) {
const common::RelDataDirection& relDirection) {
auto fName =
getAdjColumnFName(directory, relTableID, relDirection, common::DBFileType::ORIGINAL);
return StorageStructureIDAndFName(
StorageStructureID::newAdjColumnID(relTableID, relDirection), fName);
}

static std::string getAdjListsFName(const std::string& directory,
const common::table_id_t& relTableID, const common::RelDirection& relDirection,
const common::table_id_t& relTableID, const common::RelDataDirection& relDirection,
common::DBFileType dbFileType);

static std::string getRelPropertyColumnFName(const std::string& directory,
const common::table_id_t& relTableID, const common::RelDirection& relDirection,
const common::table_id_t& relTableID, const common::RelDataDirection& relDirection,
uint32_t propertyID, common::DBFileType dbFileType);

static inline StorageStructureIDAndFName getRelPropertyColumnStructureIDAndFName(
const std::string& directory, const common::table_id_t& relTableID,
const common::RelDirection& relDirection, uint32_t propertyID) {
const common::RelDataDirection& relDirection, uint32_t propertyID) {
auto fName = getRelPropertyColumnFName(
directory, relTableID, relDirection, propertyID, common::DBFileType::ORIGINAL);
return StorageStructureIDAndFName(
Expand All @@ -151,7 +151,7 @@ class StorageUtils {
}

static std::string getRelPropertyListsFName(const std::string& directory,
const common::table_id_t& relTableID, const common::RelDirection& relDirection,
const common::table_id_t& relTableID, const common::RelDataDirection& relDirection,
uint32_t propertyID, common::DBFileType dbFileType);

static inline std::string getListHeadersFName(std::string baseListFName) {
Expand Down Expand Up @@ -273,19 +273,20 @@ class StorageUtils {
StorageManager& storageManager);

static void initializeListsHeaders(const catalog::RelTableSchema* relTableSchema,
uint64_t numNodesInTable, const std::string& directory, common::RelDirection relDirection);
uint64_t numNodesInTable, const std::string& directory,
common::RelDataDirection relDirection);

private:
static std::string appendSuffixOrInsertBeforeWALSuffix(
std::string fileName, std::string suffix);

static void createFileForRelColumnPropertyWithDefaultVal(common::table_id_t relTableID,
common::table_id_t boundTableID, common::RelDirection direction,
common::table_id_t boundTableID, common::RelDataDirection direction,
const catalog::Property& property, uint8_t* defaultVal, bool isDefaultValNull,
StorageManager& storageManager);

static void createFileForRelListsPropertyWithDefaultVal(common::table_id_t relTableID,
common::table_id_t boundTableID, common::RelDirection direction,
common::table_id_t boundTableID, common::RelDataDirection direction,
const catalog::Property& property, uint8_t* defaultVal, bool isDefaultValNull,
StorageManager& storageManager);
};
Expand Down
Loading

0 comments on commit 4f75391

Please sign in to comment.