Skip to content

Commit

Permalink
Respect orderable constraint in ArgumentTypeFuzzer (#6950)
Browse files Browse the repository at this point in the history
Summary:
PR #6906 added support to restricting arguments as orderable.

This PR changes ArgumentTypeFuzzer to respect this constraint.

Part of #6718

Pull Request resolved: #6950

Reviewed By: Yuhta

Differential Revision: D50212902

Pulled By: mbasmanova

fbshipit-source-id: 318b5e443baba7b6800539fafcf34946e6f0e91d
  • Loading branch information
duanmeng authored and facebook-github-bot committed Oct 12, 2023
1 parent 3cfafbd commit 30e1854
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
10 changes: 9 additions & 1 deletion velox/expression/tests/ArgumentTypeFuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@ void ArgumentTypeFuzzer::determineUnboundedTypeVariables() {
// Random randomType() never generates unknown here.
// TODO: we should extend randomType types and exclude unknown based
// on variableInfo.
bindings_[variableName] = randType();
if (variableInfo.orderableTypesOnly()) {
bindings_[variableName] = randOrderableType();
} else {
bindings_[variableName] = randType();
}
}
}

TypePtr ArgumentTypeFuzzer::randType() {
return velox::randType(rng_, 2);
}

TypePtr ArgumentTypeFuzzer::randOrderableType() {
return velox::randOrderableType(rng_, 2);
}

bool ArgumentTypeFuzzer::fuzzArgumentTypes(uint32_t maxVariadicArgs) {
const auto& formalArgs = signature_.argumentTypes();
auto formalArgsCnt = formalArgs.size();
Expand Down
3 changes: 3 additions & 0 deletions velox/expression/tests/ArgumentTypeFuzzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class ArgumentTypeFuzzer {

TypePtr randType();

/// Generates an orderable random type, including structs, and arrays.
TypePtr randOrderableType();

const exec::FunctionSignature& signature_;

TypePtr returnType_;
Expand Down
77 changes: 77 additions & 0 deletions velox/expression/tests/ArgumentTypeFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,81 @@ TEST_F(ArgumentTypeFuzzerTest, unconstrainedSignatureTemplate) {
ASSERT_EQ(argumentTypes[0]->childAt(0), argumentTypes[1]);
}

TEST_F(ArgumentTypeFuzzerTest, orderableConstraint) {
{
auto signature = exec::FunctionSignatureBuilder()
.orderableTypeVariable("T")
.returnType("bigint")
.argumentType("T")
.build();

for (size_t i = 0; i < 100; ++i) {
std::mt19937 rng(i);
ArgumentTypeFuzzer fuzzer{*signature, nullptr, rng};
fuzzer.fuzzArgumentTypes(kMaxVariadicArgs);
ASSERT_TRUE(fuzzer.argumentTypes()[0]->isOrderable())
<< fuzzer.argumentTypes()[0]->toString();
}
}

{
auto signature = exec::FunctionSignatureBuilder()
.orderableTypeVariable("T")
.returnType("T")
.argumentType("T")
.build();
testFuzzingFailure(signature, MAP(VARCHAR(), BIGINT()));
testFuzzingFailure(signature, ARRAY(MAP(VARCHAR(), BIGINT())));
}

{
auto signature = exec::FunctionSignatureBuilder()
.orderableTypeVariable("T")
.returnType("array(T)")
.argumentType("array(T)")
.build();

testFuzzingSuccess(signature, ARRAY(DOUBLE()), {ARRAY(DOUBLE())});
testFuzzingSuccess(
signature, ARRAY(ROW({DOUBLE()})), {ARRAY(ROW({DOUBLE()}))});
testFuzzingFailure(signature, MAP(VARCHAR(), BIGINT()));
}

{
auto signature = exec::FunctionSignatureBuilder()
.typeVariable("T")
.orderableTypeVariable("U")
.returnType("row(T, U)")
.argumentType("T")
.argumentType("row(T,U)")
.build();

testFuzzingSuccess(
signature,
ROW({MAP(BIGINT(), BIGINT()), BIGINT()}),
{MAP(BIGINT(), BIGINT()), ROW({MAP(BIGINT(), BIGINT()), BIGINT()})});

testFuzzingFailure(signature, ROW({BIGINT(), MAP(VARCHAR(), BIGINT())}));
}

{
auto signature = exec::FunctionSignatureBuilder()
.typeVariable("T")
.orderableTypeVariable("U")
.returnType("row(T,U)")
.argumentType("T")
.argumentType("function(T,U)")
.build();

testFuzzingSuccess(
signature,
ROW({MAP(BIGINT(), BIGINT()), BIGINT()}),
{MAP(BIGINT(), BIGINT()),
std::make_shared<FunctionType>(
std::vector<TypePtr>{MAP(BIGINT(), BIGINT())}, BIGINT())});

testFuzzingFailure(signature, ROW({BIGINT(), MAP(VARCHAR(), BIGINT())}));
}
}

} // namespace facebook::velox::test

0 comments on commit 30e1854

Please sign in to comment.