Skip to content

Commit

Permalink
try
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Mar 18, 2024
1 parent 5ecee09 commit 3f4fd2d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
3 changes: 2 additions & 1 deletion velox/expression/tests/ArgumentTypeFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ TEST_F(ArgumentTypeFuzzerTest, decimal) {

testFuzzingDecimalSuccess(signature, BOOLEAN(), 3);

signature = exec::FunctionSignatureBuilder()
signature =
exec::FunctionSignatureBuilder()
.integerVariable("a_precision")
.integerVariable("a_scale")
.integerVariable("b_precision")
Expand Down
36 changes: 25 additions & 11 deletions velox/expression/tests/ExpressionFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ ExpressionFuzzer::FuzzedExpressionData ExpressionFuzzer::fuzzExpressions(

std::vector<core::TypedExprPtr> expressions;
for (int i = 0; i < outType->size(); i++) {
expressions.push_back(generateExpression(outType->childAt(i)));
expressions.push_back(generateExpression(outType->childAt(i), i));
}
return {
std::move(expressions),
Expand All @@ -1032,7 +1032,8 @@ ExpressionFuzzer::FuzzedExpressionData ExpressionFuzzer::fuzzExpression() {
// generated expressions of the same return type exist then there is a 30%
// chance that it will re-use one of them.
core::TypedExprPtr ExpressionFuzzer::generateExpression(
const TypePtr& returnType) {
const TypePtr& returnType,
column_index_t index) {
VELOX_CHECK_GT(state.remainingLevelOfNesting_, 0);
--state.remainingLevelOfNesting_;
auto guard = folly::makeGuard([&] { ++state.remainingLevelOfNesting_; });
Expand Down Expand Up @@ -1082,7 +1083,7 @@ core::TypedExprPtr ExpressionFuzzer::generateExpression(
if (!expression &&
(options_.enableComplexTypes || options_.enableDecimalType)) {
expression = generateExpressionFromSignatureTemplate(
returnType, chosenFunctionName);
returnType, chosenFunctionName, index);
}
}
}
Expand Down Expand Up @@ -1260,7 +1261,8 @@ const SignatureTemplate* ExpressionFuzzer::findSignatureTemplate(

core::TypedExprPtr ExpressionFuzzer::generateExpressionFromSignatureTemplate(
const TypePtr& returnType,
const std::string& functionName) {
const std::string& functionName,
column_index_t index) {
auto typeName = typeToBaseName(returnType);

auto* chosen =
Expand All @@ -1274,9 +1276,17 @@ core::TypedExprPtr ExpressionFuzzer::generateExpressionFromSignatureTemplate(
}

auto chosenSignature = *chosen->signature;
ArgumentTypeFuzzer fuzzer{chosenSignature, returnType, rng_};
VELOX_CHECK_EQ(fuzzer.fuzzArgumentTypes(options_.maxNumVarArgs), true);
auto& argumentTypes = fuzzer.argumentTypes();
const std::vector<TypePtr>& argumentTypes;
if (indexToArgumentFuzzers_.count(index)) {
const auto fuzzer = indexToArgumentFuzzers_[index];
VELOX_CHECK_EQ(fuzzer->fuzzArgumentTypes(options_.maxNumVarArgs), true);
argumentTypes = fuzzer->argumentTypes();
} else {
ArgumentTypeFuzzer fuzzer{chosenSignature, returnType, rng_};
VELOX_CHECK_EQ(fuzzer.fuzzArgumentTypes(options_.maxNumVarArgs), true);
argumentTypes = fuzzer.argumentTypes();
}

auto constantArguments = chosenSignature.constantArguments();

// ArgumentFuzzer may generate duplicate arguments if the signature's
Expand Down Expand Up @@ -1396,7 +1406,7 @@ core::TypedExprPtr ExpressionFuzzer::ExprBank::getRandomExpression(
return nullptr;
}

TypePtr ExpressionFuzzer::fuzzReturnType() {
TypePtr ExpressionFuzzer::fuzzReturnType(column_index_t index) {
auto chooseFromConcreteSignatures = rand32(0, 1);

chooseFromConcreteSignatures =
Expand All @@ -1420,8 +1430,12 @@ TypePtr ExpressionFuzzer::fuzzReturnType() {
VELOX_CHECK(
!signatureTemplates_.empty(), "No function signature available.");
size_t idx = rand32(0, signatureTemplates_.size() - 1);
ArgumentTypeFuzzer typeFuzzer{*signatureTemplates_[idx].signature, rng_};
rootType = typeFuzzer.fuzzReturnType();
const auto typeFuzzer = std::make_shared<ArgumentTypeFuzzer>(
*signatureTemplates_[idx].signature, rng_);
rootType = typeFuzzer->fuzzReturnType();
if (indexToArgumentFuzzers_.count(index) == 0) {
indexToArgumentFuzzers_[index] = typeFuzzer;
}
}
return rootType;
}
Expand All @@ -1430,7 +1444,7 @@ RowTypePtr ExpressionFuzzer::fuzzRowReturnType(size_t size, char prefix) {
std::vector<TypePtr> children;
std::vector<std::string> names;
for (int i = 0; i < size; i++) {
children.push_back(fuzzReturnType());
const auto returnType = fuzzReturnType();
names.push_back(fmt::format("{}{}", prefix, i));
}
return ROW(std::move(names), std::move(children));
Expand Down
12 changes: 9 additions & 3 deletions velox/expression/tests/ExpressionFuzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,17 @@ class ExpressionFuzzer {
}

// Generate a random return type.
TypePtr fuzzReturnType();
TypePtr fuzzReturnType(column_index_t index = 0);

RowTypePtr fuzzRowReturnType(size_t size, char prefix = 'p');

private:
// Either generates a new expression of the required return type or if
// already generated expressions of the same return type exist then there is
// a 30% chance that it will re-use one of them.
core::TypedExprPtr generateExpression(const TypePtr& type);
core::TypedExprPtr generateExpression(
const TypePtr& type,
column_index_t index);

enum ArgumentKind { kArgConstant = 0, kArgColumn = 1, kArgExpression = 2 };

Expand Down Expand Up @@ -317,7 +319,8 @@ class ExpressionFuzzer {
/// the function named 'functionName' supports.
core::TypedExprPtr generateExpressionFromSignatureTemplate(
const TypePtr& returnType,
const std::string& functionName);
const std::string& functionName,
column_index_t index);

/// Generate a cast expression that returns the specified type. Return a
/// nullptr if casting to the specified type is not supported. The supported
Expand Down Expand Up @@ -391,6 +394,9 @@ class ExpressionFuzzer {

std::unordered_map<std::string, ArgsOverrideFunc> funcArgOverrides_;

std::unordered_map<column_index_t, std::shared_ptr<ArgumentTypeFuzzer>>
indexToArgumentFuzzers_;

std::shared_ptr<VectorFuzzer> vectorFuzzer_;

FuzzerGenerator rng_;
Expand Down

0 comments on commit 3f4fd2d

Please sign in to comment.