Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getPositionOfCurrIdx #1095

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added examples/CMakeLists.txt
Empty file.
6 changes: 3 additions & 3 deletions src/common/data_chunk/data_chunk_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace kuzu {
namespace common {

shared_ptr<DataChunkState> DataChunkState::getSingleValueDataChunkState() {
auto state = make_shared<DataChunkState>(1);
state->selVector->selectedSize = 1;
std::shared_ptr<DataChunkState> DataChunkState::getSingleValueDataChunkState() {
auto state = std::make_shared<DataChunkState>(1);
state->initOriginalAndSelectedSize(1);
state->currIdx = 0;
return state;
}
Expand Down
34 changes: 15 additions & 19 deletions src/common/vector/value_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,25 @@ void ValueVector::addString(uint32_t pos, char* value, uint64_t len) const {
}

bool NodeIDVector::discardNull(ValueVector& vector) {
if (vector.state->isFlat()) {
return !vector.isNull(vector.state->getPositionOfCurrIdx());
if (vector.hasNoNullsGuarantee()) {
return true;
} else {
if (vector.hasNoNullsGuarantee()) {
return true;
auto selectedPos = 0u;
if (vector.state->selVector->isUnfiltered()) {
vector.state->selVector->resetSelectorToValuePosBuffer();
for (auto i = 0u; i < vector.state->selVector->selectedSize; i++) {
vector.state->selVector->selectedPositions[selectedPos] = i;
selectedPos += !vector.isNull(i);
}
} else {
auto selectedPos = 0u;
if (vector.state->selVector->isUnfiltered()) {
vector.state->selVector->resetSelectorToValuePosBuffer();
for (auto i = 0u; i < vector.state->selVector->selectedSize; i++) {
vector.state->selVector->selectedPositions[selectedPos] = i;
selectedPos += !vector.isNull(i);
}
} else {
for (auto i = 0u; i < vector.state->selVector->selectedSize; i++) {
auto pos = vector.state->selVector->selectedPositions[i];
vector.state->selVector->selectedPositions[selectedPos] = pos;
selectedPos += !vector.isNull(pos);
}
for (auto i = 0u; i < vector.state->selVector->selectedSize; i++) {
auto pos = vector.state->selVector->selectedPositions[i];
vector.state->selVector->selectedPositions[selectedPos] = pos;
selectedPos += !vector.isNull(pos);
}
vector.state->selVector->selectedSize = selectedPos;
return selectedPos > 0;
}
vector.state->selVector->selectedSize = selectedPos;
return selectedPos > 0;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/expression_evaluator/literal_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ void LiteralExpressionEvaluator::init(const ResultSet& resultSet, MemoryManager*
}

bool LiteralExpressionEvaluator::select(SelectionVector& selVector) {
assert(resultVector->dataType.typeID == BOOL); // TODO(Guodong): Is this expected here?
auto pos = resultVector->state->getPositionOfCurrIdx();
assert(resultVector->dataType.typeID == BOOL);
auto pos = resultVector->state->selVector->selectedPositions[0];
assert(pos == 0u);
return resultVector->getValue<bool>(pos) == true && (!resultVector->isNull(pos));
}
Expand Down
27 changes: 11 additions & 16 deletions src/expression_evaluator/reference_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@ void ReferenceExpressionEvaluator::init(const ResultSet& resultSet, MemoryManage

bool ReferenceExpressionEvaluator::select(SelectionVector& selVector) {
uint64_t numSelectedValues = 0;
if (resultVector->state->isFlat()) {
auto pos = resultVector->state->getPositionOfCurrIdx();
numSelectedValues += isTrue(*resultVector, pos);
auto selectedBuffer = resultVector->state->selVector->getSelectedPositionsBuffer();
if (resultVector->state->selVector->isUnfiltered()) {
for (auto i = 0u; i < resultVector->state->selVector->selectedSize; i++) {
selectedBuffer[numSelectedValues] = i;
numSelectedValues += isTrue(*resultVector, i);
}
} else {
auto selectedBuffer = resultVector->state->selVector->getSelectedPositionsBuffer();
if (resultVector->state->selVector->isUnfiltered()) {
for (auto i = 0u; i < resultVector->state->selVector->selectedSize; i++) {
selectedBuffer[numSelectedValues] = i;
numSelectedValues += isTrue(*resultVector, i);
}
} else {
for (auto i = 0u; i < resultVector->state->selVector->selectedSize; i++) {
auto pos = resultVector->state->selVector->selectedPositions[i];
selectedBuffer[numSelectedValues] = pos;
numSelectedValues += isTrue(*resultVector, pos);
}
for (auto i = 0u; i < resultVector->state->selVector->selectedSize; i++) {
auto pos = resultVector->state->selVector->selectedPositions[i];
selectedBuffer[numSelectedValues] = pos;
numSelectedValues += isTrue(*resultVector, pos);
}
selVector.selectedSize = numSelectedValues;
}
selVector.selectedSize = numSelectedValues;
return numSelectedValues > 0;
}

Expand Down
28 changes: 7 additions & 21 deletions src/function/vector_list_operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,21 @@ void VectorListOperations::ListCreation(
auto& childType = parameters[0]->dataType;
auto numBytesOfListElement = Types::getDataTypeSize(childType);
auto elements = make_unique<uint8_t[]>(parameters.size() * numBytesOfListElement);
if (result.state->isFlat()) {
auto pos = result.state->getPositionOfCurrIdx();
for (auto selectedPos = 0u; selectedPos < result.state->selVector->selectedSize;
++selectedPos) {
auto pos = result.state->selVector->selectedPositions[selectedPos];
auto& kuList = ((ku_list_t*)result.getData())[pos];
for (auto paramIdx = 0u; paramIdx < parameters.size(); paramIdx++) {
assert(parameters[paramIdx]->state->isFlat());
auto paramPos = parameters[paramIdx]->state->isFlat() ?
parameters[paramIdx]->state->selVector->selectedPositions[0] :
pos;
memcpy(elements.get() + paramIdx * numBytesOfListElement,
parameters[paramIdx]->getData() + pos * numBytesOfListElement,
parameters[paramIdx]->getData() + paramPos * numBytesOfListElement,
numBytesOfListElement);
}
ku_list_t tmpList(parameters.size(), (uint64_t)elements.get());
InMemOverflowBufferUtils::copyListRecursiveIfNested(
tmpList, kuList, result.dataType, result.getOverflowBuffer());
} else {
for (auto selectedPos = 0u; selectedPos < result.state->selVector->selectedSize;
++selectedPos) {
auto pos = result.state->selVector->selectedPositions[selectedPos];
auto& kuList = ((ku_list_t*)result.getData())[pos];
for (auto paramIdx = 0u; paramIdx < parameters.size(); paramIdx++) {
auto parameterPos = parameters[paramIdx]->state->isFlat() ?
parameters[paramIdx]->state->getPositionOfCurrIdx() :
pos;
memcpy(elements.get() + paramIdx * numBytesOfListElement,
parameters[paramIdx]->getData() + parameterPos * numBytesOfListElement,
numBytesOfListElement);
}
ku_list_t tmpList(parameters.size(), (uint64_t)elements.get());
InMemOverflowBufferUtils::copyListRecursiveIfNested(
tmpList, kuList, result.dataType, result.getOverflowBuffer());
}
}
}

Expand Down
53 changes: 4 additions & 49 deletions src/include/common/data_chunk/data_chunk_state.h
Original file line number Diff line number Diff line change
@@ -1,74 +1,29 @@
#pragma once

#include <cstring>
#include <memory>
#include <vector>

#include "common/configs.h"
#include "common/types/types.h"

using namespace std;
#include "common/data_chunk/sel_vector.h"

namespace kuzu {
namespace common {

class SelectionVector {
public:
explicit SelectionVector(sel_t capacity) : selectedSize{0} {
selectedPositionsBuffer = make_unique<sel_t[]>(capacity);
resetSelectorToUnselected();
}

inline bool isUnfiltered() const {
return selectedPositions == (sel_t*)&INCREMENTAL_SELECTED_POS;
}
inline void resetSelectorToUnselected() {
selectedPositions = (sel_t*)&INCREMENTAL_SELECTED_POS;
}
inline void resetSelectorToUnselectedWithSize(sel_t size) {
selectedPositions = (sel_t*)&INCREMENTAL_SELECTED_POS;
selectedSize = size;
}
inline void resetSelectorToValuePosBuffer() {
selectedPositions = selectedPositionsBuffer.get();
}
inline void resetSelectorToValuePosBufferWithSize(sel_t size) {
selectedPositions = selectedPositionsBuffer.get();
selectedSize = size;
}
inline sel_t* getSelectedPositionsBuffer() { return selectedPositionsBuffer.get(); }

static const sel_t INCREMENTAL_SELECTED_POS[DEFAULT_VECTOR_CAPACITY];

public:
sel_t* selectedPositions;
sel_t selectedSize;

private:
unique_ptr<sel_t[]> selectedPositionsBuffer;
};

class DataChunkState {

public:
DataChunkState() : DataChunkState(DEFAULT_VECTOR_CAPACITY) {}
explicit DataChunkState(uint64_t capacity) : currIdx{-1}, originalSize{0} {
selVector = make_unique<SelectionVector>(capacity);
selVector = std::make_shared<SelectionVector>(capacity);
}

// returns a dataChunkState for vectors holding a single value.
static shared_ptr<DataChunkState> getSingleValueDataChunkState();
static std::shared_ptr<DataChunkState> getSingleValueDataChunkState();

inline void initOriginalAndSelectedSize(uint64_t size) {
originalSize = size;
selVector->selectedSize = size;
}
inline bool isFlat() const { return currIdx != -1; }
inline bool isCurrIdxLast() const { return currIdx == selVector->selectedSize - 1; }
inline uint64_t getPositionOfCurrIdx() const {
assert(isFlat());
return selVector->selectedPositions[currIdx];
}
inline uint64_t getNumSelectedValues() const { return isFlat() ? 1 : selVector->selectedSize; }

public:
Expand All @@ -81,7 +36,7 @@ class DataChunkState {
// has to scan to generate a vector that is consistent with the rest of the vectors in the
// data chunk.
uint64_t originalSize;
unique_ptr<SelectionVector> selVector;
std::shared_ptr<SelectionVector> selVector;
};

} // namespace common
Expand Down
48 changes: 48 additions & 0 deletions src/include/common/data_chunk/sel_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <memory>

#include "common/configs.h"
#include "common/types/types.h"

namespace kuzu {
namespace common {

class SelectionVector {
public:
explicit SelectionVector(sel_t capacity) : selectedSize{0} {
selectedPositionsBuffer = std::make_unique<sel_t[]>(capacity);
resetSelectorToUnselected();
}

inline bool isUnfiltered() const {
return selectedPositions == (sel_t*)&INCREMENTAL_SELECTED_POS;
}
inline void resetSelectorToUnselected() {
selectedPositions = (sel_t*)&INCREMENTAL_SELECTED_POS;
}
inline void resetSelectorToUnselectedWithSize(sel_t size) {
selectedPositions = (sel_t*)&INCREMENTAL_SELECTED_POS;
selectedSize = size;
}
inline void resetSelectorToValuePosBuffer() {
selectedPositions = selectedPositionsBuffer.get();
}
inline void resetSelectorToValuePosBufferWithSize(sel_t size) {
selectedPositions = selectedPositionsBuffer.get();
selectedSize = size;
}
inline sel_t* getSelectedPositionsBuffer() { return selectedPositionsBuffer.get(); }

static const sel_t INCREMENTAL_SELECTED_POS[DEFAULT_VECTOR_CAPACITY];

public:
sel_t* selectedPositions;
sel_t selectedSize;

private:
std::unique_ptr<sel_t[]> selectedPositionsBuffer;
};

} // namespace common
} // namespace kuzu
7 changes: 1 addition & 6 deletions src/include/common/vector/value_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ class ValueVector {
inline void setMayContainNulls() { nullMask->setMayContainNulls(); }
// Note that if this function returns true, there are no null. However, if it returns false, it
// doesn't mean there are nulls, i.e., there may or may not be nulls.
inline bool hasNoNullsGuarantee() const {
// This function should not be used for flat values. For flat values, the null value
// of the value should be checked directly.
assert(!state->isFlat());
return nullMask->hasNoNullsGuarantee();
}
inline bool hasNoNullsGuarantee() const { return nullMask->hasNoNullsGuarantee(); }
inline void setRangeNonNull(uint32_t startPos, uint32_t len) {
for (auto i = 0u; i < len; ++i) {
setNull(startPos + i, false);
Expand Down
2 changes: 1 addition & 1 deletion src/include/expression_evaluator/literal_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class LiteralExpressionEvaluator : public BaseExpressionEvaluator {

public:
LiteralExpressionEvaluator(shared_ptr<Literal> literal)
: BaseExpressionEvaluator{}, literal{move(literal)} {}
: BaseExpressionEvaluator{}, literal{std::move(literal)} {}

~LiteralExpressionEvaluator() = default;

Expand Down
18 changes: 9 additions & 9 deletions src/include/function/binary_operation_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ struct BinaryOperationExecutor {
typename OP_WRAPPER>
static void executeBothFlat(ValueVector& left, ValueVector& right, ValueVector& result) {
result.state = left.state;
auto lPos = left.state->getPositionOfCurrIdx();
auto rPos = right.state->getPositionOfCurrIdx();
auto resPos = result.state->getPositionOfCurrIdx();
auto lPos = left.state->selVector->selectedPositions[0];
auto rPos = right.state->selVector->selectedPositions[0];
auto resPos = result.state->selVector->selectedPositions[0];
result.setNull(resPos, left.isNull(lPos) || right.isNull(rPos));
if (!result.isNull(resPos)) {
executeOnValue<LEFT_TYPE, RIGHT_TYPE, RESULT_TYPE, FUNC, OP_WRAPPER>(
Expand All @@ -67,7 +67,7 @@ struct BinaryOperationExecutor {
typename OP_WRAPPER>
static void executeFlatUnFlat(ValueVector& left, ValueVector& right, ValueVector& result) {
result.state = right.state;
auto lPos = left.state->getPositionOfCurrIdx();
auto lPos = left.state->selVector->selectedPositions[0];
if (left.isNull(lPos)) {
result.setAllNull();
} else if (right.hasNoNullsGuarantee()) {
Expand Down Expand Up @@ -109,7 +109,7 @@ struct BinaryOperationExecutor {
typename OP_WRAPPER>
static void executeUnFlatFlat(ValueVector& left, ValueVector& right, ValueVector& result) {
result.state = left.state;
auto rPos = right.state->getPositionOfCurrIdx();
auto rPos = right.state->selVector->selectedPositions[0];
if (right.isNull(rPos)) {
result.setAllNull();
} else if (left.hasNoNullsGuarantee()) {
Expand Down Expand Up @@ -239,8 +239,8 @@ struct BinaryOperationExecutor {

template<class LEFT_TYPE, class RIGHT_TYPE, class FUNC>
static uint64_t selectBothFlat(ValueVector& left, ValueVector& right) {
auto lPos = left.state->getPositionOfCurrIdx();
auto rPos = right.state->getPositionOfCurrIdx();
auto lPos = left.state->selVector->selectedPositions[0];
auto rPos = right.state->selVector->selectedPositions[0];
uint8_t resultValue = 0;
if (!left.isNull(lPos) && !right.isNull(rPos)) {
FUNC::operation(((LEFT_TYPE*)left.getData())[lPos],
Expand All @@ -252,7 +252,7 @@ struct BinaryOperationExecutor {
template<typename LEFT_TYPE, typename RIGHT_TYPE, typename FUNC>
static bool selectFlatUnFlat(
ValueVector& left, ValueVector& right, SelectionVector& selVector) {
auto lPos = left.state->getPositionOfCurrIdx();
auto lPos = left.state->selVector->selectedPositions[0];
uint64_t numSelectedValues = 0;
auto selectedPositionsBuffer = selVector.getSelectedPositionsBuffer();
if (left.isNull(lPos)) {
Expand Down Expand Up @@ -295,7 +295,7 @@ struct BinaryOperationExecutor {
template<typename LEFT_TYPE, typename RIGHT_TYPE, typename FUNC>
static bool selectUnFlatFlat(
ValueVector& left, ValueVector& right, SelectionVector& selVector) {
auto rPos = right.state->getPositionOfCurrIdx();
auto rPos = right.state->selVector->selectedPositions[0];
uint64_t numSelectedValues = 0;
auto selectedPositionsBuffer = selVector.getSelectedPositionsBuffer();
if (right.isNull(rPos)) {
Expand Down
Loading