Skip to content

Commit

Permalink
remove virtual function call
Browse files Browse the repository at this point in the history
  • Loading branch information
marin-ma committed Apr 26, 2024
1 parent dbb1a92 commit e9abef7
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions velox/functions/sparksql/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ ReturnType hashOne(StringView input, SeedType seed) {
return HashClass::hashBytes(input, seed);
}

template <
typename HashClass,
TypeKind kind,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class PrimitiveVectorHasher;

template <
typename HashClass,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class ArrayVectorHasher;

template <
typename HashClass,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class MapVectorHasher;

template <
typename HashClass,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class RowVectorHasher;

// Class to compute hashes identical to one produced by Spark.
// Hashes are computed using the algorithm implemented in HashClass.
template <
Expand All @@ -103,36 +128,38 @@ class SparkVectorHasher {
}

// Compute the hash value of input vector at index for non-null values.
virtual ReturnType hashNotNullAt(vector_size_t index, SeedType seed) = 0;
ReturnType hashNotNullAt(vector_size_t index, SeedType seed) {
switch (decoded_.base()->typeKind()) {
case TypeKind::ARRAY:
return static_cast<ArrayVectorHasher<HashClass, SeedType, ReturnType>*>(
this)
->hashNotNullAt(index, seed);
case TypeKind::MAP:
return static_cast<MapVectorHasher<HashClass, SeedType, ReturnType>*>(
this)
->hashNotNullAt(index, seed);
case TypeKind::ROW:
return static_cast<RowVectorHasher<HashClass, SeedType, ReturnType>*>(
this)
->hashNotNullAt(index, seed);
default:
return VELOX_DYNAMIC_SCALAR_TYPE_DISPATCH(
hashPrimitive, decoded_.base()->typeKind(), index, seed);
}
}

protected:
const DecodedVector& decoded_;
};

template <
typename HashClass,
TypeKind kind,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class PrimitiveVectorHasher;

template <
typename HashClass,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class ArrayVectorHasher;

template <
typename HashClass,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class MapVectorHasher;

template <
typename HashClass,
typename SeedType = typename HashTraits<HashClass>::SeedType,
typename ReturnType = typename HashTraits<HashClass>::ReturnType>
class RowVectorHasher;
private:
template <TypeKind kind>
ReturnType hashPrimitive(vector_size_t index, SeedType seed) {
return static_cast<
PrimitiveVectorHasher<HashClass, kind, SeedType, ReturnType>*>(
this)
->hashNotNullAt(index, seed);
}
};

template <typename HashClass, TypeKind kind>
std::shared_ptr<SparkVectorHasher<HashClass>> createPrimitiveVectorHasher(
Expand Down Expand Up @@ -169,7 +196,7 @@ class PrimitiveVectorHasher : public SparkVectorHasher<HashClass> {
PrimitiveVectorHasher(DecodedVector& decoded)
: SparkVectorHasher<HashClass>(decoded) {}

ReturnType hashNotNullAt(vector_size_t index, SeedType seed) override {
ReturnType hashNotNullAt(vector_size_t index, SeedType seed) {
return hashOne<HashClass, SeedType, ReturnType>(
this->decoded_.template valueAt<typename TypeTraits<kind>::NativeType>(
index),
Expand All @@ -190,7 +217,7 @@ class ArrayVectorHasher : public SparkVectorHasher<HashClass> {
elementHasher_ = createVectorHasher<HashClass>(decodedElements_);
}

ReturnType hashNotNullAt(vector_size_t index, SeedType seed) override {
ReturnType hashNotNullAt(vector_size_t index, SeedType seed) {
auto size = base_->sizeAt(indices_[index]);
auto offset = base_->offsetAt(indices_[index]);

Expand Down Expand Up @@ -223,7 +250,7 @@ class MapVectorHasher : public SparkVectorHasher<HashClass> {
valueHasher_ = createVectorHasher<HashClass>(decodedValues_);
}

ReturnType hashNotNullAt(vector_size_t index, SeedType seed) override {
ReturnType hashNotNullAt(vector_size_t index, SeedType seed) {
auto size = base_->sizeAt(indices_[index]);
auto offset = base_->offsetAt(indices_[index]);

Expand Down Expand Up @@ -261,7 +288,7 @@ class RowVectorHasher : public SparkVectorHasher<HashClass> {
}
}

ReturnType hashNotNullAt(vector_size_t index, SeedType seed) override {
ReturnType hashNotNullAt(vector_size_t index, SeedType seed) {
ReturnType result = seed;
for (auto i = 0; i < base_->childrenSize(); ++i) {
result = hashers_[i]->hashAt(indices_[index], result);
Expand Down

0 comments on commit e9abef7

Please sign in to comment.