Skip to content

Commit

Permalink
Extract SimpleFunctionApi.h from Type.h (#9182)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #9182

Extract types and templates used in Simple Function API from Type.h into separate header files: SimpleFunctionAPI.h and KindToSimpleType.h

Extracted types include types used to register simple functions, e.g. Varchar, Varbinary, Array, Map, Row, Generic, Constant, etc.

Extracted templates include SimpleTypeTrait, MaterializeType.

Deleted unused IsRowType template.

Fixed incorrect usage of Date type.

Removed unnecessary usage of CppToType.

Reviewed By: xiaoxmeng

Differential Revision: D55136302

fbshipit-source-id: 3423e1356438451170af88cd998f73640cba3b7d
  • Loading branch information
mbasmanova authored and facebook-github-bot committed Mar 21, 2024
1 parent c4d3f6f commit 1f4ce8c
Show file tree
Hide file tree
Showing 23 changed files with 530 additions and 499 deletions.
1 change: 1 addition & 0 deletions velox/core/SimpleFunctionMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "velox/core/QueryConfig.h"
#include "velox/expression/FunctionSignature.h"
#include "velox/expression/SignatureBinder.h"
#include "velox/type/SimpleFunctionApi.h"
#include "velox/type/Type.h"
#include "velox/type/Variant.h"

Expand Down
1 change: 1 addition & 0 deletions velox/dwio/common/tests/utils/MapBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include "velox/type/SimpleFunctionApi.h"
#include "velox/type/Type.h"
#include "velox/vector/ComplexVector.h"

Expand Down
11 changes: 0 additions & 11 deletions velox/dwio/dwrf/reader/ColumnReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,6 @@ struct TemplatedReadHelper<IntDecoderT, int32_t> {
}
};

template <class IntDecoderT>
struct TemplatedReadHelper<IntDecoderT, Date> {
static void nextValues(
IntDecoderT& decoder,
Date* data,
uint64_t numValues,
const uint64_t* nulls) {
decoder.nextInts(reinterpret_cast<int32_t*>(data), numValues, nulls);
}
};

