Skip to content

Commit fd021d0

Browse files
pedroerpmeta-codesync[bot]
authored andcommitted
refactor: Add is_string_kind/is_nested_kind() metaprogramming helpers (#15849)
Summary: Pull Request resolved: #15849 Adding two common constexpr methods commonly used in metaprogramming templates throughout the codebase. Adding a helper function for convenience since they are quite common. Reviewed By: kagamiori Differential Revision: D89706562 fbshipit-source-id: 7d213ec21083ee04edf8254d4e3682d9f3c344ee
1 parent 97fdb67 commit fd021d0

File tree

8 files changed

+29
-35
lines changed

8 files changed

+29
-35
lines changed

velox/exec/ContainerRowSerde.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,8 @@ std::optional<int32_t> compareSwitch(
388388
template <
389389
bool typeProvidesCustomComparison,
390390
TypeKind Kind,
391-
std::enable_if_t<
392-
Kind != TypeKind::VARCHAR && Kind != TypeKind::VARBINARY &&
393-
Kind != TypeKind::ARRAY && Kind != TypeKind::MAP &&
394-
Kind != TypeKind::ROW,
395-
int32_t> = 0>
391+
std::enable_if_t<!is_string_kind(Kind) && !is_nested_kind(Kind), int32_t> =
392+
0>
396393
std::optional<int32_t> compare(
397394
ByteInputStream& left,
398395
const BaseVector& right,
@@ -693,11 +690,8 @@ std::optional<int32_t> compareSwitch(
693690
template <
694691
bool typeProvidesCustomComparison,
695692
TypeKind Kind,
696-
std::enable_if_t<
697-
Kind != TypeKind::VARCHAR && Kind != TypeKind::VARBINARY &&
698-
Kind != TypeKind::ARRAY && Kind != TypeKind::MAP &&
699-
Kind != TypeKind::ROW,
700-
int32_t> = 0>
693+
std::enable_if_t<!is_string_kind(Kind) && !is_nested_kind(Kind), int32_t> =
694+
0>
701695
std::optional<int32_t> compare(
702696
ByteInputStream& left,
703697
ByteInputStream& right,
@@ -898,11 +892,8 @@ uint64_t hashSwitch(ByteInputStream& stream, const Type* type);
898892
template <
899893
bool typeProvidesCustomComparison,
900894
TypeKind Kind,
901-
std::enable_if_t<
902-
Kind != TypeKind::VARBINARY && Kind != TypeKind::VARCHAR &&
903-
Kind != TypeKind::ARRAY && Kind != TypeKind::MAP &&
904-
Kind != TypeKind::ROW,
905-
int32_t> = 0>
895+
std::enable_if_t<!is_string_kind(Kind) && !is_nested_kind(Kind), int32_t> =
896+
0>
906897
uint64_t hashOne(ByteInputStream& stream, const Type* type) {
907898
using T = typename TypeTraits<Kind>::NativeType;
908899

velox/exec/RowContainer.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ int32_t RowContainer::variableSizeAt(const char* row, column_index_t column)
593593
}
594594

595595
const auto typeKind = typeKinds_[column];
596-
if (typeKind == TypeKind::VARCHAR || typeKind == TypeKind::VARBINARY) {
596+
if (is_string_kind(typeKind)) {
597597
return reinterpret_cast<const StringView*>(row + rowColumn.offset())
598598
->size();
599599
} else {
@@ -619,7 +619,7 @@ int32_t RowContainer::extractVariableSizeAt(
619619
}
620620

621621
const auto typeKind = typeKinds_[column];
622-
if (typeKind == TypeKind::VARCHAR || typeKind == TypeKind::VARBINARY) {
622+
if (is_string_kind(typeKind)) {
623623
const auto value = valueAt<StringView>(row, rowColumn.offset());
624624
const auto size = value.size();
625625
::memcpy(output, &size, 4);
@@ -657,7 +657,7 @@ int32_t RowContainer::storeVariableSizeAt(
657657
// First 4 bytes is the size of the data.
658658
const auto size = *reinterpret_cast<const int32_t*>(data);
659659

660-
if (typeKind == TypeKind::VARCHAR || typeKind == TypeKind::VARBINARY) {
660+
if (is_string_kind(typeKind)) {
661661
if (size > 0) {
662662
stringAllocator_->copyMultipart(
663663
StringView(data + 4, size), row, rowColumn.offset());
@@ -895,13 +895,11 @@ void RowContainer::hashTyped(
895895
: BaseVector::kNullHash;
896896
} else {
897897
uint64_t hash;
898-
if constexpr (Kind == TypeKind::VARCHAR || Kind == TypeKind::VARBINARY) {
898+
if constexpr (is_string_kind(Kind)) {
899899
hash =
900900
folly::hasher<StringView>()(HashStringAllocator::contiguousString(
901901
valueAt<StringView>(row, offset), storage));
902-
} else if constexpr (
903-
Kind == TypeKind::ROW || Kind == TypeKind::ARRAY ||
904-
Kind == TypeKind::MAP) {
902+
} else if constexpr (is_nested_kind(Kind)) {
905903
auto in = prepareRead(row, offset);
906904
hash = ContainerRowSerde::hash(in, type);
907905
} else if constexpr (typeProvidesCustomComparison) {

velox/exec/RowContainer.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,7 @@ class RowContainer {
12481248
Kind == TypeKind::ROW || Kind == TypeKind::ARRAY ||
12491249
Kind == TypeKind::MAP) {
12501250
return compareComplexType(row, column.offset(), decoded, index, flags);
1251-
} else if constexpr (
1252-
Kind == TypeKind::VARCHAR || Kind == TypeKind::VARBINARY) {
1251+
} else if constexpr (is_string_kind(Kind)) {
12531252
auto result = compareStringAsc(
12541253
valueAt<StringView>(row, column.offset()), decoded, index);
12551254
return flags.ascending ? result : result * -1;
@@ -1329,8 +1328,7 @@ class RowContainer {
13291328
Kind == TypeKind::MAP) {
13301329
return compareComplexType(
13311330
left, right, type, leftOffset, rightOffset, flags);
1332-
} else if constexpr (
1333-
Kind == TypeKind::VARCHAR || Kind == TypeKind::VARBINARY) {
1331+
} else if constexpr (is_string_kind(Kind)) {
13341332
auto leftValue = valueAt<StringView>(left, leftOffset);
13351333
auto rightValue = valueAt<StringView>(right, rightOffset);
13361334
auto result = compareStringAsc(leftValue, rightValue);

velox/exec/prefixsort/tests/PrefixEncoderTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ class PrefixEncoderTest : public testing::Test,
270270
const auto rightValue = rightVector->isNullAt(i)
271271
? std::nullopt
272272
: std::optional<ValueDataType>(rightVector->valueAt(i));
273-
if constexpr (
274-
Kind == TypeKind::VARCHAR || Kind == TypeKind::VARBINARY) {
273+
if constexpr (is_string_kind(Kind)) {
275274
encoder.encode(leftValue, leftEncoded, 17, true);
276275
encoder.encode(rightValue, rightEncoded, 17, true);
277276
} else {

velox/experimental/wave/vector/WaveVector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int32_t kindSize() {
2929

3030
int32_t waveTypeKindSize(WaveTypeKind waveKind) {
3131
TypeKind kind = static_cast<TypeKind>(waveKind);
32-
if (kind == TypeKind::VARCHAR || kind == TypeKind::VARBINARY) {
32+
if (is_string_kind(kind)) {
3333
// Wave StringView is 8, not 16 bytes.
3434
return sizeof(StringView);
3535
}

velox/expression/CastExpr-inl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ void CastExpr::applyCastKernel(
175175
}
176176

177177
// Optimize empty input strings casting by avoiding throwing exceptions.
178-
if constexpr (
179-
FromKind == TypeKind::VARCHAR || FromKind == TypeKind::VARBINARY) {
178+
if constexpr (is_string_kind(FromKind)) {
180179
if constexpr (
181180
TypeTraits<ToKind>::isPrimitiveType &&
182181
TypeTraits<ToKind>::isFixedWidth) {
@@ -225,8 +224,7 @@ void CastExpr::applyCastKernel(
225224

226225
const auto& output = castResult.value();
227226

228-
if constexpr (
229-
ToKind == TypeKind::VARCHAR || ToKind == TypeKind::VARBINARY) {
227+
if constexpr (is_string_kind(ToKind)) {
230228
// Write the result output to the output vector
231229
auto writer = exec::StringWriter(result, row);
232230
writer.copy_from(output);

velox/python/legacy/pyvelox.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ static VectorPtr variantsToFlatVector(
8888
const std::vector<velox::variant>& variants,
8989
facebook::velox::memory::MemoryPool* pool) {
9090
using NativeType = typename TypeTraits<T>::NativeType;
91-
constexpr bool kNeedsHolder =
92-
(T == TypeKind::VARCHAR || T == TypeKind::VARBINARY);
91+
constexpr bool kNeedsHolder = is_string_kind(T);
9392

9493
TypePtr type = createScalarType(T);
9594
auto result =

velox/type/Type.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ struct TypeTraits<TypeKind::OPAQUE> {
363363
static constexpr const char* name = "OPAQUE";
364364
};
365365

366+
// Convenience constexpr function to check for string-like and nested type
367+
// kinds.
368+
constexpr bool is_string_kind(TypeKind kind) {
369+
return kind == TypeKind::VARCHAR || kind == TypeKind::VARBINARY;
370+
}
371+
372+
constexpr bool is_nested_kind(TypeKind kind) {
373+
return kind == TypeKind::ARRAY || kind == TypeKind::MAP ||
374+
kind == TypeKind::ROW;
375+
}
376+
366377
template <TypeKind KIND>
367378
struct TypeFactory;
368379

0 commit comments

Comments
 (0)