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

Add array vector reader for simple arrays #22

Closed
Closed
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
3 changes: 3 additions & 0 deletions velox/experimental/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ add_subdirectory(code_generator)
add_subdirectory(ast)
add_subdirectory(udf_manager)
add_subdirectory(utils)
add_subdirectory(benchmark)
add_subdirectory(functions)
add_subdirectory(vector_function)

add_library(velox_experimental_codegen Codegen.cpp CodegenStubs.cpp
CodegenLogger.cpp)
Expand Down
9 changes: 4 additions & 5 deletions velox/experimental/codegen/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
add_executable(
velox_codegen_benchmark_single_output
SingleOutputDefaultNullsBenchmark.cpp
SingleOutputNotDefaultNullsBenchmark.cpp BooleanBenchmark.cpp)
SingleOutputNotDefaultNullsBenchmark.cpp BooleanBenchmarks.cpp)

target_link_libraries(
velox_codegen_benchmark_single_output
velox_exec_test_lib
velox_codegen_utils_resource_path
velox_codegen_code_generator
${FOLLY_BENCHMARK}
${FOLLY_WITH_DEPENDENCIES}
Expand All @@ -26,7 +27,6 @@ target_link_libraries(
${GFLAGS_LIBRARIES}
${FMT}
velox_core
koski_parser
velox_exec_test_util
velox_functions_lib
velox_functions_common
Expand All @@ -46,8 +46,7 @@ add_compile_definitions(velox_codegen_test
add_compile_definitions(
velox_codegen_test
CODEGEN="$<TARGET_LINKER_FILE:velox_codegen_code_generator>")
add_compile_definitions(velox_codegen_test
KOSKI_PARSER="$<TARGET_LINKER_FILE:koski_parser>")

add_compile_definitions(
velox_codegen_test
VELOX_FUNCTIONS_LIB="$<TARGET_LINKER_FILE:velox_functions_lib>")
Expand Down Expand Up @@ -89,7 +88,7 @@ target_link_libraries(
${GLOG}
${FMT}
velox_core
koski_parser
velox_codegen_utils_resource_path
velox_exec_test_util
velox_functions_lib
velox_functions_common
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ TEST_F(ArithmeticFunctionsTest, DISABLED_testRound) {
"round(C0)", {{32}, {13}, {-13}, {1}, {-1}});
}

TEST_F(ArithmeticFunctionsTest, testHash) {
// FIXME: This test errors on macs:
// velox/velox/functions/common/HashImpl.h:22:31: error: implicit instantiation
// of undefined template 'folly::hasher<std::__1::basic_string_view<char, \
// std::__1::char_traits<char> >, void>'
TEST_F(ArithmeticFunctionsTest, DISABLED_testHash) {
StringView input("hi welcome");
evaluateAndCompare<
RowTypeTrait<TypeKind::VARCHAR>,
Expand Down
14 changes: 14 additions & 0 deletions velox/experimental/codegen/functions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

add_library(velox_codegen_functions INTERFACE)
target_link_libraries(velox_codegen_functions INTERFACE ${FMT})
4 changes: 2 additions & 2 deletions velox/experimental/codegen/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ add_dependencies(velox_experimental_codegen_test velox_codegen_expression_test)
target_link_libraries(
velox_experimental_codegen_test
${GTEST_BOTH_LIBRARIES}
velox_codegen_utils_resource_path
velox_core
velox_exec
velox_experimental_codegen
velox_exec_test_lib
velox_exec_test_util
velox_functions_common)
velox_functions_common
velox_codegen_utils_resource_path)

add_test(velox_experimental_codegen_test velox_experimental_codegen_test)
194 changes: 194 additions & 0 deletions velox/experimental/codegen/vector_function/ComplexVectorReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <math.h>
#include <sys/socket.h>
#include <array>
#include <type_traits>
#include "velox/buffer/Buffer.h"
#include "velox/common/base/Nulls.h"
#include "velox/experimental/codegen/vector_function/StringTypes.h"
#include "velox/experimental/codegen/vector_function/VectorReader-inl.h"
#include "velox/type/Type.h"
#include "velox/vector/ComplexVector.h"
#include "velox/vector/FlatVector.h"

namespace facebook {
namespace velox {
namespace codegen {

template <TypeKind SQLType>
constexpr bool arrayReadableType = TypeTraits<SQLType>::isPrimitiveType;

template <typename T, typename Config>
struct ComplexVectorReader {};

// Reader for ArrayVector(FlatVector<NativeVal>)
// Use the templated Array<> type here instead of ARRAY typekind for
// static template expansion
template <typename SQLElementType, typename Config>
struct ComplexVectorReader<Array<SQLElementType>, Config> {
static_assert(arrayReadableType<SQLElementType::NativeType::typeKind>);
using ElementType = typename SQLElementType::NativeType::NativeType;
using ElementValueType =
typename VectorReader<SQLElementType, Config>::ValueType;
using ElementInputType =
typename VectorReader<SQLElementType, Config>::InputType;
using ValueType = std::optional<std::vector<ElementValueType>>;
using InputType = std::optional<std::vector<ElementInputType>>;

explicit ComplexVectorReader(VectorPtr& vector) {
VELOX_CHECK(vector->type()->kind() == TypeKind::ARRAY);
arrayVectorPtr_ = std::dynamic_pointer_cast<ArrayVector>(vector);
VELOX_CHECK_NOT_NULL(arrayVectorPtr_);
}

struct PointerType {
explicit PointerType(
ArrayVectorPtr& arrayVectorPtr,
size_t rowIndex,
vector_size_t offset)
: arrayVectorPtr_(arrayVectorPtr),
rowIndex_(rowIndex),
vectorReader_(
const_cast<VectorPtr&>(arrayVectorPtr->elements()),
offset) {}

inline bool has_value() {
return !arrayVectorPtr_->isNullAt(rowIndex_);
}

inline std::optional<ElementType&> value() {
return *(vectorReader_[0]);
}

inline size_t size() {
if (!has_value()) {
throw std::logic_error("element has no value");
}
return arrayVectorPtr_->sizeAt(rowIndex_);
}

inline void setNullAndSize() {
static_assert(Config::isWriter_);
auto mutableNulls = arrayVectorPtr_->mutableRawNulls();
auto oldNullCount = arrayVectorPtr_->getNullCount();
if (oldNullCount.hasValue()) {
arrayVectorPtr_->setNullCount(oldNullCount.value() + 1);
}
bits::setBit(mutableNulls, rowIndex_, bits::kNull);
setSize(0);
}

inline void setNotNullAndSize(vector_size_t size) {
static_assert(Config::isWriter_);
auto mutableNulls = arrayVectorPtr_->mutableRawNulls();
auto oldNullCount = arrayVectorPtr_->getNullCount();
if (oldNullCount.hasValue()) {
arrayVectorPtr_->setNullCount(oldNullCount.value() - 1);
}
bits::setBit(mutableNulls, rowIndex_, bits::kNotNull);
setSize(size);
}

inline ElementType& operator*() {
return *(vectorReader_[0]);
}

inline const ElementType& operator*() const {
return *(vectorReader_[0]);
}

inline typename VectorReader<SQLElementType, Config>::PointerType
operator[](size_t elementIndex) {
return vectorReader_[elementIndex];
}

inline PointerType& operator=(InputType& other) {
if (!other.has_value()) {
setNullAndSize();
return *this;
} else {
auto val = other.value();
setNotNullAndSize(val.size());
for (size_t i = 0; i < val.size(); i++) {
vectorReader_[i] = val[i];
}
return *this;
}
}

operator ValueType const() {
if (!has_value()) {
return {};
} else {
std::vector<ElementValueType> val(size());
for (size_t i = 0; i < size(); i++) {
val.push_back(this[i]);
}
return val;
}
}

private:
ArrayVectorPtr& arrayVectorPtr_;
size_t rowIndex_;
VectorReader<SQLElementType, Config> vectorReader_;

inline void setSize(vector_size_t size) {
// reserve metadata vectors and resize if needed
auto mutableSizes =
arrayVectorPtr_->mutableSizes(size)->asMutable<vector_size_t>();
auto mutableOffsets =
arrayVectorPtr_->mutableOffsets(size)->asMutable<vector_size_t>();

// FIXME: this assumes that setSize() is called in sequential
// order
mutableSizes[rowIndex_] = size;
if (rowIndex_ == 0) {
mutableOffsets[rowIndex_] = 0;
} else {
mutableOffsets[rowIndex_] =
mutableOffsets[rowIndex_ - 1] + mutableSizes[rowIndex_ - 1];
}
return;
}
};

inline PointerType operator[](size_t rowIndex) {
// We only support simple arrays for now
VELOX_CHECK_NOT_NULL(
arrayVectorPtr_->elements()->asFlatVector<ElementType>());
vector_size_t offset;
if constexpr (Config::isWriter_) {
if (rowIndex == 0) {
offset = 0;
} else {
offset = arrayVectorPtr_->offsetAt(rowIndex - 1) +
arrayVectorPtr_->sizeAt(rowIndex - 1);
}
} else {
offset = arrayVectorPtr_->offsetAt(rowIndex);
}
return PointerType{arrayVectorPtr_, rowIndex, offset};
}

private:
ArrayVectorPtr arrayVectorPtr_;
};

} // namespace codegen
} // namespace velox
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <thread>
#include <type_traits>
#include <utility>
#include "velox/experimental/codegen/vector_function/ComplexVectorReader.h"
#include "velox/experimental/codegen/vector_function/ConcatExpression-inl.h"
#include "velox/experimental/codegen/vector_function/Perf.h"
#include "velox/experimental/codegen/vector_function/VectorReader-inl.h"
Expand Down
Loading