Skip to content

Commit

Permalink
Add argument generator for Presto truncate decimal function
Browse files Browse the repository at this point in the history
  • Loading branch information
rui-mo committed Jun 18, 2024
1 parent fcb15f5 commit 91e7ba7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/experimental.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ jobs:
--minloglevel=1 \
--repro_persist_path=/tmp/spark_fuzzer_repro \
--velox_fuzzer_enable_decimal_type \
--retry_with_try \
&& echo -e "\n\nSpark Fuzzer run finished successfully."
- name: Archive Spark expression production artifacts
Expand Down
4 changes: 3 additions & 1 deletion velox/expression/fuzzer/ExpressionFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#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/TruncateArgGenerator.h"
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"

DEFINE_int64(
Expand Down Expand Up @@ -84,7 +85,8 @@ int main(int argc, char** argv) {
{"divide", std::make_shared<DivideArgGenerator>()},
{"floor", std::make_shared<FloorAndRoundArgGenerator>()},
{"round", std::make_shared<FloorAndRoundArgGenerator>()},
{"mod", std::make_shared<ModulusArgGenerator>()}};
{"mod", std::make_shared<ModulusArgGenerator>()},
{"truncate", std::make_shared<TruncateArgGenerator>()}};

return FuzzerRunner::run(initialSeed, skipFunctions, {{}}, argGenerators);
}
60 changes: 60 additions & 0 deletions velox/functions/prestosql/fuzzer/TruncateArgGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
*/
#pragma once

#include "velox/expression/fuzzer/DecimalArgGeneratorBase.h"

namespace facebook::velox::exec::test {

class TruncateArgGenerator : public fuzzer::ArgGenerator {
public:
std::vector<TypePtr> generateArgs(
const exec::FunctionSignature& signature,
const TypePtr& returnType,
FuzzerGenerator& rng) override {
// Only the single-arg truncate function is supported because
// ArgumentTypeFuzzer can generate argument types for the two-arg truncate
// function.
VELOX_CHECK_EQ(1, signature.argumentTypes().size());
// Generates a decimal type following below formulas:
// p = max(p1 - s1, 1)
// s = 0
const auto [p, s] = getDecimalPrecisionScale(*returnType);
if (s != 0) {
return {};
}

// p1 = s1 or p1 = s1 + 1.
if (p == 1) {
const auto s1 = rand32(38, rng);
const auto p1 = s1 == 0 ? 1 : std::min(s1 + rand32(1, rng), (uint32_t)38);
return {DECIMAL(p1, s1)};
}

if (p == 38) {
return {DECIMAL(38, 0)};
}
const auto s1 = rand32(38 - p, rng);
return {DECIMAL(p + s1, s1)};
}

private:
static uint32_t rand32(uint32_t max, FuzzerGenerator& rng) {
return boost::random::uniform_int_distribution<uint32_t>()(rng) % max;
}
};

} // namespace facebook::velox::exec::test
17 changes: 17 additions & 0 deletions velox/functions/prestosql/tests/ArgGeneratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#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/TruncateArgGenerator.h"
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"

using namespace facebook::velox;
Expand Down Expand Up @@ -133,4 +134,20 @@ TEST_F(ArgGeneratorTest, modulus) {
assertReturnType(generator, signature, DECIMAL(38, 0));
}

TEST_F(ArgGeneratorTest, truncate) {
const auto signatures = getSignatures("truncate", "decimal");
VELOX_CHECK_EQ(signatures.size(), 2);
const auto& signature = signatures[0]->argumentTypes().size() == 1
? *signatures[0]
: *signatures[1];
const auto generator = std::make_shared<exec::test::TruncateArgGenerator>();

assertReturnType(generator, signature, DECIMAL(1, 0));
assertReturnType(generator, signature, DECIMAL(10, 0));
assertReturnType(generator, signature, DECIMAL(18, 0));
assertReturnType(generator, signature, DECIMAL(32, 0));
assertReturnType(generator, signature, DECIMAL(38, 0));
assertEmptyArgs(generator, signature, DECIMAL(10, 2));
}

} // namespace

0 comments on commit 91e7ba7

Please sign in to comment.