Skip to content

Commit

Permalink
refactor(fuzzer): add supporting map functions (#11536)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #11536

This diff adds some supporting functions for my diff for new benchmark tests for map_concat.

Reviewed By: darrenfu, pedroerp

Differential Revision: D65755011

fbshipit-source-id: c27bf8e3e75109ae48fcba7cc0b0216f1974e85f
  • Loading branch information
wuray22 authored and facebook-github-bot committed Dec 6, 2024
1 parent 2c5384e commit e603158
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
45 changes: 32 additions & 13 deletions velox/vector/fuzzer/VectorFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,16 +537,7 @@ VectorPtr VectorFuzzer::fuzzFlat(const TypePtr& type, vector_size_t size) {
// Do not initialize keys and values inline in the fuzzMap call as C++ does
// not specify the order they'll be called in, leading to inconsistent
// results across platforms.
const auto& keyType = type->asMap().keyType();
const auto& valueType = type->asMap().valueType();
auto length = getElementsVectorLength(opts_, size);

auto keys = opts_.normalizeMapKeys || !opts_.containerHasNulls
? fuzzFlatNotNull(keyType, length)
: fuzzFlat(keyType, length);
auto values = opts_.containerHasNulls ? fuzzFlat(valueType, length)
: fuzzFlatNotNull(valueType, length);
return fuzzMap(keys, values, size);
return fuzzMap(type->asMap().keyType(), type->asMap().valueType(), size);
}
// Rows.
else if (type->isRow()) {
Expand All @@ -568,6 +559,20 @@ VectorPtr VectorFuzzer::fuzzFlat(const TypePtr& type, vector_size_t size) {
}
}

VectorPtr VectorFuzzer::fuzzMap(
const TypePtr& keyType,
const TypePtr& valueType,
vector_size_t size) {
auto length = getElementsVectorLength(opts_, size);

auto keys = opts_.normalizeMapKeys || !opts_.containerHasNulls
? fuzzFlatNotNull(keyType, length)
: fuzzFlat(keyType, length);
auto values = opts_.containerHasNulls ? fuzzFlat(valueType, length)
: fuzzFlatNotNull(valueType, length);
return fuzzMap(keys, values, size);
}

VectorPtr VectorFuzzer::fuzzFlatPrimitive(
const TypePtr& type,
vector_size_t size) {
Expand Down Expand Up @@ -912,6 +917,10 @@ TypePtr VectorFuzzer::randType(
return velox::randType(rng_, scalarTypes, maxDepth);
}

TypePtr VectorFuzzer::randMapType(int maxDepth) {
return velox::randMapType(rng_, defaultScalarTypes(), maxDepth);
}

RowTypePtr VectorFuzzer::randRowType(int maxDepth) {
return velox::randRowType(rng_, maxDepth);
}
Expand All @@ -922,6 +931,10 @@ RowTypePtr VectorFuzzer::randRowType(
return velox::randRowType(rng_, scalarTypes, maxDepth);
}

size_t VectorFuzzer::randInRange(size_t min, size_t max) {
return rand(rng_, min, max);
}

VectorPtr VectorFuzzer::wrapInLazyVector(VectorPtr baseVector) {
if (hasNestedDictionaryLayers(baseVector)) {
auto indices = baseVector->wrapInfo();
Expand Down Expand Up @@ -1139,16 +1152,22 @@ TypePtr randType(
}
switch (rand<uint32_t>(rng) % 3) {
case 0:
return MAP(
randType(rng, scalarTypes, 0),
randType(rng, scalarTypes, maxDepth - 1));
return randMapType(rng, scalarTypes, maxDepth);
case 1:
return ARRAY(randType(rng, scalarTypes, maxDepth - 1));
default:
return randRowType(rng, scalarTypes, maxDepth - 1);
}
}

TypePtr randMapType(
FuzzerGenerator& rng,
const std::vector<TypePtr>& scalarTypes,
int maxDepth) {
return MAP(
randType(rng, scalarTypes, 0), randType(rng, scalarTypes, maxDepth - 1));
}

TypePtr randOrderableType(FuzzerGenerator& rng, int maxDepth) {
return randOrderableType(rng, defaultScalarTypes(), maxDepth);
}
Expand Down
19 changes: 19 additions & 0 deletions velox/vector/fuzzer/VectorFuzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ class VectorFuzzer {
VectorPtr fuzzFlatNotNull(const TypePtr& type);
VectorPtr fuzzFlatNotNull(const TypePtr& type, vector_size_t size);

/// Returns a map vector with randomized values and nulls. Returns a vector
/// containing `opts_.vectorSize` or `size` elements.
VectorPtr
fuzzMap(const TypePtr& keyType, const TypePtr& valueType, vector_size_t size);

/// Returns a random constant vector (which could be a null constant). Returns
/// a vector with size set to `opts_.vectorSize` or 'size'.
VectorPtr fuzzConstant(const TypePtr& type);
Expand Down Expand Up @@ -280,6 +285,13 @@ class VectorFuzzer {
const std::vector<TypePtr>& scalarTypes,
int maxDepth = 5);

/// Generates a random map type where keys cannot be nested. maxDepth limits
/// the maximum level of nesting for values.
TypePtr randMapType(int maxDepth = 5);

/// Returns a random integer between min and max inclusive
size_t randInRange(size_t min, size_t max);

/// Generates short decimal TypePtr with random precision and scale.
inline TypePtr randShortDecimalType() {
auto [precision, scale] =
Expand Down Expand Up @@ -401,6 +413,13 @@ TypePtr randType(
const std::vector<TypePtr>& scalarTypes,
int maxDepth = 5);

/// Generates a random map type given a vector of scalarTypes as keys. maxDepth
/// limits the maximum level of nesting for values.
TypePtr randMapType(
FuzzerGenerator& rng,
const std::vector<TypePtr>& scalarTypes,
int maxDepth = 5);

/// Same as the function above, but only generate orderable types.
/// MAP types are not generated as they are not orderable.
TypePtr randOrderableType(FuzzerGenerator& rng, int maxDepth = 5);
Expand Down
8 changes: 8 additions & 0 deletions velox/vector/fuzzer/tests/VectorFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,4 +940,12 @@ TEST_F(VectorFuzzerTest, randOrderableType) {
ASSERT_TRUE(fuzzer.randOrderableType()->isOrderable());
}
}

TEST_F(VectorFuzzerTest, randMapType) {
VectorFuzzer::Options opts;
VectorFuzzer fuzzer(opts, pool());
for (int i = 0; i < 100; ++i) {
ASSERT_TRUE(fuzzer.randMapType()->isMap());
}
}
} // namespace

0 comments on commit e603158

Please sign in to comment.