forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "velox/expression/tests/ArgGenerator.h" | ||
|
||
namespace facebook::velox::test { | ||
namespace { | ||
|
||
int32_t rand(FuzzerGenerator& rng) { | ||
return boost::random::uniform_int_distribution<int32_t>()(rng); | ||
} | ||
} // namespace | ||
|
||
std::vector<TypePtr> DecimalArgGeneratorBase::generateArgs( | ||
const exec::FunctionSignature& /*signature*/, | ||
const TypePtr& returnType, | ||
FuzzerGenerator& rng) override { | ||
auto inputs = findInputs(returnType, rng); | ||
for (const auto& input : inputs) { | ||
if (input == nullptr) { | ||
return {}; | ||
} | ||
} | ||
return inputs; | ||
} | ||
|
||
const Inputs& DecimalArgGeneratorBase::findInputs( | ||
const TypePtr& returnType, | ||
FuzzerGenerator& rng) const { | ||
auto [p, s] = getDecimalPrecisionScale(*returnType); | ||
auto it = inputs_.find({p, s}); | ||
if (it == inputs_.end()) { | ||
LOG(ERROR) << "Cannot find input types for " << returnType->toString(); | ||
return {nullptr, nullptr}; | ||
} | ||
|
||
auto index = rand(rng) % it->second.size(); | ||
return it->second[index]; | ||
} | ||
|
||
const std::vector<TypePtr>& DecimalArgGeneratorBase::getAllTypes() const { | ||
const auto generateAllTypes = []() { | ||
std::vector<TypePtr> allTypes; | ||
for (auto p = 1; p < 38; ++p) { | ||
for (auto s = 0; s <= p; ++s) { | ||
allTypes.push_back(DECIMAL(p, s)); | ||
} | ||
} | ||
return allTypes; | ||
}; | ||
|
||
static std::vector<TypePtr> allTypes = generateAllTypes(); | ||
return allTypes; | ||
} | ||
|
||
void DecimalArgGeneratorBase::initialize() { | ||
// By default, the result type is considered to be calculated from two input | ||
// decimal types. | ||
for (const auto& a : getAllTypes()) { | ||
for (const auto& b : getAllTypes()) { | ||
auto [p1, s1] = getDecimalPrecisionScale(*a); | ||
auto [p2, s2] = getDecimalPrecisionScale(*b); | ||
|
||
if (auto returnType = toReturnType(4, p1, s1, p2, s2)) { | ||
inputs_[returnType.value()].push_back({a, b}); | ||
} | ||
} | ||
} | ||
} | ||
} // namespace facebook::velox::test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "velox/expression/FunctionSignature.h" | ||
|
||
namespace facebook::velox::test { | ||
|
||
class ArgGenerator { | ||
public: | ||
virtual ~ArgGenerator() = default; | ||
|
||
/// Given a signature and a concrete return type return randomly selected | ||
/// valid input types. Returns empty vector if no input types can | ||
/// produce the specified result type. | ||
virtual std::vector<TypePtr> generateArgs( | ||
const exec::FunctionSignature& signature, | ||
const TypePtr& returnType, | ||
FuzzerGenerator& rng) = 0; | ||
}; | ||
|
||
class DecimalArgGeneratorBase : public ArgGenerator { | ||
public: | ||
std::vector<TypePtr> generateArgs( | ||
const exec::FunctionSignature& /*signature*/, | ||
const TypePtr& returnType, | ||
FuzzerGenerator& rng) override; | ||
|
||
protected: | ||
using Inputs = std::vector<TypePtr>; | ||
|
||
// Return randomly selected pair of input types that produce the specified | ||
// result type. | ||
const Inputs& findInputs(const TypePtr& returnType, FuzzerGenerator& rng) | ||
const; | ||
|
||
const std::vector<TypePtr>& getAllTypes() const; | ||
|
||
// Compute result type for all possible pairs of decimal input types. Store | ||
// the results in 'inputs_' maps keyed by return type. | ||
void initialize(); | ||
|
||
// Given precisions and scales of the inputs, return precision and scale of | ||
// the result. | ||
virtual std::optional<std::pair<int, int>> toReturnType(int count, ...) = 0; | ||
|
||
std::map<std::pair<int, int>, std::vector<Inputs>> inputs_; | ||
}; | ||
|
||
} // namespace facebook::velox::test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters