Skip to content

Commit

Permalink
Merge pull request #1291 from kuzudb/offset-function
Browse files Browse the repository at this point in the history
Add internal offset function
  • Loading branch information
ray6080 committed Feb 15, 2023
2 parents a77dec2 + 3928e4f commit 01d5c1c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/function/built_in_vector_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "function/date/vector_date_operations.h"
#include "function/interval/vector_interval_operations.h"
#include "function/list/vector_list_operations.h"
#include "function/schema/vector_offset_operations.h"
#include "function/string/vector_string_operations.h"
#include "function/timestamp/vector_timestamp_operations.h"

Expand All @@ -24,6 +25,8 @@ void BuiltInVectorOperations::registerVectorOperations() {
registerCastOperations();
registerListOperations();
registerInternalIDOperation();
// register internal offset operation
vectorOperations.insert({OFFSET_FUNC_NAME, OffsetVectorOperation::getDefinitions()});
}

bool BuiltInVectorOperations::canApplyStaticEvaluation(
Expand Down
1 change: 1 addition & 0 deletions src/include/common/expression_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ const std::string TO_MICROSECONDS_FUNC_NAME = "TO_MICROSECONDS";
// Node/Rel functions.
const std::string ID_FUNC_NAME = "ID";
const std::string LABEL_FUNC_NAME = "LABEL";
const std::string OFFSET_FUNC_NAME = "OFFSET";

enum ExpressionType : uint8_t {

Expand Down
1 change: 1 addition & 0 deletions src/include/function/built_in_vector_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class BuiltInVectorOperations {
void registerCastOperations();
void registerListOperations();
void registerInternalIDOperation();
void registerInternalOffsetOperation();

private:
std::unordered_map<std::string, std::vector<std::unique_ptr<VectorOperationDefinition>>>
Expand Down
17 changes: 17 additions & 0 deletions src/include/function/schema/offset_operations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "common/type_utils.h"

namespace kuzu {
namespace function {
namespace operation {

struct Offset {
static inline void operation(common::internalID_t& input, int64_t& result) {
result = input.offset;
}
};

} // namespace operation
} // namespace function
} // namespace kuzu
27 changes: 27 additions & 0 deletions src/include/function/schema/vector_offset_operations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "function/vector_operations.h"
#include "offset_operations.h"

namespace kuzu {
namespace function {

struct OffsetVectorOperation : public VectorOperations {
static void execFunction(const std::vector<std::shared_ptr<common::ValueVector>>& params,
common::ValueVector& result) {
assert(params.size() == 1);
UnaryOperationExecutor::execute<common::internalID_t, int64_t, operation::Offset>(
*params[0], result);
}

static std::vector<std::unique_ptr<VectorOperationDefinition>> getDefinitions() {
std::vector<std::unique_ptr<VectorOperationDefinition>> definitions;
definitions.push_back(make_unique<VectorOperationDefinition>(common::OFFSET_FUNC_NAME,
std::vector<common::DataTypeID>{common::INTERNAL_ID}, common::INT64,
OffsetVectorOperation::execFunction));
return definitions;
}
};

} // namespace function
} // namespace kuzu
1 change: 1 addition & 0 deletions test/runner/e2e_read_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ TEST_F(TinySnbReadTest, Filter) {
}

TEST_F(TinySnbReadTest, Function) {
runTest(TestHelper::appendKuzuRootPath("test/test_files/tinysnb/function/offset.test"));
runTest(TestHelper::appendKuzuRootPath("test/test_files/tinysnb/function/date.test"));
runTest(TestHelper::appendKuzuRootPath("test/test_files/tinysnb/function/timestamp.test"));
runTest(TestHelper::appendKuzuRootPath("test/test_files/tinysnb/function/interval.test"));
Expand Down
30 changes: 30 additions & 0 deletions test/test_files/tinysnb/function/offset.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-NAME NodeOffsetTest1
-QUERY MATCH (a:person) RETURN id(a), offset(id(a))
---- 8
0:0|0
0:1|1
0:2|2
0:3|3
0:4|4
0:5|5
0:6|6
0:7|7

-NAME NodeOffsetTest2
-QUERY MATCH (a:organisation) RETURN id(a), offset(id(a))
---- 3
1:0|0
1:1|1
1:2|2

-NAME NodeOffsetTest3
-QUERY MATCH (a:person) WHERE offset(id(a))=0 RETURN a.fName
---- 1
Alice

-NAME RelOffsetTest2
-QUERY MATCH (:person)-[e:studyAt]->(:organisation) RETURN id(e), offset(id(e))
---- 3
4:0|0
4:1|1
4:2|2

0 comments on commit 01d5c1c

Please sign in to comment.