Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement fb_unescape Function in Velox #12493

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions velox/common/fuzzer/ConstrainedGenerators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ folly::dynamic JsonInputGenerator::convertVariantToDynamic(
}
}

std::vector<std::string> getControlCharacters() {
static std::vector<std::string> controlCharacters = {
const std::vector<std::string>& getControlCharacters() {
static const std::vector<std::string> controlCharacters = {
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06",
"\x07", "\x08", "\x09", "\x0A", "\x0B", "\x0C", "\x0D",
"\x0E", "\x0F", "\x10", "\x11", "\x12", "\x13", "\x14",
Expand All @@ -155,13 +155,18 @@ std::vector<std::string> getControlCharacters() {
};

namespace {

void insertRandomControlCharacter(std::string& input, FuzzerGenerator& rng) {
const auto& controlCharacters = getControlCharacters();
const auto index = rand<uint32_t>(rng, 0, controlCharacters.size() - 1);
const auto& controlCharacter = controlCharacters[index];
const auto indexToInsert = rand<uint32_t>(rng, 0, input.size());
input.insert(indexToInsert, controlCharacter);
}

void makeRandomVariationImpl(std::string& input, FuzzerGenerator& rng) {
if (coinToss(rng, 0.1)) {
const auto controlCharacters = getControlCharacters();
const auto index = rand<uint32_t>(rng, 0, controlCharacters.size() - 1);
const auto& controlCharacter = controlCharacters[index];
const auto indexToInsert = rand<uint32_t>(rng, 0, input.size());
input.insert(indexToInsert, controlCharacter);
insertRandomControlCharacter(input, rng);
} else if (coinToss(rng, 0.1)) {
const auto size = rand<uint32_t>(rng, 0, input.size());
input.resize(size);
Expand All @@ -176,6 +181,39 @@ void JsonInputGenerator::makeRandomVariation(std::string& json) {
return makeRandomVariationImpl(json, rng_);
}

StringEscapingInputGenerator::StringEscapingInputGenerator(
size_t seed,
const TypePtr& type,
double nullRatio,
std::unique_ptr<AbstractInputGenerator>&& randomStrGenerator)
: AbstractInputGenerator(seed, type, nullptr, nullRatio),
randomStrGenerator_(std::move(randomStrGenerator)) {}

variant StringEscapingInputGenerator::generate() {
if (coinToss(rng_, nullRatio_)) {
return variant::null(type_->kind());
}
auto optionalVariant = randomStrGenerator_->generate();
if (!optionalVariant.hasValue()) {
return variant::null(type_->kind());
}

std::string randonStr = optionalVariant.value<TypeKind::VARCHAR>();
makeRandomVariation(randonStr);
return variant(std::move(randonStr));
}

void StringEscapingInputGenerator::makeRandomVariation(std::string& randomStr) {
makeRandomVariationImpl(randomStr, rng_);
if (coinToss(rng_, 0.5)) {
insertRandomControlCharacter(randomStr, rng_);
} else if (coinToss(rng_, 0.5)) {
// Add escaped string test cases
// make the string suitable for representation as a C string literal
randomStr = folly::cEscape<std::string>(randomStr);
}
}

// Utility functions
template <bool, TypeKind KIND>
std::unique_ptr<AbstractInputGenerator> getRandomInputGeneratorPrimitive(
Expand Down
17 changes: 17 additions & 0 deletions velox/common/fuzzer/ConstrainedGenerators.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,21 @@ class JsonInputGenerator : public AbstractInputGenerator {
folly::json::serialization_opts opts_;
};

class StringEscapingInputGenerator : public AbstractInputGenerator {
public:
StringEscapingInputGenerator(
size_t seed,
const TypePtr& type,
double nullRatio,
std::unique_ptr<AbstractInputGenerator>&& randomStrGenerator);

~StringEscapingInputGenerator() override = default;

variant generate() override;

private:
void makeRandomVariation(std::string& str);
std::unique_ptr<AbstractInputGenerator> randomStrGenerator_;
};

} // namespace facebook::velox::fuzzer
30 changes: 29 additions & 1 deletion velox/expression/fuzzer/ArgValuesGenerators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ std::vector<core::TypedExprPtr> JsonParseArgValuesGenerator::generate(
inputExpressions[0] = std::make_shared<core::FieldAccessTypedExpr>(
signature.args[0], state.inputRowNames_.back());
return inputExpressions;
}
};

std::vector<core::TypedExprPtr> StringEscapeArgValuesGenerator::generate(
const CallableSignature& signature,
const VectorFuzzer::Options& options,
FuzzerGenerator& rng,
ExpressionFuzzerState& state) {
VELOX_CHECK_EQ(signature.args.size(), 1);
populateInputTypesAndNames(signature, state);

const auto representedType = facebook::velox::randType(rng, 0);
const auto seed = rand<uint32_t>(rng);
const auto nullRatio = options.nullRatio;

state.customInputGenerators_.emplace_back(
std::make_shared<fuzzer::StringEscapingInputGenerator>(
seed,
signature.args[0],
nullRatio,
fuzzer::getRandomInputGenerator(seed, VARCHAR(), nullRatio)));

// Populate inputExpressions_ for the argument that requires custom
// generation. A nullptr should be added at inputExpressions[i] if the i-th
// argument does not require custom input generation.
std::vector<core::TypedExprPtr> inputExpressions{
signature.args.size(), nullptr};
inputExpressions[0] = std::make_shared<core::FieldAccessTypedExpr>(
signature.args[0], state.inputRowNames_.back());
return inputExpressions;
};
} // namespace facebook::velox::fuzzer
11 changes: 11 additions & 0 deletions velox/expression/fuzzer/ArgValuesGenerators.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ class JsonParseArgValuesGenerator : public ArgValuesGenerator {
ExpressionFuzzerState& state) override;
};

class StringEscapeArgValuesGenerator : public ArgValuesGenerator {
public:
~StringEscapeArgValuesGenerator() override = default;

std::vector<core::TypedExprPtr> generate(
const CallableSignature& signature,
const VectorFuzzer::Options& options,
FuzzerGenerator& rng,
ExpressionFuzzerState& state) override;
};

} // namespace facebook::velox::fuzzer
Loading