Skip to content

Commit

Permalink
misc(fuzzer): Rename ArgGenerator and argsOverrideFuncs to ArgTypesGe…
Browse files Browse the repository at this point in the history
…nerator and argValuesGenerators (#12283)

Summary:
Pull Request resolved: #12283

Rename ArgGenerator and argsOverrideFuncs used in expression fuzzer to ArgTypesGenerator and argValuesGenerators to differentiate generators for types and values of arguments.

Reviewed By: kevinwilfong

Differential Revision: D69286324
  • Loading branch information
kagamiori authored and facebook-github-bot committed Feb 10, 2025
1 parent 2e5aacc commit dac6613
Show file tree
Hide file tree
Showing 33 changed files with 223 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace facebook::velox::fuzzer {

/// Generates random, but valid input types for a specified function signature
/// with the return type.
class ArgGenerator {
class ArgTypesGenerator {
public:
virtual ~ArgGenerator() = default;
virtual ~ArgTypesGenerator() = default;

/// Given a signature and a concrete return type returns randomly selected
/// valid input types. Returns empty vector if no input types can produce the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "velox/expression/fuzzer/ArgsOverrideFunctions.h"
#include "velox/expression/fuzzer/ArgValuesGenerators.h"

#include "velox/common/fuzzer/ConstrainedGenerators.h"
#include "velox/common/fuzzer/Utils.h"
Expand Down
4 changes: 2 additions & 2 deletions velox/expression/fuzzer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ target_link_libraries(

add_library(
velox_expression_fuzzer
ArgsOverrideFunctions.cpp
ArgValuesGenerators.cpp
ArgumentTypeFuzzer.cpp
DecimalArgGeneratorBase.cpp
DecimalArgTypesGeneratorBase.cpp
ExpressionFuzzer.cpp
ExpressionFuzzerVerifier.cpp
FuzzerRunner.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "velox/expression/fuzzer/DecimalArgGeneratorBase.h"
#include "velox/expression/fuzzer/DecimalArgTypesGeneratorBase.h"

#include <boost/random/uniform_int_distribution.hpp>

namespace facebook::velox::fuzzer {
Expand All @@ -40,7 +41,7 @@ uint32_t rand32(uint32_t max, FuzzerGenerator& rng) {
}
} // namespace

std::vector<TypePtr> DecimalArgGeneratorBase::generateArgs(
std::vector<TypePtr> DecimalArgTypesGeneratorBase::generateArgs(
const exec::FunctionSignature& /*signature*/,
const TypePtr& returnType,
FuzzerGenerator& rng) {
Expand All @@ -53,7 +54,7 @@ std::vector<TypePtr> DecimalArgGeneratorBase::generateArgs(
return inputs;
}

void DecimalArgGeneratorBase::initialize(uint32_t numArgs) {
void DecimalArgTypesGeneratorBase::initialize(uint32_t numArgs) {
switch (numArgs) {
case 1: {
for (const auto& t : getAllTypes()) {
Expand Down Expand Up @@ -83,7 +84,7 @@ void DecimalArgGeneratorBase::initialize(uint32_t numArgs) {
}
}

std::vector<TypePtr> DecimalArgGeneratorBase::findInputs(
std::vector<TypePtr> DecimalArgTypesGeneratorBase::findInputs(
const TypePtr& returnType,
FuzzerGenerator& rng) const {
const auto [p, s] = getDecimalPrecisionScale(*returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
#pragma once

#include "velox/expression/fuzzer/ArgGenerator.h"
#include "velox/expression/fuzzer/ArgTypesGenerator.h"

namespace facebook::velox::fuzzer {

Expand All @@ -26,7 +26,7 @@ namespace facebook::velox::fuzzer {
/// classes should call 'initialize' from the constructor and specify the number
/// of decimal arguments. They should also implement toReturnType with matching
/// number of pairs of precision and scale.
class DecimalArgGeneratorBase : public ArgGenerator {
class DecimalArgTypesGeneratorBase : public ArgTypesGenerator {
public:
std::vector<TypePtr> generateArgs(
const exec::FunctionSignature& signature,
Expand Down
24 changes: 12 additions & 12 deletions velox/expression/fuzzer/ExpressionFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ ExpressionFuzzer::ExpressionFuzzer(
size_t initialSeed,
const std::shared_ptr<VectorFuzzer>& vectorFuzzer,
const std::optional<ExpressionFuzzer::Options>& options,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgValuesGenerator>>&
argsOverrideFuncs)
argValuesGenerators)
: options_(options.value_or(Options())),
vectorFuzzer_(vectorFuzzer),
state_{rng_, std::max(1, options_.maxLevelOfNesting)},
argGenerators_(argGenerators),
funcArgOverrides_{argsOverrideFuncs} {
argTypesGenerators_(argTypesGenerators),
argValuesGenerators_(argValuesGenerators) {
VELOX_CHECK(vectorFuzzer, "Vector fuzzer must be provided");
seed(initialSeed);

Expand Down Expand Up @@ -843,20 +843,20 @@ core::TypedExprPtr ExpressionFuzzer::generateExpression(
std::vector<core::TypedExprPtr> ExpressionFuzzer::getArgsForCallable(
const CallableSignature& callable) {
// Special case for switch because it has a variable number of arguments not
// specified in the signature. Other functions' argument override should be
// specified through funcArgOverrides_.
// specified in the signature. Other functions' argument generators should be
// specified through argValuesGenerators_.
if (callable.name == "switch") {
return generateSwitchArgs(callable);
}

auto funcIt = funcArgOverrides_.find(callable.name);
if (funcIt == funcArgOverrides_.end()) {
auto funcIt = argValuesGenerators_.find(callable.name);
if (funcIt == argValuesGenerators_.end()) {
return generateArgs(callable);
}
auto args = funcIt->second->generate(
callable, vectorFuzzer_->getOptions(), rng_, state_);
for (auto i = 0; i < args.size(); ++i) {
// Generate arguments not specified in the override.
// Generate arguments not specified in the generator.
if (args[i] == nullptr) {
args[i] = generateArg(callable.args.at(i), callable.constantArgs.at(i));
}
Expand Down Expand Up @@ -1041,11 +1041,11 @@ core::TypedExprPtr ExpressionFuzzer::generateExpressionFromSignatureTemplate(
// Use the argument fuzzer to generate argument types.
argumentTypes = fuzzer.argumentTypes();
} else {
auto it = argGenerators_.find(functionName);
auto it = argTypesGenerators_.find(functionName);
// Since the argument type fuzzer cannot produce argument types, argument
// generators should be provided.
VELOX_CHECK(
it != argGenerators_.end(),
it != argTypesGenerators_.end(),
"Cannot generate argument types for {} with return type {}.",
functionName,
returnType->toString());
Expand Down
13 changes: 7 additions & 6 deletions velox/expression/fuzzer/ExpressionFuzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "velox/core/ITypedExpr.h"
#include "velox/exec/fuzzer/ExprTransformer.h"
#include "velox/exec/fuzzer/ReferenceQueryRunner.h"
#include "velox/expression/fuzzer/ArgGenerator.h"
#include "velox/expression/fuzzer/ArgTypesGenerator.h"
#include "velox/expression/fuzzer/FuzzerToolkit.h"
#include "velox/functions/FunctionRegistry.h"
#include "velox/vector/fuzzer/VectorFuzzer.h"
Expand Down Expand Up @@ -117,11 +117,11 @@ class ExpressionFuzzer {
size_t initialSeed,
const std::shared_ptr<VectorFuzzer>& vectorFuzzer,
const std::optional<ExpressionFuzzer::Options>& options = std::nullopt,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators = {},
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators = {},
const std::unordered_map<
std::string,
std::shared_ptr<ArgValuesGenerator>>& argsOverrideFuncs = {});
std::shared_ptr<ArgValuesGenerator>>& argValuesGenerators = {});

struct FuzzedExpressionData {
// A list of generated expressions.
Expand Down Expand Up @@ -343,7 +343,8 @@ class ExpressionFuzzer {
State state_;

// Maps from function name to a specific generator of argument types.
std::unordered_map<std::string, std::shared_ptr<ArgGenerator>> argGenerators_;
std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>
argTypesGenerators_;

/// We allow the arg generation routine to be specialized for particular
/// functions. This map stores the mapping between function name and the
Expand All @@ -357,7 +358,7 @@ class ExpressionFuzzer {
/// of arguments in the overridden method. Arguments at indices beyond the
/// argument size in the input function signature cannot be left unspecified.)
std::unordered_map<std::string, std::shared_ptr<ArgValuesGenerator>>
funcArgOverrides_;
argValuesGenerators_;

friend class ExpressionFuzzerUnitTest;
};
Expand Down
43 changes: 22 additions & 21 deletions velox/expression/fuzzer/ExpressionFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
#include <unordered_set>

#include "velox/exec/fuzzer/PrestoQueryRunner.h"
#include "velox/expression/fuzzer/ArgGenerator.h"
#include "velox/expression/fuzzer/ArgsOverrideFunctions.h"
#include "velox/expression/fuzzer/ArgTypesGenerator.h"
#include "velox/expression/fuzzer/ArgValuesGenerators.h"
#include "velox/expression/fuzzer/ExpressionFuzzer.h"
#include "velox/expression/fuzzer/FuzzerRunner.h"
#include "velox/expression/fuzzer/SpecialFormSignatureGenerator.h"
#include "velox/functions/prestosql/fuzzer/DivideArgGenerator.h"
#include "velox/functions/prestosql/fuzzer/FloorAndRoundArgGenerator.h"
#include "velox/functions/prestosql/fuzzer/ModulusArgGenerator.h"
#include "velox/functions/prestosql/fuzzer/MultiplyArgGenerator.h"
#include "velox/functions/prestosql/fuzzer/PlusMinusArgGenerator.h"
#include "velox/functions/prestosql/fuzzer/DivideArgTypesGenerator.h"
#include "velox/functions/prestosql/fuzzer/FloorAndRoundArgTypesGenerator.h"
#include "velox/functions/prestosql/fuzzer/ModulusArgTypesGenerator.h"
#include "velox/functions/prestosql/fuzzer/MultiplyArgTypesGenerator.h"
#include "velox/functions/prestosql/fuzzer/PlusMinusArgTypesGenerator.h"
#include "velox/functions/prestosql/fuzzer/SortArrayTransformer.h"
#include "velox/functions/prestosql/fuzzer/TruncateArgGenerator.h"
#include "velox/functions/prestosql/fuzzer/TruncateArgTypesGenerator.h"
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"

DEFINE_int64(
Expand All @@ -53,7 +53,7 @@ DEFINE_uint32(

using namespace facebook::velox::exec::test;
using facebook::velox::exec::test::PrestoQueryRunner;
using facebook::velox::fuzzer::ArgGenerator;
using facebook::velox::fuzzer::ArgTypesGenerator;
using facebook::velox::fuzzer::ArgValuesGenerator;
using facebook::velox::fuzzer::ExpressionFuzzer;
using facebook::velox::fuzzer::FuzzerRunner;
Expand Down Expand Up @@ -110,15 +110,16 @@ int main(int argc, char** argv) {
};
size_t initialSeed = FLAGS_seed == 0 ? std::time(nullptr) : FLAGS_seed;

std::unordered_map<std::string, std::shared_ptr<ArgGenerator>> argGenerators =
{{"plus", std::make_shared<PlusMinusArgGenerator>()},
{"minus", std::make_shared<PlusMinusArgGenerator>()},
{"multiply", std::make_shared<MultiplyArgGenerator>()},
{"divide", std::make_shared<DivideArgGenerator>()},
{"floor", std::make_shared<FloorAndRoundArgGenerator>()},
{"round", std::make_shared<FloorAndRoundArgGenerator>()},
{"mod", std::make_shared<ModulusArgGenerator>()},
{"truncate", std::make_shared<TruncateArgGenerator>()}};
std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>
argTypesGenerators = {
{"plus", std::make_shared<PlusMinusArgTypesGenerator>()},
{"minus", std::make_shared<PlusMinusArgTypesGenerator>()},
{"multiply", std::make_shared<MultiplyArgTypesGenerator>()},
{"divide", std::make_shared<DivideArgTypesGenerator>()},
{"floor", std::make_shared<FloorAndRoundArgTypesGenerator>()},
{"round", std::make_shared<FloorAndRoundArgTypesGenerator>()},
{"mod", std::make_shared<ModulusArgTypesGenerator>()},
{"truncate", std::make_shared<TruncateArgTypesGenerator>()}};

std::unordered_map<std::string, std::shared_ptr<ExprTransformer>>
exprTransformers = {
Expand All @@ -128,7 +129,7 @@ int main(int argc, char** argv) {
{"map_values", std::make_shared<SortArrayTransformer>()}};

std::unordered_map<std::string, std::shared_ptr<ArgValuesGenerator>>
argsOverrideFuncs = {
argValuesGenerators = {
{"json_parse", std::make_shared<JsonParseArgValuesGenerator>()}};

std::shared_ptr<facebook::velox::memory::MemoryPool> rootPool{
Expand All @@ -148,8 +149,8 @@ int main(int argc, char** argv) {
exprTransformers,
{{"session_timezone", "America/Los_Angeles"},
{"adjust_timestamp_to_session_timezone", "true"}},
argGenerators,
argsOverrideFuncs,
argTypesGenerators,
argValuesGenerators,
referenceQueryRunner,
std::make_shared<
facebook::velox::fuzzer::SpecialFormSignatureGenerator>());
Expand Down
10 changes: 5 additions & 5 deletions velox/expression/fuzzer/ExpressionFuzzerVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ ExpressionFuzzerVerifier::ExpressionFuzzerVerifier(
const FunctionSignatureMap& signatureMap,
size_t initialSeed,
const ExpressionFuzzerVerifier::Options& options,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgValuesGenerator>>&
argsOverrideFuncs)
argValuesGenerators)
: options_(options),
queryCtx_(core::QueryCtx::create(
nullptr,
Expand All @@ -79,8 +79,8 @@ ExpressionFuzzerVerifier::ExpressionFuzzerVerifier(
initialSeed,
vectorFuzzer_,
options_.expressionFuzzerOptions,
argGenerators,
argsOverrideFuncs),
argTypesGenerators,
argValuesGenerators),
referenceQueryRunner_{
options_.expressionFuzzerOptions.referenceQueryRunner} {
filesystems::registerLocalFileSystem();
Expand Down
6 changes: 3 additions & 3 deletions velox/expression/fuzzer/ExpressionFuzzerVerifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class ExpressionFuzzerVerifier {
const FunctionSignatureMap& signatureMap,
size_t initialSeed,
const Options& options,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<
std::string,
std::shared_ptr<ArgValuesGenerator>>& argsOverrideFuncs);
std::shared_ptr<ArgValuesGenerator>>& argValuesGenerators);

// This function starts the test that is performed by the
// ExpressionFuzzerVerifier which is generating random expressions and
Expand Down
18 changes: 9 additions & 9 deletions velox/expression/fuzzer/FuzzerRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ int FuzzerRunner::run(
const std::unordered_map<std::string, std::shared_ptr<ExprTransformer>>&
exprTransformers,
const std::unordered_map<std::string, std::string>& queryConfigs,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgValuesGenerator>>&
argsOverrideFuncs,
argValuesGenerators,
std::shared_ptr<exec::test::ReferenceQueryRunner> referenceQueryRunner,
const std::shared_ptr<SpecialFormSignatureGenerator>&
specialFormSignatureGenerator) {
Expand All @@ -247,8 +247,8 @@ int FuzzerRunner::run(
skipFunctions,
exprTransformers,
queryConfigs,
argGenerators,
argsOverrideFuncs,
argTypesGenerators,
argValuesGenerators,
referenceQueryRunner,
specialFormSignatureGenerator);
return RUN_ALL_TESTS();
Expand All @@ -261,10 +261,10 @@ void FuzzerRunner::runFromGtest(
const std::unordered_map<std::string, std::shared_ptr<ExprTransformer>>&
exprTransformers,
const std::unordered_map<std::string, std::string>& queryConfigs,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgValuesGenerator>>&
argsOverrideFuncs,
argValuesGenerators,
std::shared_ptr<exec::test::ReferenceQueryRunner> referenceQueryRunner,
const std::shared_ptr<SpecialFormSignatureGenerator>&
specialFormSignatureGenerator) {
Expand All @@ -278,7 +278,7 @@ void FuzzerRunner::runFromGtest(
specialFormSignatureGenerator->appendSpecialForms(
signatures, options.expressionFuzzerOptions.specialForms);
ExpressionFuzzerVerifier(
signatures, seed, options, argGenerators, argsOverrideFuncs)
signatures, seed, options, argTypesGenerators, argValuesGenerators)
.go();
}
} // namespace facebook::velox::fuzzer
12 changes: 6 additions & 6 deletions velox/expression/fuzzer/FuzzerRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class FuzzerRunner {
const std::unordered_map<std::string, std::shared_ptr<ExprTransformer>>&
exprTransformers,
const std::unordered_map<std::string, std::string>& queryConfigs,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<
std::string,
std::shared_ptr<ArgValuesGenerator>>& argsOverrideFuncs,
std::shared_ptr<ArgValuesGenerator>>& argValuesGenerators,
std::shared_ptr<exec::test::ReferenceQueryRunner> referenceQueryRunner,
const std::shared_ptr<SpecialFormSignatureGenerator>& signatureGenerator);

Expand All @@ -56,11 +56,11 @@ class FuzzerRunner {
const std::unordered_map<std::string, std::shared_ptr<ExprTransformer>>&
exprTransformers,
const std::unordered_map<std::string, std::string>& queryConfigs,
const std::unordered_map<std::string, std::shared_ptr<ArgGenerator>>&
argGenerators,
const std::unordered_map<std::string, std::shared_ptr<ArgTypesGenerator>>&
argTypesGenerators,
const std::unordered_map<
std::string,
std::shared_ptr<ArgValuesGenerator>>& argsOverrideFuncs,
std::shared_ptr<ArgValuesGenerator>>& argValuesGenerators,
std::shared_ptr<exec::test::ReferenceQueryRunner> referenceQueryRunner,
const std::shared_ptr<SpecialFormSignatureGenerator>& signatureGenerator);
};
Expand Down
Loading

0 comments on commit dac6613

Please sign in to comment.