Skip to content

Commit

Permalink
Move VectorReader test to its own file, fixed some typos (#1330)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookexternal/f4d#1330

Since we'll be adding multiple VectorReaders, I think it's better organization to put the tests in its own directory.

Differential Revision: D30024069

fbshipit-source-id: 263bf7ac7586ac4a6ed002f17d0e95193c99c12f
  • Loading branch information
christycylee authored and facebook-github-bot committed Aug 11, 2021
1 parent 894d7de commit ef46b46
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 92 deletions.
20 changes: 4 additions & 16 deletions velox/experimental/codegen/vector_function/VectorReader-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace codegen {
// // true means set to null, false means not null
// static constexpr bool intializedWithNullSet_
//
// // when true, the reader will never reveive a null value to write
// // when true, the reader will never receive a null value to write
// static constexpr bool mayWriteNull_
//
//
Expand Down Expand Up @@ -78,17 +78,13 @@ struct VectorReader {

if constexpr (Config::isWriter_) {
mutableRawNulls_ = flatVector->mutableRawNulls();
mutableRawValues_ = flatVector->mutableRawValues();
} else {
if constexpr (Config::mayReadNull_) {
// TODO when read only vector does not have nulls we dont need to
// allocate nulls
mutableRawNulls_ = flatVector->mutableRawNulls();
}
}

if constexpr (Config::isWriter_) {
mutableRawValues_ = flatVector->mutableRawValues();
} else {
mutableRawValues_ = const_cast<NativeType*>(flatVector->rawValues());
}
}
Expand Down Expand Up @@ -183,17 +179,13 @@ struct VectorReader<

if constexpr (Config::isWriter_) {
mutableRawNulls_ = flatVector->mutableRawNulls();
mutableRawValues_ = flatVector->template mutableRawValues<uint64_t>();
} else {
// TODO when read only vector does not have nulls we dont need to allocate
// nulls
if constexpr (Config::mayReadNull_) {
mutableRawNulls_ = flatVector->mutableRawNulls();
}
}

if constexpr (Config::isWriter_) {
mutableRawValues_ = flatVector->template mutableRawValues<uint64_t>();
} else {
mutableRawValues_ =
const_cast<uint64_t*>(flatVector->template rawValues<uint64_t>());
}
Expand Down Expand Up @@ -311,17 +303,13 @@ struct VectorReader<

if constexpr (Config::isWriter_) {
mutableRawNulls_ = flatVector->mutableRawNulls();
mutableRawValues_ = flatVector->template mutableRawValues<StringView>();
} else {
// TODO when read only vector does not have nulls we dont need to allocate
// nulls
if constexpr (Config::mayReadNull_) {
mutableRawNulls_ = flatVector->mutableRawNulls();
}
}

if constexpr (Config::isWriter_) {
mutableRawValues_ = flatVector->template mutableRawValues<StringView>();
} else {
mutableRawValues_ =
const_cast<StringView*>(flatVector->template rawValues<StringView>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

add_executable(velox_codegen_vector_function_test
veloxCodegenVectorFunctionTest.cpp, TempStringTest.cpp)
add_executable(
velox_codegen_vector_function_test veloxCodegenVectorFunctionTest.cpp
TempStringTest.cpp VectorReaderTest.cpp)
add_test(velox_codegen_vector_function_test velox_codegen_vector_function_test)

target_link_libraries(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,6 @@ TEST(TestConcat, BasicConcatRow) {
EXPECT_EQ(std::get<1>(output), *std::get<0>(args) - *std::get<1>(args));
}

TEST(VectorReader, ReadDoublesVectors) {
const size_t vectorSize = 1000;
auto inRowType = ROW({"columnA", "columnB"}, {DOUBLE(), DOUBLE()});
auto outRowType = ROW({"expr1", "expr2"}, {DOUBLE(), DOUBLE()});

auto pool_ = memory::getDefaultScopedMemoryPool();
auto pool = pool_.get();
auto inRowVector = BaseVector::create(inRowType, vectorSize, pool);
auto outRowVector = BaseVector::create(outRowType, vectorSize, pool);

VectorPtr& in1 = inRowVector->as<RowVector>()->childAt(0);

SelectivityVector selectivityVector(vectorSize);
selectivityVector.setAll();
in1->resize(vectorSize);
in1->addNulls(nullptr, selectivityVector);
VectorReader<DoubleType, OutputReaderConfig<false, false>> writer(in1);
VectorReader<DoubleType, InputReaderConfig<false, false>> reader(in1);

for (size_t row = 0; row < vectorSize; row++) {
writer[row] = (double)row;
}

for (size_t row = 0; row < vectorSize; row++) {
ASSERT_DOUBLE_EQ((double)row, *reader[row]);
}

for (size_t row = 0; row < vectorSize; row++) {
ASSERT_DOUBLE_EQ(*reader[row], in1->asFlatVector<double>()->valueAt(row));
}
}

TEST(TestConcat, EvalConcatFunction) {
const size_t rowLength = 1000;
SelectivityVector rows(rowLength);
Expand Down Expand Up @@ -222,48 +190,6 @@ struct GeneratedVectorFunctionConfigBool {
static constexpr bool isDefaultNullStrict = false;
};

TEST(VectorReader, ReadBoolVectors) {
// TODO: Move those to test class
auto pool_ = memory::getDefaultScopedMemoryPool();
auto pool = pool_.get();
const size_t vectorSize = 1000;

auto inRowType = ROW({"columnA", "columnB"}, {BOOLEAN(), BOOLEAN()});
auto outRowType = ROW({"expr1", "expr2"}, {BOOLEAN(), BOOLEAN()});

auto inRowVector = BaseVector::create(inRowType, vectorSize, pool);
auto outRowVector = BaseVector::create(outRowType, vectorSize, pool);

VectorPtr& inputVector = inRowVector->as<RowVector>()->childAt(0);
inputVector->resize(vectorSize);
VectorReader<BooleanType, InputReaderConfig<false, false>> reader(
inputVector);
VectorReader<BooleanType, OutputReaderConfig<false, false>> writer(
inputVector);

for (size_t row = 0; row < vectorSize; row++) {
writer[row] = row % 2 == 0;
}

// Check that writing of values to the reader was success
for (size_t row = 0; row < vectorSize; row++) {
ASSERT_DOUBLE_EQ((row % 2 == 0), *reader[row]);
ASSERT_DOUBLE_EQ(
(row % 2 == 0), inputVector->asFlatVector<bool>()->valueAt(row));
}

// Write a null at even indices
for (size_t row = 0; row < vectorSize; row++) {
if (row % 2) {
writer[row] = std::nullopt;
}
}

for (size_t row = 0; row < vectorSize; row++) {
ASSERT_EQ(inputVector->asFlatVector<bool>()->isNullAt(row), row % 2);
}
}

TEST(TestBooEvalVectorFunction, EvalBoolExpression) {
// TODO: Move those to test class
auto pool_ = memory::getDefaultScopedMemoryPool();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.
*/

#include <folly/Random.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include "velox/experimental/codegen/vector_function/GeneratedVectorFunction-inl.h" // NOLINT (CLANGTIDY )
#include "velox/experimental/codegen/vector_function/StringTypes.h"

namespace facebook::velox::codegen {
TEST(VectorReader, ReadDoublesVectors) {
const size_t vectorSize = 1000;
auto inRowType = ROW({"columnA", "columnB"}, {DOUBLE(), DOUBLE()});
auto outRowType = ROW({"expr1", "expr2"}, {DOUBLE(), DOUBLE()});

auto pool_ = memory::getDefaultScopedMemoryPool();
auto pool = pool_.get();
auto inRowVector = BaseVector::create(inRowType, vectorSize, pool);
auto outRowVector = BaseVector::create(outRowType, vectorSize, pool);

VectorPtr& in1 = inRowVector->as<RowVector>()->childAt(0);

SelectivityVector selectivityVector(vectorSize);
selectivityVector.setAll();
in1->resize(vectorSize);
in1->addNulls(nullptr, selectivityVector);
VectorReader<DoubleType, OutputReaderConfig<false, false>> writer(in1);
VectorReader<DoubleType, InputReaderConfig<false, false>> reader(in1);

for (size_t row = 0; row < vectorSize; row++) {
writer[row] = (double)row;
}

for (size_t row = 0; row < vectorSize; row++) {
ASSERT_DOUBLE_EQ((double)row, *reader[row]);
}

for (size_t row = 0; row < vectorSize; row++) {
ASSERT_DOUBLE_EQ(*reader[row], in1->asFlatVector<double>()->valueAt(row));
}
}

TEST(VectorReader, ReadBoolVectors) {
// TODO: Move those to test class
auto pool_ = memory::getDefaultScopedMemoryPool();
auto pool = pool_.get();
const size_t vectorSize = 1000;

auto inRowType = ROW({"columnA", "columnB"}, {BOOLEAN(), BOOLEAN()});
auto outRowType = ROW({"expr1", "expr2"}, {BOOLEAN(), BOOLEAN()});

auto inRowVector = BaseVector::create(inRowType, vectorSize, pool);
auto outRowVector = BaseVector::create(outRowType, vectorSize, pool);

VectorPtr& inputVector = inRowVector->as<RowVector>()->childAt(0);
inputVector->resize(vectorSize);
VectorReader<BooleanType, InputReaderConfig<false, false>> reader(
inputVector);
VectorReader<BooleanType, OutputReaderConfig<false, false>> writer(
inputVector);

for (size_t row = 0; row < vectorSize; row++) {
writer[row] = row % 2 == 0;
}

// Check that writing of values to the reader was success
for (size_t row = 0; row < vectorSize; row++) {
ASSERT_DOUBLE_EQ((row % 2 == 0), *reader[row]);
ASSERT_DOUBLE_EQ(
(row % 2 == 0), inputVector->asFlatVector<bool>()->valueAt(row));
}

// Write a null at even indices
for (size_t row = 0; row < vectorSize; row++) {
if (row % 2) {
writer[row] = std::nullopt;
}
}

for (size_t row = 0; row < vectorSize; row++) {
ASSERT_EQ(inputVector->asFlatVector<bool>()->isNullAt(row), row % 2);
}
}
} // namespace facebook::velox::codegen

0 comments on commit ef46b46

Please sign in to comment.