-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list_sum and list_sort functions
- Loading branch information
1 parent
4f02b2d
commit d543bf9
Showing
8 changed files
with
390 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/include/function/list/operations/list_sort_operation.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#pragma once | ||
|
||
#include "common/vector/value_vector.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
template<typename T> | ||
struct ListSort { | ||
static inline void operation(common::list_entry_t& input, common::list_entry_t& result, | ||
common::ValueVector& inputVector, common::ValueVector& resultVector) { | ||
sortValues( | ||
input, result, inputVector, resultVector, true /* ascOrder */, true /* nullFirst */); | ||
} | ||
|
||
static inline void operation(common::list_entry_t& input, common::ku_string_t& sortOrder, | ||
common::list_entry_t& result, common::ValueVector& inputVector, | ||
common::ValueVector& valueVector, common::ValueVector& resultVector) { | ||
sortValues(input, result, inputVector, resultVector, isAscOrder(sortOrder.getAsString()), | ||
true /* nullFirst */); | ||
} | ||
|
||
static inline void operation(common::list_entry_t& input, common::ku_string_t& sortOrder, | ||
common::ku_string_t& nullOrder, common::list_entry_t& result, | ||
common::ValueVector& inputVector, common::ValueVector& resultVector) { | ||
sortValues(input, result, inputVector, resultVector, isAscOrder(sortOrder.getAsString()), | ||
isNullFirst(nullOrder.getAsString())); | ||
} | ||
|
||
static inline bool isAscOrder(const std::string& sortOrder) { | ||
if (sortOrder == "ASC") { | ||
return true; | ||
} else if (sortOrder == "DESC") { | ||
return false; | ||
} else { | ||
throw common::RuntimeException("Invalid sortOrder"); | ||
} | ||
} | ||
|
||
static inline bool isNullFirst(const std::string& nullOrder) { | ||
if (nullOrder == "NULLS FIRST") { | ||
return true; | ||
} else if (nullOrder == "NULLS LAST") { | ||
return false; | ||
} else { | ||
throw common::RuntimeException("Invalid nullOrder"); | ||
} | ||
} | ||
|
||
static void sortValues(common::list_entry_t& input, common::list_entry_t& result, | ||
common::ValueVector& inputVector, common::ValueVector& resultVector, bool ascOrder, | ||
bool nullFirst) { | ||
// Todo - Replace this sort implementation with radix_sort implementation: | ||
// https://github.com/kuzudb/kuzu/issues/1536 | ||
auto inputValues = common::ListVector::getListValues(&inputVector, input); | ||
auto inputDataVector = common::ListVector::getDataVector(&inputVector); | ||
|
||
// Calculate null count | ||
uint64_t nullCount = 0; | ||
for (auto i = 0; i < input.size; i++) { | ||
if (inputDataVector->isNull(input.offset + i)) { | ||
nullCount += 1; | ||
} | ||
} | ||
|
||
result = common::ListVector::addList(&resultVector, input.size); | ||
auto resultValues = common::ListVector::getListValues(&resultVector, result); | ||
auto resultDataVector = common::ListVector::getDataVector(&resultVector); | ||
auto numBytesPerValue = resultDataVector->getNumBytesPerValue(); | ||
|
||
// Add nulls first | ||
if (nullFirst) { | ||
for (auto i = 0; i < nullCount; i++) { | ||
resultDataVector->setNull(result.offset + i, true); | ||
resultValues += numBytesPerValue; | ||
} | ||
} | ||
|
||
// Add actual data | ||
for (auto i = 0; i < input.size; i++) { | ||
if (inputDataVector->isNull(input.offset + i)) { | ||
inputValues += numBytesPerValue; | ||
continue; | ||
} | ||
common::ValueVectorUtils::copyValue( | ||
resultValues, *resultDataVector, inputValues, *inputDataVector); | ||
resultValues += numBytesPerValue; | ||
inputValues += numBytesPerValue; | ||
} | ||
|
||
// Add nulls in the end | ||
if (!nullFirst) { | ||
for (auto i = input.size - nullCount; i < input.size; i++) { | ||
resultDataVector->setNull(result.offset + i, true); | ||
resultValues += numBytesPerValue; | ||
} | ||
} | ||
|
||
// Determine the starting and ending position of the data to be sorted | ||
uint64_t sortStart = nullCount; | ||
uint64_t sortEnd = input.size; | ||
if (!nullFirst) { | ||
sortStart = 0; | ||
sortEnd = input.size - nullCount; | ||
} | ||
|
||
// Sort the data based on order | ||
auto newResultValues = | ||
reinterpret_cast<T*>(common::ListVector::getListValues(&resultVector, result)); | ||
if (ascOrder) { | ||
std::sort(newResultValues + sortStart, newResultValues + sortEnd, std::less{}); | ||
} else { | ||
std::sort(newResultValues + sortStart, newResultValues + sortEnd, std::greater{}); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include "common/vector/value_vector.h" | ||
|
||
namespace kuzu { | ||
namespace function { | ||
namespace operation { | ||
|
||
struct ListSum { | ||
template<typename T> | ||
static inline void operation(common::list_entry_t& input, T& result, | ||
common::ValueVector& inputVector, common::ValueVector& resultVector) { | ||
auto inputValues = | ||
reinterpret_cast<T*>(common::ListVector::getListValues(&inputVector, input)); | ||
auto inputDataVector = common::ListVector::getDataVector(&inputVector); | ||
for (auto i = 0; i < input.size; i++) { | ||
if (inputDataVector->isNull(input.offset + i)) { | ||
continue; | ||
} | ||
result += inputValues[i]; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace operation | ||
} // namespace function | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.