template <class IntDecoderT>
struct TemplatedReadHelper<IntDecoderT, int64_t> {
static void nextValues(
Expand Down
102 changes: 49 additions & 53 deletions velox/dwio/dwrf/test/ColumnWriterIndexTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ void validateBinaryStats(

} // namespace

template <typename Type>
class WriterEncodingIndexTest2 {
public:
WriterEncodingIndexTest2()
WriterEncodingIndexTest2(TypePtr type)
: config_{std::make_shared<Config>()},
pool_{facebook::velox::memory::memoryManager()->addLeafPool()} {}
pool_{facebook::velox::memory::memoryManager()->addLeafPool()},
type_{std::move(type)} {}

virtual ~WriterEncodingIndexTest2() = default;

Expand Down Expand Up @@ -325,8 +325,8 @@ class WriterEncodingIndexTest2 {
return std::make_unique<IndexBuilder>(std::move(stream));
}
};
auto type = CppToType<Type>::create();
auto typeWithId = TypeWithId::create(type, isRoot ? 0 : 1);

auto typeWithId = TypeWithId::create(type_, isRoot ? 0 : 1);
auto batch = prepBatch(1000, pool_.get(), isRoot);

// Creating a stream calls recordPosition.
Expand Down Expand Up @@ -408,12 +408,13 @@ class WriterEncodingIndexTest2 {

std::shared_ptr<Config> config_;
std::shared_ptr<facebook::velox::memory::MemoryPool> pool_;
const TypePtr type_;
};

class TimestampWriterIndexTest : public testing::Test,
public WriterEncodingIndexTest2<Timestamp> {
public WriterEncodingIndexTest2 {
public:
TimestampWriterIndexTest() : WriterEncodingIndexTest2<Timestamp>() {}
TimestampWriterIndexTest() : WriterEncodingIndexTest2(TIMESTAMP()) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -445,10 +446,10 @@ TEST_F(TimestampWriterIndexTest, TestIndex) {
template <typename Integer>
class IntegerColumnWriterDictionaryEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<Integer> {
public WriterEncodingIndexTest2 {
public:
explicit IntegerColumnWriterDictionaryEncodingIndexTest()
: WriterEncodingIndexTest2<Integer>() {
: WriterEncodingIndexTest2(CppToType<Integer>::create()) {
this->config_->set(Config::DICTIONARY_NUMERIC_KEY_SIZE_THRESHOLD, 1.0f);
}

Expand Down Expand Up @@ -505,12 +506,11 @@ TYPED_TEST(IntegerColumnWriterDictionaryEncodingIndexTest, OmitInDictStream) {
this->runTest(2, RECORD_POSITION_COUNT, BACKFILL_POSITION_COUNT, 100);
}

class BoolColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<bool> {
class BoolColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
explicit BoolColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2<bool>() {}
: WriterEncodingIndexTest2(BOOLEAN()) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -541,12 +541,11 @@ TEST_F(BoolColumnWriterEncodingIndexTest, TestIndex) {
runTest(2, 8, 0, 100);
}

class ByteColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<int8_t> {
class ByteColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
explicit ByteColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2<int8_t>() {}
: WriterEncodingIndexTest2(TINYINT()) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -581,11 +580,11 @@ TEST_F(ByteColumnWriterEncodingIndexTest, TestIndex) {
runTest(2, 7, 0, 100);
}

class BinaryColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<Varbinary> {
class BinaryColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
BinaryColumnWriterEncodingIndexTest() = default;
BinaryColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2(VARBINARY()) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -644,11 +643,12 @@ TEST_F(BinaryColumnWriterEncodingIndexTest, TestIndex) {
}

template <typename FLOAT>
class FloatColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<FLOAT> {
class FloatColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
FloatColumnWriterEncodingIndexTest() = default;
FloatColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2(
std::is_same_v<FLOAT, float> ? (TypePtr)REAL() : DOUBLE()) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -936,8 +936,8 @@ class StringColumnWriterDictionaryEncodingIndexTest : public testing::Test {
context.indexBuilderFactory_ = [&](auto /* unused */) {
return std::move(mockIndexBuilder);
};
auto type = CppToType<folly::StringPiece>::create();
auto typeWithId = TypeWithId::create(type, 1);

auto typeWithId = TypeWithId::create(VARCHAR(), 1);
auto batch =
prepBatch(1000, leafPool_.get(), alphabeticRoundRobin, someNulls);

Expand Down Expand Up @@ -1049,8 +1049,8 @@ class StringColumnWriterDirectEncodingIndexTest : public testing::Test {
context.indexBuilderFactory_ = [&](auto /* unused */) {
return std::move(mockIndexBuilder);
};
auto type = CppToType<folly::StringPiece>::create();
auto typeWithId = TypeWithId::create(type, 1);

auto typeWithId = TypeWithId::create(VARCHAR(), 1);
auto batch = prepBatch(1000, alphabeticRoundRobin, someNulls);

// ColumnWriter::recordPosition to capture PRESENT stream positions.
Expand Down Expand Up @@ -1204,11 +1204,11 @@ TEST_F(StringColumnWriterAbandonDictionaryIndexTest, AbandonDictionary) {
runTest(10, 9, 5, abandonEveryWrite);
}

class ListColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<Array<float>> {
class ListColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
ListColumnWriterEncodingIndexTest() = default;
ListColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2(ARRAY(REAL())){};

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -1248,7 +1248,7 @@ class ListColumnWriterEncodingIndexTest

return std::make_shared<ArrayVector>(
pool,
CppToType<Array<float>>::create(),
ARRAY(REAL()),
nulls,
size,
offsets,
Expand All @@ -1274,11 +1274,11 @@ TEST_F(ListColumnWriterEncodingIndexTest, TestIndex) {
runTest(2, 7, 0, 100);
}

class MapColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<Map<float, float>> {
class MapColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
MapColumnWriterEncodingIndexTest() = default;
MapColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2(MAP(REAL(), REAL())) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -1318,7 +1318,7 @@ class MapColumnWriterEncodingIndexTest

return std::make_shared<MapVector>(
pool,
CppToType<Map<float, float>>::create(),
MAP(REAL(), REAL()),
nulls,
size,
offsets,
Expand Down Expand Up @@ -1349,11 +1349,12 @@ TEST_F(MapColumnWriterEncodingIndexTest, TestIndex) {
runTest(2, 7, 0, 100);
}

class FlatMapColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<Row<Map<int32_t, Map<int32_t, float>>>> {
class FlatMapColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
FlatMapColumnWriterEncodingIndexTest() = default;
FlatMapColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2(
ROW({MAP(INTEGER(), MAP(INTEGER(), REAL()))})) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -1443,11 +1444,11 @@ TEST_F(FlatMapColumnWriterEncodingIndexTest, TestIndex) {
2, {0, 4, 11, 6, 6, 11, 6, 6}, {0, 0, 0, 0, 0, 0, 0, 0}, 100, true, 1);
}

class StructColumnWriterEncodingIndexTest
: public testing::Test,
public WriterEncodingIndexTest2<Row<float, float>> {
class StructColumnWriterEncodingIndexTest : public testing::Test,
public WriterEncodingIndexTest2 {
public:
StructColumnWriterEncodingIndexTest() = default;
StructColumnWriterEncodingIndexTest()
: WriterEncodingIndexTest2(ROW({REAL(), REAL()})) {}

protected:
static void SetUpTestCase() {
Expand Down Expand Up @@ -1484,12 +1485,7 @@ class StructColumnWriterEncodingIndexTest
}

return std::make_shared<RowVector>(
pool,
CppToType<Row<float, float>>::create(),
nulls,
size,
children,
nullCount);
pool, ROW({REAL(), REAL()}), nulls, size, children, nullCount);
}
};

Expand Down
11 changes: 3 additions & 8 deletions velox/dwio/dwrf/test/ColumnWriterStatsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ TEST_F(ColumnWriterStatsTest, List) {
auto nodeSizePerStride = populateFloatBatch(pool, &childVector, childSize);
*vector = std::make_shared<ArrayVector>(
&pool,
CppToType<Array<float>>::create(),
ARRAY(REAL()),
nulls,
size,
offsets,
Expand Down Expand Up @@ -585,7 +585,7 @@ TEST_F(ColumnWriterStatsTest, Map) {
auto nodeSizePerStride = populateFloatBatch(pool, &valueVector, childSize);
*vector = std::make_shared<MapVector>(
&pool,
CppToType<Map<int32_t, float>>::create(),
MAP(INTEGER(), REAL()),
nulls,
size,
offsets,
Expand Down Expand Up @@ -639,12 +639,7 @@ TEST_F(ColumnWriterStatsTest, Struct) {
nodeSizePerStride.push_back(floatBatchSize);
}
*vector = std::make_shared<RowVector>(
&pool,
CppToType<Row<float, float>>::create(),
nulls,
size,
children,
nullCount);
&pool, ROW({REAL(), REAL()}), nulls, size, children, nullCount);
nodeSizePerStride.at(0) =
nullCount + nodeSizePerStride.at(2) + nodeSizePerStride.at(3);
nodeSizePerStride.at(1) =
Expand Down
1 change: 0 additions & 1 deletion velox/dwio/dwrf/writer/WriterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,6 @@ class WriterContext : public CompressionBufferPool {
friend class StringColumnWriterDictionaryEncodingIndexTest;
friend class StringColumnWriterDirectEncodingIndexTest;
// TODO: remove once writer code is consolidated
template <typename TestType>
friend class WriterEncodingIndexTest2;

VELOX_FRIEND_TEST(WriterContextTest, GetIntDictionaryEncoder);
Expand Down
1 change: 1 addition & 0 deletions velox/expression/CastTypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include "velox/type/CppToType.h"
#include "velox/type/SimpleFunctionApi.h"

namespace facebook::velox {

Expand Down
1 change: 1 addition & 0 deletions velox/expression/ComplexWriterTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "velox/core/CoreTypeSystem.h"
#include "velox/core/Metaprogramming.h"
#include "velox/expression/ComplexViewTypes.h"
#include "velox/expression/KindToSimpleType.h"
#include "velox/expression/UdfTypeResolver.h"
#include "velox/type/Type.h"
#include "velox/vector/TypeAliases.h"
Expand Down
39 changes: 39 additions & 0 deletions velox/expression/KindToSimpleType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 "velox/type/CppToType.h"
#include "velox/type/SimpleFunctionApi.h"

namespace facebook::velox {

template <TypeKind kind>
struct KindToSimpleType {
using type = typename TypeTraits<kind>::NativeType;
};

template <>
struct KindToSimpleType<TypeKind::VARCHAR> {
using type = Varchar;
};

template <>
struct KindToSimpleType<TypeKind::VARBINARY> {
using type = Varbinary;
};

} // namespace facebook::velox
1 change: 1 addition & 0 deletions velox/expression/VectorReaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "velox/expression/ComplexViewTypes.h"
#include "velox/expression/DecodedArgs.h"
#include "velox/expression/KindToSimpleType.h"
#include "velox/expression/UdfTypeResolver.h"
#include "velox/expression/VariadicView.h"
#include "velox/type/StringView.h"
Expand Down
4 changes: 2 additions & 2 deletions velox/expression/tests/ExprStatsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ TEST_F(ExprStatsTest, exceptionPreparingStatsForListener) {
std::vector<std::string> exceptions;
auto listener = std::make_shared<TestListener>(events, exceptions);
ASSERT_TRUE(exec::registerExprSetListener(listener));
auto varbinaryData = vectorMaker_.flatVector<StringView>(
{"12"_sv}, CppToType<Varbinary>::create());
auto varbinaryData =
vectorMaker_.flatVector<StringView>({"12"_sv}, VARBINARY());
std::vector<core::TypedExprPtr> expressions = {
std::make_shared<const core::ConstantTypedExpr>(
BaseVector::wrapInConstant(1, 0, varbinaryData))};
Expand Down
4 changes: 2 additions & 2 deletions velox/expression/tests/ExpressionRunnerUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ TEST_F(ExpressionRunnerUnitTest, persistAndReproComplexSql) {
}

TEST_F(ExpressionRunnerUnitTest, primitiveConstantsInexpressibleInSql) {
auto varbinaryData = vectorMaker_.flatVector<StringView>(
{"12"_sv}, CppToType<Varbinary>::create());
auto varbinaryData =
vectorMaker_.flatVector<StringView>({"12"_sv}, VARBINARY());
auto constantExpr = std::make_shared<const core::ConstantTypedExpr>(
BaseVector::wrapInConstant(1, 0, varbinaryData));

Expand Down
Loading

0 comments on commit 1f4ce8c

Please sign in to comment.